n8n/packages/nodes-base/nodes/ExecuteCommand.node.ts

120 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { exec } from 'child_process';
export interface IExecReturnData {
exitCode: number;
error?: Error;
stderr: string;
stdout: string;
}
/**
* Promisifiy exec manually to also get the exit code
*
* @param {string} command
* @returns {Promise<IExecReturnData>}
*/
function execPromise(command: string): Promise<IExecReturnData> {
const returnData: IExecReturnData = {
exitCode: 0,
stderr: '',
stdout: '',
};
return new Promise((resolve, reject) => {
exec(command, { cwd: process.cwd() }, (error, stdout, stderr) => {
2019-06-23 03:35:23 -07:00
returnData.stdout = stdout.trim();
returnData.stderr = stderr.trim();
if (error) {
returnData.error = error;
}
resolve(returnData);
}).on('exit', code => { returnData.exitCode = code || 0; });
});
}
export class ExecuteCommand implements INodeType {
description: INodeTypeDescription = {
displayName: 'Execute Command',
name: 'executeCommand',
icon: 'fa:terminal',
group: ['transform'],
version: 1,
description: 'Executes a command on the host.',
defaults: {
name: 'Execute Command',
color: '#886644',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Execute Once',
name: 'executeOnce',
type: 'boolean',
default: true,
description: 'If activated it executes only once instead of once for each entry.',
},
{
displayName: 'Command',
name: 'command',
typeOptions: {
rows: 5,
},
type: 'string',
default: '',
placeholder: 'echo "test"',
description: 'The command to execute',
},
2020-10-22 06:46:03 -07:00
],
2019-06-23 03:35:23 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
let items = this.getInputData();
let command: string;
const executeOnce = this.getNodeParameter('executeOnce', 0) as boolean;
if (executeOnce === true) {
items = [items[0]];
}
const returnItems: INodeExecutionData[] = [];
2019-06-23 03:35:23 -07:00
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
command = this.getNodeParameter('command', itemIndex) as string;
const {
// error, TODO: Later make it possible to select if it should fail on error or not
exitCode,
stdout,
stderr,
} = await execPromise(command);
returnItems.push(
{
json: {
exitCode,
stderr,
stdout,
},
2020-10-22 09:00:28 -07:00
},
);
2019-06-23 03:35:23 -07:00
}
return this.prepareOutputData(returnItems);
2019-06-23 03:35:23 -07:00
}
}