diff --git a/src/commands/_shared.ts b/src/commands/_shared.ts index 936c4cf2..4bb46cc3 100644 --- a/src/commands/_shared.ts +++ b/src/commands/_shared.ts @@ -1,32 +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 + +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 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 export const dotEnvArgs = { dotenv: { type: 'string', - description: 'Path to .env file', + description: 'Path to `.env` file to load, relative to the root directory', }, -} as const +} as const satisfies Record 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 diff --git a/src/commands/add.ts b/src/commands/add.ts index b583829f..3db6bc48 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -4,7 +4,7 @@ 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: { @@ -12,7 +12,8 @@ export default defineCommand({ description: 'Create a new template file.', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, force: { type: 'boolean', description: 'Override existing file', @@ -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] diff --git a/src/commands/analyze.ts b/src/commands/analyze.ts index e57a938f..b0150ea7 100644 --- a/src/commands/analyze.ts +++ b/src/commands/analyze.ts @@ -8,7 +8,7 @@ import { defineCommand } from 'citty' import { loadKit } from '../utils/kit' import { clearDir } from '../utils/fs' import { overrideEnv } from '../utils/env' -import { sharedArgs, legacyRootDirArgs, dotEnvArgs } from './_shared' +import { legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -16,7 +16,8 @@ export default defineCommand({ description: 'Build nuxt and analyze production bundle (experimental)', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...legacyRootDirArgs, ...dotEnvArgs, name: { @@ -33,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, '_') diff --git a/src/commands/build-module.ts b/src/commands/build-module.ts index f4411ba6..fdebb662 100644 --- a/src/commands/build-module.ts +++ b/src/commands/build-module.ts @@ -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' @@ -13,7 +13,8 @@ export default defineCommand({ description: `Helper command for using ${MODULE_BUILDER_PKG}`, }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...legacyRootDirArgs, stub: { type: 'boolean', @@ -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`, diff --git a/src/commands/build.ts b/src/commands/build.ts index 4703a6a4..9d940838 100644 --- a/src/commands/build.ts +++ b/src/commands/build.ts @@ -6,7 +6,7 @@ import { loadKit } from '../utils/kit' import { clearBuildDir } from '../utils/fs' import { overrideEnv } from '../utils/env' import { showVersions } from '../utils/banner' -import { sharedArgs, envNameArgs, legacyRootDirArgs, dotEnvArgs } from './_shared' +import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -14,7 +14,8 @@ export default defineCommand({ description: 'Build Nuxt for production deployment', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, prerender: { type: 'boolean', description: 'Build Nuxt and prerender static routes', @@ -30,7 +31,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) showVersions(cwd) diff --git a/src/commands/cleanup.ts b/src/commands/cleanup.ts index 5c564831..cfa5e5ce 100644 --- a/src/commands/cleanup.ts +++ b/src/commands/cleanup.ts @@ -3,7 +3,7 @@ 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: { @@ -11,11 +11,11 @@ export default defineCommand({ 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, overrides: { dev: true } }) await cleanupNuxtDirs(nuxtOptions.rootDir, nuxtOptions.buildDir) diff --git a/src/commands/dev-child.ts b/src/commands/dev-child.ts index f1079db4..1cfb1902 100644 --- a/src/commands/dev-child.ts +++ b/src/commands/dev-child.ts @@ -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: { @@ -14,7 +14,8 @@ export default defineCommand({ 'Run Nuxt development server (internal command to start child process)', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, }, @@ -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 diff --git a/src/commands/dev.ts b/src/commands/dev.ts index c6918731..afc99b40 100644 --- a/src/commands/dev.ts +++ b/src/commands/dev.ts @@ -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, dotEnvArgs } from './_shared' +import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared' const forkSupported = !isBun && !isTest @@ -27,7 +27,8 @@ const command = defineCommand({ description: 'Run Nuxt development server', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, ...getListhenArgs(), @@ -45,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 }) diff --git a/src/commands/devtools.ts b/src/commands/devtools.ts index ecda8ffc..3fc9d72c 100644 --- a/src/commands/devtools.ts +++ b/src/commands/devtools.ts @@ -2,7 +2,7 @@ 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: { @@ -10,7 +10,7 @@ export default defineCommand({ description: 'Enable or disable devtools in a Nuxt project', }, args: { - ...sharedArgs, + ...cwdArgs, command: { type: 'positional', description: 'Command to run', @@ -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}\`.`) diff --git a/src/commands/generate.ts b/src/commands/generate.ts index 31145921..656e8d79 100644 --- a/src/commands/generate.ts +++ b/src/commands/generate.ts @@ -1,7 +1,7 @@ import { defineCommand } from 'citty' import buildCommand from './build' -import { sharedArgs, envNameArgs, legacyRootDirArgs, dotEnvArgs } from './_shared' +import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -9,7 +9,8 @@ export default defineCommand({ description: 'Build Nuxt and prerender all routes', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, ...dotEnvArgs, diff --git a/src/commands/info.ts b/src/commands/info.ts index 745229d5..4fed630b 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -17,7 +17,7 @@ import { import { findup } from '../utils/fs' import nuxiPkg from '../../package.json' -import { legacyRootDirArgs, sharedArgs } from './_shared' +import { cwdArgs, legacyRootDirArgs } from './_shared' export default defineCommand({ meta: { @@ -25,12 +25,12 @@ export default defineCommand({ description: 'Get information about Nuxt project', }, args: { - ...sharedArgs, + ...cwdArgs, ...legacyRootDirArgs, }, async run(ctx) { // Resolve rootDir - const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.') + const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) // Load Nuxt config const nuxtConfig = await getNuxtConfig(cwd) diff --git a/src/commands/init.ts b/src/commands/init.ts index bb792dc9..e96dac56 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -6,7 +6,7 @@ import { installDependencies } from 'nypm' import type { PackageManagerName } from 'nypm' import { defineCommand } from 'citty' -import { sharedArgs } from './_shared' +import { cwdArgs } from './_shared' const DEFAULT_REGISTRY = 'https://raw.githubusercontent.com/nuxt/starter/templates/templates' @@ -18,7 +18,7 @@ export default defineCommand({ description: 'Initialize a fresh project', }, args: { - ...sharedArgs, + ...cwdArgs, dir: { type: 'positional', description: 'Project directory', @@ -61,7 +61,7 @@ export default defineCommand({ }, }, async run(ctx) { - const cwd = resolve(ctx.args.cwd || '.') + const cwd = resolve(ctx.args.cwd) // Get template name const templateName = ctx.args.template || DEFAULT_TEMPLATE_NAME diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 4cc1b7f2..9730048a 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -11,7 +11,7 @@ import { $fetch } from 'ofetch' import { satisfies } from 'semver' import { updateConfig } from 'c12/update' import { colors } from 'consola/utils' -import { sharedArgs } from '../_shared' +import { cwdArgs, logLevelArgs } from '../_shared' import { runCommand } from '../../run' import { checkNuxtCompatibility, @@ -32,7 +32,8 @@ export default defineCommand({ description: 'Add Nuxt modules', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, moduleName: { type: 'positional', description: 'Module name', @@ -47,7 +48,7 @@ export default defineCommand({ }, }, async setup(ctx) { - const cwd = resolve(ctx.args.cwd || '.') + const cwd = resolve(ctx.args.cwd) const projectPkg = await getProjectPackage(cwd) if (!projectPkg.dependencies?.nuxt && !projectPkg.devDependencies?.nuxt) { @@ -124,7 +125,7 @@ export default defineCommand({ } // update the types for new module - const args = Object.entries(ctx.args).filter(([k]) => k in sharedArgs).map(([k, v]) => `--${k}=${v}`) + const args = Object.entries(ctx.args).filter(([k]) => k in cwdArgs || k in logLevelArgs).map(([k, v]) => `--${k}=${v}`) await runCommand('prepare', args) }, }) diff --git a/src/commands/module/search.ts b/src/commands/module/search.ts index 1513cdf5..7492e19c 100644 --- a/src/commands/module/search.ts +++ b/src/commands/module/search.ts @@ -3,7 +3,7 @@ import consola from 'consola' import Fuse from 'fuse.js' import { upperFirst, kebabCase } from 'scule' import { bold, green, magenta, cyan, gray, yellow } from 'colorette' -import { sharedArgs } from '../_shared' +import { cwdArgs } from '../_shared' import { fetchModules, checkNuxtCompatibility, getNuxtVersion } from './_utils' const { format: formatNumber } = Intl.NumberFormat('en-GB', { @@ -17,7 +17,7 @@ export default defineCommand({ description: 'Search in Nuxt modules', }, args: { - ...sharedArgs, + ...cwdArgs, query: { type: 'positional', description: 'keywords to search for', @@ -31,7 +31,7 @@ export default defineCommand({ }, }, async setup(ctx) { - const nuxtVersion = await getNuxtVersion(ctx.args.cwd || '.') + const nuxtVersion = await getNuxtVersion(ctx.args.cwd) return findModuleByKeywords(ctx.args._.join(' '), nuxtVersion) }, }) diff --git a/src/commands/prepare.ts b/src/commands/prepare.ts index 2d01368c..05d4ad6e 100644 --- a/src/commands/prepare.ts +++ b/src/commands/prepare.ts @@ -7,7 +7,7 @@ import { defineCommand } from 'citty' import { clearBuildDir } from '../utils/fs' import { loadKit } from '../utils/kit' -import { sharedArgs, envNameArgs, legacyRootDirArgs, dotEnvArgs } from './_shared' +import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -16,14 +16,15 @@ export default defineCommand({ }, args: { ...dotEnvArgs, - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, }, async run(ctx) { process.env.NODE_ENV = process.env.NODE_ENV || 'production' - const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.') + const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) const { loadNuxt, diff --git a/src/commands/preview.ts b/src/commands/preview.ts index 2c35a225..a8b9a94c 100644 --- a/src/commands/preview.ts +++ b/src/commands/preview.ts @@ -8,7 +8,7 @@ import { box, colors } from 'consola/utils' import { defineCommand } from 'citty' import { loadKit } from '../utils/kit' -import { sharedArgs, envNameArgs, legacyRootDirArgs, dotEnvArgs } from './_shared' +import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -16,7 +16,8 @@ export default defineCommand({ description: 'Launches Nitro server for local testing after `nuxi build`.', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...envNameArgs, ...legacyRootDirArgs, ...dotEnvArgs, @@ -24,7 +25,7 @@ export default defineCommand({ async run(ctx) { process.env.NODE_ENV = process.env.NODE_ENV || 'production' - const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.') + const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) const { loadNuxtConfig } = await loadKit(cwd) const config = await loadNuxtConfig({ diff --git a/src/commands/test.ts b/src/commands/test.ts index 583bc897..0405a60b 100644 --- a/src/commands/test.ts +++ b/src/commands/test.ts @@ -1,7 +1,7 @@ import { resolve } from 'pathe' import { defineCommand } from 'citty' -import { legacyRootDirArgs, sharedArgs } from './_shared' +import { cwdArgs, legacyRootDirArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -9,12 +9,9 @@ export default defineCommand({ description: 'Run tests', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...legacyRootDirArgs, - cwd: { - type: 'string', - description: 'Current working directory', - }, dev: { type: 'boolean', description: 'Run in dev mode', @@ -27,7 +24,7 @@ export default defineCommand({ async run(ctx) { process.env.NODE_ENV = process.env.NODE_ENV || 'test' - const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.') + const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) const { runTests } = await importTestUtils() await runTests({ diff --git a/src/commands/typecheck.ts b/src/commands/typecheck.ts index 73e4848f..604a8956 100644 --- a/src/commands/typecheck.ts +++ b/src/commands/typecheck.ts @@ -9,7 +9,7 @@ import { writeTypes as writeTypesLegacy } from '@nuxt/kit' import { tryResolveModule } from '../utils/esm' import { loadKit } from '../utils/kit' -import { legacyRootDirArgs, sharedArgs } from './_shared' +import { cwdArgs, legacyRootDirArgs, logLevelArgs } from './_shared' export default defineCommand({ meta: { @@ -17,13 +17,14 @@ export default defineCommand({ description: 'Runs `vue-tsc` to check types throughout your app.', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...legacyRootDirArgs, }, async run(ctx) { process.env.NODE_ENV = process.env.NODE_ENV || 'production' - const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.') + const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) const { loadNuxt, diff --git a/src/commands/upgrade.ts b/src/commands/upgrade.ts index a2e297b4..7be09e0e 100644 --- a/src/commands/upgrade.ts +++ b/src/commands/upgrade.ts @@ -14,7 +14,7 @@ import { rmRecursive, touchFile } from '../utils/fs' import { cleanupNuxtDirs, nuxtVersionToGitIdentifier } from '../utils/nuxt' import { loadKit } from '../utils/kit' -import { legacyRootDirArgs, sharedArgs } from './_shared' +import { cwdArgs, legacyRootDirArgs, logLevelArgs } from './_shared' async function getNuxtVersion(path: string): Promise { try { @@ -80,7 +80,8 @@ export default defineCommand({ description: 'Upgrade Nuxt', }, args: { - ...sharedArgs, + ...cwdArgs, + ...logLevelArgs, ...legacyRootDirArgs, force: { type: 'boolean', @@ -95,7 +96,7 @@ export default defineCommand({ }, }, async run(ctx) { - const cwd = resolve(ctx.args.cwd || ctx.args.rootDir || '.') + const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) // Check package manager const packageManager = getPackageManager(cwd)