From 57f06958e621dbeaf726911122b5dfce71773344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Mar=C3=A9chal?= Date: Fri, 12 Mar 2021 15:43:16 -0500 Subject: [PATCH] core: bump nsfw to ^2.1.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improvements have been made to the library. Notably a bug was fixed where some folders were not picked up by the watchers if created too quickly and an issue on Windows was fixed where watching didn't work at all. This commit updates our code to use the new typings distributed by `nsfw` itself and re-enables file watching testing on Windows. Signed-off-by: Paul Maréchal --- packages/core/package.json | 2 +- .../core/src/node/logger-cli-contribution.ts | 2 +- packages/core/src/typings/nsfw/index.d.ts | 49 ------------------- packages/filesystem/compile.tsconfig.json | 3 -- .../nsfw-watcher/nsfw-filesystem-service.ts | 2 +- .../nsfw-filesystem-watcher.spec.ts | 22 +++++---- .../nsfw-watcher/nsfw-filesystem-watcher.ts | 2 +- yarn.lock | 17 ++++--- 8 files changed, 28 insertions(+), 71 deletions(-) delete mode 100644 packages/core/src/typings/nsfw/index.d.ts diff --git a/packages/core/package.json b/packages/core/package.json index 8866e3a56627a..983d271433788 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -37,7 +37,7 @@ "jschardet": "^2.1.1", "lodash.debounce": "^4.0.8", "lodash.throttle": "^4.1.1", - "nsfw": "^1.2.9", + "nsfw": "^2.1.2", "p-debounce": "^2.1.0", "perfect-scrollbar": "^1.3.0", "react": "^16.8.0", diff --git a/packages/core/src/node/logger-cli-contribution.ts b/packages/core/src/node/logger-cli-contribution.ts index 098d9ced8ebf2..2a255984d1301 100644 --- a/packages/core/src/node/logger-cli-contribution.ts +++ b/packages/core/src/node/logger-cli-contribution.ts @@ -90,7 +90,7 @@ export class LogLevelCliContribution implements CliContribution { } protected watchLogConfigFile(filename: string): Promise { - return nsfw(filename, async (events: nsfw.ChangeEvent[]) => { + return nsfw(filename, async (events: nsfw.FileChangeEvent[]) => { try { for (const event of events) { switch (event.action) { diff --git a/packages/core/src/typings/nsfw/index.d.ts b/packages/core/src/typings/nsfw/index.d.ts deleted file mode 100644 index ea67f9db7a15a..0000000000000 --- a/packages/core/src/typings/nsfw/index.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************** - * Copyright (C) 2018 Ericsson and others. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the Eclipse - * Public License v. 2.0 are satisfied: GNU General Public License, version 2 - * with the GNU Classpath Exception which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 - ********************************************************************************/ - -declare module 'nsfw' { - function nsfw(dir: string, eventHandler: (events: nsfw.ChangeEvent[]) => void, options?: nsfw.Options): Promise; - - namespace nsfw { - export interface NSFW { - start(): Promise; - stop(): Promise; - } - - export interface Options { - debounceMS?: number; - errorCallback?: (error: string) => void; - } - - export interface ChangeEvent { - action: number; - directory: string; - newDirectory?: string; - file?: string; - oldFile?: string; - newFile?: string; - } - - export enum actions { - CREATED, - DELETED, - MODIFIED, - RENAMED, - } - } - - export = nsfw; -} diff --git a/packages/filesystem/compile.tsconfig.json b/packages/filesystem/compile.tsconfig.json index 6bf6e997229f6..324e2d66da5ab 100644 --- a/packages/filesystem/compile.tsconfig.json +++ b/packages/filesystem/compile.tsconfig.json @@ -9,9 +9,6 @@ "mv": [ "src/typings/mv" ], - "nsfw": [ - "src/typings/nsfw" - ], "trash": [ "src/typings/trash" ] diff --git a/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-service.ts b/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-service.ts index 4caf210211811..8e15372a5ddf2 100644 --- a/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-service.ts +++ b/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-service.ts @@ -242,7 +242,7 @@ export class NsfwWatcher { }); } - protected handleNsfwEvents(events: nsfw.ChangeEvent[]): void { + protected handleNsfwEvents(events: nsfw.FileChangeEvent[]): void { // Only process events if someone is listening. if (this.isInUse()) { // This callback is async, but nsfw won't wait for it to finish before firing the next one. diff --git a/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.spec.ts b/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.spec.ts index b5eff6ac3e8b9..ac9ef9ba28880 100644 --- a/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.spec.ts +++ b/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.spec.ts @@ -16,6 +16,7 @@ import * as temp from 'temp'; import * as chai from 'chai'; +import * as cp from 'child_process'; import * as fs from 'fs-extra'; import * as assert from 'assert'; import URI from '@theia/core/lib/common/uri'; @@ -36,7 +37,16 @@ describe('nsfw-filesystem-watcher', function (): void { this.timeout(10000); beforeEach(async () => { - root = FileUri.create(fs.realpathSync(temp.mkdirSync('node-fs-root'))); + let tempPath = temp.mkdirSync('node-fs-root'); + // Sometimes tempPath will use some Windows 8.3 short name in its path. This is a problem + // since NSFW always returns paths with long names. We need to convert here. + // See: https://stackoverflow.com/a/34473971/7983255 + if (process.platform === 'win32') { + tempPath = cp.execSync(`powershell "(Get-Item -LiteralPath '${tempPath}').FullName"`, { + encoding: 'utf8', + }).trim(); + } + root = FileUri.create(fs.realpathSync(tempPath)); watcherService = createNsfwFileSystemWatcherService(); watcherId = await watcherService.watchFileChanges(0, root.toString()); await sleep(2000); @@ -48,9 +58,6 @@ describe('nsfw-filesystem-watcher', function (): void { }); it('Should receive file changes events from in the workspace by default.', async function (): Promise { - if (process.platform === 'win32') { - this.skip(); - } const actualUris = new Set(); const watcherClient = { @@ -80,13 +87,10 @@ describe('nsfw-filesystem-watcher', function (): void { expect(fs.readFileSync(FileUri.fsPath(root.resolve('foo').resolve('bar').resolve('baz.txt')), 'utf8')).to.be.equal('baz'); await sleep(2000); - assert.deepStrictEqual(expectedUris, [...actualUris]); + assert.deepStrictEqual([...actualUris], expectedUris); }); it('Should not receive file changes events from in the workspace by default if unwatched', async function (): Promise { - if (process.platform === 'win32') { - this.skip(); - } const actualUris = new Set(); const watcherClient = { @@ -113,7 +117,7 @@ describe('nsfw-filesystem-watcher', function (): void { expect(fs.readFileSync(FileUri.fsPath(root.resolve('foo').resolve('bar').resolve('baz.txt')), 'utf8')).to.be.equal('baz'); await sleep(2000); - assert.deepStrictEqual(0, actualUris.size); + assert.deepStrictEqual(actualUris.size, 0); }); function createNsfwFileSystemWatcherService(): NsfwFileSystemWatcherService { diff --git a/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.ts b/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.ts index 703f32ba4a7fa..22ad1c8a20cfc 100644 --- a/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.ts +++ b/packages/filesystem/src/node/nsfw-watcher/nsfw-filesystem-watcher.ts @@ -117,7 +117,7 @@ export class NsfwFileSystemWatcherServer implements FileSystemWatcherServer { this.debug('Files ignored for watching', options.ignored); } - let watcher: nsfw.NSFW | undefined = await nsfw(fs.realpathSync(basePath), (events: nsfw.ChangeEvent[]) => { + let watcher: nsfw.NSFW | undefined = await nsfw(fs.realpathSync(basePath), (events: nsfw.FileChangeEvent[]) => { for (const event of events) { if (event.action === nsfw.actions.CREATED) { this.pushAdded(watcherId, this.resolvePath(event.directory, event.file!)); diff --git a/yarn.lock b/yarn.lock index afb2d6d784cc0..f35ecf8b62a7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9599,7 +9599,7 @@ mute-stream@0.0.8: resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d" integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA== -nan@^2.0.0, nan@^2.12.1, nan@^2.14.0: +nan@^2.12.1, nan@^2.14.0: version "2.14.2" resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19" integrity sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ== @@ -9684,6 +9684,11 @@ node-abi@^2.11.0, node-abi@^2.18.0, node-abi@^2.7.0: dependencies: semver "^5.4.1" +node-addon-api@*: + version "3.1.0" + resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.1.0.tgz#98b21931557466c6729e51cb77cd39c965f42239" + integrity sha512-flmrDNB06LIl5lywUz7YlNGZH/5p0M7W28k8hzd9Lshtdh1wshD2Y+U4h9LD6KObOy1f+fEVdgprPrEymjM5uw== + node-dir@0.1.8: version "0.1.8" resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.8.tgz#55fb8deb699070707fb67f91a460f0448294c77d" @@ -9917,12 +9922,12 @@ npmlog@^4.0.1, npmlog@^4.1.2: gauge "~2.7.3" set-blocking "~2.0.0" -nsfw@^1.2.9: - version "1.2.9" - resolved "https://registry.yarnpkg.com/nsfw/-/nsfw-1.2.9.tgz#e49ff5c12593cbcce3fcc90c533947eb4f15a99a" - integrity sha512-/2o89nygBRTTnGRxSHt2wjagbszyh36HlgF61Ec2iaJBTIIQ6QKcqp92EzVxxZX9U/6Qpy+LZL5i8532hXzAHg== +nsfw@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/nsfw/-/nsfw-2.1.2.tgz#4fa841e7f7122b60b2e1f61187d1b57ad3403428" + integrity sha512-zGPdt32aJ5b1laK9rvgXQmXGAagrx3VkcMt0JePtu6wBfzC1o4xLCM3kq7FxZxUnxyxYhODyBYzpt3H16FhaGA== dependencies: - nan "^2.0.0" + node-addon-api "*" nugget@^2.0.1: version "2.0.1"