Skip to content

Commit

Permalink
feat(v2): add --config option to CLI (#4308)
Browse files Browse the repository at this point in the history
* feat: add --config & --generated-files-dir option to CLI

* revert --generated-files-dir option + some refactors

Co-authored-by: slorber <lorber.sebastien@gmail.com>
  • Loading branch information
longlho and slorber authored Mar 2, 2021
1 parent 9413ba9 commit f46adff
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 169 deletions.
27 changes: 17 additions & 10 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,25 @@ 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 ServeCLIOptions = HostPortCLIOptions &
ConfigOptions & {
dir: string;
build: boolean;
};

export type BuildOptions = {
export type BuildOptions = ConfigOptions & {
bundleAnalyzer: boolean;
outDir: string;
minify: boolean;
Expand All @@ -158,6 +164,7 @@ export interface LoadContext {
siteDir: string;
generatedFilesDir: string;
siteConfig: DocusaurusConfig;
siteConfigPath: string;
outDir: string;
baseUrl: string;
i18n: I18n;
Expand Down
59 changes: 45 additions & 14 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ 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(
'-l, --locale <locale>',
'Build the site in a specified locale. Build all known locales otherwise.',
Expand All @@ -117,10 +121,11 @@ cli
'--no-minify',
'Build website without minimizing JS bundles (default: false)',
)
.action((siteDir = '.', {bundleAnalyzer, outDir, locale, minify}) => {
.action((siteDir = '.', {bundleAnalyzer, config, outDir, locale, minify}) => {
wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer,
outDir,
config,
locale,
minify,
});
Expand Down Expand Up @@ -155,12 +160,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 +186,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 +216,10 @@ 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('-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 +231,14 @@ cli
port = 3000,
host = 'localhost',
build: buildSite = false,
config,
},
) => {
wrapCommand(serve)(path.resolve(siteDir), {
dir,
port,
build: buildSite,
config,
host,
});
},
Expand All @@ -236,18 +262,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
13 changes: 9 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,13 @@ export default async function build(
throw e;
}
}

const i18n = await loadI18n(loadConfig(siteDir), {
const context = await loadContext(siteDir, {
customOutDir: cliOptions.outDir,
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 +116,7 @@ async function buildLocale({

const props: Props = await load(siteDir, {
customOutDir: cliOptions.outDir,
customConfigFilePath: cliOptions.config,
locale,
localizePath: cliOptions.locale ? false : undefined,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/commands/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import fs from 'fs-extra';
import path from 'path';
import chalk = require('chalk');
import {BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants';
import {DEFAULT_BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants';

function removePath(fsPath: string) {
return fs
Expand All @@ -24,7 +24,7 @@ function removePath(fsPath: string) {
export default async function clear(siteDir: string): Promise<unknown> {
return Promise.all([
removePath(path.join(siteDir, GENERATED_FILES_DIR_NAME)),
removePath(path.join(siteDir, BUILD_DIR_NAME)),
removePath(path.join(siteDir, DEFAULT_BUILD_DIR_NAME)),
removePath(path.join(siteDir, 'node_modules/.cache/cache-loader')),
]);
}
Loading

0 comments on commit f46adff

Please sign in to comment.