Prefer using vscode.workspace.fs over node fs
This commit is contained in:
parent
0749b7aca8
commit
cf197953d7
|
@ -3,7 +3,6 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as jsonc from 'jsonc-parser';
|
||||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
|
@ -18,14 +17,14 @@ const localize = nls.loadMessageBundle();
|
|||
|
||||
type AutoDetect = 'on' | 'off' | 'build' | 'watch';
|
||||
|
||||
|
||||
const exists = (file: string): Promise<boolean> =>
|
||||
new Promise<boolean>((resolve, _reject) => {
|
||||
fs.exists(file, (value: boolean) => {
|
||||
resolve(value);
|
||||
});
|
||||
});
|
||||
|
||||
const exists = async (resource: vscode.Uri): Promise<boolean> => {
|
||||
try {
|
||||
const stat = await vscode.workspace.fs.stat(resource);
|
||||
return stat.type === vscode.FileType.File;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
interface TypeScriptTaskDefinition extends vscode.TaskDefinition {
|
||||
tsconfig: string;
|
||||
|
@ -64,8 +63,8 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
const configPaths: Set<string> = new Set();
|
||||
const tasks: vscode.Task[] = [];
|
||||
for (const project of await this.getAllTsConfigs(token)) {
|
||||
if (!configPaths.has(project.path)) {
|
||||
configPaths.add(project.path);
|
||||
if (!configPaths.has(project.fsPath)) {
|
||||
configPaths.add(project.fsPath);
|
||||
tasks.push(...(await this.getTasksForProject(project)));
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +89,8 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
const kind: TypeScriptTaskDefinition = (<any>_task.definition);
|
||||
const tsconfigUri: vscode.Uri = _task.scope.uri.with({ path: _task.scope.uri.path + '/' + kind.tsconfig });
|
||||
const tsconfig: TSConfig = {
|
||||
path: tsconfigUri.fsPath,
|
||||
uri: tsconfigUri,
|
||||
fsPath: tsconfigUri.fsPath,
|
||||
posixPath: tsconfigUri.path,
|
||||
workspaceFolder: _task.scope
|
||||
};
|
||||
|
@ -106,7 +106,7 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
...await this.getTsConfigsInWorkspace()
|
||||
];
|
||||
for (const config of configs) {
|
||||
if (await exists(config.path)) {
|
||||
if (await exists(config.uri)) {
|
||||
out.add(config);
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +119,8 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
if (isTsConfigFileName(editor.document.fileName)) {
|
||||
const uri = editor.document.uri;
|
||||
return [{
|
||||
path: uri.fsPath,
|
||||
uri,
|
||||
fsPath: uri.fsPath,
|
||||
posixPath: uri.path,
|
||||
workspaceFolder: vscode.workspace.getWorkspaceFolder(uri)
|
||||
}];
|
||||
|
@ -148,7 +149,8 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
const uri = vscode.Uri.file(normalizedConfigPath);
|
||||
const folder = vscode.workspace.getWorkspaceFolder(uri);
|
||||
return [{
|
||||
path: normalizedConfigPath,
|
||||
uri,
|
||||
fsPath: normalizedConfigPath,
|
||||
posixPath: uri.path,
|
||||
workspaceFolder: folder
|
||||
}];
|
||||
|
@ -163,7 +165,7 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
|
||||
private static async getCommand(project: TSConfig): Promise<string> {
|
||||
if (project.workspaceFolder) {
|
||||
const localTsc = await TscTaskProvider.getLocalTscAtPath(path.dirname(project.path));
|
||||
const localTsc = await TscTaskProvider.getLocalTscAtPath(path.dirname(project.fsPath));
|
||||
if (localTsc) {
|
||||
return localTsc;
|
||||
}
|
||||
|
@ -181,9 +183,9 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
private static async getLocalTscAtPath(folderPath: string): Promise<string | undefined> {
|
||||
const platform = process.platform;
|
||||
const bin = path.join(folderPath, 'node_modules', '.bin');
|
||||
if (platform === 'win32' && await exists(path.join(bin, 'tsc.cmd'))) {
|
||||
if (platform === 'win32' && await exists(vscode.Uri.file(path.join(bin, 'tsc.cmd')))) {
|
||||
return path.join(bin, 'tsc.cmd');
|
||||
} else if ((platform === 'linux' || platform === 'darwin') && await exists(path.join(bin, 'tsc'))) {
|
||||
} else if ((platform === 'linux' || platform === 'darwin') && await exists(vscode.Uri.file(path.join(bin, 'tsc')))) {
|
||||
return path.join(bin, 'tsc');
|
||||
}
|
||||
return undefined;
|
||||
|
@ -201,7 +203,7 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
}
|
||||
|
||||
private getBuildTask(workspaceFolder: vscode.WorkspaceFolder | undefined, label: string, command: string, args: string[], buildTaskidentifier: TypeScriptTaskDefinition): vscode.Task {
|
||||
const buildTask = new vscode.Task(
|
||||
const buildTask = new vscode.Task2(
|
||||
buildTaskidentifier,
|
||||
workspaceFolder || vscode.TaskScope.Workspace,
|
||||
localize('buildTscLabel', 'build - {0}', label),
|
||||
|
@ -238,7 +240,6 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
}
|
||||
|
||||
if (this.autoDetect === 'watch' || this.autoDetect === 'on') {
|
||||
|
||||
tasks.push(this.getWatchTask(project.workspaceFolder, label, command, args, { type: 'typescript', tsconfig: label, option: 'watch' }));
|
||||
}
|
||||
|
||||
|
@ -261,25 +262,19 @@ export default class TscTaskProvider implements vscode.TaskProvider {
|
|||
return task;
|
||||
}
|
||||
|
||||
private getBuildShellArgs(project: TSConfig): Promise<Array<string>> {
|
||||
const defaultArgs = ['-p', project.path];
|
||||
return new Promise<Array<string>>((resolve) => {
|
||||
fs.readFile(project.path, (error, result) => {
|
||||
if (error) {
|
||||
return resolve(defaultArgs);
|
||||
}
|
||||
|
||||
try {
|
||||
const tsconfig = jsonc.parse(result.toString());
|
||||
if (tsconfig.references) {
|
||||
return resolve(['-b', project.path]);
|
||||
}
|
||||
} catch {
|
||||
// noop
|
||||
}
|
||||
return resolve(defaultArgs);
|
||||
});
|
||||
});
|
||||
private async getBuildShellArgs(project: TSConfig): Promise<Array<string>> {
|
||||
const defaultArgs = ['-p', project.fsPath];
|
||||
try {
|
||||
const bytes = await vscode.workspace.fs.readFile(project.uri);
|
||||
const text = Buffer.from(bytes).toString('utf-8');
|
||||
const tsconfig = jsonc.parse(text);
|
||||
if (tsconfig?.references) {
|
||||
return ['-b', project.fsPath];
|
||||
}
|
||||
} catch {
|
||||
// noops
|
||||
}
|
||||
return defaultArgs;
|
||||
}
|
||||
|
||||
private getLabelForTasks(project: TSConfig): string {
|
||||
|
|
|
@ -2,10 +2,12 @@
|
|||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export interface TSConfig {
|
||||
readonly path: string;
|
||||
readonly uri: vscode.Uri;
|
||||
readonly fsPath: string;
|
||||
readonly posixPath: string;
|
||||
readonly workspaceFolder?: vscode.WorkspaceFolder;
|
||||
}
|
||||
|
@ -20,7 +22,8 @@ export default class TsConfigProvider {
|
|||
const root = vscode.workspace.getWorkspaceFolder(config);
|
||||
if (root) {
|
||||
configs.set(config.fsPath, {
|
||||
path: config.fsPath,
|
||||
uri: config,
|
||||
fsPath: config.fsPath,
|
||||
posixPath: config.path,
|
||||
workspaceFolder: root
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue