Skip to content

Commit

Permalink
refactor: Extract get*Command
Browse files Browse the repository at this point in the history
  • Loading branch information
xymopen committed Jan 7, 2025
1 parent d322825 commit 98f0b99
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
18 changes: 10 additions & 8 deletions extensions/npm/src/npmScriptLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
l10n
} from 'vscode';
import { readScripts } from './readScripts';
import { getScriptRunner } from './tasks';
import { getRunScriptCommand } from './tasks';


const enum Constants {
Expand Down Expand Up @@ -87,18 +87,20 @@ export class NpmScriptLensProvider implements CodeLensProvider, Disposable {
}

if (this.lensLocation === 'all') {
const scriptRunner = await getScriptRunner(Uri.joinPath(document.uri, '..'));
return tokens.scripts.map(
({ name, nameRange }) =>
new CodeLens(
const folder = Uri.joinPath(document.uri, '..');
return Promise.all(tokens.scripts.map(
async ({ name, nameRange }) => {
const runScriptCommand = await getRunScriptCommand(name, folder);
return new CodeLens(
nameRange,
{
title,
command: 'extension.js-debug.createDebuggerTerminal',
arguments: [`${scriptRunner} ${scriptRunner === 'node' ? '--run' : 'run'} ${name}`, workspace.getWorkspaceFolder(document.uri), { cwd }],
arguments: [runScriptCommand.join(' '), workspace.getWorkspaceFolder(document.uri), { cwd }],
},
),
);
);
},
));
}

return [];
Expand Down
42 changes: 30 additions & 12 deletions extensions/npm/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function getTaskName(script: string, relativePath: string | undefined) {
return script;
}

function getCommandLine(scope: Uri, cmd: string[]): (string | ShellQuotedString)[] {
function escapeCommandLine(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])) {
Expand All @@ -317,9 +317,6 @@ function getCommandLine(scope: Uri, cmd: string[]): (string | ShellQuotedString)
result[i] = cmd[i];
}
}
if (workspace.getConfiguration('npm', scope).get<boolean>('runSilent')) {
result.unshift('--silent');
}
return result;
}

Expand All @@ -328,8 +325,17 @@ function getRelativePath(rootUri: Uri, packageJsonUri: Uri): string {
return absolutePath.substring(rootUri.path.length + 1);
}

export async function createScriptRunnerTask(context: ExtensionContext, script: string, folder: WorkspaceFolder, packageJsonUri: Uri, scriptValue?: string, showWarning = true): Promise<Task> {
const scriptRunner = await getScriptRunner(folder.uri, context, showWarning);
export async function getRunScriptCommand(script: string, folder: Uri, context?: ExtensionContext, showWarning = true): Promise<string[]> {
const scriptRunner = await getScriptRunner(folder, context, showWarning);
const result = [scriptRunner, scriptRunner === 'node' ? '--run' : 'run'];
if (workspace.getConfiguration('npm', folder).get<boolean>('runSilent')) {
result.push('--silent');
}
result.push(script);
return result;
}

export async function createScriptRunnerTask(context: ExtensionContext, script: string, folder: WorkspaceFolder, packageJsonUri: Uri, scriptValue?: string, showWarning?: boolean): Promise<Task> {
const kind: INpmTaskDefinition = { type: 'npm', script };

const relativePackageJson = getRelativePath(folder.uri, packageJsonUri);
Expand All @@ -338,7 +344,9 @@ export async function createScriptRunnerTask(context: ExtensionContext, script:
}
const taskName = getTaskName(script, relativePackageJson);
const cwd = path.dirname(packageJsonUri.fsPath);
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(scriptRunner, getCommandLine(folder.uri, [scriptRunner === 'node' ? '--run' : 'run', script]), { cwd: cwd }));
const args = await getRunScriptCommand(script, folder.uri, context, showWarning);
const scriptRunner = args.shift()!;
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(scriptRunner, escapeCommandLine(args), { cwd: cwd }));
task.detail = scriptValue;

const lowerCaseTaskName = script.toLowerCase();
Expand All @@ -355,8 +363,16 @@ export async function createScriptRunnerTask(context: ExtensionContext, script:
return task;
}

export async function createInstallationTask(context: ExtensionContext, folder: WorkspaceFolder, packageJsonUri: Uri, scriptValue?: string, showWarning = true): Promise<Task> {
const packageManager = await getPackageManager(folder.uri, context, showWarning);
async function getInstallDependenciesCommand(folder: Uri, context?: ExtensionContext, showWarning = true): Promise<string[]> {
const packageManager = await getPackageManager(folder, context, showWarning);
const result = [packageManager, INSTALL_SCRIPT];
if (workspace.getConfiguration('npm', folder).get<boolean>('runSilent')) {
result.push('--silent');
}
return result;
}

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

const relativePackageJson = getRelativePath(folder.uri, packageJsonUri);
Expand All @@ -365,7 +381,9 @@ export async function createInstallationTask(context: ExtensionContext, folder:
}
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 }));
const args = await getInstallDependenciesCommand(folder.uri, context, showWarning);
const packageManager = args.shift()!;
const task = new Task(kind, folder, taskName, 'npm', new ShellExecution(packageManager, escapeCommandLine(args), { cwd: cwd }));
task.detail = scriptValue;
task.group = TaskGroup.Clean;

Expand Down Expand Up @@ -431,11 +449,11 @@ export async function runScript(context: ExtensionContext, script: string, docum
}

export async function startDebugging(context: ExtensionContext, scriptName: string, cwd: string, folder: WorkspaceFolder) {
const scriptRunner = await getScriptRunner(folder.uri, context, true);
const runScriptCommand = await getRunScriptCommand(scriptName, folder.uri, context, true);

commands.executeCommand(
'extension.js-debug.createDebuggerTerminal',
`${scriptRunner} ${scriptRunner === 'node' ? '--run' : 'run'} ${scriptName}`,
runScriptCommand.join(' '),
folder,
{ cwd },
);
Expand Down

0 comments on commit 98f0b99

Please sign in to comment.