Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swizzle): ask user preferred language if no language CLI option provided #9681

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ cli
'-t, --typescript',
'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(
'--config <config>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async function createTestSite() {
wrap: true,
danger: true,
typescript,
javascript: !typescript,
});
}

Expand All @@ -129,6 +130,7 @@ async function createTestSite() {
eject: true,
danger: true,
typescript,
javascript: !typescript,
});
}

Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus/src/commands/swizzle/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type SwizzleContext = {plugins: SwizzlePlugin[]};

export type SwizzleCLIOptions = {
typescript: boolean;
javascript: boolean;
danger: boolean;
list: boolean;
wrap: boolean;
Expand All @@ -75,6 +76,7 @@ export function normalizeOptions(
): SwizzleCLIOptions {
return {
typescript: options.typescript ?? false,
javascript: options.javascript ?? false,
danger: options.danger ?? false,
list: options.list ?? false,
wrap: options.wrap ?? false,
Expand Down
62 changes: 58 additions & 4 deletions packages/docusaurus/src/commands/swizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

import fs from 'fs-extra';
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 {helpTables, themeComponentsTable} from './tables';
import {normalizeOptions} from './common';
Expand All @@ -19,6 +25,41 @@ import type {SwizzleAction, SwizzleComponentConfig} from '@docusaurus/types';
import type {SwizzleCLIOptions, SwizzlePlugin} from './common';
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({
themeNames,
plugins,
Expand Down Expand Up @@ -96,17 +137,30 @@ export async function swizzle(
const siteDir = await fs.realpath(siteDirParam);

const options = normalizeOptions(optionsParam);
const {list, danger, typescript} = options;
const {list, danger} = options;

const {plugins} = await initSwizzleContext(siteDir, options);
const themeNames = getThemeNames(plugins);

if (list && !themeNameParam) {
await listAllThemeComponents({themeNames, plugins, typescript});
await listAllThemeComponents({
themeNames,
plugins,
typescript: options.typescript,
});
}

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 themeComponents = await getThemeComponents({
Expand Down