refactor(cli): normalize the application of default option values (#7220)

* refactor(cli): normalize default value application

* improve help text
This commit is contained in:
Joshua Chen 2022-04-29 18:09:03 +08:00 committed by GitHub
parent 6265f6dabb
commit 2429bfbd59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 77 deletions

View file

@ -54,14 +54,8 @@ cli
'--no-minify', '--no-minify',
'build website without minimizing JS bundles (default: false)', 'build website without minimizing JS bundles (default: false)',
) )
.action(async (siteDir, {bundleAnalyzer, config, outDir, locale, minify}) => { .action(async (siteDir, options) => {
build(await resolveDir(siteDir), { build(await resolveDir(siteDir), options);
bundleAnalyzer,
outDir,
config,
locale,
minify,
});
}); });
cli cli
@ -86,9 +80,9 @@ cli
'copy TypeScript theme files when possible (default: false)', 'copy TypeScript theme files when possible (default: false)',
) )
.option('--danger', 'enable swizzle for unsafe component of themes') .option('--danger', 'enable swizzle for unsafe component of themes')
.action(async (themeName, componentName, siteDir, options) => { .action(async (themeName, componentName, siteDir, options) =>
swizzle(await resolveDir(siteDir), themeName, componentName, options); swizzle(await resolveDir(siteDir), themeName, componentName, options),
}); );
cli cli
.command('deploy [siteDir]') .command('deploy [siteDir]')
@ -109,13 +103,9 @@ cli
'--skip-build', '--skip-build',
'skip building website before deploy it (default: false)', 'skip building website before deploy it (default: false)',
) )
.action(async (siteDir, {outDir, skipBuild, config}) => { .action(async (siteDir, options) =>
deploy(await resolveDir(siteDir), { deploy(await resolveDir(siteDir), options),
outDir, );
config,
skipBuild,
});
});
cli cli
.command('start [siteDir]') .command('start [siteDir]')
@ -136,18 +126,8 @@ cli
'--poll [interval]', '--poll [interval]',
'use polling rather than watching for reload (default: false). Can specify a poll interval in milliseconds', 'use polling rather than watching for reload (default: false). Can specify a poll interval in milliseconds',
) )
.action( .action(async (siteDir, options) =>
async (siteDir, {port, host, locale, config, hotOnly, open, poll}) => { start(await resolveDir(siteDir), options),
start(await resolveDir(siteDir), {
port,
host,
locale,
config,
hotOnly,
open,
poll,
});
},
); );
cli cli
@ -164,44 +144,25 @@ cli
.option('-p, --port <port>', 'use specified port (default: 3000)') .option('-p, --port <port>', 'use specified port (default: 3000)')
.option('--build', 'build website before serving (default: false)') .option('--build', 'build website before serving (default: false)')
.option('-h, --host <host>', 'use specified host (default: localhost)') .option('-h, --host <host>', 'use specified host (default: localhost)')
.action( .action(async (siteDir, options) =>
async ( serve(await resolveDir(siteDir), options),
siteDir,
{
dir = 'build',
port = 3000,
host = 'localhost',
build: buildSite = false,
config,
},
) => {
serve(await resolveDir(siteDir), {
dir,
port,
build: buildSite,
config,
host,
});
},
); );
cli cli
.command('clear [siteDir]') .command('clear [siteDir]')
.description('Remove build artifacts.') .description('Remove build artifacts.')
.action(async (siteDir) => { .action(async (siteDir) => clear(await resolveDir(siteDir)));
clear(await resolveDir(siteDir));
});
cli cli
.command('write-translations [siteDir]') .command('write-translations [siteDir]')
.description('Extract required translations of your site.') .description('Extract required translations of your site.')
.option( .option(
'-l, --locale <locale>', '-l, --locale <locale>',
'the locale folder to write the translations\n"--locale fr" will write translations in ./i18n/fr folder)', 'the locale folder to write the translations.\n"--locale fr" will write translations in the ./i18n/fr folder.',
) )
.option( .option(
'--override', '--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', '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( .option(
'--config <config>', '--config <config>',
@ -209,20 +170,10 @@ cli
) )
.option( .option(
'--messagePrefix <messagePrefix>', '--messagePrefix <messagePrefix>',
'allows to init new written messages with a given prefix. This might help you to highlight untranslated message to make them stand out in the UI', '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( .action(async (siteDir, options) =>
async ( writeTranslations(await resolveDir(siteDir), options),
siteDir,
{locale = undefined, override = false, messagePrefix = '', config},
) => {
writeTranslations(await resolveDir(siteDir), {
locale,
override,
config,
messagePrefix,
});
},
); );
cli cli

View file

@ -31,7 +31,7 @@ import type {HelmetServerState} from 'react-helmet-async';
export async function build( export async function build(
siteDir: string, siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {}, cliOptions: Partial<BuildCLIOptions>,
// When running build, we force terminate the process to prevent async // When running build, we force terminate the process to prevent async
// operations from never returning. However, if run as part of docusaurus // operations from never returning. However, if run as part of docusaurus
// deploy, we have to let deploy finish. // deploy, we have to let deploy finish.

View file

@ -26,7 +26,7 @@ async function removePath(entry: {path: string; description: string}) {
} }
} }
export async function clear(siteDir: string): Promise<unknown> { export async function clear(siteDir: string): Promise<void> {
const generatedFolder = { const generatedFolder = {
path: path.join(siteDir, GENERATED_FILES_DIR_NAME), path: path.join(siteDir, GENERATED_FILES_DIR_NAME),
description: 'generated folder', description: 'generated folder',
@ -40,7 +40,7 @@ export async function clear(siteDir: string): Promise<unknown> {
path: path.join(siteDir, p, '.cache'), path: path.join(siteDir, p, '.cache'),
description: 'Webpack persistent cache folder', description: 'Webpack persistent cache folder',
})); }));
return Promise.all( await Promise.all(
[generatedFolder, buildFolder, ...cacheFolders].map(removePath), [generatedFolder, buildFolder, ...cacheFolders].map(removePath),
); );
} }

View file

@ -36,7 +36,7 @@ function shellExecLog(cmd: string) {
export async function deploy( export async function deploy(
siteDir: string, siteDir: string,
cliOptions: Partial<BuildCLIOptions> = {}, cliOptions: Partial<BuildCLIOptions>,
): Promise<void> { ): Promise<void> {
const {outDir, siteConfig, siteConfigPath} = await loadContext({ const {outDir, siteConfig, siteConfigPath} = await loadContext({
siteDir, siteDir,

View file

@ -12,13 +12,15 @@ import path from 'path';
import {loadSiteConfig} from '../server/config'; import {loadSiteConfig} from '../server/config';
import {build} from './build'; import {build} from './build';
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils'; import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';
import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils';
import type {ServeCLIOptions} from '@docusaurus/types'; import type {ServeCLIOptions} from '@docusaurus/types';
export async function serve( export async function serve(
siteDir: string, siteDir: string,
cliOptions: ServeCLIOptions, cliOptions: Partial<ServeCLIOptions>,
): Promise<void> { ): Promise<void> {
let dir = path.resolve(siteDir, cliOptions.dir); const buildDir = cliOptions.dir ?? DEFAULT_BUILD_DIR_NAME;
let dir = path.resolve(siteDir, buildDir);
if (cliOptions.build) { if (cliOptions.build) {
dir = await build( dir = await build(
@ -69,7 +71,7 @@ export async function serve(
}); });
}); });
logger.success`Serving path=${cliOptions.dir} directory at url=${ logger.success`Serving path=${buildDir} directory at url=${
servingUrl + baseUrl servingUrl + baseUrl
}.`; }.`;
server.listen(port); server.listen(port);

View file

@ -42,8 +42,8 @@ async function getPathsToWatch(siteDir: string): Promise<string[]> {
export async function writeHeadingIds( export async function writeHeadingIds(
siteDir: string, siteDir: string,
files?: string[], files: string[],
options?: WriteHeadingIDOptions, options: WriteHeadingIDOptions,
): Promise<void> { ): Promise<void> {
const markdownFiles = await safeGlobby( const markdownFiles = await safeGlobby(
files ?? (await getPathsToWatch(siteDir)), files ?? (await getPathsToWatch(siteDir)),

View file

@ -74,7 +74,9 @@ async function writePluginTranslationFiles({
export async function writeTranslations( export async function writeTranslations(
siteDir: string, siteDir: string,
options: WriteTranslationsOptions & ConfigOptions & {locale?: string}, options: Partial<
WriteTranslationsOptions & ConfigOptions & {locale?: string}
>,
): Promise<void> { ): Promise<void> {
const context = await loadContext({ const context = await loadContext({
siteDir, siteDir,