From 71f3874bd5cfbe0560c444054f09c7f4babbeb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20G=C3=BCner?= Date: Thu, 20 Jun 2024 13:16:51 +0300 Subject: [PATCH] feat: ask to use recommended template (#564) ## Summary This is an effort to implement a recommended template. See the [Golden Template RFC](https://github.com/react-native-community/discussions-and-proposals/pull/721) for context. This adds a new parameter called `--with-recommended-options` and the corresponding question to the CLI. Here's how the CLI prompt looks like: ![Screenshot 2024-06-12 at 17 07 18](https://github.com/callstack/react-native-builder-bob/assets/23079646/9567f352-2e46-4073-a5b6-f30f1d97b49d) This also adds the text `(Recommended)` at the end of the recommended options (View + Module with backward compats and Kotlin + Obj-c at the moment). ## Test plan ### A. Happy Path 1. Run `create-react-native-library` and answer the mail address, git repo, etc. questions. 2. Make sure the CLI asks `Do you want to customize the library type and languages?`. 3. Select `Use recommended defaults`. 4. Wait until the library is generated and make sure the library is a view + module, kotlin + objective-c library. ### B. Pick Customize 1. Run `create-react-native-library` and answer the mail address, git repo, etc. questions. 2. Make sure the CLI asks `Do you want to customize the library type and languages?`. 3. Select `Customize`. 4. Make sure the `Fabric view and Turbo module with backward compat` option has the `(Recommended)` text. ### C. Pass `--with-recommended-options` 1. Run `create-react-native-library` with `--with-recommended-options` parameter. 2. Make sure the library doesn't ask you about library or type selection. 3. Make sure the generated library uses the golden template. ### D. Pass `--with-recommended-options` with bad parameters 1. Run `create-react-native-library` with `--with-recommended-options` and `--type module-view-new`. 2. Make sure it emits an error. --------- Co-authored-by: Satyajit Sahoo --- .../create-react-native-library/src/index.ts | 68 +++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/packages/create-react-native-library/src/index.ts b/packages/create-react-native-library/src/index.ts index ae39abb47..c72ad435e 100644 --- a/packages/create-react-native-library/src/index.ts +++ b/packages/create-react-native-library/src/index.ts @@ -115,7 +115,8 @@ type ArgName = | 'type' | 'local' | 'example' - | 'react-native-version'; + | 'react-native-version' + | 'with-recommended-options'; type ProjectLanguages = 'kotlin-objc' | 'kotlin-swift' | 'cpp' | 'js'; @@ -140,6 +141,7 @@ type Answers = { type?: ProjectType; example?: boolean; reactNativeVersion?: string; + withRecommendedOptions?: boolean; }; const LANGUAGE_CHOICES: { @@ -148,7 +150,7 @@ const LANGUAGE_CHOICES: { types: ProjectType[]; }[] = [ { - title: 'Kotlin & Objective-C', + title: `Kotlin & Objective-C`, value: 'kotlin-objc', types: ['view-module-legacy', 'view-module-mixed', 'view-module-new'], }, @@ -219,6 +221,16 @@ const TYPE_CHOICES: { }, ]; +const RECOMMENDED_TEMPLATE: { + type: ProjectType; + languages: ProjectLanguages; + description: string; +} = { + type: 'view-module-mixed', + languages: 'kotlin-objc', + description: `Backward compatible Fabric view & Turbo module with Kotlin & Objective-C`, +}; + const args: Record = { 'slug': { description: 'Name of the npm package', @@ -265,6 +277,10 @@ const args: Record = { type: 'boolean', default: true, }, + 'with-recommended-options': { + description: `Whether to use the recommended template. ${RECOMMENDED_TEMPLATE.description}`, + type: 'boolean', + }, }; // FIXME: fix the type @@ -432,24 +448,68 @@ async function create(argv: yargs.Arguments) { }, validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL', }, + 'with-recommended-options': { + type: 'select', + name: 'withRecommendedOptions', + message: 'Do you want to customize the library type and languages?', + choices: [ + { + title: 'Use recommended defaults', + value: true, + description: RECOMMENDED_TEMPLATE.description, + }, + { + title: 'Customize', + value: false, + }, + ], + }, 'type': { type: 'select', name: 'type', message: 'What type of library do you want to develop?', - choices: TYPE_CHOICES, + choices: (_, values) => { + if (values.withRecommendedOptions) { + return TYPE_CHOICES.filter( + (choice) => choice.value === RECOMMENDED_TEMPLATE.type + ); + } + + return TYPE_CHOICES.map((choice) => + choice.value === RECOMMENDED_TEMPLATE.type + ? { + ...choice, + title: `${choice.title} ${kleur.yellow('(Recommended)')}`, + } + : choice + ); + }, }, 'languages': { type: 'select', name: 'languages', message: 'Which languages do you want to use?', choices: (_, values) => { + if (values.withRecommendedOptions) { + return LANGUAGE_CHOICES.filter((choice) => { + return choice.value === RECOMMENDED_TEMPLATE.languages; + }); + } + return LANGUAGE_CHOICES.filter((choice) => { if (choice.types) { return choice.types.includes(values.type); } return true; - }); + }).map((choice) => + choice.value === RECOMMENDED_TEMPLATE.languages + ? { + ...choice, + title: `${choice.title} ${kleur.yellow('(Recommended)')}`, + } + : choice + ); }, }, };