Skip to content

Commit 71f3874

Browse files
atljsatya164
andauthored
feat: ask to use recommended template (#564)
## Summary This is an effort to implement a recommended template. See the [Golden Template RFC](react-native-community/discussions-and-proposals#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 <satyajit.happy@gmail.com>
1 parent a75cf32 commit 71f3874

File tree

1 file changed

+64
-4
lines changed
  • packages/create-react-native-library/src

1 file changed

+64
-4
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ type ArgName =
115115
| 'type'
116116
| 'local'
117117
| 'example'
118-
| 'react-native-version';
118+
| 'react-native-version'
119+
| 'with-recommended-options';
119120

120121
type ProjectLanguages = 'kotlin-objc' | 'kotlin-swift' | 'cpp' | 'js';
121122

@@ -140,6 +141,7 @@ type Answers = {
140141
type?: ProjectType;
141142
example?: boolean;
142143
reactNativeVersion?: string;
144+
withRecommendedOptions?: boolean;
143145
};
144146

145147
const LANGUAGE_CHOICES: {
@@ -148,7 +150,7 @@ const LANGUAGE_CHOICES: {
148150
types: ProjectType[];
149151
}[] = [
150152
{
151-
title: 'Kotlin & Objective-C',
153+
title: `Kotlin & Objective-C`,
152154
value: 'kotlin-objc',
153155
types: ['view-module-legacy', 'view-module-mixed', 'view-module-new'],
154156
},
@@ -219,6 +221,16 @@ const TYPE_CHOICES: {
219221
},
220222
];
221223

224+
const RECOMMENDED_TEMPLATE: {
225+
type: ProjectType;
226+
languages: ProjectLanguages;
227+
description: string;
228+
} = {
229+
type: 'view-module-mixed',
230+
languages: 'kotlin-objc',
231+
description: `Backward compatible Fabric view & Turbo module with Kotlin & Objective-C`,
232+
};
233+
222234
const args: Record<ArgName, yargs.Options> = {
223235
'slug': {
224236
description: 'Name of the npm package',
@@ -265,6 +277,10 @@ const args: Record<ArgName, yargs.Options> = {
265277
type: 'boolean',
266278
default: true,
267279
},
280+
'with-recommended-options': {
281+
description: `Whether to use the recommended template. ${RECOMMENDED_TEMPLATE.description}`,
282+
type: 'boolean',
283+
},
268284
};
269285

270286
// FIXME: fix the type
@@ -432,24 +448,68 @@ async function create(argv: yargs.Arguments<any>) {
432448
},
433449
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
434450
},
451+
'with-recommended-options': {
452+
type: 'select',
453+
name: 'withRecommendedOptions',
454+
message: 'Do you want to customize the library type and languages?',
455+
choices: [
456+
{
457+
title: 'Use recommended defaults',
458+
value: true,
459+
description: RECOMMENDED_TEMPLATE.description,
460+
},
461+
{
462+
title: 'Customize',
463+
value: false,
464+
},
465+
],
466+
},
435467
'type': {
436468
type: 'select',
437469
name: 'type',
438470
message: 'What type of library do you want to develop?',
439-
choices: TYPE_CHOICES,
471+
choices: (_, values) => {
472+
if (values.withRecommendedOptions) {
473+
return TYPE_CHOICES.filter(
474+
(choice) => choice.value === RECOMMENDED_TEMPLATE.type
475+
);
476+
}
477+
478+
return TYPE_CHOICES.map((choice) =>
479+
choice.value === RECOMMENDED_TEMPLATE.type
480+
? {
481+
...choice,
482+
title: `${choice.title} ${kleur.yellow('(Recommended)')}`,
483+
}
484+
: choice
485+
);
486+
},
440487
},
441488
'languages': {
442489
type: 'select',
443490
name: 'languages',
444491
message: 'Which languages do you want to use?',
445492
choices: (_, values) => {
493+
if (values.withRecommendedOptions) {
494+
return LANGUAGE_CHOICES.filter((choice) => {
495+
return choice.value === RECOMMENDED_TEMPLATE.languages;
496+
});
497+
}
498+
446499
return LANGUAGE_CHOICES.filter((choice) => {
447500
if (choice.types) {
448501
return choice.types.includes(values.type);
449502
}
450503

451504
return true;
452-
});
505+
}).map((choice) =>
506+
choice.value === RECOMMENDED_TEMPLATE.languages
507+
? {
508+
...choice,
509+
title: `${choice.title} ${kleur.yellow('(Recommended)')}`,
510+
}
511+
: choice
512+
);
453513
},
454514
},
455515
};

0 commit comments

Comments
 (0)