From 29f29302cecebf210c24f26f7b9a304f9299f4f7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 10 May 2023 14:45:52 +0100 Subject: [PATCH] feat(nuxi): allow passing overrides to other nuxi commands (#20760) --- packages/nuxi/src/commands/analyze.ts | 7 ++++--- packages/nuxi/src/commands/build-module.ts | 3 ++- packages/nuxi/src/commands/build.ts | 5 +++-- packages/nuxi/src/commands/generate.ts | 4 ++-- packages/nuxi/src/commands/prepare.ts | 5 +++-- packages/nuxi/src/commands/preview.ts | 4 ++-- packages/nuxi/src/commands/test.ts | 5 +++-- packages/nuxi/src/commands/typecheck.ts | 5 +++-- 8 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/nuxi/src/commands/analyze.ts b/packages/nuxi/src/commands/analyze.ts index 09987c5324da..36323082d6b1 100644 --- a/packages/nuxi/src/commands/analyze.ts +++ b/packages/nuxi/src/commands/analyze.ts @@ -3,6 +3,7 @@ import { join, resolve } from 'pathe' import { createApp, eventHandler, lazyEventHandler, toNodeListener } from 'h3' import { listen } from 'listhen' import type { NuxtAnalyzeMeta } from '@nuxt/schema' +import { defu } from 'defu' import { loadKit } from '../utils/kit' import { clearDir } from '../utils/fs' import { overrideEnv } from '../utils/env' @@ -14,7 +15,7 @@ export default defineNuxtCommand({ usage: 'npx nuxi analyze [--log-level] [--name] [--no-serve] [rootDir]', description: 'Build nuxt and analyze production bundle (experimental)' }, - async invoke (args) { + async invoke (args, options = {}) { overrideEnv('production') const name = args.name || 'default' @@ -31,7 +32,7 @@ export default defineNuxtCommand({ const nuxt = await loadNuxt({ rootDir, - overrides: { + overrides: defu(options.overrides, { build: { analyze: true }, @@ -43,7 +44,7 @@ export default defineNuxtCommand({ } }, logLevel: args['log-level'] - } + }) }) analyzeDir = nuxt.options.analyzeDir diff --git a/packages/nuxi/src/commands/build-module.ts b/packages/nuxi/src/commands/build-module.ts index e13e4cd67569..9ceebbf6f9fa 100644 --- a/packages/nuxi/src/commands/build-module.ts +++ b/packages/nuxi/src/commands/build-module.ts @@ -18,7 +18,8 @@ export default defineNuxtCommand({ const hasLocal = await tryResolveModule(`${MODULE_BUILDER_PKG}/package.json`, rootDir) const execArgs = Object.entries({ - '--stub': args.stub + '--stub': args.stub, + '--prepare': args.prepare }).filter(([, value]) => value).map(([key]) => key) let cmd = 'nuxt-module-build' diff --git a/packages/nuxi/src/commands/build.ts b/packages/nuxi/src/commands/build.ts index a582d1209d7c..7cd50c3b3d5e 100644 --- a/packages/nuxi/src/commands/build.ts +++ b/packages/nuxi/src/commands/build.ts @@ -13,7 +13,7 @@ export default defineNuxtCommand({ usage: 'npx nuxi build [--prerender] [--dotenv] [--log-level] [rootDir]', description: 'Build nuxt for production deployment' }, - async invoke (args) { + async invoke (args, options = {}) { overrideEnv('production') const rootDir = resolve(args._[0] || '.') @@ -29,7 +29,8 @@ export default defineNuxtCommand({ }, overrides: { logLevel: args['log-level'], - _generate: args.prerender + _generate: args.prerender, + ...(options?.overrides || {}) } }) diff --git a/packages/nuxi/src/commands/generate.ts b/packages/nuxi/src/commands/generate.ts index cb344e878aaa..4cee6f840db6 100644 --- a/packages/nuxi/src/commands/generate.ts +++ b/packages/nuxi/src/commands/generate.ts @@ -7,8 +7,8 @@ export default defineNuxtCommand({ usage: 'npx nuxi generate [rootDir] [--dotenv]', description: 'Build Nuxt and prerender static routes' }, - async invoke (args) { + async invoke (args, options = {}) { args.prerender = true - await buildCommand.invoke(args) + await buildCommand.invoke(args, options) } }) diff --git a/packages/nuxi/src/commands/prepare.ts b/packages/nuxi/src/commands/prepare.ts index 43cd2f05612c..026df3899332 100644 --- a/packages/nuxi/src/commands/prepare.ts +++ b/packages/nuxi/src/commands/prepare.ts @@ -11,7 +11,7 @@ export default defineNuxtCommand({ usage: 'npx nuxi prepare [--log-level] [rootDir]', description: 'Prepare nuxt for development/build' }, - async invoke (args) { + async invoke (args, options = {}) { process.env.NODE_ENV = process.env.NODE_ENV || 'production' const rootDir = resolve(args._[0] || '.') @@ -20,7 +20,8 @@ export default defineNuxtCommand({ rootDir, overrides: { _prepare: true, - logLevel: args['log-level'] + logLevel: args['log-level'], + ...(options.overrides || {}) } }) await clearBuildDir(nuxt.options.buildDir) diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index de176b058d2a..06559b1215eb 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -14,11 +14,11 @@ export default defineNuxtCommand({ usage: 'npx nuxi preview|start [--dotenv] [rootDir]', 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' const rootDir = resolve(args._[0] || '.') 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 defaultOutput = resolve(rootDir, '.output', 'nitro.json') // for backwards compatibility diff --git a/packages/nuxi/src/commands/test.ts b/packages/nuxi/src/commands/test.ts index 1761dc634f2f..3a96b832d409 100644 --- a/packages/nuxi/src/commands/test.ts +++ b/packages/nuxi/src/commands/test.ts @@ -7,14 +7,15 @@ export default defineNuxtCommand({ usage: 'npx nuxi test [--dev] [--watch] [rootDir]', description: 'Run tests' }, - async invoke (args) { + async invoke (args, options = {}) { process.env.NODE_ENV = process.env.NODE_ENV || 'test' const rootDir = resolve(args._[0] || '.') const { runTests } = await importTestUtils() await runTests({ rootDir, dev: !!args.dev, - watch: !!args.watch + watch: !!args.watch, + ...(options || {}) }) if (args.watch) { diff --git a/packages/nuxi/src/commands/typecheck.ts b/packages/nuxi/src/commands/typecheck.ts index afa901da5b48..aa21e7d1e175 100644 --- a/packages/nuxi/src/commands/typecheck.ts +++ b/packages/nuxi/src/commands/typecheck.ts @@ -12,7 +12,7 @@ export default defineNuxtCommand({ usage: 'npx nuxi typecheck [--log-level] [rootDir]', 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' const rootDir = resolve(args._[0] || '.') @@ -21,7 +21,8 @@ export default defineNuxtCommand({ rootDir, overrides: { _prepare: true, - logLevel: args['log-level'] + logLevel: args['log-level'], + ...(options?.overrides || {}) } })