UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

113 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExecuteCommand = void 0; const child_process_1 = require("child_process"); const n8n_workflow_1 = require("n8n-workflow"); /** * Promisifiy exec manually to also get the exit code * */ async function execPromise(command) { const returnData = { error: undefined, exitCode: 0, stderr: '', stdout: '', }; return await new Promise((resolve, _reject) => { (0, child_process_1.exec)(command, { cwd: process.cwd() }, (error, stdout, stderr) => { returnData.stdout = stdout.trim(); returnData.stderr = stderr.trim(); if (error) { returnData.error = error; } resolve(returnData); }).on('exit', (code) => { returnData.exitCode = code || 0; }); }); } class ExecuteCommand { description = { displayName: 'Execute Command', name: 'executeCommand', icon: 'fa:terminal', iconColor: 'crimson', group: ['transform'], version: 1, description: 'Executes a command on the host', defaults: { name: 'Execute Command', color: '#886644', }, usableAsTool: true, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], properties: [ { displayName: 'Execute Once', name: 'executeOnce', type: 'boolean', default: true, description: 'Whether to execute 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', required: true, }, ], }; async execute() { let items = this.getInputData(); let command; const executeOnce = this.getNodeParameter('executeOnce', 0); if (executeOnce) { items = [items[0]]; } const returnItems = []; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { command = this.getNodeParameter('command', itemIndex); const { error, exitCode, stdout, stderr } = await execPromise(command); if (error !== undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error.message, { itemIndex }); } returnItems.push({ json: { exitCode, stderr, stdout, }, pairedItem: { item: itemIndex, }, }); } catch (error) { if (this.continueOnFail()) { returnItems.push({ json: { error: error.message, }, pairedItem: { item: itemIndex, }, }); continue; } throw error; } } return [returnItems]; } } exports.ExecuteCommand = ExecuteCommand; //# sourceMappingURL=ExecuteCommand.node.js.map