Skip to content

Commit

Permalink
Do not prompt the user if theme only supports JS
Browse files Browse the repository at this point in the history
Fail-fast if user uses --typescript option with a theme that does not support TS
  • Loading branch information
slorber committed Feb 15, 2024
1 parent 20c8f26 commit 5a74613
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions packages/docusaurus/src/commands/swizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import fs from 'fs-extra';
import logger from '@docusaurus/logger';
import {askPreferredLanguage} from '@docusaurus/utils';
import {getThemeName, getThemePath, getThemeNames} from './themes';
import {
getThemeName,
getThemePath,
getThemeNames,
getPluginByThemeName,
} from './themes';
import {getThemeComponents, getComponentName} from './components';
import {helpTables, themeComponentsTable} from './tables';
import {normalizeOptions} from './common';
Expand All @@ -20,14 +25,39 @@ import type {SwizzleAction, SwizzleComponentConfig} from '@docusaurus/types';
import type {SwizzleCLIOptions, SwizzlePlugin} from './common';
import type {ActionResult} from './actions';

async function getLanguage(options: SwizzleCLIOptions) {
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';
}
return askPreferredLanguage({exit: true});

// 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({
Expand Down Expand Up @@ -120,10 +150,17 @@ export async function swizzle(
});
}

const typescript = (await getLanguage(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

0 comments on commit 5a74613

Please sign in to comment.