diff --git a/packages/cli/commands/execute.ts b/packages/cli/commands/execute.ts index 448af3de3d..7501b4aa9b 100644 --- a/packages/cli/commands/execute.ts +++ b/packages/cli/commands/execute.ts @@ -2,7 +2,10 @@ import { promises as fs } from 'fs'; import { Command, flags } from '@oclif/command'; import { UserSettings, -} from "n8n-core"; +} from 'n8n-core'; +import { + INode, +} from 'n8n-workflow'; import { ActiveExecutions, @@ -111,14 +114,15 @@ export class Execute extends Command { // Check if the workflow contains the required "Start" node // "requiredNodeTypes" are also defined in editor-ui/views/NodeView.vue const requiredNodeTypes = ['n8n-nodes-base.start']; - let startNodeFound = false; + let startNode: INode | undefined= undefined; for (const node of workflowData!.nodes) { if (requiredNodeTypes.includes(node.type)) { - startNodeFound = true; + startNode = node; + break; } } - if (startNodeFound === false) { + if (startNode === undefined) { // If the workflow does not contain a start-node we can not know what // should be executed and with which data to start. GenericHelpers.logOutput(`The workflow does not contain a "Start" node. So it can not be executed.`); @@ -131,6 +135,7 @@ export class Execute extends Command { const runData: IWorkflowExecutionDataProcess = { credentials, executionMode: 'cli', + startNodes: [startNode.name], workflowData: workflowData!, }; diff --git a/packages/cli/commands/start.ts b/packages/cli/commands/start.ts index 5494b5c5c3..f1951f5002 100644 --- a/packages/cli/commands/start.ts +++ b/packages/cli/commands/start.ts @@ -2,7 +2,7 @@ import * as localtunnel from 'localtunnel'; import { TUNNEL_SUBDOMAIN_ENV, UserSettings, -} from "n8n-core"; +} from 'n8n-core'; import { Command, flags } from '@oclif/command'; const open = require('open'); // import { dirname } from 'path'; diff --git a/packages/workflow/src/Workflow.ts b/packages/workflow/src/Workflow.ts index 52acf8391d..19749c7920 100644 --- a/packages/workflow/src/Workflow.ts +++ b/packages/workflow/src/Workflow.ts @@ -735,6 +735,40 @@ export class Workflow { + /** + * Returns from which of the given nodes the workflow should get started from + * + * @param {string[]} nodeNames The potential start nodes + * @returns {(INode | undefined)} + * @memberof Workflow + */ + __getStartNode(nodeNames: string[]): INode | undefined { + // Check if there are any trigger or poll nodes and then return the first one + let node: INode; + let nodeType: INodeType; + for (const nodeName of nodeNames) { + node = this.nodes[nodeName]; + nodeType = this.nodeTypes.getByName(node.type) as INodeType; + + if (nodeType.trigger !== undefined || nodeType.poll !== undefined) { + return node; + } + } + + // Check if there is the actual "start" node + const startNodeType = 'n8n-nodes-base.start'; + for (const nodeName of nodeNames) { + node = this.nodes[nodeName]; + if (node.type === startNodeType) { + return node; + } + } + + return undefined; + } + + + /** * Returns the start node to start the worfklow from * @@ -743,7 +777,6 @@ export class Workflow { * @memberof Workflow */ getStartNode(destinationNode?: string): INode | undefined { - const startNodeType = 'n8n-nodes-base.start'; if (destinationNode) { // Find the highest parent nodes of the given one @@ -756,42 +789,17 @@ export class Workflow { } // Check which node to return as start node - - // Check if there are any trigger or poll nodes and then return the first one - let node: INode; - let nodeType: INodeType; - for (const nodeName of nodeNames) { - node = this.nodes[nodeName]; - nodeType = this.nodeTypes.getByName(node.type) as INodeType; - - if (nodeType.trigger !== undefined || nodeType.poll !== undefined) { - return node; - } - } - - // Check if there is the actual "start" node - for (const nodeName of nodeNames) { - node = this.nodes[nodeName]; - if (node.type === startNodeType) { - return node; - } + const node = this.__getStartNode(nodeNames); + if (node !== undefined) { + return node; } // If none of the above did find anything simply return the // first parent node in the list return this.nodes[nodeNames[0]]; - } else { - // No node given so start from "start" node - let node: INode; - for (const nodeName of Object.keys(this.nodes)) { - node = this.nodes[nodeName]; - if (node.type === startNodeType) { - return node; - } - } } - return undefined; + return this.__getStartNode(Object.keys(this.nodes)); }