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:
翊小久 2024-02-15 22:08:01 +08:00 committed by GitHub
parent 628752d92a
commit 8abd1899a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 4 deletions

View file

@ -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>',

View file

@ -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,
}); });
} }

View file

@ -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,

View file

@ -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({