Skip to content
Merged
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
62 changes: 56 additions & 6 deletions packages/nuxi/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -225,10 +226,59 @@ 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'
description: string
}[]
}>('https://api.nuxt.com/modules')

const officialModules = response.modules
.filter(module => module.type === 'official')
.filter(module => module.npm !== '@nuxt/devtools')

const selectedOfficialModules = await logger.prompt(
`Would you like to install any of the official modules?`,
{
type: 'multiselect',
options: officialModules.map(module => ({
label: `${colors.bold(colors.greenBright(module.npm))} – ${module.description.split('.')[0]}`,
value: module.npm,
})),
required: false,
},
)

if (selectedOfficialModules === undefined) {
process.exit(1)
}

if (selectedOfficialModules.length > 0) {
modulesToAdd.push(...(selectedOfficialModules as unknown as string[]))
}
}

// 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
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxi/src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?`,
{
Expand All @@ -76,8 +77,9 @@ export default defineCommand({
cancel: 'default',
},
)

if (shouldContinue !== true) {
return false
process.exit(1)
}
}

Expand Down