Skip to content

Commit 7d10238

Browse files
committed
fixed ignored option on Mac os
1 parent 90aac96 commit 7d10238

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

packages/nodes-base/nodes/LocalFileTrigger/LocalFileTrigger.node.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class LocalFileTrigger implements INodeType {
146146
name: 'ignored',
147147
type: 'string',
148148
default: '',
149-
placeholder: '**/*.txt',
149+
placeholder: '**/*.txt or ignore-me/subfolder',
150150
description:
151151
'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.',
152152
},
@@ -202,6 +202,26 @@ export class LocalFileTrigger implements INodeType {
202202
description:
203203
'Whether to use polling for watching. Typically necessary to successfully watch files over a network.',
204204
},
205+
{
206+
displayName: 'Ignore Mode',
207+
name: 'ignoreMode',
208+
type: 'options',
209+
options: [
210+
{
211+
name: 'Regex',
212+
value: 'regex',
213+
description: 'Use regex patterns (e.g., **/*.txt)',
214+
},
215+
{
216+
name: 'Function',
217+
value: 'function',
218+
description:
219+
'Wraps the ignored value in a function that checks if the file path includes the ignored value',
220+
},
221+
],
222+
default: 'regex',
223+
description: 'Wether to use glob Anymatch patterns or a function to ignore files',
224+
},
205225
],
206226
},
207227
],
@@ -218,9 +238,9 @@ export class LocalFileTrigger implements INodeType {
218238
} else {
219239
events = this.getNodeParameter('events', []) as string[];
220240
}
221-
241+
const ignored = options.ignored === '' ? undefined : (options.ignored as string);
222242
const watcher = watch(path, {
223-
ignored: options.ignored === '' ? undefined : (options.ignored as string),
243+
ignored: options.ignoreMode === 'regex' ? ignored : (x) => x.includes(ignored as string),
224244
persistent: true,
225245
ignoreInitial:
226246
options.ignoreInitial === undefined ? true : (options.ignoreInitial as boolean),
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import chokidar from 'chokidar';
2+
import type { ITriggerFunctions } from 'n8n-workflow';
3+
4+
import { LocalFileTrigger } from '../LocalFileTrigger.node';
5+
6+
jest.mock('chokidar');
7+
8+
const mockWatcher = {
9+
on: jest.fn(),
10+
close: jest.fn().mockResolvedValue(undefined),
11+
};
12+
13+
(chokidar.watch as unknown as jest.Mock).mockReturnValue(mockWatcher);
14+
15+
describe('LocalFileTrigger', () => {
16+
let node: LocalFileTrigger;
17+
let emitSpy: jest.Mock;
18+
let context: ITriggerFunctions;
19+
20+
beforeEach(() => {
21+
node = new LocalFileTrigger();
22+
emitSpy = jest.fn();
23+
24+
context = {
25+
getNodeParameter: jest.fn(),
26+
emit: emitSpy,
27+
helpers: {
28+
returnJsonArray: (data: unknown[]) => data,
29+
},
30+
} as unknown as ITriggerFunctions;
31+
32+
jest.clearAllMocks();
33+
});
34+
35+
it('should set up chokidar with correct options for folder + regex ignore', async () => {
36+
(context.getNodeParameter as jest.Mock)
37+
.mockReturnValueOnce('folder')
38+
.mockReturnValueOnce('/some/folder')
39+
.mockReturnValueOnce({
40+
ignored: '**/*.txt',
41+
ignoreMode: 'regex',
42+
ignoreInitial: true,
43+
followSymlinks: true,
44+
depth: 1,
45+
usePolling: false,
46+
awaitWriteFinish: false,
47+
})
48+
.mockReturnValueOnce(['add']);
49+
50+
await node.trigger.call(context);
51+
52+
expect(chokidar.watch).toHaveBeenCalledWith(
53+
'/some/folder',
54+
expect.objectContaining({
55+
ignored: '**/*.txt',
56+
ignoreInitial: true,
57+
depth: 1,
58+
followSymlinks: true,
59+
usePolling: false,
60+
awaitWriteFinish: false,
61+
}),
62+
);
63+
64+
expect(mockWatcher.on).toHaveBeenCalledWith('add', expect.any(Function));
65+
});
66+
67+
it('should wrap ignored in function for ignoreMode=function', async () => {
68+
(context.getNodeParameter as jest.Mock)
69+
.mockReturnValueOnce('folder')
70+
.mockReturnValueOnce('/folder')
71+
.mockReturnValueOnce({
72+
ignored: 'node_modules',
73+
ignoreMode: 'function',
74+
})
75+
.mockReturnValueOnce(['change']);
76+
77+
await node.trigger.call(context);
78+
79+
const call = (chokidar.watch as jest.Mock).mock.calls[0][1];
80+
expect(typeof call.ignored).toBe('function');
81+
expect(call.ignored('folder/node_modules/stuff')).toBe(true);
82+
expect(call.ignored('folder/src/index.js')).toBe(false);
83+
});
84+
85+
it('should emit an event when a file changes', async () => {
86+
(context.getNodeParameter as jest.Mock)
87+
.mockReturnValueOnce('folder')
88+
.mockReturnValueOnce('/watched')
89+
.mockReturnValueOnce({})
90+
.mockReturnValueOnce(['change']);
91+
92+
await node.trigger.call(context);
93+
94+
const callback = mockWatcher.on.mock.calls.find(([event]) => event === 'change')?.[1];
95+
callback?.('/watched/file.txt');
96+
97+
expect(emitSpy).toHaveBeenCalledWith([[{ event: 'change', path: '/watched/file.txt' }]]);
98+
});
99+
100+
it('should use "change" as the only event if watching a specific file', async () => {
101+
(context.getNodeParameter as jest.Mock)
102+
.mockReturnValueOnce('file')
103+
.mockReturnValueOnce('/watched/file.txt')
104+
.mockReturnValueOnce({});
105+
106+
await node.trigger.call(context);
107+
108+
expect(mockWatcher.on).toHaveBeenCalledWith('change', expect.any(Function));
109+
});
110+
});

0 commit comments

Comments
 (0)