n8n-nodes-drop-a-log/nodes/DropALog/DropALog.node.ts
2024-09-25 20:48:43 -05:00

142 lines
3.6 KiB
TypeScript

import {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
IDataObject,
} from 'n8n-workflow';
import { execFile } from 'child_process';
interface IExecReturnData {
exitCode: number;
error?: Error;
stderr: string;
stdout: string;
}
interface ILogData {
log: string;
title: string;
date: string;
includeJson: boolean;
item: any;
out: IExecReturnData|null;
}
/**
* Promisifiy exec manually to also get the exit code
*
* @param {string} command
* @returns {Promise<IExecReturnData>}
*/
function execPromise(command: string, args: string[]): Promise<IExecReturnData> {
const returnData: IExecReturnData = {
exitCode: 0,
stderr: '',
stdout: '',
};
return new Promise((resolve, reject) => {
execFile(command, args, { 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 DropALog implements INodeType {
description: INodeTypeDescription = {
displayName: 'Drop A Log',
name: 'dropALog',
icon: 'file:dropalog.svg',
group: ['transform'],
version: 1,
description: 'Drops data into droplog',
defaults: {
name: 'Drop A Log',
color: '#772244',
},
inputs: ['main'],
outputs: ['main'],
properties: [
// Node properties which the user gets displayed and
// can change on the node.
{
displayName: 'Log',
name: 'log',
type: 'string',
default: 'n8n',
placeholder: 'posts',
description: 'Name of log to use',
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: 'New Post',
placeholder: '',
description: 'Name of title for each post'
},
{
displayName: 'Date',
name: 'date',
type: 'string',
default: 'now',
placeholder: '',
description: 'Date to use for each post'
},
{
displayName: 'Include entire post',
name: 'includeJson',
type: 'boolean',
default: true,
description: 'If activated, the entire item will be logged. If inactive, only title and date will be used'
}
]
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
for (let idx = 0; idx < items.length; idx++) {
let item = items[idx];
let data: ILogData = {
log: this.getNodeParameter('log', idx) as string,
title: this.getNodeParameter('title', idx) as string,
date: this.getNodeParameter('date', idx) as string,
includeJson: this.getNodeParameter('includeJson', idx) as boolean,
item: null,
out: null,
};
const args: string[] = ['drop'];
args.push(data.log, data.title, '-d', data.date);
if (data.includeJson) {
const copy = JSON.parse(JSON.stringify(item.json));
delete copy.title;
delete copy.date;
args.push('-j', JSON.stringify(copy));
data.item = copy;
}
const exit = await execPromise('my-log', args);
data.out = exit;
returnData.push(data as unknown as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}