Skip to content

Commit 948ebd1

Browse files
committed
refactor: introduce viewConfig and moduleConfig
1 parent e43be83 commit 948ebd1

File tree

11 files changed

+138
-99
lines changed

11 files changed

+138
-99
lines changed

packages/create-react-native-library/src/exampleApp/dependencies.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export async function getDependencyVersionsFromExampleApp(
2727
'react-native': reactNative,
2828
};
2929

30-
if (config.example === 'vanilla' && !config.project.nitro) {
30+
if (
31+
config.example === 'vanilla' &&
32+
(config.project.moduleConfig === 'turbo-modules' ||
33+
config.project.viewConfig === 'fabric-view')
34+
) {
3135
// React Native doesn't provide the community CLI as a dependency.
3236
// We have to get read the version from the example app and put to the root package json
3337
const exampleCommunityCLIVersion =

packages/create-react-native-library/src/exampleApp/generateExampleApp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export default async function generateExampleApp({
181181
'react-native-builder-bob': `^${config.versions.bob}`,
182182
};
183183

184-
if (config.project.nitro) {
184+
if (config.project.moduleConfig === 'nitro-modules') {
185185
const packagesToAddNitro = {
186186
'react-native-nitro-modules': `^${config.versions.nitroModules}`,
187187
};

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ async function create(_argv: yargs.Arguments<Args>) {
4949
const bobVersionPromise = resolveNpmPackageVersion(
5050
'react-native-builder-bob',
5151
FALLBACK_BOB_VERSION
52-
)
52+
);
5353
const nitroModulesVersionPromise = resolveNpmPackageVersion(
54-
"react-native-nitro-modules",
54+
'react-native-nitro-modules',
5555
FALLBACK_NITRO_MODULES_VERSION
5656
);
5757
const nitroCodegenVersionPromise = resolveNpmPackageVersion(
58-
"nitro-codegen",
58+
'nitro-codegen',
5959
FALLBACK_NITRO_CODEGEN_VERSION
6060
);
6161

@@ -132,8 +132,8 @@ async function create(_argv: yargs.Arguments<Args>) {
132132

133133
if (
134134
config.example === 'vanilla' &&
135-
config.project.arch === 'new' &&
136-
!config.project.nitro
135+
(config.project.moduleConfig === 'turbo-modules' ||
136+
config.project.viewConfig === 'fabric-view')
137137
) {
138138
addCodegenBuildScript(folder);
139139
}

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

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ const LANGUAGE_CHOICES: {
3333
value: ProjectLanguages;
3434
types: ProjectType[];
3535
}[] = [
36-
{
37-
title: 'Kotlin & Swift',
38-
value: 'kotlin-swift',
39-
types: ['nitro-module', 'legacy-module', 'legacy-view'],
40-
},
41-
{
42-
title: 'Kotlin & Objective-C',
43-
value: 'kotlin-objc',
44-
types: ['turbo-module', 'fabric-view', 'legacy-module', 'legacy-view'],
45-
},
46-
{
47-
title: 'C++ for Android & iOS',
48-
value: 'cpp',
49-
types: ['turbo-module', 'legacy-module'],
50-
},
51-
{
52-
title: 'JavaScript for Android, iOS & Web',
53-
value: 'js',
54-
types: ['library'],
55-
},
56-
];
36+
{
37+
title: 'Kotlin & Swift',
38+
value: 'kotlin-swift',
39+
types: ['nitro-module', 'legacy-module', 'legacy-view'],
40+
},
41+
{
42+
title: 'Kotlin & Objective-C',
43+
value: 'kotlin-objc',
44+
types: ['turbo-module', 'fabric-view', 'legacy-module', 'legacy-view'],
45+
},
46+
{
47+
title: 'C++ for Android & iOS',
48+
value: 'cpp',
49+
types: ['turbo-module', 'legacy-module'],
50+
},
51+
{
52+
title: 'JavaScript for Android, iOS & Web',
53+
value: 'js',
54+
types: ['library'],
55+
},
56+
];
5757

5858
const EXAMPLE_CHOICES = (
5959
[
@@ -85,37 +85,37 @@ const TYPE_CHOICES: {
8585
value: ProjectType;
8686
description: string;
8787
}[] = [
88-
{
89-
title: 'Nitro module',
90-
value: 'nitro-module',
91-
description: 'type-safe and fast integration for native APIs to JS',
92-
},
93-
{
94-
title: 'Turbo module',
95-
value: 'turbo-module',
96-
description: 'integration for native APIs to JS',
97-
},
98-
{
99-
title: 'Fabric view',
100-
value: 'fabric-view',
101-
description: 'integration for native views to JS',
102-
},
103-
{
104-
title: 'Legacy Native module',
105-
value: 'legacy-module',
106-
description: 'bridge for native APIs to JS (old architecture)',
107-
},
108-
{
109-
title: 'Legacy Native view',
110-
value: 'legacy-view',
111-
description: 'bridge for native views to JS (old architecture)',
112-
},
113-
{
114-
title: 'JavaScript library',
115-
value: 'library',
116-
description: 'supports Expo Go and Web',
117-
},
118-
];
88+
{
89+
title: 'Nitro module',
90+
value: 'nitro-module',
91+
description: 'type-safe and fast integration for native APIs to JS',
92+
},
93+
{
94+
title: 'Turbo module',
95+
value: 'turbo-module',
96+
description: 'integration for native APIs to JS',
97+
},
98+
{
99+
title: 'Fabric view',
100+
value: 'fabric-view',
101+
description: 'integration for native views to JS',
102+
},
103+
{
104+
title: 'Legacy Native module',
105+
value: 'legacy-module',
106+
description: 'bridge for native APIs to JS (old architecture)',
107+
},
108+
{
109+
title: 'Legacy Native view',
110+
value: 'legacy-view',
111+
description: 'bridge for native views to JS (old architecture)',
112+
},
113+
{
114+
title: 'JavaScript library',
115+
value: 'library',
116+
description: 'supports Expo Go and Web',
117+
},
118+
];
119119

120120
export const acceptedArgs: Record<ArgName, yargs.Options> = {
121121
slug: {

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

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import path from 'path';
22
import fs from 'fs-extra';
33
import ejs from 'ejs';
4-
import type { Answers, ExampleApp, SupportedArchitecture } from './input';
4+
import type {
5+
Answers,
6+
ExampleApp,
7+
ProjectType,
8+
SupportedArchitecture,
9+
} from './input';
510

611
export type TemplateVersions = {
712
bob: string;
813
nitroModules: string;
914
nitroCodegen: string;
1015
};
1116

17+
export type ModuleConfig =
18+
| 'native-modules'
19+
| 'turbo-modules'
20+
| 'nitro-modules'
21+
| null;
22+
23+
export type ViewConfig = 'paper-view' | 'fabric-view' | null;
24+
1225
// Please think at least 5 times before introducing a new config key
1326
// You can just reuse the existing ones most of the time
1427
export type TemplateConfiguration = {
@@ -22,12 +35,11 @@ export type TemplateConfiguration = {
2235
package_cpp: string;
2336
identifier: string;
2437
native: boolean;
25-
nitro: boolean;
2638
arch: SupportedArchitecture;
2739
cpp: boolean;
2840
swift: boolean;
29-
view: boolean;
30-
module: boolean;
41+
viewConfig: ViewConfig;
42+
moduleConfig: ModuleConfig;
3143
};
3244
author: {
3345
name: string;
@@ -145,12 +157,11 @@ export function generateTemplateConfiguration({
145157
package_cpp: pack.replace(/\./g, '_'),
146158
identifier: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
147159
native: languages !== 'js',
148-
nitro: type.startsWith('nitro'),
149160
arch,
150161
cpp: languages === 'cpp',
151162
swift: languages === 'kotlin-swift',
152-
view: answers.type.endsWith('-view'),
153-
module: answers.type.endsWith('-module'),
163+
viewConfig: getViewConfig(type),
164+
moduleConfig: getModuleConfig(type),
154165
},
155166
author: {
156167
name: answers.authorName,
@@ -163,6 +174,30 @@ export function generateTemplateConfiguration({
163174
};
164175
}
165176

177+
function getModuleConfig(projectType: ProjectType): ModuleConfig {
178+
switch (projectType) {
179+
case 'nitro-module':
180+
return 'nitro-modules';
181+
case 'turbo-module':
182+
return 'turbo-modules';
183+
case 'legacy-module':
184+
return 'native-modules';
185+
default:
186+
return null;
187+
}
188+
}
189+
190+
function getViewConfig(projectType: ProjectType): ViewConfig {
191+
switch (projectType) {
192+
case 'legacy-view':
193+
return 'paper-view';
194+
case 'fabric-view':
195+
return 'fabric-view';
196+
default:
197+
return null;
198+
}
199+
}
200+
166201
export async function applyTemplates(
167202
answers: Answers,
168203
config: TemplateConfiguration,
@@ -177,7 +212,7 @@ export async function applyTemplates(
177212
if (config.example !== 'none') {
178213
await applyTemplate(config, EXAMPLE_COMMON_FILES, folder);
179214

180-
if (config.project.view) {
215+
if (config.project.viewConfig !== null) {
181216
await applyTemplate(config, EXAMPLE_VIEW_FILES, folder);
182217
} else {
183218
if (config.project.arch === 'legacy') {
@@ -199,12 +234,12 @@ export async function applyTemplates(
199234
await applyTemplate(config, NATIVE_COMMON_EXAMPLE_FILES, folder);
200235
}
201236

202-
if (config.project.nitro) {
237+
if (config.project.moduleConfig === 'nitro-modules') {
203238
await applyTemplate(config, NATIVE_FILES['module_nitro'], folder);
204239
return;
205240
}
206241

207-
if (config.project.module) {
242+
if (config.project.moduleConfig !== null) {
208243
await applyTemplate(
209244
config,
210245
NATIVE_FILES[`module_${config.project.arch}`],
@@ -221,7 +256,7 @@ export async function applyTemplates(
221256
if (config.project.swift) {
222257
await applyTemplate(config, SWIFT_FILES[`module_legacy`], folder);
223258
} else {
224-
if (config.project.module) {
259+
if (config.project.moduleConfig !== null) {
225260
await applyTemplate(config, OBJC_FILES[`module_common`], folder);
226261
} else {
227262
await applyTemplate(
@@ -232,9 +267,9 @@ export async function applyTemplates(
232267
}
233268
}
234269

235-
const templateType = `${config.project.module ? 'module' : 'view'}_${
236-
config.project.arch
237-
}` as const;
270+
const templateType = `${
271+
config.project.moduleConfig !== null ? 'module' : 'view'
272+
}_${config.project.arch}` as const;
238273

239274
await applyTemplate(config, KOTLIN_FILES[templateType], folder);
240275

packages/create-react-native-library/templates/common-local/$package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "src/index",
66
<% if (project.arch === 'new') { -%>
77
"codegenConfig": {
8-
"name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec",
9-
"type": <%- project.view ? '"all"': '"modules"' %>,
8+
"name": "RN<%- project.name -%><%- project.viewConfig !== null ? 'View': '' -%>Spec",
9+
"type": <%- project.viewConfig !== null ? '"components"': '"modules"' %>,
1010
"jsSrcsDir": "src"
1111
},
1212
<% } -%>

packages/create-react-native-library/templates/common/$package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"android",
2424
"ios",
2525
"cpp",
26-
<% if (project.nitro) { -%>
26+
<% if (project.moduleConfig === "nitro-modules") { -%>
2727
"nitrogen",
2828
<% } -%>
2929
"*.podspec",
@@ -52,7 +52,7 @@
5252
"clean": "del-cli lib",
5353
<% } -%>
5454
"prepare": "bob build",
55-
<% if (project.nitro) { -%>
55+
<% if (project.moduleConfig === 'nitro-modules') { -%>
5656
"nitrogen": "nitro-codegen",
5757
<% } -%>
5858
"release": "release-it"
@@ -78,7 +78,7 @@
7878
"devDependencies": {
7979
"@commitlint/config-conventional": "^17.0.2",
8080
"@evilmartians/lefthook": "^1.5.0",
81-
<% if (example === 'vanilla' && !project.nitro) { -%>
81+
<% if (example === 'vanilla' && (project.moduleConfig === 'turbo-modules' || project.viewConfig === 'fabric-view')) { -%>
8282
"@react-native-community/cli": "15.0.0-alpha.2",
8383
<% } -%>
8484
"@react-native/eslint-config": "^0.73.1",
@@ -91,14 +91,14 @@
9191
"eslint-config-prettier": "^9.0.0",
9292
"eslint-plugin-prettier": "^5.0.1",
9393
"jest": "^29.7.0",
94-
<% if (project.nitro) { -%>
94+
<% if (project.moduleConfig === 'nitro-modules') { -%>
9595
"nitro-codegen": "^<%- versions.nitroCodegen %>",
9696
<% } -%>
9797
"prettier": "^3.0.3",
9898
"react": "17.0.2",
9999
"react-native": "0.73.0",
100100
"react-native-builder-bob": "^<%- versions.bob %>",
101-
<% if (project.nitro) { -%>
101+
<% if (project.moduleConfig === 'nitro-modules') { -%>
102102
"react-native-nitro-modules": "^<%- versions.nitroModules %>",
103103
<% } -%>
104104
"release-it": "^17.10.0",
@@ -112,7 +112,7 @@
112112
},
113113
"peerDependencies": {
114114
"react": "*",
115-
<% if (project.nitro) { -%>
115+
<% if (project.moduleConfig === 'nitro-modules') { -%>
116116
"react-native": "*",
117117
"react-native-nitro-modules": "*"
118118
<% } else { -%>
@@ -189,7 +189,7 @@
189189
"source": "src",
190190
"output": "lib",
191191
"targets": [
192-
<% if (project.arch === 'new' && !project.nitro) { -%>
192+
<% if (project.moduleConfig === 'turbo-modules' || project.viewConfig === 'fabric-view') { -%>
193193
"codegen",
194194
<% } -%>
195195
[
@@ -211,11 +211,11 @@
211211
}
212212
]
213213
]
214-
<% if (project.arch === 'new' && !project.nitro) { -%>
214+
<% if (project.moduleConfig === 'turbo-modules' || project.viewConfig === 'fabric-view') { -%>
215215
},
216216
"codegenConfig": {
217-
"name": "RN<%- project.name -%><%- project.view ? 'View': '' -%>Spec",
218-
"type": "<%- project.view ? 'all': 'modules' -%>",
217+
"name": "RN<%- project.name -%><%- project.viewConfig !== null ? 'View': '' -%>Spec",
218+
"type": "<%- project.viewConfig !== null ? 'components': 'modules' -%>",
219219
"jsSrcsDir": "src",
220220
"outputDir": {
221221
"ios": "ios/generated",

0 commit comments

Comments
 (0)