Skip to content

Commit 3f59787

Browse files
committed
refactor: organize prompt to separate sections
Which languages do you want to use? - Java & Objective-C - Java & Swift - Kotlin & Objective-C - Kotlin & Swift - C++ for both iOS & Android - JavaScript only For Java, Kotlin, Swift & Objective-C: What type of library do you want to develop? - Native module (to expose native APIs) - Native view (to use as a component) For JavaScript only: What type of example app do you want to generate? - JavaScript only (with Expo and Web support) - Native (to use other libraries with native code)
1 parent eb93059 commit 3f59787

File tree

5 files changed

+88
-111
lines changed

5 files changed

+88
-111
lines changed

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

Lines changed: 83 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,28 @@ type ArgName =
7070
| 'author-email'
7171
| 'author-url'
7272
| 'repo-url'
73-
| 'type';
73+
| 'languages'
74+
| 'type'
75+
| 'example';
7476

7577
type ModuleType = 'module' | 'view';
7678

77-
type LibraryType =
78-
| 'native'
79-
| 'native-swift'
80-
| 'native-kotlin'
81-
| 'native-kotlin-swift'
82-
| 'native-view'
83-
| 'native-view-swift'
84-
| 'native-view-kotlin'
85-
| 'native-view-kotlin-swift'
86-
| 'cpp'
87-
| 'js'
88-
| 'expo';
89-
9079
type Answers = {
9180
slug: string;
9281
description: string;
9382
authorName: string;
9483
authorEmail: string;
9584
authorUrl: string;
9685
repoUrl: string;
97-
type: LibraryType;
86+
languages:
87+
| 'java-objc'
88+
| 'java-swift'
89+
| 'kotlin-objc'
90+
| 'kotlin-swift'
91+
| 'cpp'
92+
| 'js';
93+
type?: 'module' | 'view';
94+
example?: 'expo' | 'native';
9895
};
9996

10097
const args: Record<ArgName, yargs.Options> = {
@@ -122,22 +119,25 @@ const args: Record<ArgName, yargs.Options> = {
122119
description: 'URL for the repository',
123120
type: 'string',
124121
},
125-
'type': {
126-
description: 'Type package do you want to develop',
122+
'languages': {
123+
description: 'Languages you want to use',
127124
choices: [
128-
'native',
129-
'native-swift',
130-
'native-kotlin',
131-
'native-kotlin-swift',
132-
'native-view',
133-
'native-view-swift',
134-
'native-view-kotlin',
135-
'native-view-kotlin-swift',
125+
'java-objc',
126+
'java-swift',
127+
'kotlin-objc',
128+
'kotlin-swift',
136129
'cpp',
137130
'js',
138-
'expo',
139131
],
140132
},
133+
'type': {
134+
description: 'Type of library you want to develop',
135+
choices: ['module', 'view'],
136+
},
137+
'example': {
138+
description: 'Type of example app',
139+
choices: ['expo', 'native'],
140+
},
141141
};
142142

143143
async function create(argv: yargs.Arguments<any>) {
@@ -245,45 +245,42 @@ async function create(argv: yargs.Arguments<any>) {
245245
},
246246
validate: (input) => /^https?:\/\//.test(input) || 'Must be a valid URL',
247247
},
248-
'type': {
248+
'languages': {
249249
type: 'select',
250+
name: 'languages',
251+
message: 'Which languages do you want to use?',
252+
choices: [
253+
{ title: 'Java & Objective-C', value: 'java-objc' },
254+
{ title: 'Java & Swift', value: 'java-swift' },
255+
{ title: 'Kotlin & Objective-C', value: 'kotlin-objc' },
256+
{ title: 'Kotlin & Swift', value: 'kotlin-swift' },
257+
{ title: 'C++ for both iOS & Android', value: 'cpp' },
258+
{ title: 'JavaScript only', value: 'js' },
259+
],
260+
},
261+
'type': {
262+
type: (prev: string) =>
263+
['java-objc', 'java-swift', 'kotlin-objc', 'kotlin-swift'].includes(
264+
prev
265+
)
266+
? 'select'
267+
: null,
250268
name: 'type',
251-
message: 'What type of package do you want to develop?',
269+
message: 'What type of library do you want to develop?',
252270
choices: [
253-
{ title: 'Native module in Java and Objective-C', value: 'native' },
254-
{ title: 'Native module in Java and Swift', value: 'native-swift' },
255-
{
256-
title: 'Native module in Kotlin and Objective-C',
257-
value: 'native-kotlin',
258-
},
259-
{
260-
title: 'Native module in Kotlin and Swift',
261-
value: 'native-kotlin-swift',
262-
},
263-
{ title: 'Native module with C++ code', value: 'cpp' },
264-
{
265-
title: 'Native view in Java and Objective-C',
266-
value: 'native-view',
267-
},
268-
{
269-
title: 'Native view in Java and Swift',
270-
value: 'native-view-swift',
271-
},
272-
{
273-
title: 'Native view in Kotlin and Objective-C',
274-
value: 'native-view-kotlin',
275-
},
276-
{
277-
title: 'Native view in Kotlin and Swift',
278-
value: 'native-view-kotlin-swift',
279-
},
280-
{
281-
title: 'JavaScript library with native example',
282-
value: 'js',
283-
},
271+
{ title: 'Native module (to expose native APIs)', value: 'module' },
272+
{ title: 'Native view (to use as a component)', value: 'view' },
273+
],
274+
},
275+
'example': {
276+
type: (prev: string) => (prev === 'js' ? 'select' : null),
277+
name: 'example',
278+
message: 'What type of example app do you want to generate?',
279+
choices: [
280+
{ title: 'JavaScript only (with Expo and Web support)', value: 'expo' },
284281
{
285-
title: 'JavaScript library with Expo example and Web support',
286-
value: 'expo',
282+
title: 'Native (to use other libraries with native code)',
283+
value: 'native',
287284
},
288285
],
289286
},
@@ -296,7 +293,9 @@ async function create(argv: yargs.Arguments<any>) {
296293
authorEmail,
297294
authorUrl,
298295
repoUrl,
299-
type,
296+
languages,
297+
type = 'module',
298+
example = 'native',
300299
} = {
301300
...argv,
302301
...(await prompts(
@@ -312,13 +311,6 @@ async function create(argv: yargs.Arguments<any>) {
312311
} as Answers;
313312

314313
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
315-
const moduleType: ModuleType =
316-
type === 'native-view' ||
317-
type === 'native-view-swift' ||
318-
type === 'native-view-kotlin' ||
319-
type === 'native-view-kotlin-swift'
320-
? 'view'
321-
: 'module';
322314

323315
// Get latest version of Bob from NPM
324316
let version: string;
@@ -360,29 +352,12 @@ async function create(argv: yargs.Arguments<any>) {
360352
.slice(1)}`,
361353
package: slug.replace(/[^a-z0-9]/g, '').toLowerCase(),
362354
podspec: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
363-
native:
364-
type === 'cpp' ||
365-
type === 'native' ||
366-
type === 'native-swift' ||
367-
type === 'native-kotlin' ||
368-
type === 'native-kotlin-swift' ||
369-
type === 'native-view' ||
370-
type === 'native-view-swift' ||
371-
type === 'native-view-kotlin' ||
372-
type === 'native-view-kotlin-swift',
373-
cpp: type === 'cpp',
374-
kotlin:
375-
type === 'native-kotlin' ||
376-
type === 'native-kotlin-swift' ||
377-
type === 'native-view-kotlin' ||
378-
type === 'native-view-kotlin-swift',
379-
swift:
380-
type === 'native-swift' ||
381-
type === 'native-kotlin-swift' ||
382-
type === 'native-view-swift' ||
383-
type === 'native-view-kotlin-swift',
384-
module: type !== 'js',
385-
moduleType,
355+
native: languages !== 'js',
356+
cpp: languages === 'cpp',
357+
kotlin: languages === 'kotlin-objc' || languages === 'kotlin-swift',
358+
swift: languages === 'java-swift' || languages === 'kotlin-swift',
359+
module: languages !== 'js',
360+
moduleType: type,
386361
},
387362
author: {
388363
name: authorName,
@@ -423,15 +398,17 @@ async function create(argv: yargs.Arguments<any>) {
423398

424399
await copyDir(COMMON_FILES, folder);
425400

426-
if (type === 'expo') {
401+
if (languages === 'js') {
427402
await copyDir(JS_FILES, folder);
428-
await copyDir(EXPO_FILES, folder);
429-
} else if (type === 'js') {
430-
await copyDir(JS_FILES, folder);
431-
await copyDir(
432-
path.join(EXAMPLE_FILES, 'example'),
433-
path.join(folder, 'example')
434-
);
403+
404+
if (example === 'expo') {
405+
await copyDir(EXPO_FILES, folder);
406+
} else {
407+
await copyDir(
408+
path.join(EXAMPLE_FILES, 'example'),
409+
path.join(folder, 'example')
410+
);
411+
}
435412
} else {
436413
await copyDir(
437414
path.join(EXAMPLE_FILES, 'example'),
@@ -445,14 +422,14 @@ async function create(argv: yargs.Arguments<any>) {
445422
}
446423

447424
if (options.project.swift) {
448-
await copyDir(SWIFT_FILES(moduleType), folder);
425+
await copyDir(SWIFT_FILES(type), folder);
449426
} else {
450-
await copyDir(OBJC_FILES(moduleType), folder);
427+
await copyDir(OBJC_FILES(type), folder);
451428
}
452429
if (options.project.kotlin) {
453-
await copyDir(KOTLIN_FILES(moduleType), folder);
430+
await copyDir(KOTLIN_FILES(type), folder);
454431
} else {
455-
await copyDir(JAVA_FILES(moduleType), folder);
432+
await copyDir(JAVA_FILES(type), folder);
456433
}
457434
}
458435

@@ -466,10 +443,10 @@ async function create(argv: yargs.Arguments<any>) {
466443
// Ignore error
467444
}
468445

469-
const platforms = {
446+
const platforms: Record<string, { name: string; color: string }> = {
470447
ios: { name: 'iOS', color: 'cyan' },
471448
android: { name: 'Android', color: 'green' },
472-
...(type === 'expo' ? { web: { name: 'Web', color: 'blue' } } : null),
449+
...(example === 'expo' ? { web: { name: 'Web', color: 'blue' } } : null),
473450
};
474451

475452
console.log(

packages/create-react-native-library/templates/example/example/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ dependencies {
204204
implementation jscFlavor
205205
}
206206

207-
<% if (project.module) { -%>
207+
<% if (project.native) { -%>
208208
implementation project(':<%- project.package %>')
209209
<% } -%>
210210
}

packages/create-react-native-library/templates/example/example/android/app/src/main/java/com/example/{%- project.package %}/MainApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.facebook.soloader.SoLoader;
1111
import java.lang.reflect.InvocationTargetException;
1212
import java.util.List;
13-
<% if (project.module) { -%>
13+
<% if (project.native) { -%>
1414
import com.<%- project.package %>.<%- project.name %>Package;
1515
<% } -%>
1616

@@ -29,7 +29,7 @@ protected List<ReactPackage> getPackages() {
2929
List<ReactPackage> packages = new PackageList(this).getPackages();
3030
// Packages that cannot be autolinked yet can be added manually here, for <%- project.name %>Example:
3131
// packages.add(new MyReactNativePackage());
32-
<% if (project.module) { -%>packages.add(new <%- project.name %>Package());<% } -%>
32+
<% if (project.native) { -%>packages.add(new <%- project.name %>Package());<% } -%>
3333

3434
return packages;
3535
}

packages/create-react-native-library/templates/example/example/android/settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ rootProject.name = '<%- project.name %>Example'
22
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
33
include ':app'
44

5-
<% if (project.module) { -%>
5+
<% if (project.native) { -%>
66
include ':<%- project.package %>'
77
project(':<%- project.package %>').projectDir = new File(rootProject.projectDir, '../../android')
88
<% } -%>

packages/create-react-native-library/templates/example/example/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ target '<%- project.name %>Example' do
77
config = use_native_modules!
88

99
use_react_native!(:path => config["reactNativePath"])
10-
<% if (project.module) { -%>
10+
<% if (project.native) { -%>
1111
1212
pod '<%- project.podspec %>', :path => '../..'
1313
<% } -%>

0 commit comments

Comments
 (0)