@theia/filesystem 1.26.0-next.4 → 1.26.0-next.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.d.ts +1 -2
  2. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.d.ts.map +1 -1
  3. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js +3 -4
  4. package/lib/browser/breadcrumbs/filepath-breadcrumbs-container.js.map +1 -1
  5. package/lib/browser/file-service.d.ts +5 -0
  6. package/lib/browser/file-service.d.ts.map +1 -1
  7. package/lib/browser/file-service.js +5 -0
  8. package/lib/browser/file-service.js.map +1 -1
  9. package/lib/browser/file-tree/file-tree-decorator-adapter.d.ts +1 -1
  10. package/lib/browser/file-tree/file-tree-decorator-adapter.js +1 -1
  11. package/lib/browser/file-tree/file-tree-widget.js +1 -1
  12. package/lib/browser/file-tree/file-tree-widget.js.map +1 -1
  13. package/lib/browser/file-tree/file-tree.d.ts +1 -2
  14. package/lib/browser/file-tree/file-tree.d.ts.map +1 -1
  15. package/lib/browser/file-tree/file-tree.js.map +1 -1
  16. package/lib/browser/filesystem-frontend-module.d.ts.map +1 -1
  17. package/lib/browser/filesystem-frontend-module.js +0 -165
  18. package/lib/browser/filesystem-frontend-module.js.map +1 -1
  19. package/lib/browser/index.d.ts +0 -1
  20. package/lib/browser/index.d.ts.map +1 -1
  21. package/lib/browser/index.js +0 -1
  22. package/lib/browser/index.js.map +1 -1
  23. package/lib/browser/location/location-renderer.js +2 -2
  24. package/lib/browser/location/location-renderer.js.map +1 -1
  25. package/lib/common/files.d.ts +13 -4
  26. package/lib/common/files.d.ts.map +1 -1
  27. package/lib/common/files.js.map +1 -1
  28. package/lib/common/filesystem-utils.spec.js +49 -49
  29. package/lib/common/filesystem-utils.spec.js.map +1 -1
  30. package/lib/common/filesystem-watcher-protocol.d.ts +1 -22
  31. package/lib/common/filesystem-watcher-protocol.d.ts.map +1 -1
  32. package/lib/common/filesystem-watcher-protocol.js +1 -68
  33. package/lib/common/filesystem-watcher-protocol.js.map +1 -1
  34. package/lib/common/filesystem.d.ts +0 -229
  35. package/lib/common/filesystem.d.ts.map +1 -1
  36. package/lib/common/filesystem.js +1 -51
  37. package/lib/common/filesystem.js.map +1 -1
  38. package/lib/common/remote-file-system-provider.d.ts +9 -9
  39. package/lib/common/remote-file-system-provider.d.ts.map +1 -1
  40. package/lib/common/remote-file-system-provider.js +9 -10
  41. package/lib/common/remote-file-system-provider.js.map +1 -1
  42. package/package.json +3 -3
  43. package/src/browser/breadcrumbs/filepath-breadcrumbs-container.ts +1 -2
  44. package/src/browser/file-service.ts +5 -0
  45. package/src/browser/file-tree/file-tree-decorator-adapter.ts +1 -1
  46. package/src/browser/file-tree/file-tree-widget.tsx +1 -1
  47. package/src/browser/file-tree/file-tree.ts +1 -2
  48. package/src/browser/filesystem-frontend-module.ts +0 -162
  49. package/src/browser/index.ts +0 -1
  50. package/src/browser/location/location-renderer.tsx +2 -2
  51. package/src/common/files.ts +13 -4
  52. package/src/common/filesystem-utils.spec.ts +1 -1
  53. package/src/common/filesystem-watcher-protocol.ts +1 -67
  54. package/src/common/filesystem.ts +0 -262
  55. package/src/common/remote-file-system-provider.ts +18 -19
  56. package/lib/browser/filesystem-watcher.d.ts +0 -134
  57. package/lib/browser/filesystem-watcher.d.ts.map +0 -1
  58. package/lib/browser/filesystem-watcher.js +0 -240
  59. package/lib/browser/filesystem-watcher.js.map +0 -1
  60. package/lib/node/node-file-upload.d.ts +0 -19
  61. package/lib/node/node-file-upload.d.ts.map +0 -1
  62. package/lib/node/node-file-upload.js +0 -55
  63. package/lib/node/node-file-upload.js.map +0 -1
  64. package/lib/node/nsfw-watcher/nsfw-filesystem-watcher.d.ts +0 -53
  65. package/lib/node/nsfw-watcher/nsfw-filesystem-watcher.d.ts.map +0 -1
  66. package/lib/node/nsfw-watcher/nsfw-filesystem-watcher.js +0 -197
  67. package/lib/node/nsfw-watcher/nsfw-filesystem-watcher.js.map +0 -1
  68. package/src/browser/filesystem-watcher.ts +0 -249
  69. package/src/node/node-file-upload.ts +0 -64
  70. package/src/node/nsfw-watcher/nsfw-filesystem-watcher.ts +0 -255
@@ -1,249 +0,0 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
18
- import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
19
- import { Emitter, WaitUntilEvent } from '@theia/core/lib/common/event';
20
- import URI from '@theia/core/lib/common/uri';
21
- import { FileChangeType, FileOperation } from '../common/files';
22
- import { FileService } from './file-service';
23
-
24
- export {
25
- FileChangeType
26
- };
27
-
28
- export interface FileChange {
29
- uri: URI;
30
- type: FileChangeType;
31
- }
32
- export namespace FileChange {
33
- export function isUpdated(change: FileChange, uri: URI): boolean {
34
- return change.type === FileChangeType.UPDATED && uri.toString() === change.uri.toString();
35
- }
36
- export function isAdded(change: FileChange, uri: URI): boolean {
37
- return change.type === FileChangeType.ADDED && uri.toString() === change.uri.toString();
38
- }
39
- export function isDeleted(change: FileChange, uri: URI): boolean {
40
- return change.type === FileChangeType.DELETED && change.uri.isEqualOrParent(uri);
41
- }
42
- export function isAffected(change: FileChange, uri: URI): boolean {
43
- return isDeleted(change, uri) || uri.toString() === change.uri.toString();
44
- }
45
- export function isChanged(change: FileChange, uri: URI): boolean {
46
- return !isDeleted(change, uri) && uri.toString() === change.uri.toString();
47
- }
48
- }
49
-
50
- /**
51
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileChangesEvent` instead
52
- */
53
- export type FileChangeEvent = FileChange[];
54
- export namespace FileChangeEvent {
55
- export function isUpdated(event: FileChangeEvent, uri: URI): boolean {
56
- return event.some(change => FileChange.isUpdated(change, uri));
57
- }
58
- export function isAdded(event: FileChangeEvent, uri: URI): boolean {
59
- return event.some(change => FileChange.isAdded(change, uri));
60
- }
61
- export function isDeleted(event: FileChangeEvent, uri: URI): boolean {
62
- return event.some(change => FileChange.isDeleted(change, uri));
63
- }
64
- export function isAffected(event: FileChangeEvent, uri: URI): boolean {
65
- return event.some(change => FileChange.isAffected(change, uri));
66
- }
67
- export function isChanged(event: FileChangeEvent, uri: URI): boolean {
68
- return !isDeleted(event, uri) && event.some(change => FileChange.isChanged(change, uri));
69
- }
70
- }
71
-
72
- /**
73
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `UserFileOperationEvent` instead
74
- */
75
- export interface FileMoveEvent extends WaitUntilEvent {
76
- sourceUri: URI
77
- targetUri: URI
78
- }
79
- export namespace FileMoveEvent {
80
- export function isRename({ sourceUri, targetUri }: FileMoveEvent): boolean {
81
- return sourceUri.parent.toString() === targetUri.parent.toString();
82
- }
83
- }
84
-
85
- /**
86
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `UserFileOperationEvent` instead
87
- */
88
- export interface FileEvent extends WaitUntilEvent {
89
- uri: URI
90
- }
91
-
92
- export class FileOperationEmitter<E extends WaitUntilEvent> implements Disposable {
93
-
94
- protected readonly onWillEmitter = new Emitter<E>();
95
- readonly onWill = this.onWillEmitter.event;
96
-
97
- protected readonly onDidFailEmitter = new Emitter<E>();
98
- readonly onDidFail = this.onDidFailEmitter.event;
99
-
100
- protected readonly onDidEmitter = new Emitter<E>();
101
- readonly onDid = this.onDidEmitter.event;
102
-
103
- protected readonly toDispose = new DisposableCollection(
104
- this.onWillEmitter,
105
- this.onDidFailEmitter,
106
- this.onDidEmitter
107
- );
108
-
109
- dispose(): void {
110
- this.toDispose.dispose();
111
- }
112
-
113
- async fireWill(event: Pick<E, Exclude<keyof E, 'waitUntil'>>): Promise<void> {
114
- await WaitUntilEvent.fire(this.onWillEmitter, event);
115
- }
116
-
117
- async fireDid(failed: boolean, event: Pick<E, Exclude<keyof E, 'waitUntil'>>): Promise<void> {
118
- const onDidEmitter = failed ? this.onDidFailEmitter : this.onDidEmitter;
119
- await WaitUntilEvent.fire(onDidEmitter, event);
120
- }
121
-
122
- }
123
-
124
- /**
125
- * React to file system events, including calls originating from the
126
- * application or event coming from the system's filesystem directly
127
- * (actual file watching).
128
- *
129
- * `on(will|did)(create|rename|delete)` events solely come from application
130
- * usage, not from actual filesystem.
131
- *
132
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.watch` instead
133
- */
134
- @injectable()
135
- export class FileSystemWatcher implements Disposable {
136
-
137
- protected readonly toDispose = new DisposableCollection();
138
- protected readonly toRestartAll = new DisposableCollection();
139
-
140
- protected readonly onFileChangedEmitter = new Emitter<FileChangeEvent>();
141
- /**
142
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onDidFilesChange` instead
143
- */
144
- readonly onFilesChanged = this.onFileChangedEmitter.event;
145
-
146
- protected readonly fileCreateEmitter = new FileOperationEmitter<FileEvent>();
147
- /**
148
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onWillRunUserOperation` instead
149
- */
150
- readonly onWillCreate = this.fileCreateEmitter.onWill;
151
- /**
152
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onDidFailUserOperation` instead
153
- */
154
- readonly onDidFailCreate = this.fileCreateEmitter.onDidFail;
155
- /**
156
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908),
157
- * instead use `FileService.onDidRunUserOperation` for events triggered by user gestures
158
- * or `FileService.onDidRunOperation` triggered by user gestures and programmatically
159
- */
160
- readonly onDidCreate = this.fileCreateEmitter.onDid;
161
-
162
- protected readonly fileDeleteEmitter = new FileOperationEmitter<FileEvent>();
163
- /**
164
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onWillRunUserOperation` instead
165
- */
166
- readonly onWillDelete = this.fileDeleteEmitter.onWill;
167
- /**
168
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onDidFailUserOperation` instead
169
- */
170
- readonly onDidFailDelete = this.fileDeleteEmitter.onDidFail;
171
- /**
172
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908),
173
- * instead use `FileService.onDidRunUserOperation` for events triggered by user gestures
174
- * or `FileService.onDidRunOperation` triggered by user gestures and programmatically
175
- */
176
- readonly onDidDelete = this.fileDeleteEmitter.onDid;
177
-
178
- protected readonly fileMoveEmitter = new FileOperationEmitter<FileMoveEvent>();
179
- /**
180
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onWillRunUserOperation` instead
181
- */
182
- readonly onWillMove = this.fileMoveEmitter.onWill;
183
- /**
184
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908), use `FileService.onDidFailUserOperation` instead
185
- */
186
- readonly onDidFailMove = this.fileMoveEmitter.onDidFail;
187
- /**
188
- * @deprecated since 1.4.0 - in order to support VS Code FS API (https://github.com/eclipse-theia/theia/pull/7908),
189
- * instead use `FileService.onDidRunUserOperation` for events triggered by user gestures
190
- * or `FileService.onDidRunOperation` triggered by user gestures and programmatically
191
- */
192
- readonly onDidMove = this.fileMoveEmitter.onDid;
193
-
194
- @inject(FileService)
195
- protected readonly fileService: FileService;
196
-
197
- @postConstruct()
198
- protected init(): void {
199
- this.toDispose.push(this.onFileChangedEmitter);
200
- this.toDispose.push(this.fileDeleteEmitter);
201
- this.toDispose.push(this.fileMoveEmitter);
202
-
203
- this.toDispose.push(this.fileService.onWillRunUserOperation(event => {
204
- if (event.operation === FileOperation.CREATE) {
205
- this.fileCreateEmitter.fireWill({ uri: event.target });
206
- } else if (event.operation === FileOperation.DELETE) {
207
- this.fileDeleteEmitter.fireWill({ uri: event.target });
208
- } else if (event.operation === FileOperation.MOVE && event.source) {
209
- this.fileMoveEmitter.fireWill({ sourceUri: event.source, targetUri: event.target });
210
- }
211
- }));
212
- this.toDispose.push(this.fileService.onDidFailUserOperation(event => {
213
- if (event.operation === FileOperation.CREATE) {
214
- this.fileCreateEmitter.fireDid(true, { uri: event.target });
215
- } else if (event.operation === FileOperation.DELETE) {
216
- this.fileDeleteEmitter.fireDid(true, { uri: event.target });
217
- } else if (event.operation === FileOperation.MOVE && event.source) {
218
- this.fileMoveEmitter.fireDid(true, { sourceUri: event.source, targetUri: event.target });
219
- }
220
- }));
221
- this.toDispose.push(this.fileService.onDidRunUserOperation(event => {
222
- if (event.operation === FileOperation.CREATE) {
223
- this.fileCreateEmitter.fireDid(false, { uri: event.target });
224
- } else if (event.operation === FileOperation.DELETE) {
225
- this.fileDeleteEmitter.fireDid(false, { uri: event.target });
226
- } else if (event.operation === FileOperation.MOVE && event.source) {
227
- this.fileMoveEmitter.fireDid(false, { sourceUri: event.source, targetUri: event.target });
228
- }
229
- }));
230
- }
231
-
232
- /**
233
- * Stop watching.
234
- */
235
- dispose(): void {
236
- this.toDispose.dispose();
237
- }
238
-
239
- /**
240
- * Start file watching under the given uri.
241
- *
242
- * Resolve when watching is started.
243
- * Return a disposable to stop file watching under the given uri.
244
- */
245
- async watchFileChanges(uri: URI): Promise<Disposable> {
246
- return this.fileService.watch(uri);
247
- }
248
-
249
- }
@@ -1,64 +0,0 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2019 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import * as path from 'path';
18
- import * as crypto from 'crypto';
19
- import * as fs from '@theia/core/shared/fs-extra';
20
- import { Buffer } from 'buffer';
21
- import { Disposable } from '@theia/core/lib/common/disposable';
22
- import { FileUri } from '@theia/core/lib/node/file-uri';
23
-
24
- /**
25
- * @deprecated since 1.17.0
26
- */
27
- export class NodeFileUpload implements Disposable {
28
-
29
- readonly id: string;
30
- readonly fsPath: string;
31
- readonly uploadPath: string;
32
- protected _uploadedBytes = 0;
33
- get uploadedBytes(): number {
34
- return this._uploadedBytes;
35
- }
36
-
37
- constructor(
38
- readonly uri: string,
39
- readonly size: number
40
- ) {
41
- this.fsPath = FileUri.fsPath(uri);
42
- this.id = 'theia_upload_' + crypto.randomBytes(16).toString('hex');
43
- this.uploadPath = path.join(path.dirname(this.fsPath), this.id);
44
- }
45
-
46
- async create(): Promise<void> {
47
- await fs.outputFile(this.uploadPath, '');
48
- }
49
-
50
- async append(chunk: ArrayBuffer): Promise<void> {
51
- await fs.appendFile(this.uploadPath, Buffer.from(chunk));
52
- this._uploadedBytes += chunk.byteLength;
53
- }
54
-
55
- async rename(): Promise<void> {
56
- await fs.move(this.uploadPath, this.fsPath, { overwrite: true });
57
- this.dispose = () => Promise.resolve();
58
- }
59
-
60
- dispose(): void {
61
- fs.unlink(this.uploadPath).catch(() => {/* no-op */ });
62
- }
63
-
64
- }
@@ -1,255 +0,0 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017-2018 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import * as fs from 'fs';
18
- import * as nsfw from '@theia/core/shared/nsfw';
19
- import * as paths from 'path';
20
- import { IMinimatch, Minimatch } from 'minimatch';
21
- import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
22
- import { FileUri } from '@theia/core/lib/node/file-uri';
23
- import {
24
- FileChangeType,
25
- FileSystemWatcherClient,
26
- FileSystemWatcherServer,
27
- WatchOptions
28
- } from '../../common/filesystem-watcher-protocol';
29
- import { FileChangeCollection } from '../file-change-collection';
30
- import { setInterval, clearInterval } from 'timers';
31
-
32
- import debounce = require('@theia/core/shared/lodash.debounce');
33
-
34
- /* eslint-disable @typescript-eslint/no-explicit-any */
35
-
36
- /**
37
- * @deprecated since 1.6.0
38
- */
39
- export interface WatcherOptions {
40
- ignored: IMinimatch[]
41
- }
42
-
43
- /**
44
- * @deprecated since 1.6.0 use `NsfwFileSystemWatcherService` instead.
45
- */
46
- export class NsfwFileSystemWatcherServer implements FileSystemWatcherServer {
47
-
48
- protected client: FileSystemWatcherClient | undefined;
49
-
50
- protected watcherSequence = 1;
51
- protected readonly watchers = new Map<number, Disposable>();
52
- protected readonly watcherOptions = new Map<number, WatcherOptions>();
53
-
54
- protected readonly toDispose = new DisposableCollection(
55
- Disposable.create(() => this.setClient(undefined))
56
- );
57
-
58
- protected changes = new FileChangeCollection();
59
-
60
- protected readonly options: {
61
- verbose: boolean
62
- info: (message: string, ...args: any[]) => void
63
- error: (message: string, ...args: any[]) => void,
64
- nsfwOptions: nsfw.Options
65
- };
66
-
67
- constructor(options?: {
68
- verbose?: boolean,
69
- nsfwOptions?: nsfw.Options,
70
- info?: (message: string, ...args: any[]) => void
71
- error?: (message: string, ...args: any[]) => void
72
- }) {
73
- this.options = {
74
- nsfwOptions: {},
75
- verbose: false,
76
- info: (message, ...args) => console.info(message, ...args),
77
- error: (message, ...args) => console.error(message, ...args),
78
- ...options
79
- };
80
- }
81
-
82
- dispose(): void {
83
- this.toDispose.dispose();
84
- }
85
-
86
- async watchFileChanges(uri: string, options?: WatchOptions): Promise<number> {
87
- const watcherId = this.watcherSequence++;
88
- const basePath = FileUri.fsPath(uri);
89
- this.debug('Starting watching:', basePath);
90
- const toDisposeWatcher = new DisposableCollection();
91
- this.watchers.set(watcherId, toDisposeWatcher);
92
- toDisposeWatcher.push(Disposable.create(() => this.watchers.delete(watcherId)));
93
- if (fs.existsSync(basePath)) {
94
- this.start(watcherId, basePath, options, toDisposeWatcher);
95
- } else {
96
- const toClearTimer = new DisposableCollection();
97
- const timer = setInterval(() => {
98
- if (fs.existsSync(basePath)) {
99
- toClearTimer.dispose();
100
- this.pushAdded(watcherId, basePath);
101
- this.start(watcherId, basePath, options, toDisposeWatcher);
102
- }
103
- }, 500);
104
- toClearTimer.push(Disposable.create(() => clearInterval(timer)));
105
- toDisposeWatcher.push(toClearTimer);
106
- }
107
- this.toDispose.push(toDisposeWatcher);
108
- return watcherId;
109
- }
110
-
111
- protected async start(watcherId: number, basePath: string, rawOptions: WatchOptions | undefined, toDisposeWatcher: DisposableCollection): Promise<void> {
112
- const options: WatchOptions = {
113
- ignored: [],
114
- ...rawOptions
115
- };
116
- if (options.ignored.length > 0) {
117
- this.debug('Files ignored for watching', options.ignored);
118
- }
119
-
120
- let watcher: nsfw.NSFW | undefined = await nsfw(fs.realpathSync(basePath), (events: nsfw.FileChangeEvent[]) => {
121
- for (const event of events) {
122
- if (event.action === nsfw.actions.CREATED) {
123
- this.pushAdded(watcherId, this.resolvePath(event.directory, event.file!));
124
- }
125
- if (event.action === nsfw.actions.DELETED) {
126
- this.pushDeleted(watcherId, this.resolvePath(event.directory, event.file!));
127
- }
128
- if (event.action === nsfw.actions.MODIFIED) {
129
- this.pushUpdated(watcherId, this.resolvePath(event.directory, event.file!));
130
- }
131
- if (event.action === nsfw.actions.RENAMED) {
132
- this.pushDeleted(watcherId, this.resolvePath(event.directory, event.oldFile!));
133
- this.pushAdded(watcherId, this.resolvePath(event.newDirectory || event.directory, event.newFile!));
134
- }
135
- }
136
- }, {
137
- errorCallback: error => {
138
- // see https://github.com/atom/github/issues/342
139
- console.warn(`Failed to watch "${basePath}":`, error);
140
- if (error === 'Inotify limit reached') {
141
- if (this.client) {
142
- this.client.onError();
143
- }
144
- }
145
- this.unwatchFileChanges(watcherId);
146
- },
147
- ...this.options.nsfwOptions
148
- });
149
- await watcher.start();
150
- this.options.info('Started watching:', basePath);
151
- if (toDisposeWatcher.disposed) {
152
- this.debug('Stopping watching:', basePath);
153
- await watcher.stop();
154
- // remove a reference to nsfw otherwise GC cannot collect it
155
- watcher = undefined;
156
- this.options.info('Stopped watching:', basePath);
157
- return;
158
- }
159
- toDisposeWatcher.push(Disposable.create(async () => {
160
- this.watcherOptions.delete(watcherId);
161
- if (watcher) {
162
- this.debug('Stopping watching:', basePath);
163
- await watcher.stop();
164
- // remove a reference to nsfw otherwise GC cannot collect it
165
- watcher = undefined;
166
- this.options.info('Stopped watching:', basePath);
167
- }
168
- }));
169
- this.watcherOptions.set(watcherId, {
170
- ignored: options.ignored.map(pattern => new Minimatch(pattern, { dot: true }))
171
- });
172
- }
173
-
174
- unwatchFileChanges(watcherId: number): Promise<void> {
175
- const disposable = this.watchers.get(watcherId);
176
- if (disposable) {
177
- this.watchers.delete(watcherId);
178
- disposable.dispose();
179
- }
180
- return Promise.resolve();
181
- }
182
-
183
- setClient(client: FileSystemWatcherClient | undefined): void {
184
- if (client && this.toDispose.disposed) {
185
- return;
186
- }
187
- this.client = client;
188
- }
189
-
190
- protected pushAdded(watcherId: number, path: string): void {
191
- this.debug('Added:', path);
192
- this.pushFileChange(watcherId, path, FileChangeType.ADDED);
193
- }
194
-
195
- protected pushUpdated(watcherId: number, path: string): void {
196
- this.debug('Updated:', path);
197
- this.pushFileChange(watcherId, path, FileChangeType.UPDATED);
198
- }
199
-
200
- protected pushDeleted(watcherId: number, path: string): void {
201
- this.debug('Deleted:', path);
202
- this.pushFileChange(watcherId, path, FileChangeType.DELETED);
203
- }
204
-
205
- protected pushFileChange(watcherId: number, path: string, type: FileChangeType): void {
206
- if (this.isIgnored(watcherId, path)) {
207
- return;
208
- }
209
-
210
- const uri = FileUri.create(path).toString();
211
- this.changes.push({ uri, type });
212
-
213
- this.fireDidFilesChanged();
214
- }
215
-
216
- protected resolvePath(directory: string, file: string): string {
217
- const path = paths.join(directory, file);
218
- try {
219
- return fs.realpathSync(path);
220
- } catch {
221
- try {
222
- // file does not exist try to resolve directory
223
- return paths.join(fs.realpathSync(directory), file);
224
- } catch {
225
- // directory does not exist fall back to symlink
226
- return path;
227
- }
228
- }
229
- }
230
-
231
- /**
232
- * Fires file changes to clients.
233
- * It is debounced in the case if the filesystem is spamming to avoid overwhelming clients with events.
234
- */
235
- protected readonly fireDidFilesChanged: () => void = debounce(() => this.doFireDidFilesChanged(), 50);
236
- protected doFireDidFilesChanged(): void {
237
- const changes = this.changes.values();
238
- this.changes = new FileChangeCollection();
239
- const event = { changes };
240
- if (this.client) {
241
- this.client.onDidFilesChanged(event);
242
- }
243
- }
244
-
245
- protected isIgnored(watcherId: number, path: string): boolean {
246
- const options = this.watcherOptions.get(watcherId);
247
- return !!options && options.ignored.length > 0 && options.ignored.some(m => m.match(path));
248
- }
249
-
250
- protected debug(message: string, ...params: any[]): void {
251
- if (this.options.verbose) {
252
- this.options.info(message, ...params);
253
- }
254
- }
255
- }