From 492264d2e20d738fc8d6f6a81dc6ba9d80b443d0 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Mon, 10 Oct 2016 17:33:47 +0200 Subject: [PATCH] :lipstick: pfs --- src/vs/base/node/pfs.ts | 91 +++------------------- src/vs/workbench/node/extensionHostMain.ts | 26 +++---- 2 files changed, 23 insertions(+), 94 deletions(-) diff --git a/src/vs/base/node/pfs.ts b/src/vs/base/node/pfs.ts index 7420ca61aa1..7366e49d0c8 100644 --- a/src/vs/base/node/pfs.ts +++ b/src/vs/base/node/pfs.ts @@ -12,10 +12,6 @@ import { dirname, join } from 'path'; import { nfcall } from 'vs/base/common/async'; import fs = require('fs'); -export function isRoot(path: string): boolean { - return path === dirname(path); -} - export function readdir(path: string): TPromise { return nfcall(extfs.readdir, path); } @@ -41,7 +37,8 @@ export function mkdirp(path: string, mode?: number): TPromise { return TPromise.wrapError(err); }); - if (isRoot(path)) { + // is root? + if (path === dirname(path)) { return TPromise.as(true); } @@ -84,10 +81,6 @@ export function lstat(path: string): TPromise { return nfcall(fs.lstat, path); } -export function mstat(paths: string[]): TPromise<{ path: string; stats: fs.Stats; }> { - return doStatMultiple(paths.slice(0)); -} - export function rename(oldPath: string, newPath: string): Promise { return nfcall(fs.rename, oldPath, newPath); } @@ -108,40 +101,25 @@ export function readlink(path: string): TPromise { return nfcall(fs.readlink, path); } -export function utimes(path: string, atime: Date, mtime: Date): Promise { +export function utimes(path: string, atime: Date, mtime: Date): TPromise { return nfcall(fs.utimes, path, atime, mtime); } -function doStatMultiple(paths: string[]): TPromise<{ path: string; stats: fs.Stats; }> { - let path = paths.shift(); - return stat(path).then((value) => { - return { - path: path, - stats: value - }; - }, (err) => { - if (paths.length === 0) { - return err; - } - return mstat(paths); - }); -} - export function readFile(path: string): TPromise; export function readFile(path: string, encoding: string): TPromise; export function readFile(path: string, encoding?: string): TPromise { return nfcall(fs.readFile, path, encoding); } -export function writeFile(path: string, data: string, encoding?: string): Promise; -export function writeFile(path: string, data: NodeBuffer, encoding?: string): Promise; -export function writeFile(path: string, data: any, encoding: string = 'utf8'): Promise { +export function writeFile(path: string, data: string, encoding?: string): TPromise; +export function writeFile(path: string, data: NodeBuffer, encoding?: string): TPromise; +export function writeFile(path: string, data: any, encoding: string = 'utf8'): TPromise { return nfcall(fs.writeFile, path, data, encoding); } -export function writeFileAndFlush(path: string, data: string, encoding?: string): Promise; -export function writeFileAndFlush(path: string, data: NodeBuffer, encoding?: string): Promise; -export function writeFileAndFlush(path: string, data: any, encoding: string = 'utf8'): Promise { +export function writeFileAndFlush(path: string, data: string, encoding?: string): TPromise; +export function writeFileAndFlush(path: string, data: NodeBuffer, encoding?: string): TPromise; +export function writeFileAndFlush(path: string, data: any, encoding: string = 'utf8'): TPromise { return nfcall(extfs.writeFileAndFlush, path, data, encoding); } @@ -149,21 +127,13 @@ export function writeFileAndFlush(path: string, data: any, encoding: string = 'u * Read a dir and return only subfolders */ export function readDirsInDir(dirPath: string): TPromise { - return readdir(dirPath).then((children) => { - return TPromise.join( - children.map((child) => dirExistsWithResult(paths.join(dirPath, child), child)) - ).then((subdirs) => { - return removeNull(subdirs); + return readdir(dirPath).then(children => { + return TPromise.join(children.map(c => dirExists(paths.join(dirPath, c)))).then(exists => { + return children.filter((_, i) => exists[i]); }); }); } -function dirExistsWithResult(path: string, successResult: T): TPromise { - return dirExists(path).then((exists) => { - return exists ? successResult : null; - }); -} - /** * `path` exists and is a directory */ @@ -177,40 +147,3 @@ export function dirExists(path: string): TPromise { export function fileExists(path: string): TPromise { return stat(path).then(stat => stat.isFile(), () => false); } - -/** -* Read dir at `path` and return only files matching `pattern` -*/ -export function readFiles(path: string, pattern: RegExp): TPromise { - return readdir(path).then((children) => { - children = children.filter((child) => { - return pattern.test(child); - }); - let fileChildren = children.map((child) => { - return fileExistsWithResult(paths.join(path, child), child); - }); - return TPromise.join(fileChildren).then((subdirs) => { - return removeNull(subdirs); - }); - }); -} - -export function fileExistsWithResult(path: string, successResult: T): TPromise { - return fileExists(path).then((exists) => { - return exists ? successResult : null; - }, (err) => { - return TPromise.wrapError(err); - }); -} - -export function existsWithResult(path: string, successResult: T): TPromise { - return exists(path).then((exists) => { - return exists ? successResult : null; - }, (err) => { - return TPromise.wrapError(err); - }); -} - -function removeNull(arr: T[]): T[] { - return arr.filter(item => (item !== null)); -} \ No newline at end of file diff --git a/src/vs/workbench/node/extensionHostMain.ts b/src/vs/workbench/node/extensionHostMain.ts index d84cdf3d357..0540fffaf8b 100644 --- a/src/vs/workbench/node/extensionHostMain.ts +++ b/src/vs/workbench/node/extensionHostMain.ts @@ -183,9 +183,9 @@ export class ExtensionHostMain { return TPromise.as(null); } - let folderPath = workspace.resource.fsPath; + const folderPath = workspace.resource.fsPath; - let desiredFilesMap: { + const desiredFilesMap: { [filename: string]: boolean; } = {}; @@ -203,21 +203,17 @@ export class ExtensionHostMain { } }); - return TPromise.join( - Object.keys(desiredFilesMap).map( - (fileName) => pfs.existsWithResult(paths.join(folderPath, fileName), fileName) - ) - ).then((fileNames: string[]) => { - fileNames.forEach((existingFileName) => { - if (!existingFileName) { - return; - } + const fileNames = Object.keys(desiredFilesMap); - let activationEvent = 'workspaceContains:' + existingFileName; - this._extensionService.activateByEvent(activationEvent).then(null, (err) => { - console.error(err); + return TPromise.join(fileNames.map(f => pfs.exists(paths.join(folderPath, f)))).then(exists => { + fileNames + .filter((f, i) => exists[i]) + .forEach(fileName => { + const activationEvent = `workspaceContains:${fileName}`; + + this._extensionService.activateByEvent(activationEvent) + .done(null, err => console.error(err)); }); - }); }); }