💄 pfs
This commit is contained in:
parent
42a5c57451
commit
492264d2e2
|
@ -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<string[]> {
|
||||
return nfcall(extfs.readdir, path);
|
||||
}
|
||||
|
@ -41,7 +37,8 @@ export function mkdirp(path: string, mode?: number): TPromise<boolean> {
|
|||
return TPromise.wrapError<boolean>(err);
|
||||
});
|
||||
|
||||
if (isRoot(path)) {
|
||||
// is root?
|
||||
if (path === dirname(path)) {
|
||||
return TPromise.as(true);
|
||||
}
|
||||
|
||||
|
@ -84,10 +81,6 @@ export function lstat(path: string): TPromise<fs.Stats> {
|
|||
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<string> {
|
|||
return nfcall<string>(fs.readlink, path);
|
||||
}
|
||||
|
||||
export function utimes(path: string, atime: Date, mtime: Date): Promise {
|
||||
export function utimes(path: string, atime: Date, mtime: Date): TPromise<void> {
|
||||
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<Buffer>;
|
||||
export function readFile(path: string, encoding: string): TPromise<string>;
|
||||
export function readFile(path: string, encoding?: string): TPromise<Buffer | string> {
|
||||
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<void>;
|
||||
export function writeFile(path: string, data: NodeBuffer, encoding?: string): TPromise<void>;
|
||||
export function writeFile(path: string, data: any, encoding: string = 'utf8'): TPromise<void> {
|
||||
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<void>;
|
||||
export function writeFileAndFlush(path: string, data: NodeBuffer, encoding?: string): TPromise<void>;
|
||||
export function writeFileAndFlush(path: string, data: any, encoding: string = 'utf8'): TPromise<void> {
|
||||
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<string[]> {
|
||||
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<T>(path: string, successResult: T): TPromise<T> {
|
||||
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<boolean> {
|
|||
export function fileExists(path: string): TPromise<boolean> {
|
||||
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<string[]> {
|
||||
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<T>(path: string, successResult: T): TPromise<T> {
|
||||
return fileExists(path).then((exists) => {
|
||||
return exists ? successResult : null;
|
||||
}, (err) => {
|
||||
return TPromise.wrapError(err);
|
||||
});
|
||||
}
|
||||
|
||||
export function existsWithResult<T>(path: string, successResult: T): TPromise<T> {
|
||||
return exists(path).then((exists) => {
|
||||
return exists ? successResult : null;
|
||||
}, (err) => {
|
||||
return TPromise.wrapError(err);
|
||||
});
|
||||
}
|
||||
|
||||
function removeNull<T>(arr: T[]): T[] {
|
||||
return arr.filter(item => (item !== null));
|
||||
}
|
|
@ -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));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue