Skip to content

Commit d97aecd

Browse files
authored
chore: Use user's PM when executing binary (#64)
* update pm detector * use user's pm rather than `npx`
1 parent 561b0f1 commit d97aecd

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

packages/cli/commands/add.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { exec } from 'tinyexec';
55
import { Command, Option } from 'commander';
66
import * as p from '@svelte-cli/clack-prompts';
77
import * as pkg from 'empathic/package';
8+
import { resolveCommand } from 'package-manager-detector';
89
import pc from 'picocolors';
910
import {
1011
adderCategories,
@@ -575,8 +576,10 @@ async function processExternalAdder<Args extends OptionDefinition>(
575576
if (!TESTING) p.log.message(`Executing external command ${pc.gray(`(${config.metadata.id})`)}`);
576577

577578
try {
579+
const pm = await common.guessPackageManager(cwd);
580+
const cmd = resolveCommand(pm, 'execute', config.command.split(' '))!;
578581
const env = { ...process.env, ...config.environment };
579-
await exec('npx', config.command.split(' '), {
582+
await exec(cmd.command, cmd.args, {
580583
nodeOptions: { cwd, env, stdio: TESTING ? 'pipe' : 'inherit' }
581584
});
582585
} catch (error) {

packages/cli/commands/check.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function runCheck(cwd: string, args: string[]) {
3939

4040
// avoids printing the stack trace for `sv` when `svelte-check` exits with an error code
4141
try {
42-
execSync(`npx svelte-check ${args.join(' ')}`, { stdio: 'inherit', cwd });
42+
const cmd = resolveCommand(pm, 'execute-local', ['svelte-check', ...args])!;
43+
execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd });
4344
} catch {}
4445
}

packages/cli/commands/migrate.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { execSync } from 'node:child_process';
22
import { Command } from 'commander';
3+
import { resolveCommand } from 'package-manager-detector';
4+
import { getUserAgent } from '../common.ts';
35

46
export const migrate = new Command('migrate')
57
.description('a CLI for migrating Svelte(Kit) codebases')
@@ -8,13 +10,25 @@ export const migrate = new Command('migrate')
810
.configureHelp({
911
formatHelp() {
1012
// we'll pass the responsibility of presenting the help menu over to `svelte-migrate`
11-
execSync('npx --yes svelte-migrate@latest --help', { stdio: 'inherit' });
13+
runMigrate(process.cwd(), ['--help']);
1214
return '';
1315
}
1416
})
1517
.action((migration, options) => {
16-
execSync(`npx --yes svelte-migrate@latest ${migration}`, {
17-
stdio: 'inherit',
18-
cwd: options.cwd
19-
});
18+
runMigrate(options.cwd, [migration]);
2019
});
20+
21+
function runMigrate(cwd: string, args: string[]) {
22+
const pm = getUserAgent() ?? 'npm';
23+
24+
// avoids printing the stack trace for `sv` when `svelte-migrate` exits with an error code
25+
try {
26+
const cmdArgs = ['svelte-migrate@latest', ...args];
27+
28+
// skips the download confirmation prompt for `npx`
29+
if (pm === 'npm') cmdArgs.unshift('--yes');
30+
31+
const cmd = resolveCommand(pm, 'execute', cmdArgs)!;
32+
execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd });
33+
} catch {}
34+
}

packages/cli/common.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import pkg from './package.json';
33
import { exec } from 'tinyexec';
44
import * as p from '@svelte-cli/clack-prompts';
55
import { detect, AGENTS, type AgentName } from 'package-manager-detector';
6-
import { COMMANDS, constructCommand } from 'package-manager-detector/commands';
6+
import { COMMANDS, constructCommand, resolveCommand } from 'package-manager-detector/commands';
77
import type { AdderWithoutExplicitArgs, Precondition } from '@svelte-cli/core';
88
import type { Argument, HelpConfiguration, Option } from 'commander';
99

@@ -41,7 +41,10 @@ export async function runCommand(action: MaybePromise) {
4141
}
4242

4343
export async function formatFiles(cwd: string, paths: string[]): Promise<void> {
44-
await exec('npx', ['prettier', '--write', '--ignore-unknown', ...paths], {
44+
const pm = await guessPackageManager(cwd);
45+
const args = ['prettier', '--write', '--ignore-unknown', ...paths];
46+
const cmd = resolveCommand(pm, 'execute-local', args)!;
47+
await exec(cmd.command, cmd.args, {
4548
nodeOptions: { cwd, stdio: 'pipe' }
4649
});
4750
}

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@types/tar-fs": "^2.0.4",
3737
"commander": "^12.1.0",
3838
"empathic": "^1.0.0",
39-
"package-manager-detector": "^0.2.0",
39+
"package-manager-detector": "^0.2.1",
4040
"picocolors": "^1.1.0",
4141
"tar-fs": "^3.0.6",
4242
"tinyexec": "^0.3.0",

pnpm-lock.yaml

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)