-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
273 lines (239 loc) · 7.53 KB
/
index.ts
File metadata and controls
273 lines (239 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env node
/**
* Adopted from @see https://github.com/web-infra-dev/rspress/blob/main/packages/cli/src/index.ts
*/
import module from 'node:module'
import path from 'node:path'
import { logger } from '@rsbuild/core'
import { type FSWatcher, watch } from 'chokidar'
import { type Command, program } from 'commander'
import { build, dev, serve } from 'rspress/core'
import { green } from 'yoctocolors'
import type { GlobalCliOptions, ServeOptions } from '../types.js'
import { setNodeEnv } from '../utils/index.js'
import { CWD, DEFAULT_CONFIGS, SITES_FILE } from './constants.js'
import { exportCommand } from './export.js'
import { parseBoolean, parseBooleanOrString } from './helpers.js'
import { lintCommand } from './lint.js'
import { loadConfig } from './load-config.js'
import { newCommand } from './new.js'
import { translateCommand } from './translate.js'
const META_FILE = '_meta.json'
const CONFIG_FILES = [...DEFAULT_CONFIGS, SITES_FILE]
const cjsRequire = module.createRequire(import.meta.url)
const pkg = cjsRequire('../../package.json') as {
description: string
version: string
}
program
.name('doom')
.description(pkg.description)
.version(pkg.version)
.configureHelp({
showGlobalOptions: true,
})
.option('-c, --config <config>', 'Specify the path to the config file')
.option(
'-v <version>',
'Specify the version of the documentation, can also be `unversioned` or `unversioned-x.y`',
)
.option('-b, --base <base>', 'Override the base of the documentation')
.option(
'-p, --prefix <prefix>',
'Specify the prefix of the documentation base',
)
.option(
'-f, --force [boolean]',
`Force to
1. fetch latest reference remotes or scaffolding templates, otherwise use local cache
2. translate ignore hash equality check and original text`,
parseBoolean,
false,
)
.option(
'-i, --ignore [boolean]',
'Ignore internal routes',
parseBoolean,
false,
)
.option(
'-d, --download [boolean]',
'Display download pdf link on nav bar',
parseBoolean,
false,
)
.option(
'-e, --export [boolean]',
'Run or build in exporting PDF mode, `apis/**` and `*/apis/**` routes will be ignored automatically',
parseBoolean,
false,
)
.option(
'-I, --include <language...>',
'Include **only** the specific language(s), `en ru` for example',
)
.option(
'-E, --exclude <language...>',
'Include all languages except the specific language(s), `ru` for example',
)
.option(
'-o, --out-dir <path>',
'Override the `outDir` defined in the config file or the default `dist/{base}/{version}`, the resulting path will be `dist/{outDir}/{version}`',
)
.option(
'-r, --redirect <enum>',
'Whether to redirect to the locale closest to `navigator.language` when the user visits the site, could be `auto`, `never` or `only-default-lang`',
'only-default-lang',
)
.option(
'-R, --edit-repo [boolean|url]',
'Whether to enable or override the `editRepoBaseUrl` config feature, `https://github.com/` prefix could be omitted',
parseBooleanOrString,
false,
)
.option(
'-a, --algolia [boolean|alauda]',
'Whether to enable or use the alauda (docs.alauda.io) preset for Algolia search',
parseBooleanOrString,
false,
)
.option(
'-S, --site-url',
'Whether to enable the siteUrl for sitemap generation',
parseBoolean,
false,
)
.option('-n, --no-open', 'Do not open the browser after starting the server')
.command('dev', { isDefault: true })
.description('Start the development server')
.argument('[root]', 'Root directory of the documentation')
.option('-H, --host [host]', 'Dev server host name')
.option('-P, --port [port]', 'Dev server port number')
.option(
'-l, --lazy [boolean]',
'Whether to enable `lazyCompilation` which could improve the compilation performance',
parseBoolean,
true,
)
.option('--no-lazy', 'Do not enable `lazyCompilation`')
.action(async function (this: Command, root?: string) {
setNodeEnv('development')
let devServer: Awaited<ReturnType<typeof dev>> | undefined
let cliWatcher: FSWatcher
const { port, host, ...globalOptions } = this.optsWithGlobals<
ServeOptions & GlobalCliOptions
>()
const startDevServer = async () => {
const { config, configFilePath } = await loadConfig(root, globalOptions)
const docDirectory = config.root!
try {
devServer = await dev({
config,
configFilePath,
appDirectory: CWD,
docDirectory,
extraBuilderConfig: {
server: { host, port },
},
})
} catch (err) {
logger.error(err)
devServer = undefined
}
cliWatcher = watch(
configFilePath
? [configFilePath, config.i18nSourcePath!, docDirectory, SITES_FILE]
: [...CONFIG_FILES, docDirectory],
{
ignoreInitial: true,
ignored: ['**/node_modules/**', '**/.git/**', '**/.DS_Store/**'],
cwd: CWD,
},
)
let isRestarting = false
cliWatcher.on('all', async (eventName, filepath) => {
console.log(eventName, filepath)
if (
eventName === 'add' ||
eventName === 'unlink' ||
(eventName === 'change' &&
(CONFIG_FILES.includes(path.basename(filepath)) ||
path.basename(filepath) === META_FILE))
) {
if (isRestarting) {
return
}
isRestarting = true
console.log(
`\n✨ ${eventName} ${green(
path.relative(CWD, filepath),
)}, dev server will restart...\n`,
)
await devServer?.close()
await cliWatcher.close()
await startDevServer()
isRestarting = false
}
})
}
await startDevServer()
const exitProcess = async () => {
try {
await devServer?.close()
await cliWatcher.close()
} finally {
process.exit(0)
}
}
process.on('SIGINT', exitProcess)
process.on('SIGTERM', exitProcess)
})
program
.command('build')
.description('Build the documentation')
.argument('[root]', 'Root directory of the documentation')
.action(async function (root?: string) {
setNodeEnv('production')
const { config, configFilePath } = await loadConfig(
root,
this.optsWithGlobals<GlobalCliOptions>(),
)
const docDirectory = config.root!
const runBuild = () =>
build({
config,
configFilePath,
docDirectory,
})
await runBuild()
if (process.env.__DOOM_REBUILD__ === 'true') {
logger.info('Rebuilding...')
await runBuild()
}
})
program
.command('preview')
.alias('serve')
.description('Preview the built documentation')
.argument('[root]', 'Root directory of the documentation')
.option('-H, --host [host]', 'Serve host name')
.option('-P, --port [port]', 'Serve port number', '4173')
.action(async function (root?: string) {
setNodeEnv('production')
const { port, host, ...globalOptions } = this.optsWithGlobals<
ServeOptions & GlobalCliOptions
>()
const { config, configFilePath } = await loadConfig(root, globalOptions)
await serve({ config, configFilePath, host, port })
})
program.addCommand(newCommand)
program.addCommand(translateCommand)
program.addCommand(exportCommand)
program.addCommand(lintCommand)
program.parseAsync().catch((err: unknown) => {
if (err instanceof Error && err.name === 'ExitPromptError') {
return
}
logger.error(err)
process.exit(1)
})