Skip to content

refactor: shared command descriptions and --cwd default #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 11, 2024
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
33 changes: 24 additions & 9 deletions src/commands/_shared.ts
Original file line number Diff line number Diff line change
@@ -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<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',
description: 'Path to `.env` file to load, relative to the root directory',
},
} as const
} 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>
7 changes: 4 additions & 3 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
7 changes: 4 additions & 3 deletions src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ 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: {
name: 'analyze',
description: 'Build nuxt and analyze production bundle (experimental)',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...legacyRootDirArgs,
...dotEnvArgs,
name: {
Expand All @@ -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, '_')

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
7 changes: 4 additions & 3 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, dotEnvArgs } 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 @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/commands/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ 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, overrides: { dev: true } })
await cleanupNuxtDirs(nuxtOptions.rootDir, nuxtOptions.buildDir)
Expand Down
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
7 changes: 4 additions & 3 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, dotEnvArgs } from './_shared'
import { envNameArgs, legacyRootDirArgs, dotEnvArgs, cwdArgs, logLevelArgs } from './_shared'

const forkSupported = !isBun && !isTest

Expand All @@ -27,7 +27,8 @@ const command = defineCommand({
description: 'Run Nuxt development server',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...envNameArgs,
...legacyRootDirArgs,
...getListhenArgs(),
Expand All @@ -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 })

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
5 changes: 3 additions & 2 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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: {
name: 'generate',
description: 'Build Nuxt and prerender all routes',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
...envNameArgs,
...legacyRootDirArgs,
...dotEnvArgs,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ 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: {
name: 'info',
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)
Expand Down
6 changes: 3 additions & 3 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -18,7 +18,7 @@ export default defineCommand({
description: 'Initialize a fresh project',
},
args: {
...sharedArgs,
...cwdArgs,
dir: {
type: 'positional',
description: 'Project directory',
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,7 +32,8 @@ export default defineCommand({
description: 'Add Nuxt modules',
},
args: {
...sharedArgs,
...cwdArgs,
...logLevelArgs,
moduleName: {
type: 'positional',
description: 'Module name',
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
},
})
Expand Down
Loading
Loading