If trigger nodes are in workflow use them as default start-node #379

This commit is contained in:
Jan Oberhauser
2020-03-16 09:58:49 +01:00
parent 08715b64e5
commit f145c499c0
3 changed files with 48 additions and 35 deletions

View File

@@ -2,7 +2,10 @@ import { promises as fs } from 'fs';
import { Command, flags } from '@oclif/command'; import { Command, flags } from '@oclif/command';
import { import {
UserSettings, UserSettings,
} from "n8n-core"; } from 'n8n-core';
import {
INode,
} from 'n8n-workflow';
import { import {
ActiveExecutions, ActiveExecutions,
@@ -111,14 +114,15 @@ export class Execute extends Command {
// Check if the workflow contains the required "Start" node // Check if the workflow contains the required "Start" node
// "requiredNodeTypes" are also defined in editor-ui/views/NodeView.vue // "requiredNodeTypes" are also defined in editor-ui/views/NodeView.vue
const requiredNodeTypes = ['n8n-nodes-base.start']; const requiredNodeTypes = ['n8n-nodes-base.start'];
let startNodeFound = false; let startNode: INode | undefined= undefined;
for (const node of workflowData!.nodes) { for (const node of workflowData!.nodes) {
if (requiredNodeTypes.includes(node.type)) { 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 // If the workflow does not contain a start-node we can not know what
// should be executed and with which data to start. // 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.`); 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 = { const runData: IWorkflowExecutionDataProcess = {
credentials, credentials,
executionMode: 'cli', executionMode: 'cli',
startNodes: [startNode.name],
workflowData: workflowData!, workflowData: workflowData!,
}; };

View File

@@ -2,7 +2,7 @@ import * as localtunnel from 'localtunnel';
import { import {
TUNNEL_SUBDOMAIN_ENV, TUNNEL_SUBDOMAIN_ENV,
UserSettings, UserSettings,
} from "n8n-core"; } from 'n8n-core';
import { Command, flags } from '@oclif/command'; import { Command, flags } from '@oclif/command';
const open = require('open'); const open = require('open');
// import { dirname } from 'path'; // import { dirname } from 'path';

View File

@@ -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 * Returns the start node to start the worfklow from
* *
@@ -743,7 +777,6 @@ export class Workflow {
* @memberof Workflow * @memberof Workflow
*/ */
getStartNode(destinationNode?: string): INode | undefined { getStartNode(destinationNode?: string): INode | undefined {
const startNodeType = 'n8n-nodes-base.start';
if (destinationNode) { if (destinationNode) {
// Find the highest parent nodes of the given one // 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 which node to return as start node
const node = this.__getStartNode(nodeNames);
// Check if there are any trigger or poll nodes and then return the first one if (node !== undefined) {
let node: INode; return node;
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;
}
} }
// If none of the above did find anything simply return the // If none of the above did find anything simply return the
// first parent node in the list // first parent node in the list
return this.nodes[nodeNames[0]]; 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));
} }