-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
docusaurus.mjs
executable file
·288 lines (262 loc) · 8.34 KB
/
docusaurus.mjs
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env node
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @ts-check
import {inspect} from 'node:util';
import logger from '@docusaurus/logger';
import cli from 'commander';
import {DOCUSAURUS_VERSION} from '@docusaurus/utils';
import {
build,
swizzle,
deploy,
start,
externalCommand,
serve,
clear,
writeTranslations,
writeHeadingIds,
} from '../lib/index.js';
import beforeCli from './beforeCli.mjs';
// Env variables are initialized to dev, but can be overridden by each command
// For example, "docusaurus build" overrides them to "production"
// See also https://github.com/facebook/docusaurus/issues/8599
process.env.BABEL_ENV ??= 'development';
process.env.NODE_ENV ??= 'development';
await beforeCli();
cli.version(DOCUSAURUS_VERSION).usage('<command> [options]');
cli
.command('build [siteDir]')
.description('Build website.')
.option(
'--dev',
'Builds the website in dev mode, including full React error messages.',
)
.option(
'--bundle-analyzer',
'visualize size of webpack output files with an interactive zoomable tree map (default: false)',
)
.option(
'--out-dir <dir>',
'the full path for the new output directory, relative to the current workspace (default: build)',
)
.option(
'--config <config>',
'path to docusaurus config file (default: `[siteDir]/docusaurus.config.js`)',
)
.option(
'-l, --locale <locale>',
'build the site in a specified locale. Build all known locales otherwise',
)
.option(
'--no-minify',
'build website without minimizing JS bundles (default: false)',
)
.action(build);
cli
.command('swizzle [themeName] [componentName] [siteDir]')
.description(
'Wraps or ejects the original theme files into website folder for customization.',
)
.option(
'-w, --wrap',
'Creates a wrapper around the original theme component.\nAllows rendering other components before/after the original theme component.',
)
.option(
'-e, --eject',
'Ejects the full source code of the original theme component.\nAllows overriding the original component entirely with your own UI and logic.',
)
.option(
'-l, --list',
'only list the available themes/components without further prompting (default: false)',
)
.option(
'-t, --typescript',
'copy TypeScript theme files when possible (default: false)',
)
.option(
'-j, --javascript',
'copy JavaScript theme files when possible (default: false)',
)
.option('--danger', 'enable swizzle for unsafe component of themes')
.option(
'--config <config>',
'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)',
)
.action(swizzle);
cli
.command('deploy [siteDir]')
.description('Deploy website to GitHub pages.')
.option(
'-l, --locale <locale>',
'deploy the site in a specified locale. Deploy all known locales otherwise',
)
.option(
'--out-dir <dir>',
'the full path for the new output directory, relative to the current workspace (default: build)',
)
.option(
'--config <config>',
'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)',
)
.option(
'--skip-build',
'skip building website before deploy it (default: false)',
)
.option(
'--target-dir <dir>',
'path to the target directory to deploy to (default: `.`)',
)
.action(deploy);
/**
* @param {string | undefined} value
* @returns {boolean | number}
*/
function normalizePollValue(value) {
if (value === undefined || value === '') {
return false;
}
const parsedIntValue = Number.parseInt(value, 10);
if (!Number.isNaN(parsedIntValue)) {
return parsedIntValue;
}
return value === 'true';
}
cli
.command('start [siteDir]')
.description('Start the development server.')
.option('-p, --port <port>', 'use specified port (default: 3000)')
.option('-h, --host <host>', 'use specified host (default: localhost)')
.option('-l, --locale <locale>', 'use specified site locale')
.option(
'--hot-only',
'do not fallback to page refresh if hot reload fails (default: false)',
)
.option(
'--config <config>',
'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)',
)
.option('--no-open', 'do not open page in the browser (default: false)')
.option(
'--poll [interval]',
'use polling rather than watching for reload (default: false). Can specify a poll interval in milliseconds',
normalizePollValue,
)
.option(
'--no-minify',
'build website without minimizing JS bundles (default: false)',
)
.action(start);
cli
.command('serve [siteDir]')
.description('Serve website locally.')
.option(
'--dir <dir>',
'the full path for the new output directory, relative to the current workspace (default: build)',
)
.option(
'--config <config>',
'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)',
)
.option('-p, --port <port>', 'use specified port (default: 3000)')
.option('--build', 'build website before serving (default: false)')
.option('-h, --host <host>', 'use specified host (default: localhost)')
.option(
'--no-open',
'do not open page in the browser (default: false, or true in CI)',
)
.action(serve);
cli
.command('clear [siteDir]')
.description('Remove build artifacts.')
.action(clear);
cli
.command('write-translations [siteDir]')
.description('Extract required translations of your site.')
.option(
'-l, --locale <locale>',
'the locale folder to write the translations.\n"--locale fr" will write translations in the ./i18n/fr folder.',
)
.option(
'--override',
'By default, we only append missing translation messages to existing translation files. This option allows to override existing translation messages. Make sure to commit or backup your existing translations, as they may be overridden. (default: false)',
)
.option(
'--config <config>',
'path to Docusaurus config file (default:`[siteDir]/docusaurus.config.js`)',
)
.option(
'--messagePrefix <messagePrefix>',
'Allows to init new written messages with a given prefix. This might help you to highlight untranslated message by making them stand out in the UI (default: "")',
)
.action(writeTranslations);
cli
.command('write-heading-ids [siteDir] [files...]')
.description('Generate heading ids in Markdown content.')
.option(
'--maintain-case',
"keep the headings' casing, otherwise make all lowercase (default: false)",
)
.option('--overwrite', 'overwrite existing heading IDs (default: false)')
.action(writeHeadingIds);
cli.arguments('<command>').action((cmd) => {
cli.outputHelp();
logger.error` Unknown command name=${cmd}.`;
});
// === The above is the commander configuration ===
// They don't start any code execution yet until cli.parse() is called below
/**
* @param {string | undefined} command
*/
function isInternalCommand(command) {
return (
command &&
[
'start',
'build',
'swizzle',
'deploy',
'serve',
'clear',
'write-translations',
'write-heading-ids',
].includes(command)
);
}
// process.argv always looks like this:
// [
// '/path/to/node',
// '/path/to/docusaurus.mjs',
// '<subcommand>',
// ...subcommandArgs
// ]
// There is no subcommand
// TODO: can we use commander to handle this case?
if (process.argv.length < 3 || process.argv[2]?.startsWith('--')) {
cli.outputHelp();
process.exit(1);
}
// There is an unrecognized subcommand
// Let plugins extend the CLI before parsing
if (!isInternalCommand(process.argv[2])) {
// TODO: in this step, we must assume default site structure because there's
// no way to know the siteDir/config yet. Maybe the root cli should be
// responsible for parsing these arguments?
// https://github.com/facebook/docusaurus/issues/8903
await externalCommand(cli);
}
cli.parse(process.argv);
process.on('unhandledRejection', (err) => {
console.log('');
// We need to use inspect with increased depth to log the full causal chain
// By default Node logging has depth=2
// see also https://github.com/nodejs/node/issues/51637
logger.error(inspect(err, {depth: Infinity}));
logger.info`Docusaurus version: number=${DOCUSAURUS_VERSION}
Node version: number=${process.version}`;
process.exit(1);
});