This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Feature/filesystem watcher #2116
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a8a02dc
add FSWatcher initial implementation
akinsho 0225745
add workspace default as dir to watch
akinsho fad1b95
merge upstream and fix conflicts
akinsho 24fbfc9
add html loader banner plugin and aws to stop errors
akinsho 08eaec4
merge upstream add fsevents to externals
akinsho 4ff308f
merge upstream
akinsho 1259c69
remove logs change event fns to prefix with on
akinsho eb7a328
Add ignore initial option to chokidar init
akinsho 968249d
use ready event to initialise watcher
akinsho 84efcda
update chokidar and types in fswatcher
akinsho 97d8b6b
merge upstream fix conflicts
akinsho d3c01bf
listen to workspace directory changes
akinsho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,104 @@ | ||
import * as chokidar from "chokidar" | ||
import { Stats } from "fs" | ||
import { Event, IEvent } from "oni-types" | ||
|
||
import * as Workspace from "./../Workspace" | ||
|
||
export type Targets = string | string[] | ||
|
||
interface IFSOptions { | ||
options?: chokidar.WatchOptions | ||
target?: Targets | ||
} | ||
|
||
interface IFileChangeEvent { | ||
path: string | ||
} | ||
|
||
interface IStatsChangeEvent { | ||
path: string | ||
stats: Stats | ||
} | ||
|
||
export class FileSystemWatcher { | ||
private _watcher: chokidar.FSWatcher | ||
private _workspace: Workspace.Workspace | ||
private _activeWorkspace: string | ||
|
||
private _onAdd = new Event<IFileChangeEvent>() | ||
private _onAddDir = new Event<IStatsChangeEvent>() | ||
private _onMove = new Event<IFileChangeEvent>() | ||
private _onChange = new Event<IFileChangeEvent>() | ||
private _defaultOptions = { ignored: "**/node_modules" } | ||
|
||
constructor(watch: IFSOptions = {}) { | ||
this._workspace = Workspace.getInstance() | ||
this._activeWorkspace = this._workspace.activeWorkspace | ||
const fileOrFolder = watch.target || this._activeWorkspace | ||
const optionsToUse = watch.options || this._defaultOptions | ||
this._watcher = chokidar.watch(fileOrFolder, optionsToUse) | ||
|
||
// alternatively the ignoreInitial can be set in the config | ||
// to avoid a flurry of events when the watcher is initialised | ||
this._watcher.on("ready", () => { | ||
this._attachEventListeners() | ||
}) | ||
|
||
this._workspace.onDirectoryChanged.subscribe(newDirectory => { | ||
this.unwatch(this._activeWorkspace) | ||
this.watch(newDirectory) | ||
}) | ||
} | ||
|
||
public watch(target: Targets) { | ||
return this._watcher.add(target) | ||
} | ||
|
||
public unwatch(target: Targets) { | ||
return this._watcher.unwatch(target) | ||
} | ||
|
||
public close() { | ||
this._watcher.close() | ||
} | ||
|
||
private _attachEventListeners() { | ||
this._watcher.on("add", path => { | ||
return this._onAdd.dispatch(path) | ||
}) | ||
|
||
this._watcher.on("change", path => { | ||
return this._onChange.dispatch(path) | ||
}) | ||
|
||
this._watcher.on("move", path => { | ||
return this._onMove.dispatch(path) | ||
}) | ||
|
||
this._watcher.on("addDir", (path, stats) => { | ||
return this._onAddDir.dispatch({ path, stats }) | ||
}) | ||
} | ||
|
||
get allWatched(): chokidar.WatchedPaths { | ||
return this._watcher.getWatched() | ||
} | ||
|
||
get onChange(): IEvent<IFileChangeEvent> { | ||
return this._onChange | ||
} | ||
|
||
get onMove(): IEvent<IFileChangeEvent> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the abstraction a lot - and it seems like it'd be relatively straightforward for us to have a 'mock' implementation for tests, if we need it 👍 |
||
return this._onMove | ||
} | ||
|
||
get onAdd(): IEvent<IFileChangeEvent> { | ||
return this._onAdd | ||
} | ||
|
||
get addDir(): IEvent<IStatsChangeEvent> { | ||
return this._onAddDir | ||
} | ||
} | ||
|
||
export default new FileSystemWatcher() |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering - would it make sense to hook the
onDirectoryChanged
event onWorkspace
? In that case, we'd probably want to remove the existing listeners from the old directory, and start listening to the 'new' directory.