Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/cli/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -575,8 +576,10 @@ async function processExternalAdder<Args extends OptionDefinition>(
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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
24 changes: 19 additions & 5 deletions packages/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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 {}
}
7 changes: 5 additions & 2 deletions packages/cli/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -41,7 +41,10 @@ export async function runCommand(action: MaybePromise) {
}

export async function formatFiles(cwd: string, paths: string[]): Promise<void> {
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' }
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.