Skip to content

Commit 36ac520

Browse files
fix: dynamically suggest package manager installation command in migrate scripts (#495)
* added pm-detector to migrate package, used for dynamic suggestions in cli * format, lint * Use resolveCommand for futureproofing (reverted accidental merge) * Create moody-mirrors-wait.md --------- Co-authored-by: Manuel Serret <[email protected]> Co-authored-by: Manuel <[email protected]>
1 parent ddfc60e commit 36ac520

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

.changeset/moody-mirrors-wait.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-migrate": patch
3+
---
4+
5+
fix: dynamically suggest package manager installation command in `migrate` scripts

packages/migrate/migrations/app-state/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import semver from 'semver';
66
import glob from 'tiny-glob/sync.js';
77
import { bail, check_git, migration_succeeded, update_svelte_file } from '../../utils.js';
88
import { transform_svelte_code, update_pkg_json } from './migrate.js';
9+
import { detect, resolveCommand } from 'package-manager-detector';
910

1011
export async function migrate() {
1112
if (!fs.existsSync('package.json')) {
@@ -94,8 +95,15 @@ export async function migrate() {
9495
/** @type {(s: string) => string} */
9596
const cyan = (s) => pc.bold(pc.cyan(s));
9697

97-
// TODO: use package-manager-detector here
98-
const tasks = ["install the updated dependencies ('npm i' / 'pnpm i' / etc)"];
98+
const detected = await detect({ cwd: process.cwd() });
99+
const pm = detected?.name ?? 'npm';
100+
const cmd = /** @type {import('package-manager-detector').ResolvedCommand} */ (
101+
resolveCommand(pm, 'install', [])
102+
);
103+
104+
const tasks = [
105+
`Install the updated dependencies by running ${cyan(`${cmd.command} ${cmd.args.join(' ')}`)}`
106+
];
99107
if (use_git) {
100108
tasks.push(cyan('git commit -m "migration to $app/state"'));
101109
tasks.push(`Run ${cyan('git diff')} to review changes.`);

packages/migrate/migrations/svelte-5/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { migrate as migrate_svelte_4 } from '../svelte-4/index.js';
1919
import { migrate as migrate_sveltekit_2 } from '../sveltekit-2/index.js';
2020
import { transform_svelte_code as transform_app_state_code } from '../app-state/migrate.js';
2121
import { transform_module_code, transform_svelte_code, update_pkg_json } from './migrate.js';
22+
import { detect, resolveCommand } from 'package-manager-detector';
2223

2324
export async function migrate() {
2425
if (!fs.existsSync('package.json')) {
@@ -217,8 +218,13 @@ export async function migrate() {
217218
/** @type {(s: string) => string} */
218219
const cyan = (s) => pc.bold(pc.cyan(s));
219220

221+
const detected = await detect({ cwd: process.cwd() });
222+
const pm = detected?.name ?? 'npm';
223+
const cmd = /** @type {import('package-manager-detector').ResolvedCommand} */ (
224+
resolveCommand(pm, 'install', [])
225+
);
220226
const tasks = [
221-
"install the updated dependencies ('npm i' / 'pnpm i' / etc) " +
227+
`Install the updated dependencies by running ${cyan(`${cmd.command} ${cmd.args.join(' ')}`)} ` +
222228
'(note that there may be peer dependency issues when not all your libraries officially support Svelte 5 yet. In this case try installing with the --force option)',
223229
use_git && cyan('git commit -m "migration to Svelte 5"'),
224230
'Review the migration guide at https://svelte.dev/docs/svelte/v5-migration-guide',

packages/migrate/migrations/sveltekit-2/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
update_svelte_config,
2020
update_tsconfig_content
2121
} from './migrate.js';
22+
import { detect, resolveCommand } from 'package-manager-detector';
2223

2324
export async function migrate() {
2425
if (!fs.existsSync('package.json')) {
@@ -137,8 +138,14 @@ export async function migrate() {
137138
/** @type {(s: string) => string} */
138139
const cyan = (s) => pc.bold(pc.cyan(s));
139140

141+
const detected = await detect({ cwd: process.cwd() });
142+
const pm = detected?.name ?? 'npm';
143+
const cmd = /** @type {import('package-manager-detector').ResolvedCommand} */ (
144+
resolveCommand(pm, 'install', [])
145+
);
146+
140147
const tasks = [
141-
'Run npm install (or the corresponding installation command of your package manager)',
148+
`Install the updated dependencies by running ${cyan(`${cmd.command} ${cmd.args.join(' ')}`)}`,
142149
use_git && cyan('git commit -m "migration to SvelteKit 2"'),
143150
'Review the migration guide at https://svelte.dev/docs/kit/migrating-to-sveltekit-2',
144151
'Read the updated docs at https://svelte.dev/docs/kit',

packages/migrate/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@clack/prompts": "^0.9.0",
3131
"import-meta-resolve": "^4.1.0",
3232
"magic-string": "^0.30.15",
33+
"package-manager-detector": "^0.2.7",
3334
"picocolors": "^1.1.1",
3435
"semver": "^7.6.3",
3536
"tiny-glob": "^0.2.9",

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)