From c2ed683cea9e97371e49bd9abc0c285152a1b301 Mon Sep 17 00:00:00 2001 From: Damian Glowala Date: Fri, 7 Mar 2025 20:21:40 +0100 Subject: [PATCH 1/4] feat(init): add interactive modules selector --- packages/nuxi/src/commands/init.ts | 58 +++++++++++++++++++++--- packages/nuxi/src/commands/module/add.ts | 4 +- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/packages/nuxi/src/commands/init.ts b/packages/nuxi/src/commands/init.ts index 4e4e162d1..c3bfb7ca9 100644 --- a/packages/nuxi/src/commands/init.ts +++ b/packages/nuxi/src/commands/init.ts @@ -8,10 +8,11 @@ import { defineCommand } from 'citty' import { colors } from 'consola/utils' import { downloadTemplate, startShell } from 'giget' import { installDependencies } from 'nypm' -import { relative, resolve } from 'pathe' +import { $fetch } from 'ofetch' +import { join, relative, resolve } from 'pathe' import { hasTTY } from 'std-env' -import { x } from 'tinyexec' +import { x } from 'tinyexec' import { runCommand } from '../run' import { nuxtIcon, themeColor } from '../utils/ascii' import { logger } from '../utils/logger' @@ -225,10 +226,55 @@ export default defineCommand({ } } - // Add modules when -M flag is provided - const modules = !ctx.args.modules ? [] : ctx.args.modules.split(',').map(module => module.trim()).filter(Boolean) - if (modules.length > 0) { - await runCommand('module', ['add', ...modules]) + const modulesToAdd: string[] = [] + + // Get modules from arg (if provided) + if (ctx.args.modules) { + modulesToAdd.push( + ...ctx.args.modules.split(',').map(module => module.trim()).filter(Boolean), + ) + } + // ...or offer to install official modules (if not offline) + else if (!ctx.args.offline && !ctx.args.preferOffline) { + const response = await $fetch<{ + modules: { + npm: string + type: 'community' | 'official' + }[] + }>('https://api.nuxt.com/modules') + + const officialModules = response.modules + .filter(module => module.type === 'official') + .map(module => module.npm) + + const selectedOfficialModules = await logger.prompt( + `Would you like to install any of the official modules?`, + { + type: 'multiselect', + options: officialModules, + required: false, + }, + ) + + if (selectedOfficialModules === undefined) { + process.exit(1) + } + + if (selectedOfficialModules.length > 0) { + modulesToAdd.push(...selectedOfficialModules) + } + } + + // Add modules + if (modulesToAdd.length > 0) { + await runCommand('module', [ + 'add', + ...modulesToAdd, + '--cwd', + join(ctx.args.cwd, ctx.args.dir), + '--skipInstall', + ctx.args.install ? 'false' : 'true', + ]) } // Display next steps diff --git a/packages/nuxi/src/commands/module/add.ts b/packages/nuxi/src/commands/module/add.ts index e050a2620..99624d32e 100644 --- a/packages/nuxi/src/commands/module/add.ts +++ b/packages/nuxi/src/commands/module/add.ts @@ -68,6 +68,7 @@ export default defineCommand({ if (!projectPkg.dependencies?.nuxt && !projectPkg.devDependencies?.nuxt) { logger.warn(`No \`nuxt\` dependency detected in \`${cwd}\`.`) + const shouldContinue = await logger.prompt( `Do you want to continue anyway?`, { @@ -76,8 +77,9 @@ export default defineCommand({ cancel: 'default', }, ) + if (shouldContinue !== true) { - return false + process.exit(1) } } From 546846bbeff2bf8221b8e5ec54f248e20bac92df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 7 Mar 2025 23:27:58 +0100 Subject: [PATCH 2/4] chore: update style --- packages/nuxi/src/commands/init.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/nuxi/src/commands/init.ts b/packages/nuxi/src/commands/init.ts index c3bfb7ca9..9e78b896a 100644 --- a/packages/nuxi/src/commands/init.ts +++ b/packages/nuxi/src/commands/init.ts @@ -240,12 +240,17 @@ export default defineCommand({ modules: { npm: string type: 'community' | 'official' + description: string }[] }>('https://api.nuxt.com/modules') const officialModules = response.modules .filter(module => module.type === 'official') - .map(module => module.npm) + .filter(module => module.npm !== '@nuxt/devtools') + .map(module => ({ + label: `${colors.bold(colors.greenBright(module.npm))} – ${module.description.split('.')[0]}`, + value: module.npm, + })) const selectedOfficialModules = await logger.prompt( `Would you like to install any of the official modules?`, From 5b80349cb228d7733b2c515702c862a69ff85456 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 7 Mar 2025 22:38:46 +0000 Subject: [PATCH 3/4] chore: normalise back to npm --- packages/nuxi/src/commands/init.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxi/src/commands/init.ts b/packages/nuxi/src/commands/init.ts index 9e78b896a..f97b8db1a 100644 --- a/packages/nuxi/src/commands/init.ts +++ b/packages/nuxi/src/commands/init.ts @@ -266,7 +266,7 @@ export default defineCommand({ } if (selectedOfficialModules.length > 0) { - modulesToAdd.push(...selectedOfficialModules) + modulesToAdd.push(...selectedOfficialModules.map(m => m.npm)) } } From c71632822203a5a3fba3d8df271943406d6f79fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Sat, 8 Mar 2025 00:04:18 +0100 Subject: [PATCH 4/4] chore: make it work again + cast types --- packages/nuxi/src/commands/init.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/nuxi/src/commands/init.ts b/packages/nuxi/src/commands/init.ts index f97b8db1a..867a3ef2e 100644 --- a/packages/nuxi/src/commands/init.ts +++ b/packages/nuxi/src/commands/init.ts @@ -247,16 +247,15 @@ export default defineCommand({ const officialModules = response.modules .filter(module => module.type === 'official') .filter(module => module.npm !== '@nuxt/devtools') - .map(module => ({ - label: `${colors.bold(colors.greenBright(module.npm))} – ${module.description.split('.')[0]}`, - value: module.npm, - })) const selectedOfficialModules = await logger.prompt( `Would you like to install any of the official modules?`, { type: 'multiselect', - options: officialModules, + options: officialModules.map(module => ({ + label: `${colors.bold(colors.greenBright(module.npm))} – ${module.description.split('.')[0]}`, + value: module.npm, + })), required: false, }, ) @@ -266,7 +265,7 @@ export default defineCommand({ } if (selectedOfficialModules.length > 0) { - modulesToAdd.push(...selectedOfficialModules.map(m => m.npm)) + modulesToAdd.push(...(selectedOfficialModules as unknown as string[])) } }