@react-native-windows/codegen 0.70.1 → 0.71.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +101 -14
- package/lib-commonjs/Cli.js +18 -187
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/AliasGen.d.ts +1 -1
- package/lib-commonjs/generators/AliasGen.js.map +1 -1
- package/lib-commonjs/generators/AliasManaging.d.ts +1 -1
- package/lib-commonjs/generators/AliasManaging.js.map +1 -1
- package/lib-commonjs/generators/GenerateNM2.d.ts +3 -3
- package/lib-commonjs/generators/GenerateNM2.js +2 -2
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -1
- package/lib-commonjs/generators/GenerateTypeScript.d.ts +1 -1
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -1
- package/lib-commonjs/generators/ObjectTypes.d.ts +1 -1
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -1
- package/lib-commonjs/generators/ParamTypes.d.ts +1 -1
- package/lib-commonjs/generators/ParamTypes.js.map +1 -1
- package/lib-commonjs/generators/ReturnTypes.d.ts +1 -1
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -1
- package/lib-commonjs/generators/ValidateConstants.d.ts +1 -1
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -1
- package/lib-commonjs/generators/ValidateMethods.d.ts +1 -1
- package/lib-commonjs/generators/ValidateMethods.js +2 -2
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -1
- package/lib-commonjs/index.d.ts +39 -0
- package/lib-commonjs/index.js +209 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +17 -14
- package/src/Cli.ts +18 -279
- package/src/generators/AliasGen.ts +1 -1
- package/src/generators/AliasManaging.ts +1 -1
- package/src/generators/GenerateNM2.ts +4 -4
- package/src/generators/GenerateTypeScript.ts +1 -1
- package/src/generators/ObjectTypes.ts +4 -1
- package/src/generators/ParamTypes.ts +1 -1
- package/src/generators/ReturnTypes.ts +1 -1
- package/src/generators/ValidateConstants.ts +1 -1
- package/src/generators/ValidateMethods.ts +3 -3
- package/src/index.ts +363 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import fs from '@react-native-windows/fs';
|
|
10
|
+
import globby from 'globby';
|
|
11
|
+
import {createNM2Generator} from './generators/GenerateNM2';
|
|
12
|
+
import {
|
|
13
|
+
generateTypeScript,
|
|
14
|
+
setOptionalTurboModule,
|
|
15
|
+
} from './generators/GenerateTypeScript';
|
|
16
|
+
import type {SchemaType} from 'react-native-tscodegen';
|
|
17
|
+
|
|
18
|
+
// Load react-native-codegen from react-native
|
|
19
|
+
const rnPath = path.dirname(require.resolve('react-native/package.json'));
|
|
20
|
+
const rncodegenPath = path.dirname(
|
|
21
|
+
require.resolve('react-native-codegen/package.json', {paths: [rnPath]}),
|
|
22
|
+
);
|
|
23
|
+
const FlowParser = require(path.resolve(rncodegenPath, 'lib/parsers/flow'));
|
|
24
|
+
const TypeScriptParser = require(path.resolve(
|
|
25
|
+
rncodegenPath,
|
|
26
|
+
'lib/parsers/typescript',
|
|
27
|
+
));
|
|
28
|
+
|
|
29
|
+
const schemaValidator = require(path.resolve(
|
|
30
|
+
rncodegenPath,
|
|
31
|
+
'lib/schemaValidator',
|
|
32
|
+
));
|
|
33
|
+
|
|
34
|
+
interface Options {
|
|
35
|
+
libraryName: string;
|
|
36
|
+
methodOnly: boolean;
|
|
37
|
+
modulesCxx: boolean;
|
|
38
|
+
moduleSpecName: string;
|
|
39
|
+
modulesTypeScriptTypes: boolean;
|
|
40
|
+
modulesWindows: boolean;
|
|
41
|
+
namespace: string;
|
|
42
|
+
outputDirectory: string;
|
|
43
|
+
schema: SchemaType;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface Config {
|
|
47
|
+
generators: any[] /*Generators[]*/;
|
|
48
|
+
test?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeFileMap(
|
|
52
|
+
map: Map<string, string>,
|
|
53
|
+
outputDir: string,
|
|
54
|
+
outMap: Map<string, string>,
|
|
55
|
+
): void {
|
|
56
|
+
for (const [fileName, contents] of map) {
|
|
57
|
+
const location = path.join(outputDir, fileName);
|
|
58
|
+
outMap.set(path.normalize(location), contents);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function checkFilesForChanges(
|
|
63
|
+
map: Map<string, string>,
|
|
64
|
+
outputDir: string,
|
|
65
|
+
): boolean {
|
|
66
|
+
let hasChanges = false;
|
|
67
|
+
|
|
68
|
+
const allExistingFiles = globby
|
|
69
|
+
.sync(`${outputDir}/**`)
|
|
70
|
+
.map(_ => path.normalize(_))
|
|
71
|
+
.sort();
|
|
72
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
allExistingFiles.length !== allGeneratedFiles.length ||
|
|
76
|
+
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index])
|
|
77
|
+
)
|
|
78
|
+
return true;
|
|
79
|
+
|
|
80
|
+
for (const [fileName, contents] of map) {
|
|
81
|
+
if (!fs.existsSync(fileName)) {
|
|
82
|
+
hasChanges = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const currentContents = fs.readFileSync(fileName, 'utf8');
|
|
87
|
+
if (currentContents !== contents) {
|
|
88
|
+
console.log(`- ${fileName} has changed`);
|
|
89
|
+
hasChanges = true;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return hasChanges;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
98
|
+
let success = true;
|
|
99
|
+
|
|
100
|
+
// This ensures that we delete any generated files from modules that have been deleted
|
|
101
|
+
const allExistingFiles = globby.sync(`${outputDir}/**`);
|
|
102
|
+
allExistingFiles.forEach(existingFile => {
|
|
103
|
+
if (!map.has(path.normalize(existingFile))) {
|
|
104
|
+
fs.unlinkSync(existingFile);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
for (const [fileName, contents] of map) {
|
|
109
|
+
try {
|
|
110
|
+
fs.mkdirSync(path.dirname(fileName), {recursive: true});
|
|
111
|
+
|
|
112
|
+
if (fs.existsSync(fileName)) {
|
|
113
|
+
const currentContents = fs.readFileSync(fileName, 'utf8');
|
|
114
|
+
// Don't update the files if there are no changes as this breaks incremental builds
|
|
115
|
+
if (currentContents === contents) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fs.writeFileSync(fileName, contents);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
success = false;
|
|
123
|
+
console.error(`Failed to write ${fileName} to ${fileName}`, error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return success;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function parseFile(filename: string): SchemaType {
|
|
131
|
+
try {
|
|
132
|
+
const isTypeScript =
|
|
133
|
+
path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';
|
|
134
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
135
|
+
const schema = isTypeScript
|
|
136
|
+
? TypeScriptParser.parseString(contents, filename)
|
|
137
|
+
: FlowParser.parseString(contents, filename);
|
|
138
|
+
// there will be at most one turbo module per file
|
|
139
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
140
|
+
if (moduleName) {
|
|
141
|
+
const spec = schema.modules[moduleName];
|
|
142
|
+
if (spec.type === 'NativeModule') {
|
|
143
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
144
|
+
if (contents) {
|
|
145
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
146
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
147
|
+
setOptionalTurboModule(spec, true);
|
|
148
|
+
} else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
149
|
+
setOptionalTurboModule(spec, false);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return schema;
|
|
155
|
+
} catch (e) {
|
|
156
|
+
if (e instanceof Error) {
|
|
157
|
+
e.message = `(${filename}): ${e.message}`;
|
|
158
|
+
}
|
|
159
|
+
throw e;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function combineSchemas(files: string[]): SchemaType {
|
|
164
|
+
return files.reduce(
|
|
165
|
+
(merged, filename) => {
|
|
166
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
167
|
+
if (
|
|
168
|
+
contents &&
|
|
169
|
+
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
|
|
170
|
+
contents.includes('extends TurboModule'))
|
|
171
|
+
) {
|
|
172
|
+
const schema = parseFile(filename);
|
|
173
|
+
merged.modules = {...merged.modules, ...schema.modules};
|
|
174
|
+
}
|
|
175
|
+
return merged;
|
|
176
|
+
},
|
|
177
|
+
{modules: {}},
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function generate(
|
|
182
|
+
{
|
|
183
|
+
libraryName,
|
|
184
|
+
methodOnly,
|
|
185
|
+
modulesCxx,
|
|
186
|
+
moduleSpecName,
|
|
187
|
+
modulesTypeScriptTypes,
|
|
188
|
+
modulesWindows,
|
|
189
|
+
namespace,
|
|
190
|
+
outputDirectory,
|
|
191
|
+
schema,
|
|
192
|
+
}: Options,
|
|
193
|
+
{/*generators,*/ test}: Config,
|
|
194
|
+
): boolean {
|
|
195
|
+
schemaValidator.validate(schema);
|
|
196
|
+
|
|
197
|
+
const componentOutputdir = path.join(
|
|
198
|
+
outputDirectory,
|
|
199
|
+
'react/components',
|
|
200
|
+
libraryName,
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const generatedFiles = new Map<string, string>();
|
|
204
|
+
|
|
205
|
+
generatedFiles.set(
|
|
206
|
+
path.join(outputDirectory, '.clang-format'),
|
|
207
|
+
'DisableFormat: true\nSortIncludes: false',
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
const generateNM2 = createNM2Generator({
|
|
211
|
+
methodOnly,
|
|
212
|
+
namespace,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const generateJsiModuleH = require(path.resolve(
|
|
216
|
+
rncodegenPath,
|
|
217
|
+
'lib/generators/modules/GenerateModuleH',
|
|
218
|
+
)).generate;
|
|
219
|
+
const generateJsiModuleCpp = require(path.resolve(
|
|
220
|
+
rncodegenPath,
|
|
221
|
+
'lib/generators/modules/GenerateModuleCpp',
|
|
222
|
+
)).generate;
|
|
223
|
+
const generatorPropsH = require(path.resolve(
|
|
224
|
+
rncodegenPath,
|
|
225
|
+
'lib/generators/components/GeneratePropsH',
|
|
226
|
+
)).generate;
|
|
227
|
+
const generatorPropsCPP = require(path.resolve(
|
|
228
|
+
rncodegenPath,
|
|
229
|
+
'lib/generators/components/GeneratePropsCPP',
|
|
230
|
+
)).generate;
|
|
231
|
+
const generatorShadowNodeH = require(path.resolve(
|
|
232
|
+
rncodegenPath,
|
|
233
|
+
'lib/generators/components/GenerateShadowNodeH',
|
|
234
|
+
)).generate;
|
|
235
|
+
const generatorShadowNodeCPP = require(path.resolve(
|
|
236
|
+
rncodegenPath,
|
|
237
|
+
'lib/generators/components/GenerateShadowNodeCPP',
|
|
238
|
+
)).generate;
|
|
239
|
+
const generatorComponentDescriptorH = require(path.resolve(
|
|
240
|
+
rncodegenPath,
|
|
241
|
+
'lib/generators/components/GenerateComponentDescriptorH',
|
|
242
|
+
)).generate;
|
|
243
|
+
const generatorEventEmitterH = require(path.resolve(
|
|
244
|
+
rncodegenPath,
|
|
245
|
+
'lib/generators/components/GenerateEventEmitterH',
|
|
246
|
+
)).generate;
|
|
247
|
+
const generatorEventEmitterCPP = require(path.resolve(
|
|
248
|
+
rncodegenPath,
|
|
249
|
+
'lib/generators/components/GenerateEventEmitterCpp',
|
|
250
|
+
)).generate;
|
|
251
|
+
const generatorStateCPP = require(path.resolve(
|
|
252
|
+
rncodegenPath,
|
|
253
|
+
'lib/generators/components/GenerateStateCpp',
|
|
254
|
+
)).generate;
|
|
255
|
+
const generatorStateH = require(path.resolve(
|
|
256
|
+
rncodegenPath,
|
|
257
|
+
'lib/generators/components/GenerateStateH',
|
|
258
|
+
)).generate;
|
|
259
|
+
|
|
260
|
+
const moduleGenerators = [];
|
|
261
|
+
|
|
262
|
+
if (modulesWindows) {
|
|
263
|
+
moduleGenerators.push(generateNM2);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (modulesCxx) {
|
|
267
|
+
moduleGenerators.push(generateJsiModuleH);
|
|
268
|
+
moduleGenerators.push(generateJsiModuleCpp);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (modulesTypeScriptTypes) {
|
|
272
|
+
moduleGenerators.push(generateTypeScript);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
moduleGenerators.forEach(generator => {
|
|
276
|
+
const generated: Map<string, string> = generator(
|
|
277
|
+
libraryName,
|
|
278
|
+
schema,
|
|
279
|
+
moduleSpecName,
|
|
280
|
+
);
|
|
281
|
+
normalizeFileMap(generated, outputDirectory, generatedFiles);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
if (
|
|
285
|
+
Object.keys(schema.modules).some(
|
|
286
|
+
moduleName => schema.modules[moduleName].type === 'Component',
|
|
287
|
+
)
|
|
288
|
+
) {
|
|
289
|
+
const componentGenerators = [
|
|
290
|
+
generatorComponentDescriptorH,
|
|
291
|
+
generatorEventEmitterCPP,
|
|
292
|
+
generatorEventEmitterH,
|
|
293
|
+
generatorPropsCPP,
|
|
294
|
+
generatorPropsH,
|
|
295
|
+
generatorShadowNodeCPP,
|
|
296
|
+
generatorShadowNodeH,
|
|
297
|
+
generatorStateCPP,
|
|
298
|
+
generatorStateH,
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
componentGenerators.forEach(generator => {
|
|
302
|
+
const generated: Map<string, string> = generator(
|
|
303
|
+
libraryName,
|
|
304
|
+
schema,
|
|
305
|
+
moduleSpecName,
|
|
306
|
+
);
|
|
307
|
+
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (test === true) {
|
|
312
|
+
return checkFilesForChanges(generatedFiles, outputDirectory);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return writeMapToFiles(generatedFiles, outputDirectory);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export type CodeGenOptions = {
|
|
319
|
+
file?: string;
|
|
320
|
+
files?: string[];
|
|
321
|
+
libraryName: string;
|
|
322
|
+
methodOnly: boolean;
|
|
323
|
+
modulesCxx: boolean;
|
|
324
|
+
modulesTypeScriptTypes: boolean;
|
|
325
|
+
modulesWindows: boolean;
|
|
326
|
+
namespace: string;
|
|
327
|
+
outputDirectory: string;
|
|
328
|
+
test: boolean;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
export function runCodeGen(options: CodeGenOptions): boolean {
|
|
332
|
+
if (!options.file && !options.files)
|
|
333
|
+
throw new Error('Must specify file or files option');
|
|
334
|
+
|
|
335
|
+
const schema = options.file
|
|
336
|
+
? parseFile(options.file)
|
|
337
|
+
: combineSchemas(globby.sync(options.files!));
|
|
338
|
+
|
|
339
|
+
const libraryName = options.libraryName;
|
|
340
|
+
const moduleSpecName = 'moduleSpecName';
|
|
341
|
+
const {
|
|
342
|
+
methodOnly,
|
|
343
|
+
modulesCxx,
|
|
344
|
+
modulesTypeScriptTypes,
|
|
345
|
+
modulesWindows,
|
|
346
|
+
namespace,
|
|
347
|
+
outputDirectory,
|
|
348
|
+
} = options;
|
|
349
|
+
return generate(
|
|
350
|
+
{
|
|
351
|
+
libraryName,
|
|
352
|
+
methodOnly,
|
|
353
|
+
modulesCxx,
|
|
354
|
+
moduleSpecName,
|
|
355
|
+
modulesTypeScriptTypes,
|
|
356
|
+
modulesWindows,
|
|
357
|
+
namespace,
|
|
358
|
+
outputDirectory,
|
|
359
|
+
schema,
|
|
360
|
+
},
|
|
361
|
+
{generators: [], test: options.test},
|
|
362
|
+
);
|
|
363
|
+
}
|