From 1ddfd491ebff31f72778a0b606410c6b3a6c7820 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:12:32 -0400 Subject: [PATCH 1/2] update pm detector --- packages/cli/package.json | 2 +- pnpm-lock.yaml | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index f24afeb1d..3b9326c7b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -36,7 +36,7 @@ "@types/tar-fs": "^2.0.4", "commander": "^12.1.0", "empathic": "^1.0.0", - "package-manager-detector": "^0.2.0", + "package-manager-detector": "^0.2.1", "picocolors": "^1.1.0", "tar-fs": "^3.0.6", "tinyexec": "^0.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b7fb7a5b7..5b32174ff 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -176,8 +176,8 @@ importers: specifier: ^1.0.0 version: 1.0.0 package-manager-detector: - specifier: ^0.2.0 - version: 0.2.0 + specifier: ^0.2.1 + version: 0.2.1 picocolors: specifier: ^1.1.0 version: 1.1.0 @@ -1620,6 +1620,9 @@ packages: package-manager-detector@0.2.0: resolution: {integrity: sha512-E385OSk9qDcXhcM9LNSe4sdhx8a9mAPrZ4sMLW+tmxl5ZuGtPUcdFu+MPP2jbgiWAZ6Pfe5soGFMd+0Db5Vrog==} + package-manager-detector@0.2.1: + resolution: {integrity: sha512-/hVW2fZvAdEas+wyKh0SnlZ2mx0NIa1+j11YaQkogEJkcMErbwchHCuo8z7lEtajZJQZ6rgZNVTWMVVd71Bjng==} + parent-module@1.0.1: resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==} engines: {node: '>=6'} @@ -3609,6 +3612,8 @@ snapshots: package-manager-detector@0.2.0: {} + package-manager-detector@0.2.1: {} + parent-module@1.0.1: dependencies: callsites: 3.1.0 From 62e5b00e8f88ec31e1f00f494d9c46f2b8b53cbf Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:13:14 -0400 Subject: [PATCH 2/2] use user's pm rather than `npx` --- packages/cli/commands/add.ts | 5 ++++- packages/cli/commands/check.ts | 3 ++- packages/cli/commands/migrate.ts | 24 +++++++++++++++++++----- packages/cli/common.ts | 7 +++++-- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/cli/commands/add.ts b/packages/cli/commands/add.ts index 19a61c78d..eab089add 100644 --- a/packages/cli/commands/add.ts +++ b/packages/cli/commands/add.ts @@ -5,6 +5,7 @@ import { exec } from 'tinyexec'; import { Command, Option } from 'commander'; import * as p from '@svelte-cli/clack-prompts'; import * as pkg from 'empathic/package'; +import { resolveCommand } from 'package-manager-detector'; import pc from 'picocolors'; import { adderCategories, @@ -575,8 +576,10 @@ async function processExternalAdder( if (!TESTING) p.log.message(`Executing external command ${pc.gray(`(${config.metadata.id})`)}`); try { + const pm = await common.guessPackageManager(cwd); + const cmd = resolveCommand(pm, 'execute', config.command.split(' '))!; const env = { ...process.env, ...config.environment }; - await exec('npx', config.command.split(' '), { + await exec(cmd.command, cmd.args, { nodeOptions: { cwd, env, stdio: TESTING ? 'pipe' : 'inherit' } }); } catch (error) { diff --git a/packages/cli/commands/check.ts b/packages/cli/commands/check.ts index d71e3795b..2a0a4a2cb 100644 --- a/packages/cli/commands/check.ts +++ b/packages/cli/commands/check.ts @@ -39,6 +39,7 @@ function runCheck(cwd: string, args: string[]) { // avoids printing the stack trace for `sv` when `svelte-check` exits with an error code try { - execSync(`npx svelte-check ${args.join(' ')}`, { stdio: 'inherit', cwd }); + const cmd = resolveCommand(pm, 'execute-local', ['svelte-check', ...args])!; + execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd }); } catch {} } diff --git a/packages/cli/commands/migrate.ts b/packages/cli/commands/migrate.ts index 0d129cfb3..73232190c 100644 --- a/packages/cli/commands/migrate.ts +++ b/packages/cli/commands/migrate.ts @@ -1,5 +1,7 @@ import { execSync } from 'node:child_process'; import { Command } from 'commander'; +import { resolveCommand } from 'package-manager-detector'; +import { getUserAgent } from '../common.ts'; export const migrate = new Command('migrate') .description('a CLI for migrating Svelte(Kit) codebases') @@ -8,13 +10,25 @@ export const migrate = new Command('migrate') .configureHelp({ formatHelp() { // we'll pass the responsibility of presenting the help menu over to `svelte-migrate` - execSync('npx --yes svelte-migrate@latest --help', { stdio: 'inherit' }); + runMigrate(process.cwd(), ['--help']); return ''; } }) .action((migration, options) => { - execSync(`npx --yes svelte-migrate@latest ${migration}`, { - stdio: 'inherit', - cwd: options.cwd - }); + runMigrate(options.cwd, [migration]); }); + +function runMigrate(cwd: string, args: string[]) { + const pm = getUserAgent() ?? 'npm'; + + // avoids printing the stack trace for `sv` when `svelte-migrate` exits with an error code + try { + const cmdArgs = ['svelte-migrate@latest', ...args]; + + // skips the download confirmation prompt for `npx` + if (pm === 'npm') cmdArgs.unshift('--yes'); + + const cmd = resolveCommand(pm, 'execute', cmdArgs)!; + execSync(`${cmd.command} ${cmd.args.join(' ')}`, { stdio: 'inherit', cwd }); + } catch {} +} diff --git a/packages/cli/common.ts b/packages/cli/common.ts index c4ebc1aa8..17b107fb3 100644 --- a/packages/cli/common.ts +++ b/packages/cli/common.ts @@ -3,7 +3,7 @@ import pkg from './package.json'; import { exec } from 'tinyexec'; import * as p from '@svelte-cli/clack-prompts'; import { detect, AGENTS, type AgentName } from 'package-manager-detector'; -import { COMMANDS, constructCommand } from 'package-manager-detector/commands'; +import { COMMANDS, constructCommand, resolveCommand } from 'package-manager-detector/commands'; import type { AdderWithoutExplicitArgs, Precondition } from '@svelte-cli/core'; import type { Argument, HelpConfiguration, Option } from 'commander'; @@ -41,7 +41,10 @@ export async function runCommand(action: MaybePromise) { } export async function formatFiles(cwd: string, paths: string[]): Promise { - await exec('npx', ['prettier', '--write', '--ignore-unknown', ...paths], { + const pm = await guessPackageManager(cwd); + const args = ['prettier', '--write', '--ignore-unknown', ...paths]; + const cmd = resolveCommand(pm, 'execute-local', args)!; + await exec(cmd.command, cmd.args, { nodeOptions: { cwd, stdio: 'pipe' } }); }