diff --git a/src/commands/_shared.ts b/src/commands/_shared.ts new file mode 100644 index 00000000..80ecb781 --- /dev/null +++ b/src/commands/_shared.ts @@ -0,0 +1,19 @@ +import type { ArgDef } from 'citty' + +export const sharedArgs = { + // cwd falls back to rootDir's default (indirect default) + cwd: { + type: 'string', + valueHint: 'directory', + description: 'Specify the working directory, this takes precedence over ROOTDIR (default: `.`)', + default: undefined, + }, + rootDir: { + type: 'positional', + description: 'Specifies the working directory (default: `.`)', + required: false, + default: '.', + }, +} as const satisfies Record + +export const resolveCwdArg = (args: { cwd?: string, rootDir?: string }) => args.cwd || args.rootDir || '.' diff --git a/src/commands/build.ts b/src/commands/build.ts index 36bc5941..031c4b0f 100644 --- a/src/commands/build.ts +++ b/src/commands/build.ts @@ -16,6 +16,7 @@ import { defineCommand } from 'citty' import { convertCompilerOptionsFromJson } from 'typescript' import { name, version } from '../../package.json' +import { resolveCwdArg, sharedArgs } from './_shared' export default defineCommand({ meta: { @@ -23,44 +24,41 @@ export default defineCommand({ description: 'Build module for distribution', }, args: { - cwd: { - type: 'string', - description: 'Current working directory', - }, - rootDir: { - type: 'positional', - description: 'Root directory', - required: false, - }, + ...sharedArgs, outDir: { type: 'string', + default: 'dist', + description: 'Build directory', }, sourcemap: { type: 'boolean', + default: false, + description: 'Generate sourcemaps', }, stub: { type: 'boolean', + default: false, + description: 'Stub dist instead of actually building it for development', }, }, async run(context) { const { build } = await import('unbuild') - const cwd = resolve(context.args.cwd || context.args.rootDir || '.') + const cwd = resolve(resolveCwdArg(context.args)) const jiti = createJiti(cwd) - const outDir = context.args.outDir || 'dist' await build(cwd, false, { declaration: 'node16', sourcemap: context.args.sourcemap, stub: context.args.stub, stubOptions: { absoluteJitiPath: true }, - outDir, + outDir: context.args.outDir, entries: [ 'src/module', { input: 'src/runtime/', - outDir: `${outDir}/runtime`, + outDir: `${context.args.outDir}/runtime`, addRelativeDeclarationExtensions: true, ext: 'js', pattern: [ diff --git a/src/commands/prepare.ts b/src/commands/prepare.ts index e6527681..9ca8ba73 100644 --- a/src/commands/prepare.ts +++ b/src/commands/prepare.ts @@ -1,6 +1,7 @@ import type { NuxtConfig } from '@nuxt/schema' import { defineCommand } from 'citty' import { resolve } from 'pathe' +import { resolveCwdArg, sharedArgs } from './_shared' export default defineCommand({ meta: { @@ -8,20 +9,12 @@ export default defineCommand({ description: 'Prepare @nuxt/module-builder environment by writing types and stubs', }, args: { - cwd: { - type: 'string', - description: 'Current working directory', - }, - rootDir: { - type: 'positional', - description: 'Root directory', - required: false, - }, + ...sharedArgs, }, async run(context) { const { runCommand } = await import('@nuxt/cli') - const cwd = resolve(context.args.cwd || context.args.rootDir || '.') + const cwd = resolve(resolveCwdArg(context.args)) return runCommand('prepare', [cwd], { overrides: {