Skip to content

Commit

Permalink
enable multiple locale cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 22, 2024
1 parent 776b3ee commit 15f0afc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
13 changes: 12 additions & 1 deletion packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ process.env.NODE_ENV ??= 'development';

await beforeCli();

/**
* @param {string} locale
* @param {string[]} locales
* @returns {string[]}
*/
function concatLocaleOptions(locale, locales = []) {
return locales.concat(locale);
}

cli.version(DOCUSAURUS_VERSION).usage('<command> [options]');

cli
Expand All @@ -55,8 +64,9 @@ cli
'path to docusaurus config file (default: `[siteDir]/docusaurus.config.js`)',
)
.option(
'-l, --locale <locale>',
'-l, --locale <locale...>',
'build the site in a specified locale. Build all known locales otherwise',
concatLocaleOptions,
)
.option(
'--no-minify',
Expand Down Expand Up @@ -102,6 +112,7 @@ cli
.option(
'-l, --locale <locale>',
'deploy the site in a specified locale. Deploy all known locales otherwise',
concatLocaleOptions,
)
.option(
'--out-dir <dir>',
Expand Down
56 changes: 34 additions & 22 deletions packages/docusaurus/src/commands/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import {loadContext, type LoadContextParams} from '../../server/site';
import {loadI18n} from '../../server/i18n';
import {buildLocale, type BuildLocaleParams} from './buildLocale';

export type BuildCLIOptions = Pick<
LoadContextParams,
'config' | 'locale' | 'outDir'
> & {
export type BuildCLIOptions = Pick<LoadContextParams, 'config' | 'outDir'> & {
locale?: [string, ...string[]];
bundleAnalyzer?: boolean;
minify?: boolean;
dev?: boolean;
Expand All @@ -27,7 +25,7 @@ export async function build(
): Promise<void> {
process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production';
process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale;
process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale?.[0];
if (cliOptions.dev) {
logger.info`Building in dev mode`;
process.env.BABEL_ENV = 'development';
Expand Down Expand Up @@ -57,37 +55,51 @@ export async function build(
logger.info`Use code=${'npm run serve'} command to test your build locally.`;
}

// We need the default locale to always be the 1st in the list. If we build it
// last, it would "erase" the localized sites built in sub-folders
function orderLocales({
locales,
defaultLocale,
}: {
locales: [string, ...string[]];
defaultLocale: string;
}): [string, ...string[]] {
if (locales.includes(defaultLocale)) {
return [
defaultLocale,
...locales.filter((locale) => locale !== defaultLocale),
];
} else {
return locales;
}
}

async function getLocalesToBuild({
siteDir,
cliOptions,
}: {
siteDir: string;
cliOptions: BuildCLIOptions;
}): Promise<[string, ...string[]]> {
if (cliOptions.locale) {
return [cliOptions.locale];
}
// We disable locale path localization if CLI has single "--locale" option
// yarn build --locale fr => baseUrl=/ instead of baseUrl=/fr/
const localizePath = cliOptions.locale?.length === 1 ? false : undefined;

const context = await loadContext({
siteDir,
outDir: cliOptions.outDir,
config: cliOptions.config,
locale: cliOptions.locale,
localizePath: cliOptions.locale ? false : undefined,
localizePath,
});
const i18n = await loadI18n(context.siteConfig, {
locale: cliOptions.locale,
});
if (i18n.locales.length > 1) {
logger.info`Website will be built for all these locales: ${i18n.locales}`;
}

// We need the default locale to always be the 1st in the list. If we build it
// last, it would "erase" the localized sites built in sub-folders
return [
i18n.defaultLocale,
...i18n.locales.filter((locale) => locale !== i18n.defaultLocale),
];
const i18n = await loadI18n(context.siteConfig);

const locales = cliOptions.locale ?? i18n.locales;

return orderLocales({
locales: locales as [string, ...string[]],
defaultLocale: i18n.defaultLocale,
});
}

async function tryToBuildLocale(params: BuildLocaleParams) {
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/commands/build/buildLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function buildLocale({
outDir: cliOptions.outDir,
config: cliOptions.config,
locale,
localizePath: cliOptions.locale ? false : undefined,
localizePath: cliOptions.locale?.length === 1 ? false : undefined,
}),
);

Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import {hasSSHProtocol, buildSshUrl, buildHttpsUrl} from '@docusaurus/utils';
import {loadContext, type LoadContextParams} from '../server/site';
import {build} from './build/build';

export type DeployCLIOptions = Pick<
LoadContextParams,
'config' | 'locale' | 'outDir'
> & {
export type DeployCLIOptions = Pick<LoadContextParams, 'config' | 'outDir'> & {
locale?: [string, ...string[]];
skipBuild?: boolean;
targetDir?: string;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/server/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {

export async function loadI18n(
config: DocusaurusConfig,
options: Pick<LoadContextParams, 'locale'>,
options?: Pick<LoadContextParams, 'locale'>,
): Promise<I18n> {
const {i18n: i18nConfig} = config;

const currentLocale = options.locale ?? i18nConfig.defaultLocale;
const currentLocale = options?.locale ?? i18nConfig.defaultLocale;

if (!i18nConfig.locales.includes(currentLocale)) {
logger.warn`The locale name=${currentLocale} was not found in your site configuration: Available locales are: ${i18nConfig.locales}
Expand Down

0 comments on commit 15f0afc

Please sign in to comment.