10
10
11
11
import { inspect } from 'node:util' ;
12
12
import { logger } from '@docusaurus/logger' ;
13
- import cli from 'commander' ;
14
13
import { DOCUSAURUS_VERSION } from '@docusaurus/utils' ;
15
- import {
16
- build ,
17
- swizzle ,
18
- deploy ,
19
- start ,
20
- externalCommand ,
21
- serve ,
22
- clear ,
23
- writeTranslations ,
24
- writeHeadingIds ,
25
- } from '../lib/index.js' ;
14
+ import { runCLI } from '../lib/index.js' ;
26
15
import beforeCli from './beforeCli.mjs' ;
27
16
28
17
// Env variables are initialized to dev, but can be overridden by each command
@@ -31,270 +20,28 @@ import beforeCli from './beforeCli.mjs';
31
20
process . env . BABEL_ENV ??= 'development' ;
32
21
process . env . NODE_ENV ??= 'development' ;
33
22
34
- await beforeCli ( ) ;
35
-
36
- /**
37
- * @param {string } locale
38
- * @param {string[] } locales
39
- * @returns {string[] }
40
- */
41
- function concatLocaleOptions ( locale , locales = [ ] ) {
42
- return locales . concat ( locale ) ;
43
- }
44
-
45
- cli . version ( DOCUSAURUS_VERSION ) . usage ( '<command> [options]' ) ;
46
-
47
- cli
48
- . command ( 'build [siteDir]' )
49
- . description ( 'Build website.' )
50
- . option (
51
- '--dev' ,
52
- 'Builds the website in dev mode, including full React error messages.' ,
53
- )
54
- . option (
55
- '--bundle-analyzer' ,
56
- 'visualize size of webpack output files with an interactive zoomable tree map (default: false)' ,
57
- )
58
- . option (
59
- '--out-dir <dir>' ,
60
- 'the full path for the new output directory, relative to the current workspace (default: build)' ,
61
- )
62
- . option (
63
- '--config <config>' ,
64
- 'path to docusaurus config file (default: `[siteDir]/docusaurus.config.js`)' ,
65
- )
66
- . option (
67
- '-l, --locale <locale...>' ,
68
- 'build the site in the specified locale(s). Build all known locales otherwise' ,
69
- concatLocaleOptions ,
70
- )
71
- . option (
72
- '--no-minify' ,
73
- 'build website without minimizing JS bundles (default: false)' ,
74
- )
75
- . action ( build ) ;
76
-
77
- cli
78
- . command ( 'swizzle [themeName] [componentName] [siteDir]' )
79
- . description (
80
- 'Wraps or ejects the original theme files into website folder for customization.' ,
81
- )
82
- . option (
83
- '-w, --wrap' ,
84
- 'Creates a wrapper around the original theme component.\nAllows rendering other components before/after the original theme component.' ,
85
- )
86
- . option (
87
- '-e, --eject' ,
88
- 'Ejects the full source code of the original theme component.\nAllows overriding the original component entirely with your own UI and logic.' ,
89
- )
90
- . option (
91
- '-l, --list' ,
92
- 'only list the available themes/components without further prompting (default: false)' ,
93
- )
94
- . option (
95
- '-t, --typescript' ,
96
- 'copy TypeScript theme files when possible (default: false)' ,
97
- )
98
- . option (
99
- '-j, --javascript' ,
100
- 'copy JavaScript theme files when possible (default: false)' ,
101
- )
102
- . option ( '--danger' , 'enable swizzle for unsafe component of themes' )
103
- . option (
104
- '--config <config>' ,
105
- 'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)' ,
106
- )
107
- . action ( swizzle ) ;
108
-
109
- cli
110
- . command ( 'deploy [siteDir]' )
111
- . description ( 'Deploy website to GitHub pages.' )
112
- . option (
113
- '-l, --locale <locale>' ,
114
- 'deploy the site in the specified locale(s). Deploy all known locales otherwise' ,
115
- concatLocaleOptions ,
116
- )
117
- . option (
118
- '--out-dir <dir>' ,
119
- 'the full path for the new output directory, relative to the current workspace (default: build)' ,
120
- )
121
- . option (
122
- '--config <config>' ,
123
- 'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)' ,
124
- )
125
- . option (
126
- '--skip-build' ,
127
- 'skip building website before deploy it (default: false)' ,
128
- )
129
- . option (
130
- '--target-dir <dir>' ,
131
- 'path to the target directory to deploy to (default: `.`)' ,
132
- )
133
- . action ( deploy ) ;
134
-
135
23
/**
136
- * @param {string | undefined } value
137
- * @returns {boolean | number }
24
+ * @param {unknown } error
138
25
*/
139
- function normalizePollValue ( value ) {
140
- if ( value === undefined || value === '' ) {
141
- return false ;
142
- }
143
-
144
- const parsedIntValue = Number . parseInt ( value , 10 ) ;
145
- if ( ! Number . isNaN ( parsedIntValue ) ) {
146
- return parsedIntValue ;
147
- }
148
-
149
- return value === 'true' ;
150
- }
151
-
152
- cli
153
- . command ( 'start [siteDir]' )
154
- . description ( 'Start the development server.' )
155
- . option ( '-p, --port <port>' , 'use specified port (default: 3000)' )
156
- . option ( '-h, --host <host>' , 'use specified host (default: localhost)' )
157
- . option ( '-l, --locale <locale>' , 'use specified site locale' )
158
- . option (
159
- '--hot-only' ,
160
- 'do not fallback to page refresh if hot reload fails (default: false)' ,
161
- )
162
- . option (
163
- '--config <config>' ,
164
- 'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)' ,
165
- )
166
- . option ( '--no-open' , 'do not open page in the browser (default: false)' )
167
- . option (
168
- '--poll [interval]' ,
169
- 'use polling rather than watching for reload (default: false). Can specify a poll interval in milliseconds' ,
170
- normalizePollValue ,
171
- )
172
- . option (
173
- '--no-minify' ,
174
- 'build website without minimizing JS bundles (default: false)' ,
175
- )
176
- . action ( start ) ;
177
-
178
- cli
179
- . command ( 'serve [siteDir]' )
180
- . description ( 'Serve website locally.' )
181
- . option (
182
- '--dir <dir>' ,
183
- 'the full path for the new output directory, relative to the current workspace (default: build)' ,
184
- )
185
- . option (
186
- '--config <config>' ,
187
- 'path to Docusaurus config file (default: `[siteDir]/docusaurus.config.js`)' ,
188
- )
189
- . option ( '-p, --port <port>' , 'use specified port (default: 3000)' )
190
- . option ( '--build' , 'build website before serving (default: false)' )
191
- . option ( '-h, --host <host>' , 'use specified host (default: localhost)' )
192
- . option (
193
- '--no-open' ,
194
- 'do not open page in the browser (default: false, or true in CI)' ,
195
- )
196
- . action ( serve ) ;
197
-
198
- cli
199
- . command ( 'clear [siteDir]' )
200
- . description ( 'Remove build artifacts.' )
201
- . action ( clear ) ;
202
-
203
- cli
204
- . command ( 'write-translations [siteDir]' )
205
- . description ( 'Extract required translations of your site.' )
206
- . option (
207
- '-l, --locale <locale>' ,
208
- 'the locale folder to write the translations.\n"--locale fr" will write translations in the ./i18n/fr folder.' ,
209
- )
210
- . option (
211
- '--override' ,
212
- '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)' ,
213
- )
214
- . option (
215
- '--config <config>' ,
216
- 'path to Docusaurus config file (default:`[siteDir]/docusaurus.config.js`)' ,
217
- )
218
- . option (
219
- '--messagePrefix <messagePrefix>' ,
220
- '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: "")' ,
221
- )
222
- . action ( writeTranslations ) ;
223
-
224
- cli
225
- . command ( 'write-heading-ids [siteDir] [files...]' )
226
- . description ( 'Generate heading ids in Markdown content.' )
227
- . option (
228
- '--maintain-case' ,
229
- "keep the headings' casing, otherwise make all lowercase (default: false)" ,
230
- )
231
- . option ( '--overwrite' , 'overwrite existing heading IDs (default: false)' )
232
- . action ( writeHeadingIds ) ;
233
-
234
- cli . arguments ( '<command>' ) . action ( ( cmd ) => {
235
- cli . outputHelp ( ) ;
236
- logger . error `Unknown Docusaurus CLI command name=${ cmd } .` ;
237
- process . exit ( 1 ) ;
238
- } ) ;
239
-
240
- // === The above is the commander configuration ===
241
- // They don't start any code execution yet until cli.parse() is called below
242
-
243
- /**
244
- * @param {string | undefined } command
245
- */
246
- function isInternalCommand ( command ) {
247
- return (
248
- command &&
249
- [
250
- 'start' ,
251
- 'build' ,
252
- 'swizzle' ,
253
- 'deploy' ,
254
- 'serve' ,
255
- 'clear' ,
256
- 'write-translations' ,
257
- 'write-heading-ids' ,
258
- ] . includes ( command )
259
- ) ;
260
- }
261
-
262
- /**
263
- * @param {string | undefined } command
264
- */
265
- function isExternalCommand ( command ) {
266
- return ! ! ( command && ! isInternalCommand ( command ) && ! command . startsWith ( '-' ) ) ;
267
- }
268
-
269
- // No command? We print the help message because Commander doesn't
270
- // Note argv looks like this: ['../node','../docusaurus.mjs','<command>',...rest]
271
- if ( process . argv . length < 3 ) {
272
- cli . outputHelp ( ) ;
273
- logger . error `Please provide a Docusaurus CLI command.` ;
274
- process . exit ( 1 ) ;
275
- }
276
-
277
- // There is an unrecognized subcommand
278
- // Let plugins extend the CLI before parsing
279
- if ( isExternalCommand ( process . argv [ 2 ] ) ) {
280
- // TODO: in this step, we must assume default site structure because there's
281
- // no way to know the siteDir/config yet. Maybe the root cli should be
282
- // responsible for parsing these arguments?
283
- // https://github.com/facebook/docusaurus/issues/8903
284
- await externalCommand ( cli ) ;
285
- }
286
-
287
- cli . parse ( process . argv ) ;
288
-
289
- process . on ( 'unhandledRejection' , ( err ) => {
26
+ function handleError ( error ) {
290
27
console . log ( '' ) ;
291
28
292
29
// We need to use inspect with increased depth to log the full causal chain
293
30
// By default Node logging has depth=2
294
31
// see also https://github.com/nodejs/node/issues/51637
295
- logger . error ( inspect ( err , { depth : Infinity } ) ) ;
32
+ logger . error ( inspect ( error , { depth : Infinity } ) ) ;
296
33
297
34
logger . info `Docusaurus version: number=${ DOCUSAURUS_VERSION }
298
35
Node version: number=${ process . version } ` ;
299
36
process . exit ( 1 ) ;
300
- } ) ;
37
+ }
38
+
39
+ process . on ( 'unhandledRejection' , handleError ) ;
40
+
41
+ try {
42
+ await beforeCli ( ) ;
43
+ // @ts -expect-error: we know it has at least 2 args
44
+ await runCLI ( process . argv ) ;
45
+ } catch ( e ) {
46
+ handleError ( e ) ;
47
+ }
0 commit comments