mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-11 09:13:24 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 964b11c0c9 | |||
| 3d7b746779 | |||
| 7686495283 | |||
| 265d5affc6 | |||
| 06571a4aae | |||
| c5b2e6f286 | |||
| 7cf102e0b9 | |||
| 75ec74270c | |||
| cadb40e401 |
29
.github/workflows/publish.yml
vendored
29
.github/workflows/publish.yml
vendored
@@ -35,25 +35,34 @@ jobs:
|
||||
- name: Build
|
||||
run: pnpm build
|
||||
|
||||
- name: Check if version changed
|
||||
- name: Check if should publish
|
||||
id: version-check
|
||||
run: |
|
||||
# Get current version from package.json
|
||||
CURRENT_VERSION=$(node -p "require('./package.json').version")
|
||||
|
||||
# Get previous commit's version
|
||||
git show HEAD~1:package.json > prev-package.json 2>/dev/null || echo '{"version":"0.0.0"}' > prev-package.json
|
||||
PREVIOUS_VERSION=$(node -p "require('./prev-package.json').version")
|
||||
|
||||
echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
||||
echo "previous=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
|
||||
|
||||
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
|
||||
# Check if this is a version bump commit (created by npm version)
|
||||
COMMIT_MSG=$(git log -1 --pretty=%B)
|
||||
if echo "$COMMIT_MSG" | grep -q "^Release v[0-9]\|^v[0-9]"; then
|
||||
echo "changed=true" >> $GITHUB_OUTPUT
|
||||
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
|
||||
echo "Version bump commit detected: $COMMIT_MSG"
|
||||
# Or check if package.json was modified in this commit
|
||||
elif git diff --name-only HEAD~1 HEAD | grep -q "package.json"; then
|
||||
# Check if the version actually changed
|
||||
git show HEAD~1:package.json > prev-package.json 2>/dev/null || echo '{"version":"0.0.0"}' > prev-package.json
|
||||
PREVIOUS_VERSION=$(node -p "require('./prev-package.json').version")
|
||||
|
||||
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
|
||||
echo "changed=true" >> $GITHUB_OUTPUT
|
||||
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
|
||||
else
|
||||
echo "changed=false" >> $GITHUB_OUTPUT
|
||||
echo "package.json modified but version unchanged: $CURRENT_VERSION"
|
||||
fi
|
||||
else
|
||||
echo "changed=false" >> $GITHUB_OUTPUT
|
||||
echo "Version unchanged: $CURRENT_VERSION"
|
||||
echo "No version change detected"
|
||||
fi
|
||||
|
||||
- name: Publish to NPM
|
||||
|
||||
28
README.md
28
README.md
@@ -27,20 +27,40 @@ yarn add @xtr-dev/payload-automation
|
||||
|
||||
```typescript
|
||||
import { buildConfig } from 'payload'
|
||||
import { payloadAutomation } from '@xtr-dev/payload-automation'
|
||||
import { workflowsPlugin } from '@xtr-dev/payload-automation/server'
|
||||
|
||||
export default buildConfig({
|
||||
// ... your config
|
||||
plugins: [
|
||||
payloadAutomation({
|
||||
collections: ['posts', 'users'], // Collections to monitor
|
||||
globals: ['settings'], // Globals to monitor
|
||||
workflowsPlugin({
|
||||
collectionTriggers: {
|
||||
posts: true, // Enable all CRUD triggers for posts
|
||||
users: {
|
||||
create: true, // Only enable create trigger for users
|
||||
update: true
|
||||
}
|
||||
},
|
||||
enabled: true,
|
||||
}),
|
||||
],
|
||||
})
|
||||
```
|
||||
|
||||
## Import Structure
|
||||
|
||||
The plugin uses separate exports to avoid bundling server-side code in client bundles:
|
||||
|
||||
```typescript
|
||||
// Server-side plugin and functions
|
||||
import { workflowsPlugin } from '@xtr-dev/payload-automation/server'
|
||||
|
||||
// Client-side components
|
||||
import { TriggerWorkflowButton } from '@xtr-dev/payload-automation/client'
|
||||
|
||||
// Types only (safe for both server and client)
|
||||
import type { WorkflowsPluginConfig } from '@xtr-dev/payload-automation'
|
||||
```
|
||||
|
||||
## Step Types
|
||||
|
||||
- **HTTP Request** - Make external API calls
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-workflows",
|
||||
"version": "0.0.9",
|
||||
"version": "0.0.13",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@xtr-dev/payload-workflows",
|
||||
"version": "0.0.9",
|
||||
"version": "0.0.13",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"jsonpath-plus": "^10.3.0",
|
||||
|
||||
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-automation",
|
||||
"version": "0.0.9",
|
||||
"version": "0.0.13",
|
||||
"description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
@@ -30,10 +30,10 @@
|
||||
"types": "./dist/exports/views.d.ts",
|
||||
"default": "./dist/exports/views.js"
|
||||
},
|
||||
"./steps": {
|
||||
"import": "./dist/steps/index.js",
|
||||
"types": "./dist/steps/index.d.ts",
|
||||
"default": "./dist/steps/index.js"
|
||||
"./server": {
|
||||
"import": "./dist/exports/server.js",
|
||||
"types": "./dist/exports/server.d.ts",
|
||||
"default": "./dist/exports/server.js"
|
||||
}
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -606,6 +606,12 @@ export class WorkflowExecutor {
|
||||
previousDoc: unknown,
|
||||
req: PayloadRequest
|
||||
): Promise<void> {
|
||||
this.logger.info({
|
||||
collection,
|
||||
operation,
|
||||
docId: (doc as any)?.id
|
||||
}, 'executeTriggeredWorkflows called')
|
||||
|
||||
try {
|
||||
// Find workflows with matching triggers
|
||||
const workflows = await this.payload.find({
|
||||
@@ -614,21 +620,46 @@ export class WorkflowExecutor {
|
||||
limit: 100,
|
||||
req
|
||||
})
|
||||
|
||||
this.logger.info({
|
||||
workflowCount: workflows.docs.length
|
||||
}, 'Found workflows to check')
|
||||
|
||||
for (const workflow of workflows.docs) {
|
||||
// Check if this workflow has a matching trigger
|
||||
const triggers = workflow.triggers as Array<{
|
||||
collection: string
|
||||
collection?: string
|
||||
collectionSlug?: string
|
||||
condition?: string
|
||||
operation: string
|
||||
type: string
|
||||
}>
|
||||
|
||||
this.logger.debug({
|
||||
workflowId: workflow.id,
|
||||
workflowName: workflow.name,
|
||||
triggerCount: triggers?.length || 0,
|
||||
triggers: triggers?.map(t => ({
|
||||
type: t.type,
|
||||
collection: t.collection,
|
||||
collectionSlug: t.collectionSlug,
|
||||
operation: t.operation
|
||||
}))
|
||||
}, 'Checking workflow triggers')
|
||||
|
||||
const matchingTriggers = triggers?.filter(trigger =>
|
||||
trigger.type === 'collection-trigger' &&
|
||||
trigger.collection === collection &&
|
||||
(trigger.collection === collection || trigger.collectionSlug === collection) &&
|
||||
trigger.operation === operation
|
||||
) || []
|
||||
|
||||
this.logger.info({
|
||||
workflowId: workflow.id,
|
||||
workflowName: workflow.name,
|
||||
matchingTriggerCount: matchingTriggers.length,
|
||||
targetCollection: collection,
|
||||
targetOperation: operation
|
||||
}, 'Matching triggers found')
|
||||
|
||||
for (const trigger of matchingTriggers) {
|
||||
// Create execution context for condition evaluation
|
||||
|
||||
26
src/exports/server.ts
Normal file
26
src/exports/server.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// Server-side only exports - should never be bundled for client
|
||||
// These contain Node.js dependencies and should only be used server-side
|
||||
|
||||
export { triggerCustomWorkflow, triggerWorkflowById } from '../core/trigger-custom-workflow.js'
|
||||
export { WorkflowExecutor } from '../core/workflow-executor.js'
|
||||
export { workflowsPlugin } from '../plugin/index.js'
|
||||
|
||||
// Export all step handlers (server-side only)
|
||||
export {
|
||||
createDocumentHandler,
|
||||
deleteDocumentHandler,
|
||||
httpStepHandler,
|
||||
readDocumentHandler,
|
||||
sendEmailHandler,
|
||||
updateDocumentHandler
|
||||
} from '../steps/index.js'
|
||||
|
||||
// Export step tasks configurations (server-side only)
|
||||
export {
|
||||
CreateDocumentStepTask,
|
||||
DeleteDocumentStepTask,
|
||||
HttpRequestStepTask,
|
||||
ReadDocumentStepTask,
|
||||
SendEmailStepTask,
|
||||
UpdateDocumentStepTask
|
||||
} from '../steps/index.js'
|
||||
36
src/index.ts
36
src/index.ts
@@ -1,19 +1,21 @@
|
||||
export { triggerCustomWorkflow, triggerWorkflowById } from './core/trigger-custom-workflow.js'
|
||||
export type { CustomTriggerOptions, TriggerResult } from './core/trigger-custom-workflow.js'
|
||||
export { WorkflowExecutor } from './core/workflow-executor.js'
|
||||
export type { ExecutionContext, Workflow, WorkflowStep, WorkflowTrigger } from './core/workflow-executor.js'
|
||||
export type { WorkflowsPluginConfig } from './plugin/config-types.js'
|
||||
export { workflowsPlugin } from './plugin/index.js'
|
||||
// Main export contains only types and client-safe utilities
|
||||
// Server-side functions are exported via '@xtr-dev/payload-automation/server'
|
||||
|
||||
// Export all step tasks
|
||||
export {
|
||||
CreateDocumentStepTask,
|
||||
DeleteDocumentStepTask,
|
||||
HttpRequestStepTask,
|
||||
ReadDocumentStepTask,
|
||||
SendEmailStepTask,
|
||||
UpdateDocumentStepTask
|
||||
} from './steps/index.js'
|
||||
// Pure types only - completely safe for client bundling
|
||||
export type {
|
||||
CustomTriggerOptions,
|
||||
TriggerResult,
|
||||
ExecutionContext,
|
||||
Workflow,
|
||||
WorkflowStep,
|
||||
WorkflowTrigger,
|
||||
WorkflowsPluginConfig
|
||||
} from './types/index.js'
|
||||
|
||||
// UI components are exported via separate client export to avoid CSS import issues during type generation
|
||||
// Use: import { TriggerWorkflowButton } from '@xtr-dev/payload-automation/client'
|
||||
// Server-side functions are NOT re-exported here to avoid bundling issues
|
||||
// Import server-side functions from the /server export instead
|
||||
|
||||
// Server functions and plugin should be imported from '/server':
|
||||
// import { workflowsPlugin } from '@xtr-dev/payload-automation/server'
|
||||
// UI components should be imported from '/client':
|
||||
// import { TriggerWorkflowButton } from '@xtr-dev/payload-automation/client'
|
||||
|
||||
14
src/test/basic.test.ts
Normal file
14
src/test/basic.test.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
|
||||
describe('PayloadCMS Automation Plugin', () => {
|
||||
it('should export the plugin function from server export', async () => {
|
||||
const { workflowsPlugin } = await import('../exports/server.js')
|
||||
expect(workflowsPlugin).toBeDefined()
|
||||
expect(typeof workflowsPlugin).toBe('function')
|
||||
})
|
||||
|
||||
it('should have the correct package name', async () => {
|
||||
// Basic test to ensure the plugin can be imported
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
62
src/types/index.ts
Normal file
62
src/types/index.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
// Pure type definitions for client-safe exports
|
||||
// This file contains NO runtime code and can be safely bundled
|
||||
|
||||
export interface CustomTriggerOptions {
|
||||
workflowId: string
|
||||
triggerData?: any
|
||||
req?: any // PayloadRequest type, but avoiding import to keep this client-safe
|
||||
}
|
||||
|
||||
export interface TriggerResult {
|
||||
success: boolean
|
||||
runId?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
export interface ExecutionContext {
|
||||
trigger: {
|
||||
type: string
|
||||
doc?: any
|
||||
data?: any
|
||||
}
|
||||
steps: Record<string, {
|
||||
output?: any
|
||||
state: 'pending' | 'running' | 'succeeded' | 'failed'
|
||||
}>
|
||||
payload: any // Payload instance
|
||||
req: any // PayloadRequest
|
||||
}
|
||||
|
||||
export interface WorkflowStep {
|
||||
id: string
|
||||
type: string
|
||||
input: Record<string, any>
|
||||
dependencies?: string[]
|
||||
}
|
||||
|
||||
export interface WorkflowTrigger {
|
||||
type: 'collection' | 'global' | 'webhook' | 'cron' | 'manual'
|
||||
collection?: string
|
||||
global?: string
|
||||
event?: 'create' | 'update' | 'delete' | 'read'
|
||||
path?: string
|
||||
cron?: string
|
||||
}
|
||||
|
||||
export interface Workflow {
|
||||
id: string
|
||||
name: string
|
||||
description?: string
|
||||
active: boolean
|
||||
triggers: WorkflowTrigger[]
|
||||
steps: WorkflowStep[]
|
||||
}
|
||||
|
||||
export interface WorkflowsPluginConfig {
|
||||
collections?: string[]
|
||||
globals?: string[]
|
||||
logging?: {
|
||||
level?: 'debug' | 'info' | 'warn' | 'error'
|
||||
enabled?: boolean
|
||||
}
|
||||
}
|
||||
8
vitest.config.ts
Normal file
8
vitest.config.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
globals: true,
|
||||
environment: 'node',
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user