-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathtemplate.ts
323 lines (286 loc) · 8.82 KB
/
template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import path from 'path';
import fs from 'fs-extra';
import ejs from 'ejs';
import type {
Answers,
ExampleApp,
ProjectType,
SupportedArchitecture,
} from './input';
export type TemplateVersions = {
bob: string;
nitroModules?: string;
nitroCodegen?: string;
};
export type ModuleConfig =
| 'native-modules'
| 'turbo-modules'
| 'nitro-modules'
| null;
export type ViewConfig = 'paper-view' | 'fabric-view' | null;
// Please think at least 5 times before introducing a new config key
// You can just reuse the existing ones most of the time
export type TemplateConfiguration = {
versions: TemplateVersions;
project: {
slug: string;
description: string;
name: string;
package: string;
package_dir: string;
package_cpp: string;
identifier: string;
native: boolean;
arch: SupportedArchitecture;
cpp: boolean;
swift: boolean;
viewConfig: ViewConfig;
moduleConfig: ModuleConfig;
};
author: {
name: string;
email: string;
url: string;
};
repo: string;
example: ExampleApp;
year: number;
};
const BINARIES = [
/(gradlew|\.(jar|keystore|png|jpg|gif))$/,
/\$\.yarn(?![a-z])/,
];
const COMMON_FILES = path.resolve(__dirname, '../templates/common');
const COMMON_LOCAL_FILES = path.resolve(__dirname, '../templates/common-local');
const EXAMPLE_COMMON_FILES = path.resolve(
__dirname,
'../templates/example-common'
);
const EXAMPLE_MODULE_LEGACY_FILES = path.resolve(
__dirname,
'../templates/example-module-legacy'
);
const EXAMPLE_MODULE_NEW_FILES = path.resolve(
__dirname,
'../templates/example-module-new'
);
const EXAMPLE_VIEW_FILES = path.resolve(__dirname, '../templates/example-view');
const EXAMPLE_EXPO_FILES = path.resolve(__dirname, '../templates/example-expo');
const JS_FILES = path.resolve(__dirname, '../templates/js-library');
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
const NATIVE_COMMON_FILES = path.resolve(
__dirname,
'../templates/native-common'
);
const NATIVE_COMMON_EXAMPLE_FILES = path.resolve(
__dirname,
'../templates/native-common-example'
);
const NATIVE_FILES = {
module_legacy: path.resolve(__dirname, '../templates/native-library-legacy'),
module_new: path.resolve(__dirname, '../templates/native-library-new'),
view_legacy: path.resolve(__dirname, '../templates/native-view-legacy'),
view_new: path.resolve(__dirname, '../templates/native-view-new'),
module_nitro: path.resolve(__dirname, '../templates/nitro-module'),
} as const;
const OBJC_FILES = {
module_common: path.resolve(__dirname, '../templates/objc-library'),
view_legacy: path.resolve(__dirname, '../templates/objc-view-legacy'),
view_new: path.resolve(__dirname, '../templates/objc-view-new'),
} as const;
const KOTLIN_FILES = {
module_legacy: path.resolve(__dirname, '../templates/kotlin-library-legacy'),
module_new: path.resolve(__dirname, '../templates/kotlin-library-new'),
view_legacy: path.resolve(__dirname, '../templates/kotlin-view-legacy'),
view_new: path.resolve(__dirname, '../templates/kotlin-view-new'),
} as const;
const SWIFT_FILES = {
module_legacy: path.resolve(__dirname, '../templates/swift-library-legacy'),
view_legacy: path.resolve(__dirname, '../templates/swift-view-legacy'),
} as const;
export function generateTemplateConfiguration({
versions,
basename,
answers,
}: {
versions: TemplateVersions;
basename: string;
answers: Answers;
}): TemplateConfiguration {
const { slug, languages, type } = answers;
const arch =
type === 'legacy-module' || type === 'legacy-view' || type === 'library'
? 'legacy'
: 'new';
const project = slug.replace(/^(react-native-|@[^/]+\/)/, '');
let namespace: string | undefined;
if (slug.startsWith('@') && slug.includes('/')) {
namespace = slug
.split('/')[0]
?.replace(/[^a-z0-9]/g, '')
.toLowerCase();
}
// Create a package identifier with specified namespace when possible
const pack = `${namespace ? `${namespace}.` : ''}${project
.replace(/[^a-z0-9]/g, '')
.toLowerCase()}`;
return {
versions,
project: {
slug,
description: answers.description,
name:
/^[A-Z]/.test(basename) && /^[a-z0-9]+$/i.test(basename)
? // If the project name is already in PascalCase, use it as-is
basename
: // Otherwise, convert it to PascalCase and remove any non-alphanumeric characters
`${project.charAt(0).toUpperCase()}${project
.replace(/[^a-z0-9](\w)/g, (_, $1) => $1.toUpperCase())
.slice(1)}`,
package: pack,
package_dir: pack.replace(/\./g, '/'),
package_cpp: pack.replace(/\./g, '_'),
identifier: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
native: languages !== 'js',
arch,
cpp: languages === 'cpp',
swift: languages === 'kotlin-swift',
viewConfig: getViewConfig(type),
moduleConfig: getModuleConfig(type),
},
author: {
name: answers.authorName,
email: answers.authorEmail,
url: answers.authorUrl,
},
repo: answers.repoUrl,
example: answers.example,
year: new Date().getFullYear(),
};
}
function getModuleConfig(projectType: ProjectType): ModuleConfig {
switch (projectType) {
case 'nitro-module':
return 'nitro-modules';
case 'turbo-module':
return 'turbo-modules';
case 'legacy-module':
return 'native-modules';
default:
return null;
}
}
function getViewConfig(projectType: ProjectType): ViewConfig {
switch (projectType) {
case 'legacy-view':
return 'paper-view';
case 'fabric-view':
return 'fabric-view';
default:
return null;
}
}
export async function applyTemplates(
answers: Answers,
config: TemplateConfiguration,
folder: string
) {
const { local } = answers;
if (local) {
await applyTemplate(config, COMMON_LOCAL_FILES, folder);
} else {
await applyTemplate(config, COMMON_FILES, folder);
if (config.example !== 'none') {
await applyTemplate(config, EXAMPLE_COMMON_FILES, folder);
if (config.project.viewConfig !== null) {
await applyTemplate(config, EXAMPLE_VIEW_FILES, folder);
} else {
if (config.project.arch === 'legacy') {
await applyTemplate(config, EXAMPLE_MODULE_LEGACY_FILES, folder);
} else {
await applyTemplate(config, EXAMPLE_MODULE_NEW_FILES, folder);
}
}
}
}
if (answers.languages === 'js') {
await applyTemplate(config, JS_FILES, folder);
await applyTemplate(config, EXAMPLE_EXPO_FILES, folder);
} else {
await applyTemplate(config, NATIVE_COMMON_FILES, folder);
if (config.example !== 'none') {
await applyTemplate(config, NATIVE_COMMON_EXAMPLE_FILES, folder);
}
if (config.example === 'expo') {
await applyTemplate(config, EXAMPLE_EXPO_FILES, folder);
}
if (config.project.moduleConfig === 'nitro-modules') {
await applyTemplate(config, NATIVE_FILES['module_nitro'], folder);
return;
}
if (config.project.moduleConfig !== null) {
await applyTemplate(
config,
NATIVE_FILES[`module_${config.project.arch}`],
folder
);
} else {
await applyTemplate(
config,
NATIVE_FILES[`view_${config.project.arch}`],
folder
);
}
if (config.project.swift) {
await applyTemplate(config, SWIFT_FILES[`module_legacy`], folder);
} else {
if (config.project.moduleConfig !== null) {
await applyTemplate(config, OBJC_FILES[`module_common`], folder);
} else {
await applyTemplate(
config,
OBJC_FILES[`view_${config.project.arch}`],
folder
);
}
}
const templateType = `${
config.project.moduleConfig !== null ? 'module' : 'view'
}_${config.project.arch}` as const;
await applyTemplate(config, KOTLIN_FILES[templateType], folder);
if (config.project.cpp) {
await applyTemplate(config, CPP_FILES, folder);
await fs.remove(path.join(folder, 'ios', `${config.project.name}.m`));
}
}
}
/**
* This copies the template files and renders them via ejs
*/
async function applyTemplate(
config: TemplateConfiguration,
source: string,
destination: string
) {
await fs.mkdirp(destination);
const files = await fs.readdir(source);
for (const f of files) {
const target = path.join(
destination,
ejs.render(f.replace(/^\$/, ''), config, {
openDelimiter: '{',
closeDelimiter: '}',
})
);
const file = path.join(source, f);
const stats = await fs.stat(file);
if (stats.isDirectory()) {
await applyTemplate(config, file, target);
} else if (!BINARIES.some((r) => r.test(file))) {
const content = await fs.readFile(file, 'utf8');
await fs.writeFile(target, ejs.render(content, config));
} else {
await fs.copyFile(file, target);
}
}
}