n8n/packages/cli/src/utils.ts
कारतोफ्फेलस्क्रिप्ट™ 9bd7529193
refactor(core): Use an IoC container to manage singleton classes [Part-2] (no-changelog) (#5690)
* use typedi for UserManagementMailer

* use typedi for SamlService

* fix typos

* use typedi for Queue

* use typedi for License

* convert some more code to use typedi
2023-03-16 15:34:13 +01:00

61 lines
1.7 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { CliWorkflowOperationError, SubworkflowOperationError } from 'n8n-workflow';
import type { INode } from 'n8n-workflow';
import { START_NODES } from './constants';
/**
* Returns if the given id is a valid workflow id
*/
export function isWorkflowIdValid(id: string | null | undefined): boolean {
return !(typeof id === 'string' && isNaN(parseInt(id, 10)));
}
function findWorkflowStart(executionMode: 'integrated' | 'cli') {
return function (nodes: INode[]) {
const executeWorkflowTriggerNode = nodes.find(
(node) => node.type === 'n8n-nodes-base.executeWorkflowTrigger',
);
if (executeWorkflowTriggerNode) return executeWorkflowTriggerNode;
const startNode = nodes.find((node) => START_NODES.includes(node.type));
if (startNode) return startNode;
const title = 'Missing node to start execution';
const description =
"Please make sure the workflow you're calling contains an Execute Workflow Trigger node";
if (executionMode === 'integrated') {
throw new SubworkflowOperationError(title, description);
}
throw new CliWorkflowOperationError(title, description);
};
}
export const findSubworkflowStart = findWorkflowStart('integrated');
export const findCliWorkflowStart = findWorkflowStart('cli');
export const alphabetizeKeys = (obj: INode) =>
Object.keys(obj)
.sort()
.reduce<Partial<INode>>(
(acc, key) => ({
...acc,
// @ts-expect-error @TECH_DEBT Adding index signature to INode causes type issues downstream
[key]: obj[key],
}),
{},
);
export const separate = <T>(array: T[], test: (element: T) => boolean) => {
const pass: T[] = [];
const fail: T[] = [];
array.forEach((i) => (test(i) ? pass : fail).push(i));
return [pass, fail];
};