From 751bb580417bdbc0e1a5976bfb1ec64b1ee48695 Mon Sep 17 00:00:00 2001 From: Patrick Taylor Date: Mon, 17 Jun 2024 17:55:39 +0100 Subject: [PATCH] =?UTF-8?q?Added=20watch=20events=20for=20adding/removing?= =?UTF-8?q?=20a=20file=20=F0=9F=97=B3=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/watchFiles.spec.ts | 64 ++++++++++++++++++++++++++++---- src/index.ts | 5 ++- src/watchFiles.ts | 19 +++++++++- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/__tests__/watchFiles.spec.ts b/src/__tests__/watchFiles.spec.ts index 7db26b0..b09ea03 100644 --- a/src/__tests__/watchFiles.spec.ts +++ b/src/__tests__/watchFiles.spec.ts @@ -50,11 +50,14 @@ describe('watchFiles', () => { it('emits a "FILE_CHANGED" event when saving the file for the first time', done => { expect.assertions(1) - const mockPath = 'mock/new-file.ts' + const mockPath = 'mock/existing-file.ts' mockReadFile('first-time-save') - fileChangeEvent.on(Events.FILE_CHANGED, path => { - expect(path).toBe(mockPath) + fileChangeEvent.on(Events.FILE_CHANGED, params => { + expect(params).toStrictEqual({ + message: `File \`${mockPath}\` has been changed.`, + path: mockPath, + }) done() }) @@ -70,8 +73,11 @@ describe('watchFiles', () => { const mockPath = 'mock/old-file.ts' mockReadFile('old-content') - fileChangeEvent.on(Events.FILE_CHANGED, path => { - expect(path).toBe(mockPath) + fileChangeEvent.on(Events.FILE_CHANGED, params => { + expect(params).toStrictEqual({ + message: `File \`${mockPath}\` has been changed.`, + path: mockPath, + }) }) watchFiles({ filePatterns: [mockPath], ignorePatterns: [] }) @@ -93,8 +99,11 @@ describe('watchFiles', () => { const mockPath = 'mock/old-file.ts' mockReadFile('old-content') - fileChangeEvent.on(Events.FILE_CHANGED, path => { - expect(path).toBe(mockPath) + fileChangeEvent.on(Events.FILE_CHANGED, params => { + expect(params).toStrictEqual({ + message: `File \`${mockPath}\` has been changed.`, + path: mockPath, + }) }) watchFiles({ filePatterns: [mockPath], ignorePatterns: [] }) @@ -108,4 +117,45 @@ describe('watchFiles', () => { done() }, 100) }) + + it('emits a "FILE_CHANGED" event if a file is added', done => { + expect.assertions(1) + + const mockPath = 'mock/new-file.ts' + mockReadFile('new-content') + + fileChangeEvent.on(Events.FILE_CHANGED, params => { + expect(params).toStrictEqual({ + message: `File \`${mockPath}\` has been added.`, + path: mockPath, + }) + done() + }) + + watchFiles({ filePatterns: [mockPath], ignorePatterns: [] }) + + const changeHandler = (mockWatcher.on as jest.Mock).mock.calls.find(call => call[0] === 'add')[1] + changeHandler(mockPath) + }) + + it('emits a "FILE_CHANGED" event if a file is removed', done => { + expect.assertions(1) + + const mockPath = 'mock/legacy-file.ts' + mockReadFile('legacy-content') + + fileChangeEvent.on(Events.FILE_CHANGED, params => { + expect(params).toStrictEqual({ + message: `File \`${mockPath}\` has been removed.`, + path: mockPath, + }) + done() + }) + + watchFiles({ filePatterns: [mockPath], ignorePatterns: [] }) + + const changeHandler = (mockWatcher.on as jest.Mock).mock.calls.find(call => call[0] === 'unlink')[1] + changeHandler(mockPath) + }) + }) diff --git a/src/index.ts b/src/index.ts index fca66e9..2a34611 100755 --- a/src/index.ts +++ b/src/index.ts @@ -104,9 +104,10 @@ program ignorePatterns: ['**/+(coverage|node_modules)/**'], }) - fileChangeEvent.on(Events.FILE_CHANGED, path => { + fileChangeEvent.on(Events.FILE_CHANGED, ({ message }) => { clearTerminal() - colourLog.info(`File \`${path}\` has been changed.\n`) + colourLog.info(message) + console.log() runLintPilot({ debug, title, watch }) }) } diff --git a/src/watchFiles.ts b/src/watchFiles.ts index 39f62f8..74a2bcd 100644 --- a/src/watchFiles.ts +++ b/src/watchFiles.ts @@ -23,15 +23,32 @@ const watchFiles = ({ filePatterns, ignorePatterns }: WatchFiles) => { persistent: true, }) + watcher.on('add', (path, _stats) => { + fileChangeEvent.emit(Events.FILE_CHANGED, { + message: `File \`${path}\` has been added.`, + path, + }) + }) + watcher.on('change', (path, _stats) => { readFile(path, 'utf8', (_error, data) => { const newHash = getHash(data) if (fileHashes.get(path) !== newHash) { fileHashes.set(path, newHash) - fileChangeEvent.emit(Events.FILE_CHANGED, path) + fileChangeEvent.emit(Events.FILE_CHANGED, { + message: `File \`${path}\` has been changed.`, + path, + }) } }) }) + + watcher.on('unlink', path => { + fileChangeEvent.emit(Events.FILE_CHANGED, { + message: `File \`${path}\` has been removed.`, + path, + }) + }) } export {