mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-16 18:46:57 +02:00
feat(create-docusaurus): ask user for preferred language when no language CLI option provided (#9442)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
parent
6fd8408a2c
commit
628752d92a
8 changed files with 301 additions and 160 deletions
65
packages/docusaurus-utils/src/cliUtils.ts
Normal file
65
packages/docusaurus-utils/src/cliUtils.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import prompts, {type Choice} from 'prompts';
|
||||
import logger from '@docusaurus/logger';
|
||||
|
||||
type PreferredLanguage = 'javascript' | 'typescript';
|
||||
|
||||
type AskPreferredLanguageOptions = {
|
||||
fallback: PreferredLanguage | undefined;
|
||||
exit: boolean;
|
||||
};
|
||||
|
||||
const DefaultOptions: AskPreferredLanguageOptions = {
|
||||
fallback: undefined,
|
||||
exit: false,
|
||||
};
|
||||
|
||||
const ExitChoice: Choice = {title: logger.yellow('[Exit]'), value: '[Exit]'};
|
||||
|
||||
export async function askPreferredLanguage(
|
||||
options: Partial<AskPreferredLanguageOptions> = {},
|
||||
): Promise<'javascript' | 'typescript'> {
|
||||
const {fallback, exit} = {...DefaultOptions, ...options};
|
||||
|
||||
const choices: Choice[] = [
|
||||
{title: logger.bold('JavaScript'), value: 'javascript'},
|
||||
{title: logger.bold('TypeScript'), value: 'typescript'},
|
||||
];
|
||||
if (exit) {
|
||||
choices.push(ExitChoice);
|
||||
}
|
||||
|
||||
const {language} = await prompts(
|
||||
{
|
||||
type: 'select',
|
||||
name: 'language',
|
||||
message: 'Which language do you want to use?',
|
||||
choices,
|
||||
},
|
||||
{
|
||||
onCancel() {
|
||||
exit && process.exit(0);
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (language === ExitChoice.value) {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (!language) {
|
||||
if (fallback) {
|
||||
logger.info`Falling back to language=${fallback}`;
|
||||
return fallback;
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
|
@ -117,3 +117,4 @@ export {
|
|||
} from './dataFileUtils';
|
||||
export {isDraft, isUnlisted} from './contentVisibilityUtils';
|
||||
export {escapeRegexp} from './regExpUtils';
|
||||
export {askPreferredLanguage} from './cliUtils';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue