From a59965238e1935a074f62962459ce5f0a33aa6eb Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Tue, 24 Dec 2024 12:39:42 +0100 Subject: [PATCH 01/13] use `picocolors` instead of `kleur` --- packages/migrate/bin.js | 8 ++-- .../migrate/migrations/app-state/index.js | 27 ++++++------ packages/migrate/migrations/package/index.js | 13 +++--- .../migrations/package/migrate_config.js | 10 +++-- .../migrate/migrations/package/migrate_pkg.js | 10 ++--- packages/migrate/migrations/routes/index.js | 9 ++-- .../migrations/self-closing-tags/index.js | 8 ++-- packages/migrate/migrations/svelte-4/index.js | 13 +++--- packages/migrate/migrations/svelte-5/index.js | 43 ++++++++++--------- .../migrate/migrations/sveltekit-2/index.js | 23 +++++----- packages/migrate/package.json | 2 +- packages/migrate/utils.js | 8 ++-- pnpm-lock.yaml | 12 ++---- 13 files changed, 94 insertions(+), 92 deletions(-) diff --git a/packages/migrate/bin.js b/packages/migrate/bin.js index df8d7de7..447c651f 100755 --- a/packages/migrate/bin.js +++ b/packages/migrate/bin.js @@ -2,7 +2,7 @@ import fs from 'node:fs'; import process from 'node:process'; import { fileURLToPath } from 'node:url'; -import colors from 'kleur'; +import pc from 'picocolors'; const migration = process.argv[2]; const dir = fileURLToPath(new URL('.', import.meta.url)); @@ -16,12 +16,12 @@ if (migrations.includes(migration)) { migrate(); } else { console.error( - colors - .bold() - .red( + pc.bold( + pc.red( `You must specify one of the following migrations: ${migrations.join(', ')}\n` + 'If you expected this to work, try re-running the command with the latest svelte-migrate version:\n' + ` npx svelte-migrate@latest ${migration}` ) + ) ); } diff --git a/packages/migrate/migrations/app-state/index.js b/packages/migrate/migrations/app-state/index.js index 8ba9a326..dd4ed056 100644 --- a/packages/migrate/migrations/app-state/index.js +++ b/packages/migrate/migrations/app-state/index.js @@ -1,4 +1,4 @@ -import colors from 'kleur'; +import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; import prompts from 'prompts'; @@ -17,9 +17,9 @@ export async function migrate() { const svelte_dep = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; if (svelte_dep && semver.validRange(svelte_dep) && semver.gtr('5.0.0', svelte_dep)) { console.log( - colors - .bold() - .red('\nYou need to upgrade to Svelte version 5 first (`npx sv migrate svelte-5`).\n') + pc.bold( + pc.red('\nYou need to upgrade to Svelte version 5 first (`npx sv migrate svelte-5`).\n') + ) ); process.exit(1); } @@ -27,20 +27,22 @@ export async function migrate() { const kit_dep = pkg.devDependencies?.['@sveltejs/kit'] ?? pkg.dependencies?.['@sveltejs/kit']; if (kit_dep && semver.validRange(kit_dep) && semver.gtr('2.0.0', kit_dep)) { console.log( - colors - .bold() - .red('\nYou need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).\n') + pc.bold( + pc.red( + '\nYou need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).\n' + ) + ) ); process.exit(1); } console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nThis will update files in the current directory\n' + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" ) + ) ); const use_git = check_git(); @@ -96,11 +98,12 @@ export async function migrate() { ); } - console.log(colors.bold().green('✔ Your project has been migrated')); + console.log(pc.bold(pc.green('✔ Your project has been migrated'))); console.log('\nRecommended next steps:\n'); - const cyan = colors.bold().cyan; + /** @type {(s: string) => string} */ + const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ "install the updated dependencies ('npm i' / 'pnpm i' / etc) " + use_git && diff --git a/packages/migrate/migrations/package/index.js b/packages/migrate/migrations/package/index.js index 0e365476..ef7dd639 100644 --- a/packages/migrate/migrations/package/index.js +++ b/packages/migrate/migrations/package/index.js @@ -1,5 +1,5 @@ import fs from 'node:fs'; -import colors from 'kleur'; +import pc from 'picocolors'; import path from 'node:path'; import process from 'node:process'; import prompts from 'prompts'; @@ -17,11 +17,11 @@ export async function migrate() { } console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nThis will update your svelte.config.js and package.json in the current directory\n' ) + ) ); const use_git = check_git(); @@ -57,11 +57,12 @@ export async function migrate() { migrate_config(); } - console.log(colors.bold().green('✔ Your project has been migrated')); + console.log(pc.bold(pc.green('✔ Your project has been migrated'))); console.log('\nRecommended next steps:\n'); - const cyan = colors.bold().cyan; + /** @type {(s: string) => string} */ + const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ use_git && cyan('git commit -m "migration to @sveltejs/package v2"'), diff --git a/packages/migrate/migrations/package/migrate_config.js b/packages/migrate/migrations/package/migrate_config.js index b401626d..a7d27923 100644 --- a/packages/migrate/migrations/package/migrate_config.js +++ b/packages/migrate/migrations/package/migrate_config.js @@ -1,5 +1,5 @@ import fs from 'node:fs'; -import colors from 'kleur'; +import pc from 'picocolors'; import MagicString from 'magic-string'; import ts from 'typescript'; @@ -9,9 +9,11 @@ export function migrate_config() { fs.writeFileSync('svelte.config.js', remove_package_from_config(content)); } catch { console.log( - colors - .bold() - .yellow('Could not remove package config from svelte.config.js, please remove it manually') + pc.bold( + pc.yellow( + 'Could not remove package config from svelte.config.js, please remove it manually' + ) + ) ); } } diff --git a/packages/migrate/migrations/package/migrate_pkg.js b/packages/migrate/migrations/package/migrate_pkg.js index 3e5f5b6a..bc4bab1e 100644 --- a/packages/migrate/migrations/package/migrate_pkg.js +++ b/packages/migrate/migrations/package/migrate_pkg.js @@ -1,6 +1,6 @@ import fs from 'node:fs'; import path from 'node:path'; -import colors from 'kleur'; +import pc from 'picocolors'; import { guess_indent, posixify, walk } from '../../utils.js'; /** @@ -59,7 +59,7 @@ export function update_pkg_json(config, pkg, files) { // See: https://pnpm.io/package_json#publishconfigdirectory if (pkg.publishConfig?.directory || pkg.linkDirectory?.directory) { console.log( - colors.yellow( + pc.yellow( 'Detected "publishConfig.directory" or "linkDirectory.directory" fields in your package.json. ' + 'This migration removes them, which may or may not be what you want. Please review closely.' ) @@ -102,7 +102,7 @@ export function update_pkg_json(config, pkg, files) { if (clashes[key]) { console.log( - colors.yellow( + pc.yellow( `Duplicate "${key}" export. Closely review your "exports" field in package.json after the migration.` ) ); @@ -170,14 +170,14 @@ export function update_pkg_json(config, pkg, files) { pkg.svelte = svelte_export; } else { console.log( - colors.yellow( + pc.yellow( 'Cannot generate a "svelte" entry point because the "." entry in "exports" is not a string. Please specify a "svelte" entry point yourself\n' ) ); } } else { console.log( - colors.yellow( + pc.yellow( 'Cannot generate a "svelte" entry point because the "." entry in "exports" is missing. Please specify a "svelte" entry point yourself\n' ) ); diff --git a/packages/migrate/migrations/routes/index.js b/packages/migrate/migrations/routes/index.js index 9f043327..c92b9095 100644 --- a/packages/migrate/migrations/routes/index.js +++ b/packages/migrate/migrations/routes/index.js @@ -1,5 +1,5 @@ import fs from 'node:fs'; -import colors from 'kleur'; +import pc from 'picocolors'; import path from 'node:path'; import process from 'node:process'; import prompts from 'prompts'; @@ -56,7 +56,7 @@ export async function migrate() { } } - console.log(colors.bold().yellow('\nThis will overwrite files in the current directory!\n')); + console.log(pc.bold(pc.yellow('\nThis will overwrite files in the current directory!\n'))); const use_git = check_git(); @@ -185,11 +185,12 @@ export async function migrate() { } } - console.log(colors.bold().green('✔ Your project has been migrated')); + console.log(pc.bold(pc.green('✔ Your project has been migrated'))); console.log('\nRecommended next steps:\n'); - const cyan = colors.bold().cyan; + /** @type {(s: string) => string} */ + const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ use_git && cyan('git commit -m "svelte-migrate: renamed files"'), diff --git a/packages/migrate/migrations/self-closing-tags/index.js b/packages/migrate/migrations/self-closing-tags/index.js index ced6ca30..5adf66d2 100644 --- a/packages/migrate/migrations/self-closing-tags/index.js +++ b/packages/migrate/migrations/self-closing-tags/index.js @@ -1,4 +1,4 @@ -import colors from 'kleur'; +import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; import prompts from 'prompts'; @@ -12,12 +12,12 @@ export async function migrate() { try { compiler = await import_from_cwd('svelte/compiler'); } catch { - console.log(colors.bold().red('❌ Could not find a local Svelte installation.')); + console.log(pc.bold(pc.red('❌ Could not find a local Svelte installation.'))); return; } console.log( - colors.bold().yellow('\nThis will update .svelte files inside the current directory\n') + pc.bold(pc.yellow('\nThis will update .svelte files inside the current directory\n')) ); const response = await prompts({ @@ -44,7 +44,7 @@ export async function migrate() { } } - console.log(colors.bold().green('✔ Your project has been updated')); + console.log(pc.bold(pc.green('✔ Your project has been updated'))); console.log(' If using Prettier, please upgrade to the latest prettier-plugin-svelte version'); } diff --git a/packages/migrate/migrations/svelte-4/index.js b/packages/migrate/migrations/svelte-4/index.js index 4fea5173..ab8cc271 100644 --- a/packages/migrate/migrations/svelte-4/index.js +++ b/packages/migrate/migrations/svelte-4/index.js @@ -1,4 +1,4 @@ -import colors from 'kleur'; +import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; import prompts from 'prompts'; @@ -12,12 +12,12 @@ export async function migrate() { } console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nThis will update files in the current directory\n' + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" ) + ) ); const use_git = check_git(); @@ -88,11 +88,12 @@ export async function migrate() { } } - console.log(colors.bold().green('✔ Your project has been migrated')); + console.log(pc.bold(pc.green('✔ Your project has been migrated'))); console.log('\nRecommended next steps:\n'); - const cyan = colors.bold().cyan; + /** @type {(s: string) => string} */ + const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ use_git && cyan('git commit -m "migration to Svelte 4"'), diff --git a/packages/migrate/migrations/svelte-5/index.js b/packages/migrate/migrations/svelte-5/index.js index 186a0ee3..14165d89 100644 --- a/packages/migrate/migrations/svelte-5/index.js +++ b/packages/migrate/migrations/svelte-5/index.js @@ -1,5 +1,5 @@ import { resolve } from 'import-meta-resolve'; -import colors from 'kleur'; +import pc from 'picocolors'; import { execSync } from 'node:child_process'; import process from 'node:process'; import fs from 'node:fs'; @@ -27,11 +27,11 @@ export async function migrate() { const svelte_dep = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; if (svelte_dep && semver.validRange(svelte_dep) && semver.gtr('4.0.0', svelte_dep)) { console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nDetected Svelte 3. You need to upgrade to Svelte version 4 first (`npx sv migrate svelte-4`).\n' ) + ) ); const response = await prompts({ type: 'confirm', @@ -44,11 +44,11 @@ export async function migrate() { } else { await migrate_svelte_4(); console.log( - colors - .bold() - .green( + pc.bold( + pc.green( 'svelte-4 migration complete. Check that everything is ok, then run `npx sv migrate svelte-5` again to continue the Svelte 5 migration.\n' ) + ) ); process.exit(0); } @@ -57,11 +57,11 @@ export async function migrate() { const kit_dep = pkg.devDependencies?.['@sveltejs/kit'] ?? pkg.dependencies?.['@sveltejs/kit']; if (kit_dep && semver.validRange(kit_dep) && semver.gtr('2.0.0', kit_dep)) { console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nDetected SvelteKit 1. You need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).\n' ) + ) ); const response = await prompts({ type: 'confirm', @@ -74,11 +74,11 @@ export async function migrate() { } else { await migrate_sveltekit_2(); console.log( - colors - .bold() - .green( + pc.bold( + pc.green( 'sveltekit-2 migration complete. Check that everything is ok, then run `npx sv migrate svelte-5` again to continue the Svelte 5 migration.\n' ) + ) ); process.exit(0); } @@ -100,22 +100,22 @@ export async function migrate() { } catch (e) { console.log(e); console.log( - colors - .bold() - .red( + pc.bold( + pc.red( '❌ Could not install Svelte. Manually bump the dependency to version 5 in your package.json, install it, then try again.' ) + ) ); return; } console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nThis will update files in the current directory\n' + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" ) + ) ); const use_git = check_git(); @@ -180,11 +180,12 @@ export async function migrate() { } } - console.log(colors.bold().green('✔ Your project has been migrated')); + console.log(pc.bold(pc.green('✔ Your project has been migrated'))); console.log('\nRecommended next steps:\n'); - const cyan = colors.bold().cyan; + /** @type {(s: string) => string} */ + const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ "install the updated dependencies ('npm i' / 'pnpm i' / etc) " + diff --git a/packages/migrate/migrations/sveltekit-2/index.js b/packages/migrate/migrations/sveltekit-2/index.js index c62442ab..45cb9da3 100644 --- a/packages/migrate/migrations/sveltekit-2/index.js +++ b/packages/migrate/migrations/sveltekit-2/index.js @@ -1,4 +1,4 @@ -import colors from 'kleur'; +import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; import prompts from 'prompts'; @@ -29,12 +29,12 @@ export async function migrate() { } console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nThis will update files in the current directory\n' + "If you're inside a monorepo, run this in individual project directories rather than the workspace root.\n" ) + ) ); const use_git = check_git(); @@ -58,11 +58,11 @@ export async function migrate() { if (semver.validRange(svelte_dep) && semver.gtr('4.0.0', svelte_dep)) { console.log( - colors - .bold() - .yellow( + pc.bold( + pc.yellow( '\nSvelteKit 2 requires Svelte 4 or newer. We recommend running the `svelte-4` migration first (`npx sv migrate svelte-4`).\n' ) + ) ); const response = await prompts({ type: 'confirm', @@ -75,9 +75,7 @@ export async function migrate() { } else { await migrate_svelte_4(); console.log( - colors - .bold() - .green('`svelte-4` migration complete. Continue with `sveltekit-2` migration?\n') + pc.bold(pc.green('`svelte-4` migration complete. Continue with `sveltekit-2` migration?\n')) ); const response = await prompts({ type: 'confirm', @@ -142,11 +140,12 @@ export async function migrate() { } } - console.log(colors.bold().green('✔ Your project has been migrated')); + console.log(pc.bold(pc.green('✔ Your project has been migrated'))); console.log('\nRecommended next steps:\n'); - const cyan = colors.bold().cyan; + /** @type {(s: string) => string} */ + const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ 'Run npm install (or the corresponding installation command of your package manager)', diff --git a/packages/migrate/package.json b/packages/migrate/package.json index d3159af8..71283f7a 100644 --- a/packages/migrate/package.json +++ b/packages/migrate/package.json @@ -28,8 +28,8 @@ }, "dependencies": { "import-meta-resolve": "^4.1.0", - "kleur": "^4.1.5", "magic-string": "^0.30.15", + "picocolors": "^1.1.1", "prompts": "^2.4.2", "semver": "^7.6.3", "tiny-glob": "^0.2.9", diff --git a/packages/migrate/utils.js b/packages/migrate/utils.js index 944461f0..179775f8 100644 --- a/packages/migrate/utils.js +++ b/packages/migrate/utils.js @@ -1,4 +1,4 @@ -import colors from 'kleur'; +import pc from 'picocolors'; import MagicString from 'magic-string'; import { execFileSync, execSync } from 'node:child_process'; import fs from 'node:fs'; @@ -9,7 +9,7 @@ import ts from 'typescript'; /** @param {string} message */ export function bail(message) { - console.error(colors.bold().red(message)); + console.error(pc.bold(pc.red(message))); process.exit(1); } @@ -153,14 +153,14 @@ export function check_git() { if (status) { const message = 'Your git working directory is dirty — we recommend committing your changes before running this migration.\n'; - console.log(colors.bold().red(message)); + console.log(pc.bold(pc.red(message))); } } catch { // would be weird to have a .git folder if git is not installed, // but always expect the unexpected const message = 'Could not detect a git installation. If this is unexpected, please raise an issue: https://github.com/sveltejs/kit.\n'; - console.log(colors.bold().red(message)); + console.log(pc.bold(pc.red(message))); use_git = false; } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ff7592a3..cd2a64ec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -259,12 +259,12 @@ importers: import-meta-resolve: specifier: ^4.1.0 version: 4.1.0 - kleur: - specifier: ^4.1.5 - version: 4.1.5 magic-string: specifier: ^0.30.15 version: 0.30.15 + picocolors: + specifier: ^1.1.1 + version: 1.1.1 prompts: specifier: ^2.4.2 version: 2.4.2 @@ -1752,10 +1752,6 @@ packages: resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==} engines: {node: '>=6'} - kleur@4.1.5: - resolution: {integrity: sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==} - engines: {node: '>=6'} - known-css-properties@0.35.0: resolution: {integrity: sha512-a/RAk2BfKk+WFGhhOCAYqSiFLc34k8Mt/6NWRI4joER0EYUzXIcFivjjnoD3+XU1DggLn/tZc3DOAgke7l8a4A==} @@ -3921,8 +3917,6 @@ snapshots: kleur@3.0.3: {} - kleur@4.1.5: {} - known-css-properties@0.35.0: {} levn@0.4.1: From 2f944d6de5cc47daa239a2f031f5cc76463775b7 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Tue, 24 Dec 2024 13:25:31 +0100 Subject: [PATCH 02/13] use `@clack/prompts` instead of `prompts` --- .../migrate/migrations/app-state/index.js | 20 ++++------ packages/migrate/migrations/package/index.js | 10 ++--- packages/migrate/migrations/routes/index.js | 10 ++--- .../migrations/self-closing-tags/index.js | 10 ++--- packages/migrate/migrations/svelte-4/index.js | 34 ++++++++--------- packages/migrate/migrations/svelte-5/index.js | 36 +++++++----------- .../migrate/migrations/sveltekit-2/index.js | 38 ++++++++----------- packages/migrate/package.json | 2 +- pnpm-lock.yaml | 32 ++++++++++------ 9 files changed, 86 insertions(+), 106 deletions(-) diff --git a/packages/migrate/migrations/app-state/index.js b/packages/migrate/migrations/app-state/index.js index dd4ed056..31128f7a 100644 --- a/packages/migrate/migrations/app-state/index.js +++ b/packages/migrate/migrations/app-state/index.js @@ -1,7 +1,7 @@ import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import semver from 'semver'; import glob from 'tiny-glob/sync.js'; import { bail, check_git, update_svelte_file } from '../../utils.js'; @@ -47,22 +47,18 @@ export async function migrate() { const use_git = check_git(); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } - const folders = await prompts({ - type: 'multiselect', - name: 'value', + const folders = await p.multiselect({ message: 'Which folders should be migrated?', - choices: fs + options: fs .readdirSync('.') .filter( (dir) => fs.statSync(dir).isDirectory() && dir !== 'node_modules' && !dir.startsWith('.') @@ -70,14 +66,14 @@ export async function migrate() { .map((dir) => ({ title: dir, value: dir, selected: true })) }); - if (!folders.value?.length) { + if (p.isCancel(folders) || !folders?.length) { process.exit(1); } update_pkg_json(); // For some reason {folders.value.join(',')} as part of the glob doesn't work and returns less files - const files = folders.value.flatMap( + const files = folders.flatMap( /** @param {string} folder */ (folder) => glob(`${folder}/**`, { filesOnly: true, dot: true }) .map((file) => file.replace(/\\/g, '/')) diff --git a/packages/migrate/migrations/package/index.js b/packages/migrate/migrations/package/index.js index ef7dd639..2b72c473 100644 --- a/packages/migrate/migrations/package/index.js +++ b/packages/migrate/migrations/package/index.js @@ -2,7 +2,7 @@ import fs from 'node:fs'; import pc from 'picocolors'; import path from 'node:path'; import process from 'node:process'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import { pathToFileURL } from 'node:url'; import { bail, check_git } from '../../utils.js'; import { migrate_config } from './migrate_config.js'; @@ -26,14 +26,12 @@ export async function migrate() { const use_git = check_git(); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } diff --git a/packages/migrate/migrations/routes/index.js b/packages/migrate/migrations/routes/index.js index c92b9095..aad15ca3 100644 --- a/packages/migrate/migrations/routes/index.js +++ b/packages/migrate/migrations/routes/index.js @@ -2,7 +2,7 @@ import fs from 'node:fs'; import pc from 'picocolors'; import path from 'node:path'; import process from 'node:process'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import glob from 'tiny-glob/sync.js'; import { pathToFileURL } from 'node:url'; import { migrate_scripts } from './migrate_scripts/index.js'; @@ -60,14 +60,12 @@ export async function migrate() { const use_git = check_git(); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } diff --git a/packages/migrate/migrations/self-closing-tags/index.js b/packages/migrate/migrations/self-closing-tags/index.js index 5adf66d2..402b2dc0 100644 --- a/packages/migrate/migrations/self-closing-tags/index.js +++ b/packages/migrate/migrations/self-closing-tags/index.js @@ -1,7 +1,7 @@ import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import glob from 'tiny-glob/sync.js'; import { remove_self_closing_tags } from './migrate.js'; import { pathToFileURL } from 'node:url'; @@ -20,14 +20,12 @@ export async function migrate() { pc.bold(pc.yellow('\nThis will update .svelte files inside the current directory\n')) ); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } diff --git a/packages/migrate/migrations/svelte-4/index.js b/packages/migrate/migrations/svelte-4/index.js index ab8cc271..44037410 100644 --- a/packages/migrate/migrations/svelte-4/index.js +++ b/packages/migrate/migrations/svelte-4/index.js @@ -1,7 +1,7 @@ import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import glob from 'tiny-glob/sync.js'; import { bail, check_git, update_js_file, update_svelte_file } from '../../utils.js'; import { transform_code, transform_svelte_code, update_pkg_json } from './migrate.js'; @@ -22,22 +22,18 @@ export async function migrate() { const use_git = check_git(); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } - const folders = await prompts({ - type: 'multiselect', - name: 'value', + const folders = await p.multiselect({ message: 'Which folders should be migrated?', - choices: fs + options: fs .readdirSync('.') .filter( (dir) => fs.statSync(dir).isDirectory() && dir !== 'node_modules' && !dir.startsWith('.') @@ -45,18 +41,20 @@ export async function migrate() { .map((dir) => ({ title: dir, value: dir, selected: true })) }); - if (!folders.value?.length) { + if (p.isCancel(folders) || !folders?.length) { process.exit(1); } - const migrate_transition = await prompts({ - type: 'confirm', - name: 'value', + const migrate_transition = await p.confirm({ message: 'Add the `|global` modifier to currently global transitions for backwards compatibility? More info at https://svelte.dev/docs/svelte/v4-migration-guide#transitions-are-local-by-default', - initial: true + initialValue: true }); + if (p.isCancel(migrate_transition)) { + process.exit(1); + } + update_pkg_json(); // const { default: config } = fs.existsSync('svelte.config.js') @@ -68,8 +66,8 @@ export async function migrate() { '.svelte' ]; const extensions = [...svelte_extensions, '.ts', '.js']; - // For some reason {folders.value.join(',')} as part of the glob doesn't work and returns less files - const files = folders.value.flatMap( + // For some reason {folders.join(',')} as part of the glob doesn't work and returns less files + const files = folders.flatMap( /** @param {string} folder */ (folder) => glob(`${folder}/**`, { filesOnly: true, dot: true }) .map((file) => file.replace(/\\/g, '/')) @@ -80,7 +78,7 @@ export async function migrate() { if (extensions.some((ext) => file.endsWith(ext))) { if (svelte_extensions.some((ext) => file.endsWith(ext))) { update_svelte_file(file, transform_code, (code) => - transform_svelte_code(code, migrate_transition.value) + transform_svelte_code(code, migrate_transition) ); } else { update_js_file(file, transform_code); diff --git a/packages/migrate/migrations/svelte-5/index.js b/packages/migrate/migrations/svelte-5/index.js index 14165d89..d750a226 100644 --- a/packages/migrate/migrations/svelte-5/index.js +++ b/packages/migrate/migrations/svelte-5/index.js @@ -5,7 +5,7 @@ import process from 'node:process'; import fs from 'node:fs'; import { dirname } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import semver from 'semver'; import glob from 'tiny-glob/sync.js'; import { bail, check_git, update_js_file, update_svelte_file } from '../../utils.js'; @@ -33,13 +33,11 @@ export async function migrate() { ) ) ); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Run svelte-4 migration now?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } else { await migrate_svelte_4(); @@ -63,13 +61,11 @@ export async function migrate() { ) ) ); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Run sveltekit-2 migration now?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } else { await migrate_sveltekit_2(); @@ -120,22 +116,18 @@ export async function migrate() { const use_git = check_git(); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } - const folders = await prompts({ - type: 'multiselect', - name: 'value', + const folders = await p.multiselect({ message: 'Which folders should be migrated?', - choices: fs + options: fs .readdirSync('.') .filter( (dir) => fs.statSync(dir).isDirectory() && dir !== 'node_modules' && !dir.startsWith('.') @@ -143,7 +135,7 @@ export async function migrate() { .map((dir) => ({ title: dir, value: dir, selected: true })) }); - if (!folders.value?.length) { + if (p.isCancel(folders) || !folders?.length) { process.exit(1); } @@ -161,7 +153,7 @@ export async function migrate() { ]; const extensions = [...svelte_extensions, '.ts', '.js']; // For some reason {folders.value.join(',')} as part of the glob doesn't work and returns less files - const files = folders.value.flatMap( + const files = folders.flatMap( /** @param {string} folder */ (folder) => glob(`${folder}/**`, { filesOnly: true, dot: true }) .map((file) => file.replace(/\\/g, '/')) diff --git a/packages/migrate/migrations/sveltekit-2/index.js b/packages/migrate/migrations/sveltekit-2/index.js index 45cb9da3..00f61caf 100644 --- a/packages/migrate/migrations/sveltekit-2/index.js +++ b/packages/migrate/migrations/sveltekit-2/index.js @@ -1,7 +1,7 @@ import pc from 'picocolors'; import fs from 'node:fs'; import process from 'node:process'; -import prompts from 'prompts'; +import * as p from '@clack/prompts'; import semver from 'semver'; import glob from 'tiny-glob/sync.js'; import { @@ -39,14 +39,12 @@ export async function migrate() { const use_git = check_git(); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } @@ -64,36 +62,30 @@ export async function migrate() { ) ) ); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Run `svelte-4` migration now?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } else { await migrate_svelte_4(); console.log( pc.bold(pc.green('`svelte-4` migration complete. Continue with `sveltekit-2` migration?\n')) ); - const response = await prompts({ - type: 'confirm', - name: 'value', + const response = await p.confirm({ message: 'Continue?', - initial: false + initialValue: false }); - if (!response.value) { + if (p.isCancel(response) || !response) { process.exit(1); } } } - const folders = await prompts({ - type: 'multiselect', - name: 'value', + const folders = await p.multiselect({ message: 'Which folders should be migrated?', - choices: fs + options: fs .readdirSync('.') .filter( (dir) => @@ -105,7 +97,7 @@ export async function migrate() { .map((dir) => ({ title: dir, value: dir, selected: dir === 'src' })) }); - if (!folders.value?.length) { + if (p.isCancel(folders) || !folders?.length) { process.exit(1); } @@ -122,8 +114,8 @@ export async function migrate() { '.svelte' ]; const extensions = [...svelte_extensions, '.ts', '.js']; - // For some reason {folders.value.join(',')} as part of the glob doesn't work and returns less files - const files = folders.value.flatMap( + // For some reason {folders.join(',')} as part of the glob doesn't work and returns less files + const files = folders.flatMap( /** @param {string} folder */ (folder) => glob(`${folder}/**`, { filesOnly: true, dot: true }) .map((file) => file.replace(/\\/g, '/')) diff --git a/packages/migrate/package.json b/packages/migrate/package.json index 71283f7a..3844aa10 100644 --- a/packages/migrate/package.json +++ b/packages/migrate/package.json @@ -27,10 +27,10 @@ "svelte-migrate": "./bin.js" }, "dependencies": { + "@clack/prompts": "^0.9.0", "import-meta-resolve": "^4.1.0", "magic-string": "^0.30.15", "picocolors": "^1.1.1", - "prompts": "^2.4.2", "semver": "^7.6.3", "tiny-glob": "^0.2.9", "ts-morph": "^24.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd2a64ec..fa68d69c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -256,6 +256,9 @@ importers: packages/migrate: dependencies: + '@clack/prompts': + specifier: ^0.9.0 + version: 0.9.0 import-meta-resolve: specifier: ^4.1.0 version: 4.1.0 @@ -265,9 +268,6 @@ importers: picocolors: specifier: ^1.1.1 version: 1.1.1 - prompts: - specifier: ^2.4.2 - version: 2.4.2 semver: specifier: ^7.6.3 version: 7.6.3 @@ -382,6 +382,12 @@ packages: '@changesets/write@0.3.2': resolution: {integrity: sha512-kDxDrPNpUgsjDbWBvUo27PzKX4gqeKOlhibaOXDJA6kuBisGqNHv/HwGJrAu8U/dSf8ZEFIeHIPtvSlZI1kULw==} + '@clack/core@0.4.0': + resolution: {integrity: sha512-YJCYBsyJfNDaTbvDUVSJ3SgSuPrcujarRgkJ5NLjexDZKvaOiVVJvAQYx8lIgG0qRT8ff0fPgqyBCVivanIZ+A==} + + '@clack/prompts@0.9.0': + resolution: {integrity: sha512-nGsytiExgUr4FL0pR/LeqxA28nz3E0cW7eLTSh3Iod9TGrbBt8Y7BHbV3mmkNC4G0evdYyQ3ZsbiBkk7ektArA==} + '@esbuild/aix-ppc64@0.21.5': resolution: {integrity: sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ==} engines: {node: '>=12'} @@ -2034,10 +2040,6 @@ packages: engines: {node: '>=14'} hasBin: true - prompts@2.4.2: - resolution: {integrity: sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==} - engines: {node: '>= 6'} - ps-tree@1.2.0: resolution: {integrity: sha512-0VnamPPYHl4uaU/nSFeZZpR21QAWRz+sRv4iW9+v/GS/J5U5iZB5BNN6J0RMoOvdx2gWM2+ZFMIm58q24e4UYA==} engines: {node: '>= 0.10'} @@ -2664,6 +2666,17 @@ snapshots: human-id: 1.0.2 prettier: 2.8.8 + '@clack/core@0.4.0': + dependencies: + picocolors: 1.1.1 + sisteransi: 1.0.5 + + '@clack/prompts@0.9.0': + dependencies: + '@clack/core': 0.4.0 + picocolors: 1.1.1 + sisteransi: 1.0.5 + '@esbuild/aix-ppc64@0.21.5': optional: true @@ -4150,11 +4163,6 @@ snapshots: prettier@3.4.2: {} - prompts@2.4.2: - dependencies: - kleur: 3.0.3 - sisteransi: 1.0.5 - ps-tree@1.2.0: dependencies: event-stream: 3.3.4 From 39763c77348a9e33a3ee2c58e12e4c522a80147c Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Tue, 24 Dec 2024 15:14:08 +0100 Subject: [PATCH 03/13] improve logs --- packages/migrate/migrations/app-state/index.js | 13 +++++++------ packages/migrate/migrations/svelte-4/index.js | 13 +++++++------ packages/migrate/migrations/svelte-5/index.js | 13 +++++++------ packages/migrate/migrations/sveltekit-2/index.js | 13 +++++++------ packages/migrate/utils.js | 9 +++++---- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/packages/migrate/migrations/app-state/index.js b/packages/migrate/migrations/app-state/index.js index 31128f7a..a15d34cc 100644 --- a/packages/migrate/migrations/app-state/index.js +++ b/packages/migrate/migrations/app-state/index.js @@ -36,13 +36,14 @@ export async function migrate() { process.exit(1); } - console.log( - pc.bold( - pc.yellow( - '\nThis will update files in the current directory\n' + - "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" + p.log.warning( + pc.bold(pc.yellow('This will update files in the current directory.')) + + '\n' + + pc.bold( + pc.yellow( + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently." + ) ) - ) ); const use_git = check_git(); diff --git a/packages/migrate/migrations/svelte-4/index.js b/packages/migrate/migrations/svelte-4/index.js index 44037410..ee3213cb 100644 --- a/packages/migrate/migrations/svelte-4/index.js +++ b/packages/migrate/migrations/svelte-4/index.js @@ -11,13 +11,14 @@ export async function migrate() { bail('Please re-run this script in a directory with a package.json'); } - console.log( - pc.bold( - pc.yellow( - '\nThis will update files in the current directory\n' + - "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" + p.log.warning( + pc.bold(pc.yellow('This will update files in the current directory.')) + + '\n' + + pc.bold( + pc.yellow( + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently." + ) ) - ) ); const use_git = check_git(); diff --git a/packages/migrate/migrations/svelte-5/index.js b/packages/migrate/migrations/svelte-5/index.js index d750a226..ebd97a4a 100644 --- a/packages/migrate/migrations/svelte-5/index.js +++ b/packages/migrate/migrations/svelte-5/index.js @@ -105,13 +105,14 @@ export async function migrate() { return; } - console.log( - pc.bold( - pc.yellow( - '\nThis will update files in the current directory\n' + - "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" + p.log.warning( + pc.bold(pc.yellow('This will update files in the current directory.')) + + '\n' + + pc.bold( + pc.yellow( + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently." + ) ) - ) ); const use_git = check_git(); diff --git a/packages/migrate/migrations/sveltekit-2/index.js b/packages/migrate/migrations/sveltekit-2/index.js index 00f61caf..bb83125c 100644 --- a/packages/migrate/migrations/sveltekit-2/index.js +++ b/packages/migrate/migrations/sveltekit-2/index.js @@ -28,13 +28,14 @@ export async function migrate() { bail('Please re-run this script in a directory with a svelte.config.js'); } - console.log( - pc.bold( - pc.yellow( - '\nThis will update files in the current directory\n' + - "If you're inside a monorepo, run this in individual project directories rather than the workspace root.\n" + p.log.warning( + pc.bold(pc.yellow('This will update files in the current directory.')) + + '\n' + + pc.bold( + pc.yellow( + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently." + ) ) - ) ); const use_git = check_git(); diff --git a/packages/migrate/utils.js b/packages/migrate/utils.js index 179775f8..c95dcd86 100644 --- a/packages/migrate/utils.js +++ b/packages/migrate/utils.js @@ -1,3 +1,4 @@ +import * as p from '@clack/prompts'; import pc from 'picocolors'; import MagicString from 'magic-string'; import { execFileSync, execSync } from 'node:child_process'; @@ -152,15 +153,15 @@ export function check_git() { if (status) { const message = - 'Your git working directory is dirty — we recommend committing your changes before running this migration.\n'; - console.log(pc.bold(pc.red(message))); + 'Your git working directory is dirty — we recommend committing your changes before running this migration.'; + p.log.warning(pc.bold(pc.red(message))); } } catch { // would be weird to have a .git folder if git is not installed, // but always expect the unexpected const message = - 'Could not detect a git installation. If this is unexpected, please raise an issue: https://github.com/sveltejs/kit.\n'; - console.log(pc.bold(pc.red(message))); + 'Could not detect a git installation. If this is unexpected, please raise an issue: https://github.com/sveltejs/cli.\n'; + p.log.warning(pc.bold(pc.red(message))); use_git = false; } } From 7dea0990d5670956850f0e65feb6721f8076629e Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Tue, 24 Dec 2024 15:23:56 +0100 Subject: [PATCH 04/13] changeset --- .changeset/dull-toys-share.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dull-toys-share.md diff --git a/.changeset/dull-toys-share.md b/.changeset/dull-toys-share.md new file mode 100644 index 00000000..fd1db740 --- /dev/null +++ b/.changeset/dull-toys-share.md @@ -0,0 +1,5 @@ +--- +'svelte-migrate': patch +--- + +chore: unify svelte-migrate to sv From 5df3d81fea4b975b8f16d031ead321db18d9dcbf Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Fri, 27 Dec 2024 10:41:03 +0100 Subject: [PATCH 05/13] migrate to prompt logging --- .../migrate/migrations/app-state/index.js | 35 +++++---------- packages/migrate/migrations/package/index.js | 30 ++++--------- .../migrations/package/migrate_config.js | 3 +- .../migrate/migrations/package/migrate_pkg.js | 9 ++-- packages/migrate/migrations/routes/index.js | 21 +++------ .../migrations/self-closing-tags/index.js | 10 +++-- packages/migrate/migrations/svelte-4/index.js | 25 +++++------ packages/migrate/migrations/svelte-5/index.js | 45 +++++++++---------- .../migrate/migrations/sveltekit-2/index.js | 24 +++------- packages/migrate/utils.js | 24 +++++++++- 10 files changed, 97 insertions(+), 129 deletions(-) diff --git a/packages/migrate/migrations/app-state/index.js b/packages/migrate/migrations/app-state/index.js index a15d34cc..eaa34c2c 100644 --- a/packages/migrate/migrations/app-state/index.js +++ b/packages/migrate/migrations/app-state/index.js @@ -4,7 +4,7 @@ import process from 'node:process'; import * as p from '@clack/prompts'; import semver from 'semver'; import glob from 'tiny-glob/sync.js'; -import { bail, check_git, update_svelte_file } from '../../utils.js'; +import { bail, check_git, migration_succeeded, update_svelte_file } from '../../utils.js'; import { transform_svelte_code, update_pkg_json } from './migrate.js'; export async function migrate() { @@ -16,21 +16,17 @@ export async function migrate() { const svelte_dep = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; if (svelte_dep && semver.validRange(svelte_dep) && semver.gtr('5.0.0', svelte_dep)) { - console.log( - pc.bold( - pc.red('\nYou need to upgrade to Svelte version 5 first (`npx sv migrate svelte-5`).\n') - ) + p.log.error( + pc.bold(pc.red('You need to upgrade to Svelte version 5 first (`npx sv migrate svelte-5`).')) ); process.exit(1); } const kit_dep = pkg.devDependencies?.['@sveltejs/kit'] ?? pkg.dependencies?.['@sveltejs/kit']; if (kit_dep && semver.validRange(kit_dep) && semver.gtr('2.0.0', kit_dep)) { - console.log( + p.log.error( pc.bold( - pc.red( - '\nYou need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).\n' - ) + pc.red('You need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).') ) ); process.exit(1); @@ -95,25 +91,14 @@ export async function migrate() { ); } - console.log(pc.bold(pc.green('✔ Your project has been migrated'))); - - console.log('\nRecommended next steps:\n'); - /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); - const tasks = [ - "install the updated dependencies ('npm i' / 'pnpm i' / etc) " + use_git && - cyan('git commit -m "migration to $app/state"') - ].filter(Boolean); - - tasks.forEach((task, i) => { - console.log(` ${i + 1}: ${task}`); - }); - - console.log(''); - + const tasks = ["install the updated dependencies ('npm i' / 'pnpm i' / etc)"]; if (use_git) { - console.log(`Run ${cyan('git diff')} to review changes.\n`); + tasks.push(cyan('git commit -m "migration to $app/state"')); + tasks.push(`Run ${cyan('git diff')} to review changes.`); } + + migration_succeeded(tasks); } diff --git a/packages/migrate/migrations/package/index.js b/packages/migrate/migrations/package/index.js index 2b72c473..05565eed 100644 --- a/packages/migrate/migrations/package/index.js +++ b/packages/migrate/migrations/package/index.js @@ -4,7 +4,7 @@ import path from 'node:path'; import process from 'node:process'; import * as p from '@clack/prompts'; import { pathToFileURL } from 'node:url'; -import { bail, check_git } from '../../utils.js'; +import { bail, check_git, migration_succeeded } from '../../utils.js'; import { migrate_config } from './migrate_config.js'; import { migrate_pkg } from './migrate_pkg.js'; @@ -16,11 +16,9 @@ export async function migrate() { bail('Please re-run this script in a directory with a package.json'); } - console.log( + p.log.warning( pc.bold( - pc.yellow( - '\nThis will update your svelte.config.js and package.json in the current directory\n' - ) + pc.yellow('This will update your svelte.config.js and package.json in the current directory') ) ); @@ -55,26 +53,16 @@ export async function migrate() { migrate_config(); } - console.log(pc.bold(pc.green('✔ Your project has been migrated'))); - - console.log('\nRecommended next steps:\n'); - /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); - const tasks = [ - use_git && cyan('git commit -m "migration to @sveltejs/package v2"'), - 'Review the migration guide at https://github.com/sveltejs/kit/pull/8922', - 'Read the updated docs at https://svelte.dev/docs/kit/packaging' - ].filter(Boolean); + /** @type {string[]} */ + const tasks = []; - tasks.forEach((task, i) => { - console.log(` ${i + 1}: ${task}`); - }); + if (use_git) tasks.push(cyan('git commit -m "migration to @sveltejs/package v2"')); - console.log(''); + tasks.push('Review the migration guide at https://github.com/sveltejs/kit/pull/8922'); + tasks.push('Read the updated docs at https://svelte.dev/docs/kit/packaging'); - if (use_git) { - console.log(`Run ${cyan('git diff')} to review changes.\n`); - } + migration_succeeded; } diff --git a/packages/migrate/migrations/package/migrate_config.js b/packages/migrate/migrations/package/migrate_config.js index a7d27923..5d42e227 100644 --- a/packages/migrate/migrations/package/migrate_config.js +++ b/packages/migrate/migrations/package/migrate_config.js @@ -1,3 +1,4 @@ +import * as p from '@clack/prompts'; import fs from 'node:fs'; import pc from 'picocolors'; import MagicString from 'magic-string'; @@ -8,7 +9,7 @@ export function migrate_config() { const content = fs.readFileSync('svelte.config.js', 'utf8'); fs.writeFileSync('svelte.config.js', remove_package_from_config(content)); } catch { - console.log( + p.log.warning( pc.bold( pc.yellow( 'Could not remove package config from svelte.config.js, please remove it manually' diff --git a/packages/migrate/migrations/package/migrate_pkg.js b/packages/migrate/migrations/package/migrate_pkg.js index bc4bab1e..f79a2294 100644 --- a/packages/migrate/migrations/package/migrate_pkg.js +++ b/packages/migrate/migrations/package/migrate_pkg.js @@ -1,3 +1,4 @@ +import * as p from '@clack/prompts'; import fs from 'node:fs'; import path from 'node:path'; import pc from 'picocolors'; @@ -58,7 +59,7 @@ export function update_pkg_json(config, pkg, files) { // See: https://pnpm.io/package_json#publishconfigdirectory if (pkg.publishConfig?.directory || pkg.linkDirectory?.directory) { - console.log( + p.log.warning( pc.yellow( 'Detected "publishConfig.directory" or "linkDirectory.directory" fields in your package.json. ' + 'This migration removes them, which may or may not be what you want. Please review closely.' @@ -101,7 +102,7 @@ export function update_pkg_json(config, pkg, files) { const key = `./${file.dest}`.replace(/\/index\.js$|(\/[^/]+)\.js$/, '$1'); if (clashes[key]) { - console.log( + p.log.warning( pc.yellow( `Duplicate "${key}" export. Closely review your "exports" field in package.json after the migration.` ) @@ -169,14 +170,14 @@ export function update_pkg_json(config, pkg, files) { if (svelte_export) { pkg.svelte = svelte_export; } else { - console.log( + p.log.warning( pc.yellow( 'Cannot generate a "svelte" entry point because the "." entry in "exports" is not a string. Please specify a "svelte" entry point yourself\n' ) ); } } else { - console.log( + p.log.warning( pc.yellow( 'Cannot generate a "svelte" entry point because the "." entry in "exports" is missing. Please specify a "svelte" entry point yourself\n' ) diff --git a/packages/migrate/migrations/routes/index.js b/packages/migrate/migrations/routes/index.js index aad15ca3..fa95d446 100644 --- a/packages/migrate/migrations/routes/index.js +++ b/packages/migrate/migrations/routes/index.js @@ -10,7 +10,7 @@ import { migrate_page } from './migrate_page_js/index.js'; import { migrate_page_server } from './migrate_page_server/index.js'; import { migrate_server } from './migrate_server/index.js'; import { adjust_imports, task } from './utils.js'; -import { bail, relative, move_file, check_git } from '../../utils.js'; +import { bail, relative, move_file, check_git, migration_succeeded } from '../../utils.js'; export async function migrate() { if (!fs.existsSync('svelte.config.js')) { @@ -56,7 +56,7 @@ export async function migrate() { } } - console.log(pc.bold(pc.yellow('\nThis will overwrite files in the current directory!\n'))); + p.log.warning(pc.bold(pc.yellow('This will overwrite files in the current directory!'))); const use_git = check_git(); @@ -183,10 +183,6 @@ export async function migrate() { } } - console.log(pc.bold(pc.green('✔ Your project has been migrated'))); - - console.log('\nRecommended next steps:\n'); - /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); @@ -195,16 +191,9 @@ export async function migrate() { 'Review the migration guide at https://github.com/sveltejs/kit/discussions/5774', `Search codebase for ${cyan('"@migration"')} and manually complete migration tasks`, use_git && cyan('git add -A'), - use_git && cyan('git commit -m "svelte-migrate: updated files"') + use_git && cyan('git commit -m "svelte-migrate: updated files"'), + use_git && `Run ${cyan('git diff')} to review changes.` ].filter(Boolean); - tasks.forEach((task, i) => { - console.log(` ${i + 1}: ${task}`); - }); - - console.log(''); - - if (use_git) { - console.log(`Run ${cyan('git diff')} to review changes.\n`); - } + migration_succeeded(tasks); } diff --git a/packages/migrate/migrations/self-closing-tags/index.js b/packages/migrate/migrations/self-closing-tags/index.js index 402b2dc0..d724f2f3 100644 --- a/packages/migrate/migrations/self-closing-tags/index.js +++ b/packages/migrate/migrations/self-closing-tags/index.js @@ -6,17 +6,18 @@ import glob from 'tiny-glob/sync.js'; import { remove_self_closing_tags } from './migrate.js'; import { pathToFileURL } from 'node:url'; import { resolve } from 'import-meta-resolve'; +import { migration_succeeded } from '../../utils.js'; export async function migrate() { let compiler; try { compiler = await import_from_cwd('svelte/compiler'); } catch { - console.log(pc.bold(pc.red('❌ Could not find a local Svelte installation.'))); + p.log.error(pc.bold(pc.red('❌ Could not find a local Svelte installation.'))); return; } - console.log( + p.log.warning( pc.bold(pc.yellow('\nThis will update .svelte files inside the current directory\n')) ); @@ -42,8 +43,9 @@ export async function migrate() { } } - console.log(pc.bold(pc.green('✔ Your project has been updated'))); - console.log(' If using Prettier, please upgrade to the latest prettier-plugin-svelte version'); + const tasks = ['If using Prettier, please upgrade to the latest prettier-plugin-svelte version']; + + migration_succeeded(tasks); } /** @param {string} name */ diff --git a/packages/migrate/migrations/svelte-4/index.js b/packages/migrate/migrations/svelte-4/index.js index ee3213cb..4bab4316 100644 --- a/packages/migrate/migrations/svelte-4/index.js +++ b/packages/migrate/migrations/svelte-4/index.js @@ -3,7 +3,13 @@ import fs from 'node:fs'; import process from 'node:process'; import * as p from '@clack/prompts'; import glob from 'tiny-glob/sync.js'; -import { bail, check_git, update_js_file, update_svelte_file } from '../../utils.js'; +import { + bail, + check_git, + migration_succeeded, + update_js_file, + update_svelte_file +} from '../../utils.js'; import { transform_code, transform_svelte_code, update_pkg_json } from './migrate.js'; export async function migrate() { @@ -87,26 +93,15 @@ export async function migrate() { } } - console.log(pc.bold(pc.green('✔ Your project has been migrated'))); - - console.log('\nRecommended next steps:\n'); - /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); const tasks = [ use_git && cyan('git commit -m "migration to Svelte 4"'), 'Review the migration guide at https://svelte.dev/docs/svelte/v4-migration-guide', - 'Read the updated docs at https://svelte.dev/docs/svelte' + 'Read the updated docs at https://svelte.dev/docs/svelte', + use_git && `Run ${cyan('git diff')} to review changes.` ].filter(Boolean); - tasks.forEach((task, i) => { - console.log(` ${i + 1}: ${task}`); - }); - - console.log(''); - - if (use_git) { - console.log(`Run ${cyan('git diff')} to review changes.\n`); - } + migration_succeeded(tasks); } diff --git a/packages/migrate/migrations/svelte-5/index.js b/packages/migrate/migrations/svelte-5/index.js index ebd97a4a..f2e1548d 100644 --- a/packages/migrate/migrations/svelte-5/index.js +++ b/packages/migrate/migrations/svelte-5/index.js @@ -8,7 +8,13 @@ import { fileURLToPath, pathToFileURL } from 'node:url'; import * as p from '@clack/prompts'; import semver from 'semver'; import glob from 'tiny-glob/sync.js'; -import { bail, check_git, update_js_file, update_svelte_file } from '../../utils.js'; +import { + bail, + check_git, + migration_succeeded, + update_js_file, + update_svelte_file +} from '../../utils.js'; import { migrate as migrate_svelte_4 } from '../svelte-4/index.js'; import { migrate as migrate_sveltekit_2 } from '../sveltekit-2/index.js'; import { transform_module_code, transform_svelte_code, update_pkg_json } from './migrate.js'; @@ -18,7 +24,7 @@ export async function migrate() { bail('Please re-run this script in a directory with a package.json'); } - console.log( + p.log.warning( 'This migration is experimental — please report any bugs to https://github.com/sveltejs/svelte/issues' ); @@ -26,10 +32,10 @@ export async function migrate() { const svelte_dep = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; if (svelte_dep && semver.validRange(svelte_dep) && semver.gtr('4.0.0', svelte_dep)) { - console.log( + p.log.warning( pc.bold( pc.yellow( - '\nDetected Svelte 3. You need to upgrade to Svelte version 4 first (`npx sv migrate svelte-4`).\n' + 'Detected Svelte 3. You need to upgrade to Svelte version 4 first (`npx sv migrate svelte-4`).' ) ) ); @@ -41,10 +47,10 @@ export async function migrate() { process.exit(1); } else { await migrate_svelte_4(); - console.log( + p.log.success( pc.bold( pc.green( - 'svelte-4 migration complete. Check that everything is ok, then run `npx sv migrate svelte-5` again to continue the Svelte 5 migration.\n' + 'svelte-4 migration complete. Check that everything is ok, then run `npx sv migrate svelte-5` again to continue the Svelte 5 migration.' ) ) ); @@ -54,10 +60,10 @@ export async function migrate() { const kit_dep = pkg.devDependencies?.['@sveltejs/kit'] ?? pkg.dependencies?.['@sveltejs/kit']; if (kit_dep && semver.validRange(kit_dep) && semver.gtr('2.0.0', kit_dep)) { - console.log( + p.log.warning( pc.bold( pc.yellow( - '\nDetected SvelteKit 1. You need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).\n' + 'Detected SvelteKit 1. You need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).' ) ) ); @@ -69,10 +75,10 @@ export async function migrate() { process.exit(1); } else { await migrate_sveltekit_2(); - console.log( + p.log.success( pc.bold( pc.green( - 'sveltekit-2 migration complete. Check that everything is ok, then run `npx sv migrate svelte-5` again to continue the Svelte 5 migration.\n' + 'sveltekit-2 migration complete. Check that everything is ok, then run `npx sv migrate svelte-5` again to continue the Svelte 5 migration.' ) ) ); @@ -95,7 +101,7 @@ export async function migrate() { } } catch (e) { console.log(e); - console.log( + p.log.error( pc.bold( pc.red( '❌ Could not install Svelte. Manually bump the dependency to version 5 in your package.json, install it, then try again.' @@ -173,10 +179,6 @@ export async function migrate() { } } - console.log(pc.bold(pc.green('✔ Your project has been migrated'))); - - console.log('\nRecommended next steps:\n'); - /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); @@ -184,18 +186,11 @@ export async function migrate() { "install the updated dependencies ('npm i' / 'pnpm i' / etc) " + '(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)', use_git && cyan('git commit -m "migration to Svelte 5"'), - 'Review the migration guide at https://svelte.dev/docs/svelte/v5-migration-guide' + 'Review the migration guide at https://svelte.dev/docs/svelte/v5-migration-guide', + `Run ${cyan('git diff')} to review changes.` ].filter(Boolean); - tasks.forEach((task, i) => { - console.log(` ${i + 1}: ${task}`); - }); - - console.log(''); - - if (use_git) { - console.log(`Run ${cyan('git diff')} to review changes.\n`); - } + migration_succeeded(tasks); } /** @param {string} name */ diff --git a/packages/migrate/migrations/sveltekit-2/index.js b/packages/migrate/migrations/sveltekit-2/index.js index bb83125c..466233d3 100644 --- a/packages/migrate/migrations/sveltekit-2/index.js +++ b/packages/migrate/migrations/sveltekit-2/index.js @@ -7,6 +7,7 @@ import glob from 'tiny-glob/sync.js'; import { bail, check_git, + migration_succeeded, update_js_file, update_svelte_file, update_tsconfig @@ -56,10 +57,10 @@ export async function migrate() { } if (semver.validRange(svelte_dep) && semver.gtr('4.0.0', svelte_dep)) { - console.log( + p.log.warning( pc.bold( pc.yellow( - '\nSvelteKit 2 requires Svelte 4 or newer. We recommend running the `svelte-4` migration first (`npx sv migrate svelte-4`).\n' + 'SvelteKit 2 requires Svelte 4 or newer. We recommend running the `svelte-4` migration first (`npx sv migrate svelte-4`).' ) ) ); @@ -71,7 +72,7 @@ export async function migrate() { process.exit(1); } else { await migrate_svelte_4(); - console.log( + p.log.success( pc.bold(pc.green('`svelte-4` migration complete. Continue with `sveltekit-2` migration?\n')) ); const response = await p.confirm({ @@ -133,10 +134,6 @@ export async function migrate() { } } - console.log(pc.bold(pc.green('✔ Your project has been migrated'))); - - console.log('\nRecommended next steps:\n'); - /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); @@ -144,16 +141,9 @@ export async function migrate() { 'Run npm install (or the corresponding installation command of your package manager)', use_git && cyan('git commit -m "migration to SvelteKit 2"'), 'Review the migration guide at https://svelte.dev/docs/kit/migrating-to-sveltekit-2', - 'Read the updated docs at https://svelte.dev/docs/kit' + 'Read the updated docs at https://svelte.dev/docs/kit', + use_git && `Run ${cyan('git diff')} to review changes.` ].filter(Boolean); - tasks.forEach((task, i) => { - console.log(` ${i + 1}: ${task}`); - }); - - console.log(''); - - if (use_git) { - console.log(`Run ${cyan('git diff')} to review changes.\n`); - } + migration_succeeded(tasks); } diff --git a/packages/migrate/utils.js b/packages/migrate/utils.js index c95dcd86..4fd00665 100644 --- a/packages/migrate/utils.js +++ b/packages/migrate/utils.js @@ -10,7 +10,7 @@ import ts from 'typescript'; /** @param {string} message */ export function bail(message) { - console.error(pc.bold(pc.red(message))); + p.log.error(pc.bold(pc.red(message))); process.exit(1); } @@ -420,3 +420,25 @@ export function add_named_import(source, _import, method) { }); } } + +/** + * @param {(string | false)[]} next_steps + */ +export function migration_succeeded(next_steps) { + p.log.success(pc.bold(pc.green('✔ Your project has been migrated'))); + + if (!next_steps || next_steps.length === 0) { + return; + } + + /** @type {string[]} */ + const messages = []; + + next_steps.forEach((step, i) => { + if (!step) return; + + messages.push(`${i + 1}: ${step}`); + }); + + p.note(messages.join('\n'), 'Recommended next steps:'); +} From 12d5c5f286550c89a46f79482664126c2a254797 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Fri, 27 Dec 2024 11:08:08 +0100 Subject: [PATCH 06/13] add intro, outro and migration selection --- packages/migrate/bin.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/migrate/bin.js b/packages/migrate/bin.js index 447c651f..21cc8364 100755 --- a/packages/migrate/bin.js +++ b/packages/migrate/bin.js @@ -3,6 +3,7 @@ import fs from 'node:fs'; import process from 'node:process'; import { fileURLToPath } from 'node:url'; import pc from 'picocolors'; +import * as p from '@clack/prompts'; const migration = process.argv[2]; const dir = fileURLToPath(new URL('.', import.meta.url)); @@ -11,17 +12,29 @@ const migrations = fs .readdirSync(`${dir}/migrations`) .filter((migration) => fs.existsSync(`${dir}/migrations/${migration}/index.js`)); +const pkg = JSON.parse(fs.readFileSync(`${dir}/package.json`, 'utf8')); + +p.intro(`Welcome to the svelte-migrate CLI! ${pc.gray(`(v${pkg.version})`)}`); + if (migrations.includes(migration)) { - const { migrate } = await import(`./migrations/${migration}/index.js`); - migrate(); + await run_migration(migration); } else { - console.error( - pc.bold( - pc.red( - `You must specify one of the following migrations: ${migrations.join(', ')}\n` + - 'If you expected this to work, try re-running the command with the latest svelte-migrate version:\n' + - ` npx svelte-migrate@latest ${migration}` - ) - ) - ); + if (migration) p.log.warning(pc.yellow(`Invalid migration "${migration}" provided.`)); + + const selectedMigration = await p.select({ + message: 'Which migration should be run?', + options: migrations.map((x) => ({ value: x, label: x })) + }); + + if (!p.isCancel(selectedMigration)) await run_migration(selectedMigration); +} + +p.outro("You're all set!"); + +/** + * @param {string} migration + */ +async function run_migration(migration) { + const { migrate } = await import(`./migrations/${migration}/index.js`); + await migrate(); } From 34a399af81ebe4d86acc3c22ec331f65e3457148 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Fri, 27 Dec 2024 11:10:54 +0100 Subject: [PATCH 07/13] changeset --- .changeset/happy-singers-roll.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/happy-singers-roll.md diff --git a/.changeset/happy-singers-roll.md b/.changeset/happy-singers-roll.md new file mode 100644 index 00000000..f76778a7 --- /dev/null +++ b/.changeset/happy-singers-roll.md @@ -0,0 +1,5 @@ +--- +'svelte-migrate': minor +--- + +feat: add ability to select migration to run From 651b3c2789cd469bf1d43770e2c9076e8b6edc63 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Fri, 27 Dec 2024 11:12:36 +0100 Subject: [PATCH 08/13] fix docs / readme --- documentation/docs/20-commands/40-sv-migrate.md | 4 ++++ packages/migrate/README.md | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/documentation/docs/20-commands/40-sv-migrate.md b/documentation/docs/20-commands/40-sv-migrate.md index 4c236462..1e59e28b 100644 --- a/documentation/docs/20-commands/40-sv-migrate.md +++ b/documentation/docs/20-commands/40-sv-migrate.md @@ -8,6 +8,10 @@ Some migrations may annotate your codebase with tasks for completion that you ca ## Usage +```bash +npx sv migrate +``` + ```bash npx sv migrate [migration] ``` diff --git a/packages/migrate/README.md b/packages/migrate/README.md index 07ae73a9..b1b55472 100644 --- a/packages/migrate/README.md +++ b/packages/migrate/README.md @@ -5,11 +5,9 @@ A CLI for migrating Svelte(Kit) codebases. Run it directly using: ``` -npx svelte-migrate [migration] +npx sv migrate ``` -Or via the unified Svlete CLI with: - ``` npx sv migrate [migration] ``` From c15b41939eaac47082ea62ea4cf4d861d8b3e9b0 Mon Sep 17 00:00:00 2001 From: Manuel <30698007+manuel3108@users.noreply.github.com> Date: Wed, 1 Jan 2025 11:07:39 +0100 Subject: [PATCH 09/13] Apply suggestions from code review Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- .changeset/dull-toys-share.md | 2 +- documentation/docs/20-commands/40-sv-migrate.md | 1 + packages/migrate/README.md | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.changeset/dull-toys-share.md b/.changeset/dull-toys-share.md index fd1db740..ab14d441 100644 --- a/.changeset/dull-toys-share.md +++ b/.changeset/dull-toys-share.md @@ -2,4 +2,4 @@ 'svelte-migrate': patch --- -chore: unify svelte-migrate to sv +chore: align dependencies with `sv` diff --git a/documentation/docs/20-commands/40-sv-migrate.md b/documentation/docs/20-commands/40-sv-migrate.md index 1e59e28b..0da6b8bf 100644 --- a/documentation/docs/20-commands/40-sv-migrate.md +++ b/documentation/docs/20-commands/40-sv-migrate.md @@ -12,6 +12,7 @@ Some migrations may annotate your codebase with tasks for completion that you ca npx sv migrate ``` +You can also specify a migration directly via the CLI: ```bash npx sv migrate [migration] ``` diff --git a/packages/migrate/README.md b/packages/migrate/README.md index b1b55472..cc0797d7 100644 --- a/packages/migrate/README.md +++ b/packages/migrate/README.md @@ -4,11 +4,12 @@ A CLI for migrating Svelte(Kit) codebases. Run it directly using: -``` +```bash npx sv migrate ``` -``` +You can also specify a migration directly via the CLI: +```bash npx sv migrate [migration] ``` From f20770952ad58d6f92334d67318ee28e66b8c1f7 Mon Sep 17 00:00:00 2001 From: Manuel <30698007+manuel3108@users.noreply.github.com> Date: Wed, 1 Jan 2025 11:08:05 +0100 Subject: [PATCH 10/13] Update packages/migrate/bin.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/migrate/bin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/migrate/bin.js b/packages/migrate/bin.js index 21cc8364..18f1ad8b 100755 --- a/packages/migrate/bin.js +++ b/packages/migrate/bin.js @@ -22,7 +22,7 @@ if (migrations.includes(migration)) { if (migration) p.log.warning(pc.yellow(`Invalid migration "${migration}" provided.`)); const selectedMigration = await p.select({ - message: 'Which migration should be run?', + message: 'Which migration would you like to run?', options: migrations.map((x) => ({ value: x, label: x })) }); From 1bcfb2f7deeae6c1b891624ab2d7af77ea3445c2 Mon Sep 17 00:00:00 2001 From: Manuel <30698007+manuel3108@users.noreply.github.com> Date: Wed, 1 Jan 2025 11:08:15 +0100 Subject: [PATCH 11/13] Update packages/migrate/migrations/app-state/index.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/migrate/migrations/app-state/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/migrate/migrations/app-state/index.js b/packages/migrate/migrations/app-state/index.js index eaa34c2c..b3775e20 100644 --- a/packages/migrate/migrations/app-state/index.js +++ b/packages/migrate/migrations/app-state/index.js @@ -94,6 +94,7 @@ export async function migrate() { /** @type {(s: string) => string} */ const cyan = (s) => pc.bold(pc.cyan(s)); + // TODO: use package-manager-detector here const tasks = ["install the updated dependencies ('npm i' / 'pnpm i' / etc)"]; if (use_git) { tasks.push(cyan('git commit -m "migration to $app/state"')); From 969433ec7106d77dd005d4e5d34f007c5723183a Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Wed, 1 Jan 2025 11:10:40 +0100 Subject: [PATCH 12/13] lint --- packages/migrate/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/migrate/README.md b/packages/migrate/README.md index cc0797d7..ed7413bd 100644 --- a/packages/migrate/README.md +++ b/packages/migrate/README.md @@ -9,6 +9,7 @@ npx sv migrate ``` You can also specify a migration directly via the CLI: + ```bash npx sv migrate [migration] ``` From 26e4a0be14a3a1e973becf73878948c0db0394e2 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Fri, 10 Jan 2025 16:19:51 +0100 Subject: [PATCH 13/13] make it work again --- packages/migrate/migrations/svelte-5/index.js | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/migrate/migrations/svelte-5/index.js b/packages/migrate/migrations/svelte-5/index.js index deb2dc64..92a1dfa9 100644 --- a/packages/migrate/migrations/svelte-5/index.js +++ b/packages/migrate/migrations/svelte-5/index.js @@ -128,49 +128,49 @@ export async function migrate() { process.exit(1); } - const folders = await p.multiselect({ + const dirs = fs + .readdirSync('.') + .filter( + (dir) => fs.statSync(dir).isDirectory() && dir !== 'node_modules' && !dir.startsWith('.') + ); + + let folders = await p.multiselect({ message: 'Which folders should be migrated?', - options: fs - .readdirSync('.') - .filter( - (dir) => fs.statSync(dir).isDirectory() && dir !== 'node_modules' && !dir.startsWith('.') - ) - .map((dir) => ({ title: dir, value: dir, selected: true })) + options: dirs + .map((dir) => ({ label: dir, value: dir })) .concat([ { - title: 'custom (overrides selection, allows to specify sub folders)', - value: ',', // a value that definitely isn't a valid folder name so it cannot clash - selected: false + label: 'custom (overrides selection, allows to specify sub folders)', + value: ',' // a value that definitely isn't a valid folder name so it cannot clash } - ]) + ]), + initialValues: dirs }); if (p.isCancel(folders) || !folders?.length) { process.exit(1); } - if (folders.value.includes(',')) { - const custom = await prompts({ - type: 'list', - name: 'value', + if (folders.includes(',')) { + const custom = await p.text({ message: 'Specify folder paths (comma separated)' }); - if (!custom.value) { + if (p.isCancel(custom) || !custom) { process.exit(1); } - folders.value = custom.value.map((/** @type {string} */ folder) => (folder = folder.trim())); + folders = custom.split(',').map((/** @type {string} */ folder) => (folder = folder.trim())); } - const do_migration = await prompts({ - type: 'confirm', - name: 'value', + const do_migration = await p.confirm({ message: 'Do you want to use the migration tool to convert your Svelte components to the new syntax? (You can also do this per component or sub path later)', - initial: true + initialValue: true }); + if (p.isCancel(do_migration)) process.exit(1); + update_pkg_json(); const use_ts = fs.existsSync('tsconfig.json'); @@ -195,7 +195,7 @@ export async function migrate() { for (const file of files) { if (extensions.some((ext) => file.endsWith(ext))) { if (svelte_extensions.some((ext) => file.endsWith(ext))) { - if (do_migration.value) { + if (do_migration) { update_svelte_file(file, transform_module_code, (code) => transform_svelte_code(code, migrate, { filename: file, use_ts }) );