|
|
|
@ -5,7 +5,7 @@
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
TaskDefinition, Task, TaskGroup, WorkspaceFolder, RelativePattern, ShellExecution, Uri, workspace,
|
|
|
|
|
DebugConfiguration, debug, TaskProvider, TextDocument, tasks, TaskScope, QuickPickItem, window, Position
|
|
|
|
|
DebugConfiguration, debug, TaskProvider, TextDocument, tasks, TaskScope, QuickPickItem, window, Position, ExtensionContext, env
|
|
|
|
|
} from 'vscode';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
@ -44,15 +44,15 @@ export interface TaskWithLocation {
|
|
|
|
|
|
|
|
|
|
export class NpmTaskProvider implements TaskProvider {
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
constructor(private context: ExtensionContext) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get tasksWithLocation(): Promise<TaskWithLocation[]> {
|
|
|
|
|
return provideNpmScripts();
|
|
|
|
|
return provideNpmScripts(this.context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async provideTasks() {
|
|
|
|
|
const tasks = await provideNpmScripts();
|
|
|
|
|
const tasks = await provideNpmScripts(this.context);
|
|
|
|
|
return tasks.map(task => task.task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -70,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;
|
|
|
|
|
}
|
|
|
|
@ -123,16 +123,23 @@ export function isWorkspaceFolder(value: any): value is WorkspaceFolder {
|
|
|
|
|
return value && typeof value !== 'number';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getPackageManager(folder: Uri): Promise<string> {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (multiplePMDetected) {
|
|
|
|
|
const multiplePMWarning = localize('npm.multiplePMWarning', 'Found multiple lockfiles for {0}. Using {1} as the preferred package manager.', folder.fsPath, packageManagerName);
|
|
|
|
|
window.showWarningMessage(multiplePMWarning);
|
|
|
|
|
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'));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -160,7 +167,7 @@ export async function hasNpmScripts(): Promise<boolean> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function detectNpmScripts(): Promise<TaskWithLocation[]> {
|
|
|
|
|
async function detectNpmScripts(context: ExtensionContext): Promise<TaskWithLocation[]> {
|
|
|
|
|
|
|
|
|
|
let emptyTasks: TaskWithLocation[] = [];
|
|
|
|
|
let allTasks: TaskWithLocation[] = [];
|
|
|
|
@ -177,7 +184,7 @@ async function detectNpmScripts(): Promise<TaskWithLocation[]> {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
@ -191,7 +198,7 @@ async function detectNpmScripts(): Promise<TaskWithLocation[]> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function detectNpmScriptsForFolder(folder: Uri): Promise<FolderTaskItem[]> {
|
|
|
|
|
export async function detectNpmScriptsForFolder(context: ExtensionContext, folder: Uri): Promise<FolderTaskItem[]> {
|
|
|
|
|
|
|
|
|
|
let folderTasks: FolderTaskItem[] = [];
|
|
|
|
|
|
|
|
|
@ -202,7 +209,7 @@ 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.task.name, task: t.task })));
|
|
|
|
|
}
|
|
|
|
@ -213,9 +220,9 @@ export async function detectNpmScriptsForFolder(folder: Uri): Promise<FolderTask
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function provideNpmScripts(): Promise<TaskWithLocation[]> {
|
|
|
|
|
export async function provideNpmScripts(context: ExtensionContext): Promise<TaskWithLocation[]> {
|
|
|
|
|
if (!cachedTasks) {
|
|
|
|
|
cachedTasks = await detectNpmScripts();
|
|
|
|
|
cachedTasks = await detectNpmScripts(context);
|
|
|
|
|
}
|
|
|
|
|
return cachedTasks;
|
|
|
|
|
}
|
|
|
|
@ -251,7 +258,7 @@ function isDebugScript(script: string): boolean {
|
|
|
|
|
return match !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<TaskWithLocation[]> {
|
|
|
|
|
async function provideNpmScriptsForFolder(context: ExtensionContext, packageJsonUri: Uri): Promise<TaskWithLocation[]> {
|
|
|
|
|
let emptyTasks: TaskWithLocation[] = [];
|
|
|
|
|
|
|
|
|
|
let folder = workspace.getWorkspaceFolder(packageJsonUri);
|
|
|
|
@ -269,7 +276,7 @@ async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<TaskWith
|
|
|
|
|
|
|
|
|
|
for (const each of scripts.keys()) {
|
|
|
|
|
const scriptValue = scripts.get(each)!;
|
|
|
|
|
const task = await createTask(each, `run ${each}`, folder!, packageJsonUri, scriptValue.script);
|
|
|
|
|
const task = await createTask(context, each, `run ${each}`, folder!, packageJsonUri, scriptValue.script);
|
|
|
|
|
const lowerCaseTaskName = each.toLowerCase();
|
|
|
|
|
if (isBuildTask(lowerCaseTaskName)) {
|
|
|
|
|
task.group = TaskGroup.Build;
|
|
|
|
@ -288,7 +295,7 @@ async function provideNpmScriptsForFolder(packageJsonUri: Uri): Promise<TaskWith
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// always add npm install (without a problem matcher)
|
|
|
|
|
result.push({ task: await 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -299,7 +306,7 @@ export function getTaskName(script: string, relativePath: string | undefined) {
|
|
|
|
|
return script;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createTask(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, detail?: string, matcher?: any): Promise<Task> {
|
|
|
|
|
let kind: NpmTaskDefinition;
|
|
|
|
|
if (typeof script === 'string') {
|
|
|
|
|
kind = { type: 'npm', script: script };
|
|
|
|
@ -307,7 +314,7 @@ export async function createTask(script: NpmTaskDefinition | string, cmd: string
|
|
|
|
|
kind = script;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const packageManager = await 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}`;
|
|
|
|
@ -368,22 +375,22 @@ async function exists(file: string): Promise<boolean> {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async 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 = await createTask(script, `run ${script}`, folder, uri);
|
|
|
|
|
let task = await createTask(context, script, `run ${script}`, folder, uri);
|
|
|
|
|
tasks.executeTask(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async 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: await getPackageManager(folder.uri),
|
|
|
|
|
runtimeExecutable: await getPackageManager(context, folder.uri),
|
|
|
|
|
runtimeArgs: [
|
|
|
|
|
'run',
|
|
|
|
|
scriptName,
|
|
|
|
|