Skip to content

Commit

Permalink
refactor: Separate createScriptRunnerTask and `createInstallationTa…
Browse files Browse the repository at this point in the history
…sk` from `createTask`
  • Loading branch information
xymopen committed Dec 26, 2024
1 parent 4fa5611 commit 5fdf3d9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
4 changes: 2 additions & 2 deletions extensions/npm/src/npmView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from 'vscode';
import { readScripts } from './readScripts';
import {
createTask, getPackageManager, getTaskName, isAutoDetectionEnabled, isWorkspaceFolder, INpmTaskDefinition,
createInstallationTask, getPackageManager, getTaskName, isAutoDetectionEnabled, isWorkspaceFolder, INpmTaskDefinition,
NpmTaskProvider,
startDebugging,
ITaskWithLocation,
Expand Down Expand Up @@ -181,7 +181,7 @@ export class NpmScriptsTreeDataProvider implements TreeDataProvider<TreeItem> {
if (!uri) {
return;
}
const task = await createTask(await getPackageManager(this.context, selection.folder.workspaceFolder.uri, true), 'install', ['install'], selection.folder.workspaceFolder, uri, undefined, []);
const task = await createInstallationTask(await getPackageManager(this.context, selection.folder.workspaceFolder.uri, true), selection.folder.workspaceFolder, uri);
tasks.executeTask(task);
}

Expand Down
4 changes: 2 additions & 2 deletions extensions/npm/src/scriptHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'vscode';
import { INpmScriptInfo, readScripts } from './readScripts';
import {
createTask,
createScriptRunnerTask,
getPackageManager, startDebugging
} from './tasks';

Expand Down Expand Up @@ -114,7 +114,7 @@ export class NpmScriptHoverProvider implements HoverProvider {
const documentUri = args.documentUri;
const folder = workspace.getWorkspaceFolder(documentUri);
if (folder) {
const task = await createTask(await getPackageManager(this.context, folder.uri), script, ['run', script], folder, documentUri);
const task = await createScriptRunnerTask(await getPackageManager(this.context, folder.uri), script, folder, documentUri);
await tasks.executeTask(task);
}
}
Expand Down
80 changes: 44 additions & 36 deletions extensions/npm/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ export class NpmTaskProvider implements TaskProvider {
} else {
packageJsonUri = _task.scope.uri.with({ path: _task.scope.uri.path + '/package.json' });
}
const cmd = [kind.script];
if (kind.script !== INSTALL_SCRIPT) {
cmd.unshift('run');
if (kind.script === INSTALL_SCRIPT) {
return createInstallationTask(await getPackageManager(this.context, _task.scope.uri), _task.scope, packageJsonUri);
}
return createTask(await getPackageManager(this.context, _task.scope.uri), kind, cmd, _task.scope, packageJsonUri);
return createScriptRunnerTask(await getPackageManager(this.context, _task.scope.uri), kind.script, _task.scope, packageJsonUri);
}
return undefined;
}
Expand Down Expand Up @@ -281,12 +280,12 @@ async function provideNpmScriptsForFolder(context: ExtensionContext, packageJson
const packageManager = await getPackageManager(context, folder.uri, showWarning);

for (const { name, value, nameRange } of scripts.scripts) {
const task = await createTask(packageManager, name, ['run', name], folder!, packageJsonUri, value, undefined);
const task = await createScriptRunnerTask(packageManager, name, folder!, packageJsonUri, value);
result.push({ task, location: new Location(packageJsonUri, nameRange) });
}

if (!workspace.getConfiguration('npm', folder).get<string[]>('scriptExplorerExclude', []).find(e => e.includes(INSTALL_SCRIPT))) {
result.push({ task: await createTask(packageManager, INSTALL_SCRIPT, [INSTALL_SCRIPT], folder, packageJsonUri, 'install dependencies from package', []) });
result.push({ task: await createInstallationTask(packageManager, folder, packageJsonUri, 'install dependencies from package') });
}
return result;
}
Expand All @@ -298,45 +297,39 @@ export function getTaskName(script: string, relativePath: string | undefined) {
return script;
}

export async function createTask(packageManager: string, script: INpmTaskDefinition | string, cmd: string[], folder: WorkspaceFolder, packageJsonUri: Uri, scriptValue?: string, matcher?: any): Promise<Task> {
let kind: INpmTaskDefinition;
if (typeof script === 'string') {
kind = { type: 'npm', script: script };
} else {
kind = script;
}

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];
}
}
if (workspace.getConfiguration('npm', folder.uri).get<boolean>('runSilent')) {
result.unshift('--silent');
function getCommandLine(scope: Uri, 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 result;
}

function getRelativePath(packageJsonUri: Uri): string {
const rootUri = folder.uri;
const absolutePath = packageJsonUri.path.substring(0, packageJsonUri.path.length - 'package.json'.length);
return absolutePath.substring(rootUri.path.length + 1);
if (workspace.getConfiguration('npm', scope).get<boolean>('runSilent')) {
result.unshift('--silent');
}
return result;
}

function getRelativePath(rootUri: Uri, packageJsonUri: Uri): string {
const absolutePath = packageJsonUri.path.substring(0, packageJsonUri.path.length - 'package.json'.length);
return absolutePath.substring(rootUri.path.length + 1);
}

const relativePackageJson = getRelativePath(packageJsonUri);
export async function createScriptRunnerTask(packageManager: string, script: string, folder: WorkspaceFolder, packageJsonUri: Uri, scriptValue?: string): Promise<Task> {
let kind: INpmTaskDefinition = { type: 'npm', script };

const relativePackageJson = getRelativePath(folder.uri, packageJsonUri);
if (relativePackageJson.length && !kind.path) {
kind.path = relativePackageJson.substring(0, relativePackageJson.length - 1);
}
const taskName = getTaskName(kind.script, relativePackageJson);
const taskName = getTaskName(script, relativePackageJson);
const cwd = path.dirname(packageJsonUri.fsPath);
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(packageManager, getCommandLine(cmd), { cwd: cwd }), matcher);
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(packageManager, getCommandLine(folder.uri, ['run', script]), { cwd: cwd }));
task.detail = scriptValue;

const lowerCaseTaskName = kind.script.toLowerCase();
const lowerCaseTaskName = script.toLowerCase();
if (isBuildTask(lowerCaseTaskName)) {
task.group = TaskGroup.Build;
} else if (isTestTask(lowerCaseTaskName)) {
Expand All @@ -350,6 +343,21 @@ export async function createTask(packageManager: string, script: INpmTaskDefinit
return task;
}

export async function createInstallationTask(packageManager: string, folder: WorkspaceFolder, packageJsonUri: Uri, scriptValue?: string): Promise<Task> {
const kind: INpmTaskDefinition = { type: 'npm', script: INSTALL_SCRIPT };

const relativePackageJson = getRelativePath(folder.uri, packageJsonUri);
if (relativePackageJson.length && !kind.path) {
kind.path = relativePackageJson.substring(0, relativePackageJson.length - 1);
}
const taskName = getTaskName(INSTALL_SCRIPT, relativePackageJson);
const cwd = path.dirname(packageJsonUri.fsPath);
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(packageManager, getCommandLine(folder.uri, [INSTALL_SCRIPT]), { cwd: cwd }));
task.detail = scriptValue;

return task;
}


export function getPackageJsonUriFromTask(task: Task): Uri | null {
if (isWorkspaceFolder(task.scope)) {
Expand Down Expand Up @@ -403,7 +411,7 @@ export async function runScript(context: ExtensionContext, script: string, docum
const uri = document.uri;
const folder = workspace.getWorkspaceFolder(uri);
if (folder) {
const task = await createTask(await getPackageManager(context, folder.uri), script, ['run', script], folder, uri);
const task = await createScriptRunnerTask(await getPackageManager(context, folder.uri), script, folder, uri);
tasks.executeTask(task);
}
}
Expand Down

0 comments on commit 5fdf3d9

Please sign in to comment.