Skip to content

Commit

Permalink
Added watch events for adding/removing a file 🗳️
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jun 17, 2024
1 parent 95ff873 commit 751bb58
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
64 changes: 57 additions & 7 deletions src/__tests__/watchFiles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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: [] })
Expand All @@ -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: [] })
Expand All @@ -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)
})

})
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
}
Expand Down
19 changes: 18 additions & 1 deletion src/watchFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 751bb58

Please sign in to comment.