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(create-docusaurus): ask user for preferred language when no language CLI option provided #9442

Merged
1 change: 1 addition & 0 deletions packages/create-docusaurus/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ program
'Do not run package manager immediately after scaffolding',
)
.option('-t, --typescript', 'Use the TypeScript template variant')
.option('-j, --javascript', 'Use the JavaScript template variant')
.option(
'-g, --git-strategy <strategy>',
`Only used if the template is a git repository.
Expand Down
37 changes: 34 additions & 3 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import prompts, {type Choice} from 'prompts';
import supportsColor from 'supports-color';
import {escapeShellArg} from '@docusaurus/utils';

type Languages = {
javascript?: boolean;
typescript?: boolean;
};

type CLIOptions = {
packageManager?: PackageManager;
skipInstall?: boolean;
typescript?: boolean;
gitStrategy?: GitStrategy;
};
} & Languages;

// Only used in the rare, rare case of running globally installed create +
// using --skip-install. We need a default name to show the tip text
Expand Down Expand Up @@ -418,6 +422,7 @@ async function getSource(
};
}
let useTS = cliOptions.typescript;

if (!useTS && template.tsVariantPath) {
({useTS} = (await prompts({
type: 'confirm',
Expand Down Expand Up @@ -452,7 +457,33 @@ export default async function init(
getSiteName(reqName, rootDir),
]);
const dest = path.resolve(rootDir, siteName);
const source = await getSource(reqTemplate, templates, cliOptions);

let language: {javascript?: boolean; typescript?: boolean} = {};
if (!cliOptions.typescript && !cliOptions.javascript) {
const {language: selectedLanguage} = (await prompts(
{
type: 'select',
name: 'language',
message: 'What language you want to use?',
slorber marked this conversation as resolved.
Show resolved Hide resolved
choices: [
{title: 'JavaScript', value: 'javascript'},
{title: 'TypeScript', value: 'typescript'},
],
},
{
onCancel() {
logger.info`Falling back to language=${'javascript'}`;
},
},
)) as {language: keyof Languages};

language = {[selectedLanguage]: true};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you encapsulate all this under an abstraction?

Something like:

async function getLanguage(languages: LanguagesOptions): Promise<"javascript" | "typescript">

We'll likely use in other places in the future, cf swizzle CLI:
#9402

So that's worth extracting this to docusaurus-utils > cliUtils.ts immediately IMHO so that we'll be able to reuse it in other places


const source = await getSource(reqTemplate, templates, {
...cliOptions,
...language,
});
slorber marked this conversation as resolved.
Show resolved Hide resolved

logger.info('Creating new Docusaurus project...');

Expand Down