Changing command to always output JSON information

This commit is contained in:
Omar Ajoue 2021-03-15 09:40:49 +01:00
parent 2091b0e1f2
commit aeaf11bb8e

View file

@ -13,7 +13,6 @@ import {
CredentialTypes, CredentialTypes,
Db, Db,
ExternalHooks, ExternalHooks,
GenericHelpers,
IWorkflowExecutionDataProcess, IWorkflowExecutionDataProcess,
LoadNodesAndCredentials, LoadNodesAndCredentials,
NodeTypes, NodeTypes,
@ -36,7 +35,7 @@ export class ExecuteAll extends Command {
static examples = [ static examples = [
`$ n8n executeAll`, `$ n8n executeAll`,
`$ n8n executeAll --debug --json --output=/data/output.json`, `$ n8n executeAll --debug --output=/data/output.json`,
`$ n8n executeAll --snapshot=/data/snapshots`, `$ n8n executeAll --snapshot=/data/snapshots`,
`$ n8n executeAll --compare=/data/previousExecutionData`, `$ n8n executeAll --compare=/data/previousExecutionData`,
]; ];
@ -46,9 +45,6 @@ export class ExecuteAll extends Command {
debug: flags.boolean({ debug: flags.boolean({
description: 'Toggles on displaying all errors and debug messages.', description: 'Toggles on displaying all errors and debug messages.',
}), }),
json: flags.boolean({
description: 'Toggles on displaying results in JSON format.',
}),
output: flags.string({ output: flags.string({
description: 'Enable execution saving, You must inform an existing folder to save execution via this param', description: 'Enable execution saving, You must inform an existing folder to save execution via this param',
}), }),
@ -65,46 +61,39 @@ export class ExecuteAll extends Command {
const { flags } = this.parse(ExecuteAll); const { flags } = this.parse(ExecuteAll);
const debug = flags.debug !== undefined; const debug = flags.debug !== undefined;
const json = flags.json !== undefined;
if (flags.snapshot !== undefined) { if (flags.snapshot !== undefined) {
if (fs.existsSync(flags.snapshot)) { if (fs.existsSync(flags.snapshot)) {
if (!fs.lstatSync(flags.snapshot).isDirectory()) { if (!fs.lstatSync(flags.snapshot).isDirectory()) {
GenericHelpers.logOutput(`The paramenter --snapshot must be an existing directory`); console.log(`The paramenter --snapshot must be an existing directory`);
return; return;
} }
} else { } else {
GenericHelpers.logOutput(`The paramenter --snapshot must be an existing directory`); console.log(`The paramenter --snapshot must be an existing directory`);
return; return;
} }
} }
if (flags.compare !== undefined) { if (flags.compare !== undefined) {
if (fs.existsSync(flags.compare)) { if (fs.existsSync(flags.compare)) {
if (!fs.lstatSync(flags.compare).isDirectory()) { if (!fs.lstatSync(flags.compare).isDirectory()) {
GenericHelpers.logOutput(`The paramenter --compare must be an existing directory`); console.log(`The paramenter --compare must be an existing directory`);
return; return;
} }
} else { } else {
GenericHelpers.logOutput(`The paramenter --compare must be an existing directory`); console.log(`The paramenter --compare must be an existing directory`);
return; return;
} }
} }
if (flags.output !== undefined) { if (flags.output !== undefined) {
if (fs.existsSync(flags.output)) { if (fs.existsSync(flags.output)) {
if (!fs.lstatSync(flags.output).isDirectory()) { if (fs.lstatSync(flags.output).isDirectory()) {
GenericHelpers.logOutput(`The paramenter --output must be an existing directory`); console.log(`The paramenter --output must be a writable file`);
return; return;
} }
} else {
GenericHelpers.logOutput(`The paramenter --output must be an existing directory`);
return;
} }
} }
if (flags.output !== undefined && json!==true) {
GenericHelpers.logOutput(`You must use --json when using --output`);
}
// Start directly with the init of the database to improve startup time // Start directly with the init of the database to improve startup time
const startDbInitPromise = Db.init(); const startDbInitPromise = Db.init();
@ -185,13 +174,9 @@ export class ExecuteAll extends Command {
if (startNode === undefined) { 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.
if (json === true) { executionResult.error = 'Workflow cannot be started as it does not contain a "Start" node.';
executionResult.error = 'Workflow cannot be started as it does not contain a "Start" node.'; result.summary.failedExecutions++;
result.summary.failedExecutions++; result.executions.push(executionResult);
result.executions.push(executionResult);
}else{
GenericHelpers.logOutput(`Workflow ID ${workflowData.id} cannot be started as it does not contain a "Start" node.`);
}
continue; continue;
} }
@ -212,13 +197,9 @@ export class ExecuteAll extends Command {
const data = await activeExecutions.getPostExecutePromise(executionId); const data = await activeExecutions.getPostExecutePromise(executionId);
if (data === undefined) { if (data === undefined) {
if (json === true) { executionResult.error = 'Workflow did not return any data.';
executionResult.error = 'Workflow did not return any data.'; result.summary.failedExecutions++;
result.summary.failedExecutions++; result.executions.push(executionResult);
result.executions.push(executionResult);
}else{
GenericHelpers.logOutput(`Workflow ${workflowData.id} did not return any data.`);
}
continue; continue;
} }
workflowData.nodes.forEach(node => { workflowData.nodes.forEach(node => {
@ -228,13 +209,9 @@ export class ExecuteAll extends Command {
executionResult.finished = (data?.finished !== undefined) as boolean; executionResult.finished = (data?.finished !== undefined) as boolean;
if (data.data.resultData.error) { if (data.data.resultData.error) {
if (json === true) { executionResult.error = data.data.resultData.error.message;
executionResult.error = data.data.resultData.error.message; result.summary.failedExecutions++;
result.summary.failedExecutions++; result.executions.push(executionResult);
result.executions.push(executionResult);
}else{
GenericHelpers.logOutput(`Workflow ${workflowData.id} failed.`);
}
if (debug === true) { if (debug === true) {
this.log(JSON.stringify(data, null, 2)); this.log(JSON.stringify(data, null, 2));
console.log(data.data.resultData.error); console.log(data.data.resultData.error);
@ -243,15 +220,10 @@ export class ExecuteAll extends Command {
} }
const serializedData = JSON.stringify(data, null, 2); const serializedData = JSON.stringify(data, null, 2);
if (json === true) { if (flags.compare === undefined){
if (flags.compare === undefined){ result.summary.succeededExecution++;
result.summary.succeededExecution++; result.executions.push(executionResult);
result.executions.push(executionResult); } else {
}
}else{
GenericHelpers.logOutput(`Workflow ${workflowData.id} succeeded.`);
}
if (flags.compare !== undefined) {
const fileName = (flags.compare.endsWith(sep) ? flags.compare : flags.compare + sep) + `${workflowData.id}-snapshot.json`; const fileName = (flags.compare.endsWith(sep) ? flags.compare : flags.compare + sep) + `${workflowData.id}-snapshot.json`;
if (fs.existsSync(fileName) === true) { if (fs.existsSync(fileName) === true) {
@ -262,32 +234,22 @@ export class ExecuteAll extends Command {
if (changes !== undefined) { if (changes !== undefined) {
// we have structural changes. Report them. // we have structural changes. Report them.
if (json === true) { executionResult.error = `Workflow may contain breaking changes`;
executionResult.error = `Workflow may contain breaking changes`; executionResult.changes = changes;
executionResult.changes = changes; result.summary.failedExecutions++;
result.summary.failedExecutions++; result.executions.push(executionResult);
result.executions.push(executionResult);
}else{
console.log(`Workflow ID ${workflowData.id} may contain breaking changes: `, changes);
}
if (debug === true) { if (debug === true) {
// @ts-ignore // @ts-ignore
console.log('Detailed changes: ', diffString(JSON.parse(contents), data, undefined, {keysOnly: true})); console.log('Detailed changes: ', diffString(JSON.parse(contents), data, undefined, {keysOnly: true}));
} }
}else{ }else{
if (json === true) { result.summary.succeededExecution++;
result.summary.succeededExecution++; result.executions.push(executionResult);
result.executions.push(executionResult);
}
} }
} else { } else {
if (json === true) { executionResult.error = 'Snapshot for not found.';
executionResult.error = 'Snapshot for not found.'; result.summary.failedExecutions++;
result.summary.failedExecutions++; result.executions.push(executionResult);
result.executions.push(executionResult);
}else{
GenericHelpers.logOutput(`Snapshot for ${workflowData.id} not found.`);
}
} }
} }
// Save snapshots only after comparing - this is to make sure we're updating // Save snapshots only after comparing - this is to make sure we're updating
@ -298,13 +260,9 @@ export class ExecuteAll extends Command {
} }
} catch (e) { } catch (e) {
if (json === true) { executionResult.error = 'Workflow failed to execute.';
executionResult.error = 'Workflow failed to execute.'; result.summary.exceptions++;
result.summary.exceptions++; result.executions.push(executionResult);
result.executions.push(executionResult);
}else{
GenericHelpers.logOutput(`Workflow ${workflowData.id} failed to execute.`);
}
if (debug === true) { if (debug === true) {
console.error(e.message); console.error(e.message);
console.error(e.stack); console.error(e.stack);
@ -312,13 +270,20 @@ export class ExecuteAll extends Command {
} }
} }
if (json === true){ if(flags.output !== undefined){
if(flags.output !== undefined){ fs.writeFileSync(flags.output,JSON.stringify(result, null, 2));
const fileName = (flags.output.endsWith(sep) ? flags.output : flags.output + sep) + `workflows-executions-${Date.now()}.json`; console.log('Execution finished.');
fs.writeFileSync(fileName,JSON.stringify(result, null, 2)); console.log('Summary:');
}else{ console.log(`\tSuccess: ${result.summary.succeededExecution}`);
GenericHelpers.logOutput(JSON.stringify(result, null, 2)); console.log(`\tFailures: ${result.summary.failedExecutions}`);
} console.log(`\tExceptions: ${result.summary.exceptions}`);
console.log('\nNodes covered:');
Object.entries(result.coveredNodes).map(entry => {
console.log(`\t${entry[0]}: ${entry[1]}`);
});
console.log('\nCheck the JSON file for more details.');
}else{
console.log(JSON.stringify(result, null, 2));
} }
if(result.summary.succeededExecution !== result.workflowsNumber){ if(result.summary.succeededExecution !== result.workflowsNumber){
this.exit(1); this.exit(1);