mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
63b6c9f128
* ✏️ Alphabetize rules * 🔖 Update version * ⚡ Update lintfix command * ⚡ Run baseline lintfix * 📦 Update package-lock.json * 👕 Apply `node-param-description-untrimmed` (#3200) * Removing unneeded backticks (#3249) * 👕 Apply node-param-description-wrong-for-return-all (#3253) * 👕 Apply node-param-description-missing-limit (#3252) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply node-param-description-excess-final-period (#3250) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply node-param-description-unencoded-angle-brackets (#3256) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply node-param-description-url-missing-protocol (#3258) * 👕 Apply `node-param-description-miscased-id` (#3254) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply node-param-description-wrong-for-limit (#3257) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply node-param-description-wrong-for-ignore-ssl-issues (#3261) * 👕 Apply rule * ⚡ Restore lintfix script * ⚡ Restore lintfix script Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
137 lines
2.7 KiB
TypeScript
137 lines
2.7 KiB
TypeScript
import { IExecuteFunctions } from 'n8n-core';
|
|
import {
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeOperationError
|
|
} 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 = {
|
|
error: undefined,
|
|
exitCode: 0,
|
|
stderr: '',
|
|
stdout: '',
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
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; });
|
|
});
|
|
}
|
|
|
|
|
|
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',
|
|
},
|
|
],
|
|
};
|
|
|
|
|
|
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[] = [];
|
|
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
|
|
try{
|
|
|
|
command = this.getNodeParameter('command', itemIndex) as string;
|
|
|
|
const {
|
|
error,
|
|
exitCode,
|
|
stdout,
|
|
stderr,
|
|
} = await execPromise(command);
|
|
|
|
if (error !== undefined) {
|
|
throw new NodeOperationError(this.getNode(), error.message);
|
|
}
|
|
|
|
returnItems.push(
|
|
{
|
|
json: {
|
|
exitCode,
|
|
stderr,
|
|
stdout,
|
|
},
|
|
},
|
|
);
|
|
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnItems.push({json:{ error: error.message }});
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return this.prepareOutputData(returnItems);
|
|
}
|
|
}
|