-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: abstract file watching backend (#22)
* feat: abstract backend * feat: abstract backend * feat: abstract backend * feat: abstract backend * feat: abstract backend * feat: abstract backend
- Loading branch information
Showing
12 changed files
with
190 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { FileWatchingBackend } from './FileWatchingBackend'; | ||
import * as chokidar from 'chokidar'; | ||
|
||
export class ChokidarWatcher extends FileWatchingBackend { | ||
private chokidar: chokidar.FSWatcher; | ||
|
||
public constructor(project: string) { | ||
super(); | ||
|
||
this.chokidar = chokidar.watch(project); | ||
|
||
this.chokidar.on('ready', () => { | ||
this.emit('ready'); | ||
}); | ||
|
||
this.chokidar.on('all', (event, filename) => { | ||
this.emit('change', { filename }); | ||
}); | ||
} | ||
|
||
public close() { | ||
return this.chokidar.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-disable canonical/filename-match-regex */ | ||
|
||
import { FileWatchingBackend } from './FileWatchingBackend'; | ||
import fs, { type FSWatcher as NativeFSWatcher } from 'node:fs'; | ||
|
||
export class FSWatcher extends FileWatchingBackend { | ||
private fsWatcher: NativeFSWatcher; | ||
|
||
public constructor(project: string) { | ||
super(); | ||
|
||
this.fsWatcher = fs.watch( | ||
project, | ||
{ | ||
recursive: true, | ||
}, | ||
(eventType, filename) => { | ||
this.emit('change', { filename }); | ||
}, | ||
); | ||
|
||
setImmediate(() => { | ||
this.emit('ready'); | ||
}); | ||
} | ||
|
||
public async close() { | ||
this.fsWatcher.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
/* eslint-disable @typescript-eslint/method-signature-style */ | ||
|
||
import { type FileChangeEvent } from '../types'; | ||
import { EventEmitter } from 'node:events'; | ||
|
||
interface BackendEventEmitter { | ||
on(event: 'ready', listener: () => void): this; | ||
on(event: 'change', listener: ({ filename }: FileChangeEvent) => void): this; | ||
} | ||
|
||
export abstract class FileWatchingBackend | ||
extends EventEmitter | ||
implements BackendEventEmitter | ||
{ | ||
public constructor() { | ||
super(); | ||
} | ||
|
||
public abstract close(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable canonical/filename-match-regex */ | ||
|
||
import { Logger } from '../Logger'; | ||
import { ChokidarWatcher } from './ChokidarWatcher'; | ||
import { FileWatchingBackend } from './FileWatchingBackend'; | ||
import { FSWatcher } from './FSWatcher'; | ||
import { platform } from 'node:os'; | ||
import * as semver from 'semver'; | ||
|
||
const log = Logger.child({ | ||
namespace: 'TurboWatcher', | ||
}); | ||
|
||
const isMacOs = () => { | ||
return platform() === 'darwin'; | ||
}; | ||
|
||
export class TurboWatcher extends FileWatchingBackend { | ||
private backend: FileWatchingBackend; | ||
|
||
public constructor(project: string) { | ||
super(); | ||
|
||
if (semver.gte(process.version, '19.1.0') && isMacOs()) { | ||
log.info('using native FSWatcher'); | ||
this.backend = new FSWatcher(project); | ||
} else { | ||
log.info('using native ChokidarWatcher'); | ||
this.backend = new ChokidarWatcher(project); | ||
} | ||
|
||
this.backend.on('ready', () => { | ||
this.emit('ready'); | ||
}); | ||
|
||
this.backend.on('change', (event) => { | ||
this.emit('change', event); | ||
}); | ||
} | ||
|
||
public close() { | ||
return this.backend.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export { ChokidarWatcher } from './backends/ChokidarWatcher'; | ||
export { FileWatchingBackend } from './backends/FileWatchingBackend'; | ||
export { FSWatcher } from './backends/FSWatcher'; | ||
export { type ChangeEvent, type Expression } from './types'; | ||
export { watch } from './watch'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.