feat(nuxi): allow passing overrides to other nuxi commands (#20760)

This commit is contained in:
Daniel Roe 2023-05-10 14:45:52 +01:00 committed by GitHub
parent 53fef72031
commit 29f29302ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 16 deletions

View File

@ -3,6 +3,7 @@ import { join, resolve } from 'pathe'
import { createApp, eventHandler, lazyEventHandler, toNodeListener } from 'h3' import { createApp, eventHandler, lazyEventHandler, toNodeListener } from 'h3'
import { listen } from 'listhen' import { listen } from 'listhen'
import type { NuxtAnalyzeMeta } from '@nuxt/schema' import type { NuxtAnalyzeMeta } from '@nuxt/schema'
import { defu } from 'defu'
import { loadKit } from '../utils/kit' import { loadKit } from '../utils/kit'
import { clearDir } from '../utils/fs' import { clearDir } from '../utils/fs'
import { overrideEnv } from '../utils/env' import { overrideEnv } from '../utils/env'
@ -14,7 +15,7 @@ export default defineNuxtCommand({
usage: 'npx nuxi analyze [--log-level] [--name] [--no-serve] [rootDir]', usage: 'npx nuxi analyze [--log-level] [--name] [--no-serve] [rootDir]',
description: 'Build nuxt and analyze production bundle (experimental)' description: 'Build nuxt and analyze production bundle (experimental)'
}, },
async invoke (args) { async invoke (args, options = {}) {
overrideEnv('production') overrideEnv('production')
const name = args.name || 'default' const name = args.name || 'default'
@ -31,7 +32,7 @@ export default defineNuxtCommand({
const nuxt = await loadNuxt({ const nuxt = await loadNuxt({
rootDir, rootDir,
overrides: { overrides: defu(options.overrides, {
build: { build: {
analyze: true analyze: true
}, },
@ -43,7 +44,7 @@ export default defineNuxtCommand({
} }
}, },
logLevel: args['log-level'] logLevel: args['log-level']
} })
}) })
analyzeDir = nuxt.options.analyzeDir analyzeDir = nuxt.options.analyzeDir

View File

@ -18,7 +18,8 @@ export default defineNuxtCommand({
const hasLocal = await tryResolveModule(`${MODULE_BUILDER_PKG}/package.json`, rootDir) const hasLocal = await tryResolveModule(`${MODULE_BUILDER_PKG}/package.json`, rootDir)
const execArgs = Object.entries({ const execArgs = Object.entries({
'--stub': args.stub '--stub': args.stub,
'--prepare': args.prepare
}).filter(([, value]) => value).map(([key]) => key) }).filter(([, value]) => value).map(([key]) => key)
let cmd = 'nuxt-module-build' let cmd = 'nuxt-module-build'

View File

@ -13,7 +13,7 @@ export default defineNuxtCommand({
usage: 'npx nuxi build [--prerender] [--dotenv] [--log-level] [rootDir]', usage: 'npx nuxi build [--prerender] [--dotenv] [--log-level] [rootDir]',
description: 'Build nuxt for production deployment' description: 'Build nuxt for production deployment'
}, },
async invoke (args) { async invoke (args, options = {}) {
overrideEnv('production') overrideEnv('production')
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
@ -29,7 +29,8 @@ export default defineNuxtCommand({
}, },
overrides: { overrides: {
logLevel: args['log-level'], logLevel: args['log-level'],
_generate: args.prerender _generate: args.prerender,
...(options?.overrides || {})
} }
}) })

View File

@ -7,8 +7,8 @@ export default defineNuxtCommand({
usage: 'npx nuxi generate [rootDir] [--dotenv]', usage: 'npx nuxi generate [rootDir] [--dotenv]',
description: 'Build Nuxt and prerender static routes' description: 'Build Nuxt and prerender static routes'
}, },
async invoke (args) { async invoke (args, options = {}) {
args.prerender = true args.prerender = true
await buildCommand.invoke(args) await buildCommand.invoke(args, options)
} }
}) })

View File

@ -11,7 +11,7 @@ export default defineNuxtCommand({
usage: 'npx nuxi prepare [--log-level] [rootDir]', usage: 'npx nuxi prepare [--log-level] [rootDir]',
description: 'Prepare nuxt for development/build' description: 'Prepare nuxt for development/build'
}, },
async invoke (args) { async invoke (args, options = {}) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production' process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
@ -20,7 +20,8 @@ export default defineNuxtCommand({
rootDir, rootDir,
overrides: { overrides: {
_prepare: true, _prepare: true,
logLevel: args['log-level'] logLevel: args['log-level'],
...(options.overrides || {})
} }
}) })
await clearBuildDir(nuxt.options.buildDir) await clearBuildDir(nuxt.options.buildDir)

View File

@ -14,11 +14,11 @@ export default defineNuxtCommand({
usage: 'npx nuxi preview|start [--dotenv] [rootDir]', usage: 'npx nuxi preview|start [--dotenv] [rootDir]',
description: 'Launches nitro server for local testing after `nuxi build`.' description: 'Launches nitro server for local testing after `nuxi build`.'
}, },
async invoke (args) { async invoke (args, options = {}) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production' process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
const { loadNuxtConfig } = await loadKit(rootDir) const { loadNuxtConfig } = await loadKit(rootDir)
const config = await loadNuxtConfig({ cwd: rootDir }) const config = await loadNuxtConfig({ cwd: rootDir, overrides: options?.overrides || {} })
const resolvedOutputDir = resolve(config.srcDir || rootDir, config.nitro.srcDir || 'server', config.nitro.output?.dir || '.output', 'nitro.json') const resolvedOutputDir = resolve(config.srcDir || rootDir, config.nitro.srcDir || 'server', config.nitro.output?.dir || '.output', 'nitro.json')
const defaultOutput = resolve(rootDir, '.output', 'nitro.json') // for backwards compatibility const defaultOutput = resolve(rootDir, '.output', 'nitro.json') // for backwards compatibility

View File

@ -7,14 +7,15 @@ export default defineNuxtCommand({
usage: 'npx nuxi test [--dev] [--watch] [rootDir]', usage: 'npx nuxi test [--dev] [--watch] [rootDir]',
description: 'Run tests' description: 'Run tests'
}, },
async invoke (args) { async invoke (args, options = {}) {
process.env.NODE_ENV = process.env.NODE_ENV || 'test' process.env.NODE_ENV = process.env.NODE_ENV || 'test'
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
const { runTests } = await importTestUtils() const { runTests } = await importTestUtils()
await runTests({ await runTests({
rootDir, rootDir,
dev: !!args.dev, dev: !!args.dev,
watch: !!args.watch watch: !!args.watch,
...(options || {})
}) })
if (args.watch) { if (args.watch) {

View File

@ -12,7 +12,7 @@ export default defineNuxtCommand({
usage: 'npx nuxi typecheck [--log-level] [rootDir]', usage: 'npx nuxi typecheck [--log-level] [rootDir]',
description: 'Runs `vue-tsc` to check types throughout your app.' description: 'Runs `vue-tsc` to check types throughout your app.'
}, },
async invoke (args) { async invoke (args, options = {}) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production' process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
@ -21,7 +21,8 @@ export default defineNuxtCommand({
rootDir, rootDir,
overrides: { overrides: {
_prepare: true, _prepare: true,
logLevel: args['log-level'] logLevel: args['log-level'],
...(options?.overrides || {})
} }
}) })