From 2ecf669bc2e91ee7ae7aa6c36b314f8ce8b12994 Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 20 Jan 2025 11:57:30 +0000 Subject: [PATCH 1/2] feat(upgrade): add `--dedupe` option --- .gitignore | 1 + packages/nuxi/package.json | 2 +- packages/nuxi/src/commands/upgrade.ts | 57 ++++++++++++++++++++------- packages/nuxi/src/utils/fs.ts | 9 ----- packages/nuxt-cli/package.json | 2 +- pnpm-lock.yaml | 22 +++++++++-- 6 files changed, 63 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 2aeecfa8d..dce6d10d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules dist .vscode +.idea .nuxt nuxt-app .pnpm-store diff --git a/packages/nuxi/package.json b/packages/nuxi/package.json index e94a0dc0f..6676f6b1d 100644 --- a/packages/nuxi/package.json +++ b/packages/nuxi/package.json @@ -58,7 +58,7 @@ "listhen": "^1.9.0", "magicast": "^0.3.5", "nitropack": "npm:nitropack-nightly", - "nypm": "^0.4.1", + "nypm": "^0.5.0", "ofetch": "^1.4.1", "ohash": "^1.1.4", "pathe": "^2.0.2", diff --git a/packages/nuxi/src/commands/upgrade.ts b/packages/nuxi/src/commands/upgrade.ts index 357d5e062..3aa592332 100644 --- a/packages/nuxi/src/commands/upgrade.ts +++ b/packages/nuxi/src/commands/upgrade.ts @@ -5,11 +5,10 @@ import process from 'node:process' import { defineCommand } from 'citty' import { colors } from 'consola/utils' -import { addDependency, detectPackageManager } from 'nypm' +import { addDependency, dedupeDependencies, detectPackageManager } from 'nypm' import { resolve } from 'pathe' import { readPackageJSON } from 'pkg-types' -import { rmRecursive, touchFile } from '../utils/fs' import { loadKit } from '../utils/kit' import { logger } from '../utils/logger' import { cleanupNuxtDirs, nuxtVersionToGitIdentifier } from '../utils/nuxt' @@ -78,6 +77,10 @@ export default defineCommand({ ...cwdArgs, ...logLevelArgs, ...legacyRootDirArgs, + dedupe: { + type: 'boolean', + description: 'Dedupe dependencies after upgrading', + }, force: { type: 'boolean', alias: 'f', @@ -131,23 +134,47 @@ export default defineCommand({ const forceRemovals = toRemove .map(p => colors.cyan(p)) .join(' and ') - if (ctx.args.force === undefined) { - ctx.args.force = await logger.prompt( - `Would you like to recreate ${forceRemovals} to fix problems with hoisted dependency versions and ensure you have the most up-to-date dependencies?`, - { - type: 'confirm', - default: true, - }, - ) === true + + let method: 'force' | 'dedupe' | 'skip' | undefined = ctx.args.force ? 'force' : ctx.args.dedupe ? 'dedupe' : undefined + + method ||= await logger.prompt( + `Would you like to dedupe your lockfile (recommended) or recreate ${forceRemovals}? This can fix problems with hoisted dependency versions and ensure you have the most up-to-date dependencies.`, + { + type: 'select', + initial: 'dedupe', + options: [ + { + label: 'dedupe lockfile', + value: 'dedupe' as const, + hint: 'recommended', + }, + { + label: `recreate ${forceRemovals}`, + value: 'force' as const, + }, + { + label: 'skip', + value: 'skip' as const, + }, + ], + }, + ) + + // user bails on the question with Ctrl+C + if (typeof method !== 'string') { + process.exit(1) } - if (ctx.args.force) { + + if (method === 'force') { logger.info( `Recreating ${forceRemovals}. If you encounter any issues, revert the changes and try with \`--no-force\``, ) - await rmRecursive(toRemove.map(file => resolve(cwd, file))) - if (lockFile) { - await touchFile(resolve(cwd, lockFile)) - } + await dedupeDependencies({ recreateLockfile: true }) + } + + if (method === 'dedupe') { + logger.info('Try deduping dependencies...') + await dedupeDependencies() } const versionType = ctx.args.channel === 'nightly' ? 'nightly' : 'latest stable' diff --git a/packages/nuxi/src/utils/fs.ts b/packages/nuxi/src/utils/fs.ts index 13258f08b..9ceb1a729 100644 --- a/packages/nuxi/src/utils/fs.ts +++ b/packages/nuxi/src/utils/fs.ts @@ -45,12 +45,3 @@ export async function rmRecursive(paths: string[]) { }), ) } - -export async function touchFile(path: string) { - if (await exists(path)) { - return - } - await fsp.writeFile(path, '').catch(() => { - logger.error(`Failed to create file: ${path}`) - }) -} diff --git a/packages/nuxt-cli/package.json b/packages/nuxt-cli/package.json index 81f85aac6..46d3fcbf2 100644 --- a/packages/nuxt-cli/package.json +++ b/packages/nuxt-cli/package.json @@ -45,7 +45,7 @@ "httpxy": "^0.1.6", "jiti": "^2.4.2", "listhen": "^1.9.0", - "nypm": "^0.4.1", + "nypm": "^0.5.0", "ofetch": "^1.4.1", "ohash": "^1.1.4", "pathe": "^2.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 76d8d3ac5..4b85e9fe2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -141,8 +141,8 @@ importers: specifier: npm:nitropack-nightly version: nitropack-nightly@2.11.0-20250115-115413.749f035d(typescript@5.7.3) nypm: - specifier: ^0.4.1 - version: 0.4.1 + specifier: ^0.5.0 + version: 0.5.0 ofetch: specifier: ^1.4.1 version: 1.4.1 @@ -231,8 +231,8 @@ importers: specifier: ^1.9.0 version: 1.9.0 nypm: - specifier: ^0.4.1 - version: 0.4.1 + specifier: ^0.5.0 + version: 0.5.0 ofetch: specifier: ^1.4.1 version: 1.4.1 @@ -3508,6 +3508,11 @@ packages: engines: {node: ^14.16.0 || >=16.10.0} hasBin: true + nypm@0.5.0: + resolution: {integrity: sha512-+2aEZ9h9Ocvsq1AR9hXsCsRW/7ZFVoPbk3GpYBCosKI1WuSyni58mi3v0WS7UczRA741gFjQ/9ivg4YRR3qv6w==} + engines: {node: ^14.16.0 || >=16.10.0} + hasBin: true + ofetch@1.4.1: resolution: {integrity: sha512-QZj2DfGplQAr2oj9KzceK9Hwz6Whxazmn85yYeVuS3u9XTMOGMRx0kO95MQ+vLsj/S/NwBDMMLU5hpxvI6Tklw==} @@ -8807,6 +8812,15 @@ snapshots: tinyexec: 0.3.2 ufo: 1.5.4 + nypm@0.5.0: + dependencies: + citty: 0.1.6 + consola: 3.4.0 + pathe: 2.0.2 + pkg-types: 1.3.1 + tinyexec: 0.3.2 + ufo: 1.5.4 + ofetch@1.4.1: dependencies: destr: 2.0.3 From e8a5dbcc19cc1557f793f1ab69876a565894aa3f Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 20 Jan 2025 12:02:53 +0000 Subject: [PATCH 2/2] chore: remove unused fs function --- packages/nuxi/src/utils/fs.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/nuxi/src/utils/fs.ts b/packages/nuxi/src/utils/fs.ts index 9ceb1a729..d422241b7 100644 --- a/packages/nuxi/src/utils/fs.ts +++ b/packages/nuxi/src/utils/fs.ts @@ -3,17 +3,6 @@ import { join } from 'pathe' import { logger } from '../utils/logger' -// Check if a file exists -async function exists(path: string) { - try { - await fsp.access(path) - return true - } - catch { - return false - } -} - export async function clearDir(path: string, exclude?: string[]) { if (!exclude) { await fsp.rm(path, { recursive: true, force: true })