Skip to content
Open
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
19 changes: 19 additions & 0 deletions src/commands/_shared.ts
Original file line number Diff line number Diff line change
@@ -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<string, ArgDef>

export const resolveCwdArg = (args: { cwd?: string, rootDir?: string }) => args.cwd || args.rootDir || '.'
24 changes: 11 additions & 13 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,49 @@ import { defineCommand } from 'citty'
import { convertCompilerOptionsFromJson } from 'typescript'

import { name, version } from '../../package.json'
import { resolveCwdArg, sharedArgs } from './_shared'

export default defineCommand({
meta: {
name: 'build',
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: [
Expand Down
13 changes: 3 additions & 10 deletions src/commands/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import type { NuxtConfig } from '@nuxt/schema'
import { defineCommand } from 'citty'
import { resolve } from 'pathe'
import { resolveCwdArg, sharedArgs } from './_shared'

export default defineCommand({
meta: {
name: 'prepare',
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: {
Expand Down
Loading