Skip to content

Commit

Permalink
Merge branch 'main' into feat/multiple-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Dec 11, 2024
2 parents 20cff62 + 0245c91 commit f613ad3
Show file tree
Hide file tree
Showing 24 changed files with 236 additions and 375 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
"@nuxt/eslint-config": "^0.7.2",
"@nuxt/kit": "^3.14.1592",
"@nuxt/schema": "^3.14.1592",
"@nuxt/test-utils": "^3.15.0",
"@nuxt/test-utils": "^3.15.1",
"@types/http-proxy": "^1.17.15",
"@types/node": "^22.10.1",
"@types/node": "^22.10.2",
"@types/semver": "^7.5.8",
"@types/ws": "^8.5.13",
"@vitest/coverage-v8": "^2.1.8",
Expand All @@ -56,7 +56,7 @@
"consola": "^3.2.3",
"destr": "^2.0.3",
"eslint": "^9.16.0",
"execa": "^9.5.1",
"execa": "^9.5.2",
"fuse.js": "^7.0.0",
"giget": "^1.2.3",
"h3": "^1.13.0",
Expand Down Expand Up @@ -85,7 +85,7 @@
"nitropack": "npm:nitropack-nightly",
"nuxt": "^3.14.1592"
},
"packageManager": "pnpm@9.14.4",
"packageManager": "pnpm@9.15.0",
"engines": {
"node": "^16.10.0 || >=18.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nuxt-cli-playground",
"private": true,
"version": "1.0.0",
"packageManager": "pnpm@9.14.4",
"packageManager": "pnpm@9.15.0",
"scripts": {
"nuxi": "node ../bin/nuxi.mjs"
},
Expand Down
341 changes: 84 additions & 257 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

36 changes: 29 additions & 7 deletions src/commands/_shared.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
export const sharedArgs = {
import type { ArgDef } from 'citty'

export const cwdArgs = {
cwd: {
type: 'string',
description: 'Current working directory',
description: 'Specify the working directory',
valueHint: 'directory',
default: '.',
},
} as const satisfies Record<string, ArgDef>

export const logLevelArgs = {
logLevel: {
type: 'string',
description: 'Log level',
description: 'Specify build-time log level',
valueHint: 'silent|info|verbose',
},
} as const
} as const satisfies Record<string, ArgDef>

export const envNameArgs = {
envName: {
type: 'string',
description: 'The environment to use when resolving configuration overrides (default is `production` when building, and `development` when running the dev server)',
},
} as const
} as const satisfies Record<string, ArgDef>

export const dotEnvArgs = {
dotenv: {
type: 'string',
description: 'Path to `.env` file to load, relative to the root directory',
},
} as const satisfies Record<string, ArgDef>

export const legacyRootDirArgs = {
// cwd falls back to rootDir's default (indirect default)
cwd: {
...cwdArgs.cwd,
description: 'Specify the working directory, this takes precedence over ROOTDIR (default: `.`)',
default: undefined,
},
rootDir: {
type: 'positional',
description: 'Root Directory',
description: 'Specifies the working directory (default: `.`)',
required: false,
default: '.',
},
} as const
} as const satisfies Record<string, ArgDef>
22 changes: 10 additions & 12 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import { consola } from 'consola'
import { defineCommand } from 'citty'
import { loadKit } from '../utils/kit'
import { templates } from '../utils/templates'
import { sharedArgs } from './_shared'
import { cwdArgs, logLevelArgs } from './_shared'

export default defineCommand({
meta: {
name: 'add',
description: 'Create a new template file.',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
force: {
type: 'boolean',
description: 'Override existing file',
Expand All @@ -30,7 +31,7 @@ export default defineCommand({
},
},
async run(ctx) {
const cwd = resolve(ctx.args.cwd || '.')
const cwd = resolve(ctx.args.cwd)

const templateName = ctx.args.template
const template = templates[templateName]
Expand Down Expand Up @@ -61,21 +62,18 @@ export default defineCommand({
const config = await kit.loadNuxtConfig({ cwd })

// Resolve template
const res = template({ name, args: ctx.args })

// Resolve full path to generated file
const path = resolve(config.srcDir, res.path)
const res = template({ name, args: ctx.args, nuxtOptions: config })

// Ensure not overriding user code
if (!ctx.args.force && existsSync(path)) {
if (!ctx.args.force && existsSync(res.path)) {
consola.error(
`File exists: ${path} . Use --force to override or use a different name.`,
`File exists: ${res.path} . Use --force to override or use a different name.`,
)
process.exit(1)
}

// Ensure parent directory exists
const parentDir = dirname(path)
const parentDir = dirname(res.path)
if (!existsSync(parentDir)) {
consola.info('Creating directory', parentDir)
if (templateName === 'page') {
Expand All @@ -85,7 +83,7 @@ export default defineCommand({
}

// Write file
await fsp.writeFile(path, res.contents.trim() + '\n')
consola.info(`🪄 Generated a new ${templateName} in ${path}`)
await fsp.writeFile(res.path, res.contents.trim() + '\n')
consola.info(`🪄 Generated a new ${templateName} in ${res.path}`)
},
})
22 changes: 19 additions & 3 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import { defineCommand } from 'citty'
import { loadKit } from '../utils/kit'
import { clearDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
import { sharedArgs, legacyRootDirArgs } from './_shared'
import { legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared'

export default defineCommand({
meta: {
name: 'analyze',
description: 'Build nuxt and analyze production bundle (experimental)',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...legacyRootDirArgs,
...dotEnvArgs,
name: {
type: 'string',
description: 'Name of the analysis',
Expand All @@ -32,7 +34,7 @@ export default defineCommand({
async run(ctx) {
overrideEnv('production')

const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
const name = ctx.args.name || 'default'
const slug = name.trim().replace(/[^\w-]/g, '_')

Expand All @@ -42,12 +44,26 @@ export default defineCommand({

const nuxt = await loadNuxt({
cwd,
dotenv: {
cwd,
fileName: ctx.args.dotenv,
},
overrides: defu(ctx.data?.overrides, {
build: {
analyze: {
enabled: true,
},
},
vite: {
build: {
rollupOptions: {
output: {
chunkFileNames: '_nuxt/[name].js',
entryFileNames: '_nuxt/[name].js',
},
},
},
},
logLevel: ctx.args.logLevel,
}),
})
Expand Down
7 changes: 4 additions & 3 deletions src/commands/build-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { consola } from 'consola'
import { resolve } from 'pathe'
import { defineCommand } from 'citty'
import { tryResolveModule } from '../utils/esm'
import { legacyRootDirArgs, sharedArgs } from './_shared'
import { cwdArgs, legacyRootDirArgs, logLevelArgs } from './_shared'

const MODULE_BUILDER_PKG = '@nuxt/module-builder'

Expand All @@ -13,7 +13,8 @@ export default defineCommand({
description: `Helper command for using ${MODULE_BUILDER_PKG}`,
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...legacyRootDirArgs,
stub: {
type: 'boolean',
Expand All @@ -30,7 +31,7 @@ export default defineCommand({
},
async run(ctx) {
// Find local installed version
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)

const hasLocal = await tryResolveModule(
`${MODULE_BUILDER_PKG}/package.json`,
Expand Down
12 changes: 5 additions & 7 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { loadKit } from '../utils/kit'
import { clearBuildDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
import { showVersions } from '../utils/banner'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'
import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared'

export default defineCommand({
meta: {
name: 'build',
description: 'Build Nuxt for production deployment',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
prerender: {
type: 'boolean',
description: 'Build Nuxt and prerender static routes',
Expand All @@ -23,17 +24,14 @@ export default defineCommand({
type: 'string',
description: 'Nitro server preset',
},
dotenv: {
type: 'string',
description: 'Path to .env file',
},
...dotEnvArgs,
...envNameArgs,
...legacyRootDirArgs,
},
async run(ctx) {
overrideEnv('production')

const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)

showVersions(cwd)

Expand Down
8 changes: 4 additions & 4 deletions src/commands/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { defineCommand } from 'citty'
import { cleanupNuxtDirs } from '../utils/nuxt'

import { loadKit } from '../utils/kit'
import { sharedArgs, legacyRootDirArgs } from './_shared'
import { legacyRootDirArgs, cwdArgs } from './_shared'

export default defineCommand({
meta: {
name: 'cleanup',
description: 'Clean up generated Nuxt files and caches',
},
args: {
...sharedArgs,
...cwdArgs,
...legacyRootDirArgs,
},
async run(ctx) {
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
const { loadNuxtConfig } = await loadKit(cwd)
const nuxtOptions = await loadNuxtConfig({ cwd })
const nuxtOptions = await loadNuxtConfig({ cwd, overrides: { dev: true } })
await cleanupNuxtDirs(nuxtOptions.rootDir, nuxtOptions.buildDir)
},
})
7 changes: 4 additions & 3 deletions src/commands/dev-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isTest } from 'std-env'
import { overrideEnv } from '../utils/env'
import type { NuxtDevContext, NuxtDevIPCMessage } from '../utils/dev'
import { createNuxtDevServer } from '../utils/dev'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'
import { envNameArgs, legacyRootDirArgs, cwdArgs, logLevelArgs } from './_shared'

export default defineCommand({
meta: {
Expand All @@ -14,7 +14,8 @@ export default defineCommand({
'Run Nuxt development server (internal command to start child process)',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...envNameArgs,
...legacyRootDirArgs,
},
Expand All @@ -29,7 +30,7 @@ export default defineCommand({

// Prepare
overrideEnv('development')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)

// Get dev context info
const devContext: NuxtDevContext
Expand Down
12 changes: 5 additions & 7 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { loadKit } from '../utils/kit'
import { importModule } from '../utils/esm'
import { overrideEnv } from '../utils/env'
import type { NuxtDevContext, NuxtDevIPCMessage } from '../utils/dev'
import { sharedArgs, envNameArgs, legacyRootDirArgs } from './_shared'
import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared'

const forkSupported = !isBun && !isTest

Expand All @@ -27,14 +27,12 @@ const command = defineCommand({
description: 'Run Nuxt development server',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...envNameArgs,
...legacyRootDirArgs,
...getListhenArgs(),
dotenv: {
type: 'string',
description: 'Path to .env file',
},
...dotEnvArgs,
clear: {
type: 'boolean',
description: 'Clear console on restart',
Expand All @@ -48,7 +46,7 @@ const command = defineCommand({
async run(ctx) {
// Prepare
overrideEnv('development')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
showVersions(cwd)
await setupDotenv({ cwd, fileName: ctx.args.dotenv })

Expand Down
6 changes: 3 additions & 3 deletions src/commands/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { resolve } from 'pathe'
import { execa } from 'execa'
import { defineCommand } from 'citty'

import { legacyRootDirArgs, sharedArgs } from './_shared'
import { cwdArgs, legacyRootDirArgs } from './_shared'

export default defineCommand({
meta: {
name: 'devtools',
description: 'Enable or disable devtools in a Nuxt project',
},
args: {
...sharedArgs,
...cwdArgs,
command: {
type: 'positional',
description: 'Command to run',
Expand All @@ -19,7 +19,7 @@ export default defineCommand({
...legacyRootDirArgs,
},
async run(ctx) {
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)

if (!['enable', 'disable'].includes(ctx.args.command)) {
console.error(`Unknown command \`${ctx.args.command}\`.`)
Expand Down
Loading

0 comments on commit f613ad3

Please sign in to comment.