Skip to content

Commit

Permalink
feat: add --config & --generated-files-dir option to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Feb 27, 2021
1 parent 12afb9e commit 82b0a26
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 146 deletions.
35 changes: 25 additions & 10 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,36 @@ export type HostPortCLIOptions = {
port?: string;
};

export type StartCLIOptions = HostPortCLIOptions & {
hotOnly: boolean;
open: boolean;
poll: boolean | number;
locale?: string;
export type ConfigOptions = {
config: string;
};

export type ServeCLIOptions = HostPortCLIOptions & {
build: boolean;
dir: string;
};
export type StartCLIOptions = HostPortCLIOptions &
ConfigOptions & {
hotOnly: boolean;
open: boolean;
poll: boolean | number;
locale?: string;
};

export type BuildOptions = {
export type ServeCLIOptions = HostPortCLIOptions &
ConfigOptions & {
dir: string;
} & (
| {
build: true;
// We only need generatedFilesDir when `build` is true
generatedFilesDir: string;
}
| {
build: false;
}
);

export type BuildOptions = ConfigOptions & {
bundleAnalyzer: boolean;
outDir: string;
generatedFilesDir: string;
minify: boolean;
skipBuild: boolean;
};
Expand Down
89 changes: 68 additions & 21 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ cli
'--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 to `[siteDir]/docusaurus.config.js`',
)
.option(
'--generated-files-dir <dir>',
'Path to temporary `.docusaurus` folder, default to `[siteDir]/.docusaurus`.',
)
.option(
'-l, --locale <locale>',
'Build the site in a specified locale. Build all known locales otherwise.',
Expand All @@ -117,14 +125,21 @@ cli
'--no-minify',
'Build website without minimizing JS bundles (default: false)',
)
.action((siteDir = '.', {bundleAnalyzer, outDir, locale, minify}) => {
wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer,
outDir,
locale,
minify,
});
});
.action(
(
siteDir = '.',
{bundleAnalyzer, config, generatedFilesDir, outDir, locale, minify},
) => {
wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer,
outDir,
config,
generatedFilesDir,
locale,
minify,
});
},
);

cli
.command('swizzle [themeName] [componentName] [siteDir]')
Expand Down Expand Up @@ -155,12 +170,20 @@ cli
'--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 to `[siteDir]/docusaurus.config.js`',
)
.option(
'--skip-build',
'Skip building website before deploy it (default: false)',
)
.action((siteDir = '.', {outDir, skipBuild}) => {
wrapCommand(deploy)(path.resolve(siteDir), {outDir, skipBuild});
.action((siteDir = '.', {outDir, skipBuild, config}) => {
wrapCommand(deploy)(path.resolve(siteDir), {
outDir,
config,
skipBuild,
});
});

cli
Expand All @@ -173,21 +196,28 @@ cli
'--hot-only',
'Do not fallback to page refresh if hot reload fails (default: false)',
)
.option(
'--config <config>',
'Path to docusaurus config file, default to `[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.',
)
.action((siteDir = '.', {port, host, locale, hotOnly, open, poll}) => {
wrapCommand(start)(path.resolve(siteDir), {
port,
host,
locale,
hotOnly,
open,
poll,
});
});
.action(
(siteDir = '.', {port, host, locale, config, hotOnly, open, poll}) => {
wrapCommand(start)(path.resolve(siteDir), {
port,
host,
locale,
config,
hotOnly,
open,
poll,
});
},
);

cli
.command('serve [siteDir]')
Expand All @@ -196,6 +226,14 @@ cli
'--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 to `[siteDir]/docusaurus.config.js`',
)
.option(
'--generated-files-dir <dir>',
'Path to temporary `.docusaurus` folder, default to `[siteDir]/.docusaurus`. This is only relevant when `--build` passed in.',
)
.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')
Expand All @@ -207,12 +245,16 @@ cli
port = 3000,
host = 'localhost',
build: buildSite = false,
config,
generatedFilesDir,
},
) => {
wrapCommand(serve)(path.resolve(siteDir), {
dir,
port,
build: buildSite,
config,
generatedFilesDir,
host,
});
},
Expand All @@ -236,18 +278,23 @@ cli
'--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.',
)
.option(
'--config <config>',
'Path to docusaurus config file, default to `[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 to make them stand out in the UI.',
)
.action(
(
siteDir = '.',
{locale = undefined, override = false, messagePrefix = ''},
{locale = undefined, override = false, messagePrefix = '', config},
) => {
wrapCommand(writeTranslations)(path.resolve(siteDir), {
locale,
override,
config,
messagePrefix,
});
},
Expand Down
15 changes: 11 additions & 4 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {Configuration, Plugin} from 'webpack';
import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import merge from 'webpack-merge';
import {STATIC_DIR_NAME} from '../constants';
import {load} from '../server';
import {load, loadContext} from '../server';
import {handleBrokenLinks} from '../server/brokenLinks';

import {BuildCLIOptions, Props} from '@docusaurus/types';
Expand All @@ -28,7 +28,6 @@ import {
import CleanWebpackPlugin from '../webpack/plugins/CleanWebpackPlugin';
import {loadI18n} from '../server/i18n';
import {mapAsyncSequencial} from '@docusaurus/utils';
import loadConfig from '../server/config';

export default async function build(
siteDir: string,
Expand Down Expand Up @@ -59,8 +58,14 @@ export default async function build(
throw e;
}
}

const i18n = await loadI18n(loadConfig(siteDir), {
const context = await loadContext(siteDir, {
customOutDir: cliOptions.outDir,
customGeneratedFilesDir: cliOptions.generatedFilesDir,
customConfigFilePath: cliOptions.config,
locale: cliOptions.locale,
localizePath: cliOptions.locale ? false : undefined,
});
const i18n = await loadI18n(context.siteConfig, {
locale: cliOptions.locale,
});
if (cliOptions.locale) {
Expand Down Expand Up @@ -112,6 +117,8 @@ async function buildLocale({

const props: Props = await load(siteDir, {
customOutDir: cliOptions.outDir,
customConfigFilePath: cliOptions.config,
customGeneratedFilesDir: cliOptions.generatedFilesDir,
locale,
localizePath: cliOptions.locale ? false : undefined,
});
Expand Down
Loading

0 comments on commit 82b0a26

Please sign in to comment.