|
|
|
@ -5,7 +5,8 @@
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
TaskDefinition, Task, TaskGroup, WorkspaceFolder, RelativePattern, ShellExecution, Uri, workspace,
|
|
|
|
|
DebugConfiguration, debug, TaskProvider, TextDocument, tasks, TaskScope, QuickPickItem, window, Position, ExtensionContext, env
|
|
|
|
|
DebugConfiguration, debug, TaskProvider, TextDocument, tasks, TaskScope, QuickPickItem, window, Position, ExtensionContext, env,
|
|
|
|
|
ShellQuotedString, ShellQuoting
|
|
|
|
|
} from 'vscode';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
@ -48,11 +49,11 @@ export class NpmTaskProvider implements TaskProvider {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get tasksWithLocation(): Promise<TaskWithLocation[]> {
|
|
|
|
|
return provideNpmScripts(this.context);
|
|
|
|
|
return provideNpmScripts(this.context, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async provideTasks() {
|
|
|
|
|
const tasks = await provideNpmScripts(this.context);
|
|
|
|
|
const tasks = await provideNpmScripts(this.context, true);
|
|
|
|
|
return tasks.map(task => task.task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -70,7 +71,11 @@ export class NpmTaskProvider implements TaskProvider {
|
|
|
|
|
} else {
|
|
|
|
|
packageJsonUri = _task.scope.uri.with({ path: _task.scope.uri.path + '/package.json' });
|
|
|
|
|
}
|
|
|
|
|
return createTask(this.context, kind, `${kind.script === INSTALL_SCRIPT ? '' : 'run '}${kind.script}`, _task.scope, packageJsonUri);
|
|
|
|
|
const cmd = [kind.script];
|
|
|
|
|
if (kind.script !== INSTALL_SCRIPT) {
|
|
|
|
|
cmd.unshift('run');
|
|
|
|
|
}
|
|
|
|
|
return createTask(this.context, kind, cmd, _task.scope, packageJsonUri);
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
@ -123,14 +128,14 @@ export function isWorkspaceFolder(value: any): value is WorkspaceFolder {
|
|
|
|
|
return value && typeof value !== 'number';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getPackageManager(extensionContext: ExtensionContext, folder: Uri): Promise<string> {
|
|
|
|
|
export async function getPackageManager(extensionContext: ExtensionContext, folder: Uri, showWarning: boolean = true): 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)) {
|
|
|
|
|
if (showWarning && 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");
|
|
|
|
@ -167,7 +172,7 @@ export async function hasNpmScripts(): Promise<boolean> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function detectNpmScripts(context: ExtensionContext): Promise<TaskWithLocation[]> {
|
|
|
|
|
async function detectNpmScripts(context: ExtensionContext, showWarning: boolean): Promise<TaskWithLocation[]> {
|
|
|
|
|
|
|
|
|
|
let emptyTasks: TaskWithLocation[] = [];
|
|
|
|
|
let allTasks: TaskWithLocation[] = [];
|
|
|
|
@ -184,7 +189,7 @@ async function detectNpmScripts(context: ExtensionContext): Promise<TaskWithLoca
|
|
|
|
|
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(context, path);
|
|
|
|
|
let tasks = await provideNpmScriptsForFolder(context, path, showWarning);
|
|
|
|
|
visitedPackageJsonFiles.add(path.fsPath);
|
|
|
|
|
allTasks.push(...tasks);
|
|
|
|
|
}
|
|
|
|
@ -209,7 +214,7 @@ export async function detectNpmScriptsForFolder(context: ExtensionContext, folde
|
|
|
|
|
let visitedPackageJsonFiles: Set<string> = new Set();
|
|
|
|
|
for (const path of paths) {
|
|
|
|
|
if (!visitedPackageJsonFiles.has(path.fsPath)) {
|
|
|
|
|
let tasks = await provideNpmScriptsForFolder(context, path);
|
|
|
|
|
let tasks = await provideNpmScriptsForFolder(context, path, true);
|
|
|
|
|
visitedPackageJsonFiles.add(path.fsPath);
|
|
|
|
|
folderTasks.push(...tasks.map(t => ({ label: t.task.name, task: t.task })));
|
|
|
|
|
}
|
|
|
|
@ -220,9 +225,9 @@ export async function detectNpmScriptsForFolder(context: ExtensionContext, folde
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function provideNpmScripts(context: ExtensionContext): Promise<TaskWithLocation[]> {
|
|
|
|
|
export async function provideNpmScripts(context: ExtensionContext, showWarning: boolean): Promise<TaskWithLocation[]> {
|
|
|
|
|
if (!cachedTasks) {
|
|
|
|
|
cachedTasks = await detectNpmScripts(context);
|
|
|
|
|
cachedTasks = await detectNpmScripts(context, showWarning);
|
|
|
|
|
}
|
|
|
|
|
return cachedTasks;
|
|
|
|
|
}
|
|
|
|
@ -258,7 +263,7 @@ function isDebugScript(script: string): boolean {
|
|
|
|
|
return match !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function provideNpmScriptsForFolder(context: ExtensionContext, packageJsonUri: Uri): Promise<TaskWithLocation[]> {
|
|
|
|
|
async function provideNpmScriptsForFolder(context: ExtensionContext, packageJsonUri: Uri, showWarning: boolean): Promise<TaskWithLocation[]> {
|
|
|
|
|
let emptyTasks: TaskWithLocation[] = [];
|
|
|
|
|
|
|
|
|
|
let folder = workspace.getWorkspaceFolder(packageJsonUri);
|
|
|
|
@ -276,7 +281,7 @@ async function provideNpmScriptsForFolder(context: ExtensionContext, packageJson
|
|
|
|
|
|
|
|
|
|
for (const each of scripts.keys()) {
|
|
|
|
|
const scriptValue = scripts.get(each)!;
|
|
|
|
|
const task = await createTask(context, each, `run ${each}`, folder!, packageJsonUri, scriptValue.script);
|
|
|
|
|
const task = await createTask(context, each, ['run', each], folder!, packageJsonUri, showWarning, scriptValue.script);
|
|
|
|
|
const lowerCaseTaskName = each.toLowerCase();
|
|
|
|
|
if (isBuildTask(lowerCaseTaskName)) {
|
|
|
|
|
task.group = TaskGroup.Build;
|
|
|
|
@ -295,7 +300,7 @@ async function provideNpmScriptsForFolder(context: ExtensionContext, packageJson
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// always add npm install (without a problem matcher)
|
|
|
|
|
result.push({ task: await createTask(context, INSTALL_SCRIPT, INSTALL_SCRIPT, folder, packageJsonUri, 'install dependencies from package', []) });
|
|
|
|
|
result.push({ task: await createTask(context, INSTALL_SCRIPT, [INSTALL_SCRIPT], folder, packageJsonUri, showWarning, 'install dependencies from package', []) });
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -306,7 +311,7 @@ export function getTaskName(script: string, relativePath: string | undefined) {
|
|
|
|
|
return script;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createTask(context: ExtensionContext, script: NpmTaskDefinition | string, cmd: string, folder: WorkspaceFolder, packageJsonUri: Uri, detail?: string, matcher?: any): Promise<Task> {
|
|
|
|
|
export async function createTask(context: ExtensionContext, script: NpmTaskDefinition | string, cmd: string[], folder: WorkspaceFolder, packageJsonUri: Uri, showWarning: boolean = true, detail?: string, matcher?: any): Promise<Task> {
|
|
|
|
|
let kind: NpmTaskDefinition;
|
|
|
|
|
if (typeof script === 'string') {
|
|
|
|
|
kind = { type: 'npm', script: script };
|
|
|
|
@ -314,12 +319,20 @@ export async function createTask(context: ExtensionContext, script: NpmTaskDefin
|
|
|
|
|
kind = script;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}`;
|
|
|
|
|
const packageManager = await getPackageManager(context, folder.uri, showWarning);
|
|
|
|
|
function getCommandLine(cmd: string[]): (string | ShellQuotedString)[] {
|
|
|
|
|
const result: (string | ShellQuotedString)[] = new Array(cmd.length);
|
|
|
|
|
for (let i = 0; i < cmd.length; i++) {
|
|
|
|
|
if (/\s/.test(cmd[i])) {
|
|
|
|
|
result[i] = { value: cmd[i], quoting: cmd[i].includes('--') ? ShellQuoting.Weak : ShellQuoting.Strong };
|
|
|
|
|
} else {
|
|
|
|
|
result[i] = cmd[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return `${packageManager} ${cmd}`;
|
|
|
|
|
if (workspace.getConfiguration('npm', folder.uri).get<boolean>('runSilent')) {
|
|
|
|
|
result.unshift('--silent');
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRelativePath(packageJsonUri: Uri): string {
|
|
|
|
@ -334,7 +347,7 @@ export async function createTask(context: ExtensionContext, script: NpmTaskDefin
|
|
|
|
|
}
|
|
|
|
|
let taskName = getTaskName(kind.script, relativePackageJson);
|
|
|
|
|
let cwd = path.dirname(packageJsonUri.fsPath);
|
|
|
|
|
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(await getCommandLine(cmd), { cwd: cwd }), matcher);
|
|
|
|
|
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(packageManager, getCommandLine(cmd), { cwd: cwd }), matcher);
|
|
|
|
|
task.detail = detail;
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
@ -379,7 +392,7 @@ export async function runScript(context: ExtensionContext, script: string, docum
|
|
|
|
|
let uri = document.uri;
|
|
|
|
|
let folder = workspace.getWorkspaceFolder(uri);
|
|
|
|
|
if (folder) {
|
|
|
|
|
let task = await createTask(context, script, `run ${script}`, folder, uri);
|
|
|
|
|
let task = await createTask(context, script, ['run', script], folder, uri);
|
|
|
|
|
tasks.executeTask(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|