Skip to content

Commit aa40735

Browse files
authored
fix(Local File Trigger Node): Fix ignored option on Mac os (#15872)
1 parent 023aa15 commit aa40735

File tree

2 files changed

+135
-4
lines changed

2 files changed

+135
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ 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:
151-
'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.',
151+
"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. Regex patterns may not work on macOS. To ignore files based on substring matching, use the 'Ignore Mode' option with 'Contain'.",
152152
},
153153
{
154154
displayName: 'Ignore Existing Files/Folders',
@@ -202,6 +202,27 @@ 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: 'Match',
212+
value: 'match',
213+
description:
214+
'Ignore files using regex patterns (e.g., **/*.txt), Not supported on macOS',
215+
},
216+
{
217+
name: 'Contain',
218+
value: 'contain',
219+
description: 'Ignore files if their path contains the specified value',
220+
},
221+
],
222+
default: 'match',
223+
description:
224+
'Whether to ignore files using regex matching (Anymatch patterns) or by checking if the path contains a specified value',
225+
},
205226
],
206227
},
207228
],
@@ -218,9 +239,9 @@ export class LocalFileTrigger implements INodeType {
218239
} else {
219240
events = this.getNodeParameter('events', []) as string[];
220241
}
221-
242+
const ignored = options.ignored === '' ? undefined : (options.ignored as string);
222243
const watcher = watch(path, {
223-
ignored: options.ignored === '' ? undefined : (options.ignored as string),
244+
ignored: options.ignoreMode === 'match' ? ignored : (x) => x.includes(ignored as string),
224245
persistent: true,
225246
ignoreInitial:
226247
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 + match ignore', async () => {
36+
(context.getNodeParameter as jest.Mock)
37+
.mockReturnValueOnce('folder')
38+
.mockReturnValueOnce('/some/folder')
39+
.mockReturnValueOnce({
40+
ignored: '**/*.txt',
41+
ignoreMode: 'match',
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=contain', async () => {
68+
(context.getNodeParameter as jest.Mock)
69+
.mockReturnValueOnce('folder')
70+
.mockReturnValueOnce('/folder')
71+
.mockReturnValueOnce({
72+
ignored: 'node_modules',
73+
ignoreMode: 'contain',
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)