first commit
This commit is contained in:
95
cli/cli.ts
Normal file
95
cli/cli.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import { resolve } from 'node:path'
|
||||
import { defineCommand, runMain } from 'citty'
|
||||
import type { ArgsDef } from 'citty'
|
||||
import { getNuxtConfig } from './setup'
|
||||
import type { CLIOptions } from './types'
|
||||
|
||||
export function createCLI(opts: CLIOptions) {
|
||||
const sharedArgs: ArgsDef = {
|
||||
dir: {
|
||||
type: 'positional',
|
||||
description: 'Docs directory',
|
||||
required: true,
|
||||
default: '.',
|
||||
},
|
||||
}
|
||||
|
||||
const init = defineCommand({
|
||||
meta: {
|
||||
name: 'init',
|
||||
description: 'Initialize a fresh Docus project',
|
||||
},
|
||||
args: { ...sharedArgs },
|
||||
async setup({ args }) {
|
||||
const dir = resolve(args.dir as string)
|
||||
|
||||
const { runCommand } = await import('nuxi')
|
||||
await runCommand('init', [dir, '-t', 'gh:nuxtlabs/docus/.starter', dir])
|
||||
},
|
||||
})
|
||||
|
||||
const dev = defineCommand({
|
||||
meta: {
|
||||
name: 'dev',
|
||||
description: 'Start docs in development mode',
|
||||
},
|
||||
args: { ...sharedArgs },
|
||||
async setup({ args }) {
|
||||
const dir = resolve(args.dir as string)
|
||||
const nuxtConfig = await getNuxtConfig(dir, {
|
||||
...opts.setup,
|
||||
dev: true,
|
||||
})
|
||||
|
||||
const { runCommand } = await import('nuxi')
|
||||
await runCommand('dev', [dir, '--no-fork', '--port', process.env.PORT || '4000'], { overrides: nuxtConfig })
|
||||
},
|
||||
})
|
||||
|
||||
const prepare = defineCommand({
|
||||
meta: {
|
||||
name: 'prepare',
|
||||
description: 'Prepare docs for development or production',
|
||||
},
|
||||
args: { ...sharedArgs },
|
||||
async setup({ args }) {
|
||||
const dir = resolve(args.dir as string)
|
||||
const nuxtConfig = await getNuxtConfig(dir, opts.setup)
|
||||
|
||||
const { runCommand } = await import('nuxi')
|
||||
await runCommand('prepare', [dir], { overrides: nuxtConfig })
|
||||
},
|
||||
})
|
||||
|
||||
const build = defineCommand({
|
||||
meta: {
|
||||
name: 'build',
|
||||
description: 'Build docs for production',
|
||||
},
|
||||
args: { ...sharedArgs },
|
||||
async setup({ args }) {
|
||||
const dir = resolve(args.dir as string)
|
||||
const nuxtConfig = await getNuxtConfig(dir, opts.setup)
|
||||
|
||||
const { runCommand } = await import('nuxi')
|
||||
await runCommand('build', [dir], { overrides: nuxtConfig })
|
||||
},
|
||||
})
|
||||
|
||||
const main = defineCommand({
|
||||
meta: {
|
||||
name: opts.name,
|
||||
description: opts.description,
|
||||
},
|
||||
subCommands: {
|
||||
init,
|
||||
dev,
|
||||
prepare,
|
||||
build,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
runMain: () => runMain(main),
|
||||
}
|
||||
}
|
16
cli/main.ts
Executable file
16
cli/main.ts
Executable file
@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import * as dotenv from 'dotenv'
|
||||
import { createCLI } from './cli'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const cli = createCLI({
|
||||
name: 'Docus',
|
||||
description: 'Docus Docs CLI',
|
||||
setup: {
|
||||
defaults: {},
|
||||
},
|
||||
})
|
||||
|
||||
cli.runMain()
|
38
cli/setup.ts
Normal file
38
cli/setup.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { resolve } from 'node:path'
|
||||
import type { NuxtConfig } from 'nuxt/config'
|
||||
import type { DocsOptions } from './types'
|
||||
|
||||
declare global {
|
||||
const __DOCS_DIR__: string
|
||||
}
|
||||
|
||||
const appDir = fileURLToPath(new URL('../app', import.meta.url))
|
||||
|
||||
const pkgDir = fileURLToPath(new URL('..', import.meta.url))
|
||||
|
||||
export async function getNuxtConfig(dir: string, _opts: DocsOptions = {}) {
|
||||
const fixLayers = (_, nuxt) => {
|
||||
const hasDocsDir = nuxt.options._layers.some(layer => layer.cwd === dir)
|
||||
if (!hasDocsDir) {
|
||||
nuxt.options._layers.unshift({
|
||||
cwd: dir,
|
||||
config: {
|
||||
rootDir: dir,
|
||||
srcDir: dir,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-expect-error __DOCS_DIR__ is not defined
|
||||
global.__DOCS_DIR__ = resolve(dir, 'content')
|
||||
|
||||
// Prepare loadNuxt overrides
|
||||
return {
|
||||
compatibilityDate: '2025-06-17',
|
||||
extends: [appDir],
|
||||
modulesDir: [resolve(pkgDir, 'node_modules'), resolve(appDir, 'node_modules')],
|
||||
modules: [fixLayers],
|
||||
} as NuxtConfig
|
||||
}
|
23
cli/types.ts
Normal file
23
cli/types.ts
Normal file
@ -0,0 +1,23 @@
|
||||
export interface CLIOptions {
|
||||
name: string
|
||||
description: string
|
||||
setup: DocsOptions
|
||||
}
|
||||
|
||||
export interface DocsOptions {
|
||||
dev?: boolean
|
||||
defaults?: {
|
||||
// Module name
|
||||
name?: string
|
||||
// Module description
|
||||
description?: string
|
||||
// Docs directory
|
||||
dir?: string
|
||||
// Website URL
|
||||
url?: string
|
||||
// GitHub repository
|
||||
github?: string
|
||||
// GitHub branch
|
||||
branch?: string
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user