n8n/packages/nodes-base/nodes/LocalFileTrigger/LocalFileTrigger.node.ts
Iván Ovejero 70ae90fa3c
refactor: Apply more eslint-plugin-n8n-nodes-base autofixable rules (#3432)
*  Update `lintfix` script

* 👕 Remove unneeded lint exceptions

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-description-miscased-url` (#3441)

* 👕 Apply `rule node-param-placeholder-miscased-id` (#3443)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-name-wrong-for-upsert` (#3446)

* 👕 Apply `node-param-min-value-wrong-for-limit` (#3442)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-wrong-for-dynamic-options` (#3454)

* 🔨 fix

*  Fix `Assigned To` fields

Co-authored-by: Michael Kret <michael.k@radency.com>

* 👕 Apply `rule node-param-default-wrong-for-number` (#3453)

* 👕 Apply `node-param-default-wrong-for-string` (#3452)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-miscased` (#3449)

* 🔨 fix

* 🔨 exceptions

*  review fixes

* 👕 Apply `node-param-description-lowercase-first-char` (#3451)

*  fix

*  review fixes

*  fix

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-description-wrong-for-dynamic-options` (#3456)

* Rule working as intended

* Add rule

* 🔥 Remove repetitions

* 👕 Add exceptions

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Small fix for `node-param-description-wrong-for-dynamic-options`

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3460)

* 👕 Apply `node-param-description-line-break-html-tag` (#3462)

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-options-type-unsorted-items` (#3459)

*  fix

* 🔨 exceptions

* Add exception for Salesmate and Zoom

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

*  Restore `lintfix` command

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 19:23:49 +02:00

222 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ITriggerFunctions } from 'n8n-core';
import {
IDataObject,
INodeType,
INodeTypeDescription,
ITriggerResponse,
} from 'n8n-workflow';
import { watch } from 'chokidar';
export class LocalFileTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Local File Trigger',
name: 'localFileTrigger',
icon: 'fa:folder-open',
group: ['trigger'],
version: 1,
subtitle: '=Path: {{$parameter["path"]}}',
description: 'Triggers a workflow on file system changes',
eventTriggerDescription: '',
defaults: {
name: 'Local File Trigger',
color: '#404040',
},
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Trigger On',
name: 'triggerOn',
type: 'options',
options: [
{
name: 'Changes to a Specific File',
value: 'file',
},
{
name: 'Changes Involving a Specific Folder',
value: 'folder',
},
],
required: true,
default: '',
},
{
displayName: 'File to Watch',
name: 'path',
type: 'string',
displayOptions: {
show: {
triggerOn: [
'file',
],
},
},
default: '',
placeholder: '/data/invoices/1.pdf',
},
{
displayName: 'Folder to Watch',
name: 'path',
type: 'string',
displayOptions: {
show: {
triggerOn: [
'folder',
],
},
},
default: '',
placeholder: '/data/invoices',
},
{
displayName: 'Watch for',
name: 'events',
type: 'multiOptions',
displayOptions: {
show: {
triggerOn: [
'folder',
],
},
},
options: [
{
name: 'File Added',
value: 'add',
description: 'Triggers whenever a new file was added',
},
{
name: 'File Changed',
value: 'change',
description: 'Triggers whenever a file was changed',
},
{
name: 'File Deleted',
value: 'unlink',
description: 'Triggers whenever a file was deleted',
},
{
name: 'Folder Added',
value: 'addDir',
description: 'Triggers whenever a new folder was added',
},
{
name: 'Folder Deleted',
value: 'unlinkDir',
description: 'Triggers whenever a folder was deleted',
},
],
required: true,
default: [],
description: 'The events to listen to',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Include Linked Files/Folders',
name: 'followSymlinks',
type: 'boolean',
default: true,
description: 'When activated, linked files/folders will also be watched (this includes symlinks, aliases on MacOS and shortcuts on Windows). Otherwise only the links themselves will be monitored).',
},
{
displayName: 'Ignore',
name: 'ignored',
type: 'string',
default: '',
placeholder: '**/*.txt',
description: 'Files or paths to ignore. The whole path is tested, not just the filename. Supports <a href="https://github.com/micromatch/anymatch">Anymatch</a>- syntax.',
},
// eslint-disable-next-line n8n-nodes-base/node-param-default-missing
{
displayName: 'Max Folder Depth',
name: 'depth',
type: 'options',
options: [
{
name: '1 Levels Down',
value: 1,
},
{
name: '2 Levels Down',
value: 2,
},
{
name: '3 Levels Down',
value: 3,
},
{
name: '4 Levels Down',
value: 4,
},
{
name: '5 Levels Down',
value: 5,
},
{
name: 'Top Folder Only',
value: 0,
},
{
name: 'Unlimited',
value: -1,
},
],
default: -1,
description: 'How deep into the folder structure to watch for changes',
},
],
},
],
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const triggerOn = this.getNodeParameter('triggerOn') as string;
const path = this.getNodeParameter('path') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
let events: string[];
if (triggerOn === 'file') {
events = [ 'change' ];
} else {
events = this.getNodeParameter('events', []) as string[];
}
const watcher = watch(path, {
ignored: options.ignored,
persistent: true,
ignoreInitial: true,
followSymlinks: options.followSymlinks === undefined ? true : options.followSymlinks as boolean,
depth: [-1, undefined].includes(options.depth as number) ? undefined : options.depth as number,
});
const executeTrigger = (event: string, path: string) => {
this.emit([this.helpers.returnJsonArray([{ event,path }])]);
};
for (const eventName of events) {
watcher.on(eventName, path => executeTrigger(eventName, path));
}
function closeFunction() {
return watcher.close();
}
return {
closeFunction,
};
}
}