mirror of
https://github.com/coder/code-server.git
synced 2025-08-01 19:30:05 +08:00
Update to VS Code 1.52.1
This commit is contained in:
@ -5,13 +5,14 @@
|
||||
|
||||
import {
|
||||
TaskDefinition, Task, TaskGroup, WorkspaceFolder, RelativePattern, ShellExecution, Uri, workspace,
|
||||
DebugConfiguration, debug, TaskProvider, TextDocument, tasks, TaskScope, QuickPickItem
|
||||
DebugConfiguration, debug, TaskProvider, TextDocument, tasks, TaskScope, QuickPickItem, window, Position, ExtensionContext, env
|
||||
} from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as minimatch from 'minimatch';
|
||||
import * as nls from 'vscode-nls';
|
||||
import { JSONVisitor, visit, ParseErrorCode } from 'jsonc-parser';
|
||||
import { findPreferredPM } from './preferred-pm';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
@ -27,20 +28,35 @@ export interface FolderTaskItem extends QuickPickItem {
|
||||
|
||||
type AutoDetect = 'on' | 'off';
|
||||
|
||||
let cachedTasks: Task[] | undefined = undefined;
|
||||
let cachedTasks: TaskWithLocation[] | undefined = undefined;
|
||||
|
||||
const INSTALL_SCRIPT = 'install';
|
||||
|
||||
export interface TaskLocation {
|
||||
document: Uri,
|
||||
line: Position
|
||||
}
|
||||
|
||||
export interface TaskWithLocation {
|
||||
task: Task,
|
||||
location?: TaskLocation
|
||||
}
|
||||
|
||||
export class NpmTaskProvider implements TaskProvider {
|
||||
|
||||
constructor() {
|
||||
constructor(private context: ExtensionContext) {
|
||||
}
|
||||
|
||||
public provideTasks() {
|
||||
return provideNpmScripts();
|
||||
get tasksWithLocation(): Promise<TaskWithLocation[]> {
|
||||
return provideNpmScripts(this.context);
|
||||
}
|
||||
|
||||
public resolveTask(_task: Task): Task | undefined {
|
||||
public async provideTasks() {
|
||||
const tasks = await provideNpmScripts(this.context);
|
||||
return tasks.map(task => task.task);
|
||||
}
|
||||
|
||||
public resolveTask(_task: Task): Promise<Task> | undefined {
|
||||
const npmTask = (<any>_task.definition).script;
|
||||
if (npmTask) {
|
||||
const kind: NpmTaskDefinition = (<any>_task.definition);
|
||||
@ -54,7 +70,7 @@ export class NpmTaskProvider implements TaskProvider {
|
||||
} else {
|
||||
packageJsonUri = _task.scope.uri.with({ path: _task.scope.uri.path + '/package.json' });
|
||||
}
|
||||
return createTask(kind, `${kind.script === INSTALL_SCRIPT ? '' : 'run '}${kind.script}`, _task.scope, packageJsonUri);
|
||||
return createTask(this.context, kind, `${kind.script === INSTALL_SCRIPT ? '' : 'run '}${kind.script}`, _task.scope, packageJsonUri);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@ -107,8 +123,27 @@ export function isWorkspaceFolder(value: any): value is WorkspaceFolder {
|
||||
return value && typeof value !== 'number';
|
||||
}
|
||||
|
||||
export function getPackageManager(folder: Uri): string {
|
||||
return workspace.getConfiguration('npm', folder).get<string>('packageManager', 'npm');
|
||||
export async function getPackageManager(extensionContext: ExtensionContext, folder: Uri): Promise<string> {
|
||||
let packageManagerName = workspace.getConfiguration('npm', folder).get<string>('packageManager', 'npm');
|
||||
|
||||
if (packageManagerName === 'auto') {
|
||||
const { name, multiplePMDetected } = await findPreferredPM(folder.fsPath);
|
||||
packageManagerName = name;
|
||||
const neverShowWarning = 'npm.multiplePMWarning.neverShow';
|
||||
if (multiplePMDetected && !extensionContext.globalState.get<boolean>(neverShowWarning)) {
|
||||
const multiplePMWarning = localize('npm.multiplePMWarning', 'Using {0} as the preferred package manager. Found multiple lockfiles for {1}.', packageManagerName, folder.fsPath);
|
||||
const neverShowAgain = localize('npm.multiplePMWarning.doNotShow', "Do not show again");
|
||||
const learnMore = localize('npm.multiplePMWarning.learnMore', "Learn more");
|
||||
window.showInformationMessage(multiplePMWarning, learnMore, neverShowAgain).then(result => {
|
||||
switch (result) {
|
||||
case neverShowAgain: extensionContext.globalState.update(neverShowWarning, true); break;
|
||||
case learnMore: env.openExternal(Uri.parse('https://nodejs.dev/learn/the-package-lock-json-file'));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return packageManagerName;
|
||||
}
|
||||
|
||||
export async function hasNpmScripts(): Promise<boolean> {
|
||||
@ -132,10 +167,10 @@ export async function hasNpmScripts(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
async function detectNpmScripts(): Promise<Task[]> {
|
||||
async function detectNpmScripts(context: ExtensionContext): Promise<TaskWithLocation[]> {
|
||||
|
||||
let emptyTasks: Task[] = [];
|
||||
let allTasks: Task[] = [];
|
||||
let emptyTasks: TaskWithLocation[] = [];
|
||||
let allTasks: TaskWithLocation[] = [];
|
||||
let visitedPackageJsonFiles: Set<string> = new Set();
|
||||
|
||||
let folders = workspace.workspaceFolders;
|
||||
@ -149,7 +184,7 @@ async function detectNpmScripts(): Promise<Task[]> {
|
||||
let paths = await workspace.findFiles(relativePattern, '**/{node_modules,.vscode-test}/**');
|
||||
for (const path of paths) {
|
||||
if (!isExcluded(folder, path) && !visitedPackageJsonFiles.has(path.fsPath)) {
|
||||
let tasks = await provideNpmScriptsForFolder(path);
|
||||
let tasks = await provideNpmScriptsForFolder(context, path);
|
||||
visitedPackageJsonFiles.add(path.fsPath);
|
||||
allTasks.push(...tasks);
|
||||
}
|
||||
@ -163,7 +198,7 @@ async function detectNpmScripts(): Promise<Task[]> {
|
||||
}
|
||||
|
||||
|
||||
export async function detectNpmScriptsForFolder(folder: Uri): Promise<FolderTaskItem[]> {
|
||||
export async function detectNpmScriptsForFolder(context: ExtensionContext, folder: Uri): Promise<FolderTaskItem[]> {
|
||||
|
||||
let folderTasks: FolderTaskItem[] = [];
|
||||
|
||||
@ -174,9 +209,9 @@ export async function detectNpmScriptsForFolder(folder: Uri): Promise<FolderTask
|
||||
let visitedPackageJsonFiles: Set<string> = new Set();
|
||||
for (const path of paths) {
|
||||
if (!visitedPackageJsonFiles.has(path.fsPath)) {
|
||||
let tasks = await provideNpmScriptsForFolder(path);
|
||||
let tasks = await provideNpmScriptsForFolder(context, path);
|
||||
visitedPackageJsonFiles.add(path.fsPath);
|
||||
folderTasks.push(...tasks.map(t => ({ label: t.name, task: t })));
|
||||
folderTasks.push(...tasks.map(t => ({ label: t.task.name, task: t.task })));
|
||||
}
|
||||
}
|
||||
return folderTasks;
|
||||
@ -185,9 +220,9 @@ export async function detectNpmScriptsForFolder(folder: Uri): Promise<FolderTask
|
||||
}
|
||||
}
|
||||
|
||||
export async function provideNpmScripts(): Promise<Task[]> {
|
||||
export async function provideNpmScripts(context: ExtensionContext): Promise<TaskWithLocation[]> {
|
||||
if (!cachedTasks) {
|
||||
cachedTasks = await detectNpmScripts();
|
||||
cachedTasks = await detectNpmScripts(context);
|
||||
}
|
||||
return cachedTasks;
|
||||
}
|
||||
@ -223,8 +258,8 @@ function isDebugScript(script: string): boolean {
|
||||
return match !== null;
|
||||
}
|
||||
|
||||
async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<Task[]> {
|
||||
let emptyTasks: Task[] = [];
|
||||
async function provideNpmScriptsForFolder(context: ExtensionContext, packageJsonUri: Uri): Promise<TaskWithLocation[]> {
|
||||
let emptyTasks: TaskWithLocation[] = [];
|
||||
|
||||
let folder = workspace.getWorkspaceFolder(packageJsonUri);
|
||||
if (!folder) {
|
||||
@ -235,11 +270,13 @@ async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<Task[]>
|
||||
return emptyTasks;
|
||||
}
|
||||
|
||||
const result: Task[] = [];
|
||||
const result: TaskWithLocation[] = [];
|
||||
|
||||
const prePostScripts = getPrePostScripts(scripts);
|
||||
Object.keys(scripts).forEach(each => {
|
||||
const task = createTask(each, `run ${each}`, folder!, packageJsonUri, scripts![each]);
|
||||
|
||||
for (const each of scripts.keys()) {
|
||||
const scriptValue = scripts.get(each)!;
|
||||
const task = await createTask(context, each, `run ${each}`, folder!, packageJsonUri, scriptValue.script);
|
||||
const lowerCaseTaskName = each.toLowerCase();
|
||||
if (isBuildTask(lowerCaseTaskName)) {
|
||||
task.group = TaskGroup.Build;
|
||||
@ -251,13 +288,14 @@ async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<Task[]>
|
||||
}
|
||||
|
||||
// todo@connor4312: all scripts are now debuggable, what is a 'debug script'?
|
||||
if (isDebugScript(scripts![each])) {
|
||||
if (isDebugScript(scriptValue.script)) {
|
||||
task.group = TaskGroup.Rebuild; // hack: use Rebuild group to tag debug scripts
|
||||
}
|
||||
result.push(task);
|
||||
});
|
||||
result.push({ task, location: scriptValue.location });
|
||||
}
|
||||
|
||||
// always add npm install (without a problem matcher)
|
||||
result.push(createTask(INSTALL_SCRIPT, INSTALL_SCRIPT, folder, packageJsonUri, 'install dependencies from package', []));
|
||||
result.push({ task: await createTask(context, INSTALL_SCRIPT, INSTALL_SCRIPT, folder, packageJsonUri, 'install dependencies from package', []) });
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -268,7 +306,7 @@ export function getTaskName(script: string, relativePath: string | undefined) {
|
||||
return script;
|
||||
}
|
||||
|
||||
export function createTask(script: NpmTaskDefinition | string, cmd: string, folder: WorkspaceFolder, packageJsonUri: Uri, detail?: string, matcher?: any): Task {
|
||||
export async function createTask(context: ExtensionContext, script: NpmTaskDefinition | string, cmd: string, folder: WorkspaceFolder, packageJsonUri: Uri, detail?: string, matcher?: any): Promise<Task> {
|
||||
let kind: NpmTaskDefinition;
|
||||
if (typeof script === 'string') {
|
||||
kind = { type: 'npm', script: script };
|
||||
@ -276,27 +314,27 @@ export function createTask(script: NpmTaskDefinition | string, cmd: string, fold
|
||||
kind = script;
|
||||
}
|
||||
|
||||
function getCommandLine(folder: WorkspaceFolder, cmd: string): string {
|
||||
let packageManager = getPackageManager(folder.uri);
|
||||
const packageManager = await getPackageManager(context, folder.uri);
|
||||
async function getCommandLine(cmd: string): Promise<string> {
|
||||
if (workspace.getConfiguration('npm', folder.uri).get<boolean>('runSilent')) {
|
||||
return `${packageManager} --silent ${cmd}`;
|
||||
}
|
||||
return `${packageManager} ${cmd}`;
|
||||
}
|
||||
|
||||
function getRelativePath(folder: WorkspaceFolder, packageJsonUri: Uri): string {
|
||||
function getRelativePath(packageJsonUri: Uri): string {
|
||||
let rootUri = folder.uri;
|
||||
let absolutePath = packageJsonUri.path.substring(0, packageJsonUri.path.length - 'package.json'.length);
|
||||
return absolutePath.substring(rootUri.path.length + 1);
|
||||
}
|
||||
|
||||
let relativePackageJson = getRelativePath(folder, packageJsonUri);
|
||||
let relativePackageJson = getRelativePath(packageJsonUri);
|
||||
if (relativePackageJson.length) {
|
||||
kind.path = getRelativePath(folder, packageJsonUri);
|
||||
kind.path = relativePackageJson;
|
||||
}
|
||||
let taskName = getTaskName(kind.script, relativePackageJson);
|
||||
let cwd = path.dirname(packageJsonUri.fsPath);
|
||||
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(getCommandLine(folder, cmd), { cwd: cwd }), matcher);
|
||||
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(await getCommandLine(cmd), { cwd: cwd }), matcher);
|
||||
task.detail = detail;
|
||||
return task;
|
||||
}
|
||||
@ -337,33 +375,22 @@ async function exists(file: string): Promise<boolean> {
|
||||
});
|
||||
}
|
||||
|
||||
async function readFile(file: string): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(data.toString());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function runScript(script: string, document: TextDocument) {
|
||||
export async function runScript(context: ExtensionContext, script: string, document: TextDocument) {
|
||||
let uri = document.uri;
|
||||
let folder = workspace.getWorkspaceFolder(uri);
|
||||
if (folder) {
|
||||
let task = createTask(script, `run ${script}`, folder, uri);
|
||||
let task = await createTask(context, script, `run ${script}`, folder, uri);
|
||||
tasks.executeTask(task);
|
||||
}
|
||||
}
|
||||
|
||||
export function startDebugging(scriptName: string, cwd: string, folder: WorkspaceFolder) {
|
||||
export async function startDebugging(context: ExtensionContext, scriptName: string, cwd: string, folder: WorkspaceFolder) {
|
||||
const config: DebugConfiguration = {
|
||||
type: 'pwa-node',
|
||||
request: 'launch',
|
||||
name: `Debug ${scriptName}`,
|
||||
cwd,
|
||||
runtimeExecutable: getPackageManager(folder.uri),
|
||||
runtimeExecutable: await getPackageManager(context, folder.uri),
|
||||
runtimeArgs: [
|
||||
'run',
|
||||
scriptName,
|
||||
@ -378,10 +405,11 @@ export function startDebugging(scriptName: string, cwd: string, folder: Workspac
|
||||
|
||||
export type StringMap = { [s: string]: string; };
|
||||
|
||||
async function findAllScripts(buffer: string): Promise<StringMap> {
|
||||
let scripts: StringMap = {};
|
||||
async function findAllScripts(document: TextDocument, buffer: string): Promise<Map<string, { script: string, location: TaskLocation }>> {
|
||||
let scripts: Map<string, { script: string, location: TaskLocation }> = new Map();
|
||||
let script: string | undefined = undefined;
|
||||
let inScripts = false;
|
||||
let scriptOffset = 0;
|
||||
|
||||
let visitor: JSONVisitor = {
|
||||
onError(_error: ParseErrorCode, _offset: number, _length: number) {
|
||||
@ -395,17 +423,18 @@ async function findAllScripts(buffer: string): Promise<StringMap> {
|
||||
onLiteralValue(value: any, _offset: number, _length: number) {
|
||||
if (script) {
|
||||
if (typeof value === 'string') {
|
||||
scripts[script] = value;
|
||||
scripts.set(script, { script: value, location: { document: document.uri, line: document.positionAt(scriptOffset) } });
|
||||
}
|
||||
script = undefined;
|
||||
}
|
||||
},
|
||||
onObjectProperty(property: string, _offset: number, _length: number) {
|
||||
onObjectProperty(property: string, offset: number, _length: number) {
|
||||
if (property === 'scripts') {
|
||||
inScripts = true;
|
||||
}
|
||||
else if (inScripts && !script) {
|
||||
script = property;
|
||||
scriptOffset = offset;
|
||||
} else { // nested object which is invalid, ignore the script
|
||||
script = undefined;
|
||||
}
|
||||
@ -493,7 +522,7 @@ export function findScriptAtPosition(buffer: string, offset: number): string | u
|
||||
return foundScript;
|
||||
}
|
||||
|
||||
export async function getScripts(packageJsonUri: Uri): Promise<StringMap | undefined> {
|
||||
export async function getScripts(packageJsonUri: Uri): Promise<Map<string, { script: string, location: TaskLocation }> | undefined> {
|
||||
|
||||
if (packageJsonUri.scheme !== 'file') {
|
||||
return undefined;
|
||||
@ -505,8 +534,9 @@ export async function getScripts(packageJsonUri: Uri): Promise<StringMap | undef
|
||||
}
|
||||
|
||||
try {
|
||||
let contents = await readFile(packageJson);
|
||||
let json = findAllScripts(contents);//JSON.parse(contents);
|
||||
const document: TextDocument = await workspace.openTextDocument(packageJsonUri);
|
||||
let contents = document.getText();
|
||||
let json = findAllScripts(document, contents);//JSON.parse(contents);
|
||||
return json;
|
||||
} catch (e) {
|
||||
let localizedParseError = localize('npm.parseError', 'Npm task detection: failed to parse the file {0}', packageJsonUri.fsPath);
|
||||
|
Reference in New Issue
Block a user