mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 10:17:55 +02:00
feat(swizzle): ask user preferred language if no language CLI option provided (#9681)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
parent
628752d92a
commit
8abd1899a6
4 changed files with 66 additions and 4 deletions
|
@ -85,6 +85,10 @@ cli
|
||||||
'-t, --typescript',
|
'-t, --typescript',
|
||||||
'copy TypeScript theme files when possible (default: false)',
|
'copy TypeScript theme files when possible (default: false)',
|
||||||
)
|
)
|
||||||
|
.option(
|
||||||
|
'-j, --javascript',
|
||||||
|
'copy JavaScript theme files when possible (default: false)',
|
||||||
|
)
|
||||||
.option('--danger', 'enable swizzle for unsafe component of themes')
|
.option('--danger', 'enable swizzle for unsafe component of themes')
|
||||||
.option(
|
.option(
|
||||||
'--config <config>',
|
'--config <config>',
|
||||||
|
|
|
@ -115,6 +115,7 @@ async function createTestSite() {
|
||||||
wrap: true,
|
wrap: true,
|
||||||
danger: true,
|
danger: true,
|
||||||
typescript,
|
typescript,
|
||||||
|
javascript: !typescript,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +130,7 @@ async function createTestSite() {
|
||||||
eject: true,
|
eject: true,
|
||||||
danger: true,
|
danger: true,
|
||||||
typescript,
|
typescript,
|
||||||
|
javascript: !typescript,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,7 @@ export type SwizzleContext = {plugins: SwizzlePlugin[]};
|
||||||
|
|
||||||
export type SwizzleCLIOptions = {
|
export type SwizzleCLIOptions = {
|
||||||
typescript: boolean;
|
typescript: boolean;
|
||||||
|
javascript: boolean;
|
||||||
danger: boolean;
|
danger: boolean;
|
||||||
list: boolean;
|
list: boolean;
|
||||||
wrap: boolean;
|
wrap: boolean;
|
||||||
|
@ -75,6 +76,7 @@ export function normalizeOptions(
|
||||||
): SwizzleCLIOptions {
|
): SwizzleCLIOptions {
|
||||||
return {
|
return {
|
||||||
typescript: options.typescript ?? false,
|
typescript: options.typescript ?? false,
|
||||||
|
javascript: options.javascript ?? false,
|
||||||
danger: options.danger ?? false,
|
danger: options.danger ?? false,
|
||||||
list: options.list ?? false,
|
list: options.list ?? false,
|
||||||
wrap: options.wrap ?? false,
|
wrap: options.wrap ?? false,
|
||||||
|
|
|
@ -7,7 +7,13 @@
|
||||||
|
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import logger from '@docusaurus/logger';
|
import logger from '@docusaurus/logger';
|
||||||
import {getThemeName, getThemePath, getThemeNames} from './themes';
|
import {askPreferredLanguage} from '@docusaurus/utils';
|
||||||
|
import {
|
||||||
|
getThemeName,
|
||||||
|
getThemePath,
|
||||||
|
getThemeNames,
|
||||||
|
getPluginByThemeName,
|
||||||
|
} from './themes';
|
||||||
import {getThemeComponents, getComponentName} from './components';
|
import {getThemeComponents, getComponentName} from './components';
|
||||||
import {helpTables, themeComponentsTable} from './tables';
|
import {helpTables, themeComponentsTable} from './tables';
|
||||||
import {normalizeOptions} from './common';
|
import {normalizeOptions} from './common';
|
||||||
|
@ -19,6 +25,41 @@ import type {SwizzleAction, SwizzleComponentConfig} from '@docusaurus/types';
|
||||||
import type {SwizzleCLIOptions, SwizzlePlugin} from './common';
|
import type {SwizzleCLIOptions, SwizzlePlugin} from './common';
|
||||||
import type {ActionResult} from './actions';
|
import type {ActionResult} from './actions';
|
||||||
|
|
||||||
|
async function getLanguageForThemeName({
|
||||||
|
themeName,
|
||||||
|
plugins,
|
||||||
|
options,
|
||||||
|
}: {
|
||||||
|
themeName: string;
|
||||||
|
plugins: SwizzlePlugin[];
|
||||||
|
options: SwizzleCLIOptions;
|
||||||
|
}): Promise<'javascript' | 'typescript'> {
|
||||||
|
const plugin = getPluginByThemeName(plugins, themeName);
|
||||||
|
const supportsTS = !!plugin.instance.getTypeScriptThemePath?.();
|
||||||
|
|
||||||
|
if (options.typescript) {
|
||||||
|
if (!supportsTS) {
|
||||||
|
throw new Error(
|
||||||
|
logger.interpolate`Theme name=${
|
||||||
|
plugin.instance.name
|
||||||
|
} does not support the code=${'--typescript'} CLI option.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return 'typescript';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.javascript) {
|
||||||
|
return 'javascript';
|
||||||
|
}
|
||||||
|
|
||||||
|
// It's only useful to prompt the user for themes that support both JS/TS
|
||||||
|
if (supportsTS) {
|
||||||
|
return askPreferredLanguage({exit: true});
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'javascript';
|
||||||
|
}
|
||||||
|
|
||||||
async function listAllThemeComponents({
|
async function listAllThemeComponents({
|
||||||
themeNames,
|
themeNames,
|
||||||
plugins,
|
plugins,
|
||||||
|
@ -96,17 +137,30 @@ export async function swizzle(
|
||||||
const siteDir = await fs.realpath(siteDirParam);
|
const siteDir = await fs.realpath(siteDirParam);
|
||||||
|
|
||||||
const options = normalizeOptions(optionsParam);
|
const options = normalizeOptions(optionsParam);
|
||||||
const {list, danger, typescript} = options;
|
const {list, danger} = options;
|
||||||
|
|
||||||
const {plugins} = await initSwizzleContext(siteDir, options);
|
const {plugins} = await initSwizzleContext(siteDir, options);
|
||||||
const themeNames = getThemeNames(plugins);
|
const themeNames = getThemeNames(plugins);
|
||||||
|
|
||||||
if (list && !themeNameParam) {
|
if (list && !themeNameParam) {
|
||||||
await listAllThemeComponents({themeNames, plugins, typescript});
|
await listAllThemeComponents({
|
||||||
|
themeNames,
|
||||||
|
plugins,
|
||||||
|
typescript: options.typescript,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const themeName = await getThemeName({themeNameParam, themeNames, list});
|
const themeName = await getThemeName({themeNameParam, themeNames, list});
|
||||||
const themePath = getThemePath({themeName, plugins, typescript});
|
|
||||||
|
const language = await getLanguageForThemeName({themeName, plugins, options});
|
||||||
|
const typescript = language === 'typescript';
|
||||||
|
|
||||||
|
const themePath = getThemePath({
|
||||||
|
themeName,
|
||||||
|
plugins,
|
||||||
|
typescript,
|
||||||
|
});
|
||||||
|
|
||||||
const swizzleConfig = getThemeSwizzleConfig(themeName, plugins);
|
const swizzleConfig = getThemeSwizzleConfig(themeName, plugins);
|
||||||
|
|
||||||
const themeComponents = await getThemeComponents({
|
const themeComponents = await getThemeComponents({
|
||||||
|
|
Loading…
Add table
Reference in a new issue