🎉 dropALog for n8n 1.x
This commit is contained in:
parent
7eb8838df5
commit
1a75c1ba1c
12 changed files with 182 additions and 542 deletions
142
nodes/DropALog/DropALog.node.ts
Normal file
142
nodes/DropALog/DropALog.node.ts
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
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)];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue