mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 19:18:59 +01:00
💄 pfs
This commit is contained in:
@@ -12,10 +12,6 @@ import { dirname, join } from 'path';
|
|||||||
import { nfcall } from 'vs/base/common/async';
|
import { nfcall } from 'vs/base/common/async';
|
||||||
import fs = require('fs');
|
import fs = require('fs');
|
||||||
|
|
||||||
export function isRoot(path: string): boolean {
|
|
||||||
return path === dirname(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function readdir(path: string): TPromise<string[]> {
|
export function readdir(path: string): TPromise<string[]> {
|
||||||
return nfcall(extfs.readdir, path);
|
return nfcall(extfs.readdir, path);
|
||||||
}
|
}
|
||||||
@@ -41,7 +37,8 @@ export function mkdirp(path: string, mode?: number): TPromise<boolean> {
|
|||||||
return TPromise.wrapError<boolean>(err);
|
return TPromise.wrapError<boolean>(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isRoot(path)) {
|
// is root?
|
||||||
|
if (path === dirname(path)) {
|
||||||
return TPromise.as(true);
|
return TPromise.as(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,10 +81,6 @@ export function lstat(path: string): TPromise<fs.Stats> {
|
|||||||
return nfcall(fs.lstat, path);
|
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 {
|
export function rename(oldPath: string, newPath: string): Promise {
|
||||||
return nfcall(fs.rename, oldPath, newPath);
|
return nfcall(fs.rename, oldPath, newPath);
|
||||||
}
|
}
|
||||||
@@ -108,40 +101,25 @@ export function readlink(path: string): TPromise<string> {
|
|||||||
return nfcall<string>(fs.readlink, path);
|
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);
|
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): TPromise<Buffer>;
|
||||||
export function readFile(path: string, encoding: string): TPromise<string>;
|
export function readFile(path: string, encoding: string): TPromise<string>;
|
||||||
export function readFile(path: string, encoding?: string): TPromise<Buffer | string> {
|
export function readFile(path: string, encoding?: string): TPromise<Buffer | string> {
|
||||||
return nfcall(fs.readFile, path, encoding);
|
return nfcall(fs.readFile, path, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeFile(path: string, data: string, encoding?: string): Promise;
|
export function writeFile(path: string, data: string, encoding?: string): TPromise<void>;
|
||||||
export function writeFile(path: string, data: NodeBuffer, encoding?: string): Promise;
|
export function writeFile(path: string, data: NodeBuffer, encoding?: string): TPromise<void>;
|
||||||
export function writeFile(path: string, data: any, encoding: string = 'utf8'): Promise {
|
export function writeFile(path: string, data: any, encoding: string = 'utf8'): TPromise<void> {
|
||||||
return nfcall(fs.writeFile, path, data, encoding);
|
return nfcall(fs.writeFile, path, data, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeFileAndFlush(path: string, data: string, encoding?: string): Promise;
|
export function writeFileAndFlush(path: string, data: string, encoding?: string): TPromise<void>;
|
||||||
export function writeFileAndFlush(path: string, data: NodeBuffer, encoding?: string): Promise;
|
export function writeFileAndFlush(path: string, data: NodeBuffer, encoding?: string): TPromise<void>;
|
||||||
export function writeFileAndFlush(path: string, data: any, encoding: string = 'utf8'): Promise {
|
export function writeFileAndFlush(path: string, data: any, encoding: string = 'utf8'): TPromise<void> {
|
||||||
return nfcall(extfs.writeFileAndFlush, path, data, encoding);
|
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
|
* Read a dir and return only subfolders
|
||||||
*/
|
*/
|
||||||
export function readDirsInDir(dirPath: string): TPromise<string[]> {
|
export function readDirsInDir(dirPath: string): TPromise<string[]> {
|
||||||
return readdir(dirPath).then((children) => {
|
return readdir(dirPath).then(children => {
|
||||||
return TPromise.join(
|
return TPromise.join(children.map(c => dirExists(paths.join(dirPath, c)))).then(exists => {
|
||||||
children.map((child) => dirExistsWithResult(paths.join(dirPath, child), child))
|
return children.filter((_, i) => exists[i]);
|
||||||
).then((subdirs) => {
|
|
||||||
return removeNull(subdirs);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function dirExistsWithResult<T>(path: string, successResult: T): TPromise<T> {
|
|
||||||
return dirExists(path).then((exists) => {
|
|
||||||
return exists ? successResult : null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `path` exists and is a directory
|
* `path` exists and is a directory
|
||||||
*/
|
*/
|
||||||
@@ -177,40 +147,3 @@ export function dirExists(path: string): TPromise<boolean> {
|
|||||||
export function fileExists(path: string): TPromise<boolean> {
|
export function fileExists(path: string): TPromise<boolean> {
|
||||||
return stat(path).then(stat => stat.isFile(), () => false);
|
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);
|
return TPromise.as(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
let folderPath = workspace.resource.fsPath;
|
const folderPath = workspace.resource.fsPath;
|
||||||
|
|
||||||
let desiredFilesMap: {
|
const desiredFilesMap: {
|
||||||
[filename: string]: boolean;
|
[filename: string]: boolean;
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
@@ -203,20 +203,16 @@ export class ExtensionHostMain {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return TPromise.join(
|
const fileNames = Object.keys(desiredFilesMap);
|
||||||
Object.keys(desiredFilesMap).map(
|
|
||||||
(fileName) => pfs.existsWithResult(paths.join(folderPath, fileName), fileName)
|
|
||||||
)
|
|
||||||
).then((fileNames: string[]) => {
|
|
||||||
fileNames.forEach((existingFileName) => {
|
|
||||||
if (!existingFileName) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let activationEvent = 'workspaceContains:' + existingFileName;
|
return TPromise.join(fileNames.map(f => pfs.exists(paths.join(folderPath, f)))).then(exists => {
|
||||||
this._extensionService.activateByEvent(activationEvent).then(null, (err) => {
|
fileNames
|
||||||
console.error(err);
|
.filter((f, i) => exists[i])
|
||||||
});
|
.forEach(fileName => {
|
||||||
|
const activationEvent = `workspaceContains:${fileName}`;
|
||||||
|
|
||||||
|
this._extensionService.activateByEvent(activationEvent)
|
||||||
|
.done(null, err => console.error(err));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user