Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(upgrade): add --dedupe as option #573

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
nuxt-app
.pnpm-store
coverage
.idea
56 changes: 44 additions & 12 deletions src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export default defineCommand({
alias: 'f',
description: 'Force upgrade to recreate lockfile and node_modules',
},
dedupe: {
type: 'boolean',
description: 'Dedupe dependencies after upgrading',
},
channel: {
type: 'string',
alias: 'ch',
Expand Down Expand Up @@ -130,19 +134,39 @@ export default defineCommand({
const forceRemovals = ['node_modules', relative(process.cwd(), pmLockFile)]
.map(p => colors.cyan(p))
.join(' and ')
if (ctx.args.force === undefined) {
ctx.args.force = await consola.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,
},
)

let method: 'force' | 'dedupe' | 'skip' | undefined = ctx.args.force ? 'force' : ctx.args.dedupe ? 'dedupe' : undefined
// @ts-expect-error can be removed on next consola release
method ||= await consola.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) {
consola.info(
`Recreating ${forceRemovals}. If you encounter any issues, revert the changes and try with \`--no-force\``,
)

if (method === 'force') {
consola.info(`Recreating ${forceRemovals}. If you encounter any issues, revert the changes and try with \`--no-force\``)
await rmRecursive([pmLockFile, resolve(cwd, 'node_modules')])
await touchFile(pmLockFile)
}
Expand All @@ -160,6 +184,14 @@ export default defineCommand({

execSync(command, { stdio: 'inherit', cwd })

if (method === 'dedupe') {
if (packageManager !== 'bun') {
consola.info('Deduping dependencies...')
execSync(`${packageManager} dedupe`, { stdio: 'inherit', cwd })
}
consola.info(`Deduping dependencies is not yet supported with ${packageManager}.`)
}

// Clean up after upgrade
let buildDir: string = '.nuxt'
try {
Expand Down