@react-native-windows/codegen 0.68.1 → 0.70.0-preview.1
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 +103 -20
- package/lib-commonjs/Cli.js +50 -10
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/GenerateNM2.d.ts +2 -1
- package/lib-commonjs/generators/GenerateNM2.js +4 -4
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -1
- package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
- package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.js +2 -2
- package/lib-commonjs/generators/ParamTypes.js.map +1 -1
- package/lib-commonjs/generators/ValidateConstants.js +1 -1
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -1
- package/lib-commonjs/generators/ValidateMethods.js +2 -2
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -1
- package/package.json +11 -11
- package/src/Cli.ts +59 -10
- package/src/generators/GenerateNM2.ts +10 -4
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ParamTypes.ts +2 -2
- package/src/generators/ValidateConstants.ts +1 -1
- package/src/generators/ValidateMethods.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,45 +1,128 @@
|
|
|
1
1
|
# Change Log - @react-native-windows/codegen
|
|
2
2
|
|
|
3
|
-
This log was last generated on Mon,
|
|
3
|
+
This log was last generated on Mon, 22 Aug 2022 20:19:25 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
-
## 0.
|
|
7
|
+
## 0.70.0-preview.1
|
|
8
8
|
|
|
9
|
-
Mon,
|
|
9
|
+
Mon, 22 Aug 2022 20:19:25 GMT
|
|
10
10
|
|
|
11
|
-
###
|
|
11
|
+
### Changes
|
|
12
12
|
|
|
13
|
-
- Promote 0.
|
|
14
|
-
- Bump @react-native-windows/fs to v0.
|
|
13
|
+
- Promote 0.70 to preview (34109996+chiaramooney@users.noreply.github.com)
|
|
14
|
+
- Bump @react-native-windows/fs to v0.70.0-preview.1
|
|
15
15
|
|
|
16
|
-
## 0.
|
|
16
|
+
## 0.0.0-canary.35
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Sat, 04 Jun 2022 05:10:05 GMT
|
|
19
19
|
|
|
20
|
-
###
|
|
20
|
+
### Changes
|
|
21
|
+
|
|
22
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.5
|
|
23
|
+
- Bump @rnw-scripts/jest-unittest-config to v1.2.8
|
|
24
|
+
- Bump @rnw-scripts/just-task to v2.2.5
|
|
25
|
+
|
|
26
|
+
## 0.0.0-canary.34
|
|
27
|
+
|
|
28
|
+
Thu, 12 May 2022 05:09:43 GMT
|
|
29
|
+
|
|
30
|
+
### Changes
|
|
31
|
+
|
|
32
|
+
- Build/dependency fixes (ngerlem@microsoft.com)
|
|
33
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.4
|
|
34
|
+
- Bump @rnw-scripts/eslint-config to v1.1.13
|
|
35
|
+
- Bump @rnw-scripts/jest-unittest-config to v1.2.7
|
|
36
|
+
- Bump @rnw-scripts/just-task to v2.2.4
|
|
37
|
+
|
|
38
|
+
## 0.0.0-canary.33
|
|
39
|
+
|
|
40
|
+
Wed, 11 May 2022 05:08:51 GMT
|
|
41
|
+
|
|
42
|
+
### Changes
|
|
43
|
+
|
|
44
|
+
- Add --methodonly to RNW/codegen (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
45
|
+
|
|
46
|
+
## 0.0.0-canary.32
|
|
47
|
+
|
|
48
|
+
Wed, 27 Apr 2022 05:10:23 GMT
|
|
49
|
+
|
|
50
|
+
### Changes
|
|
51
|
+
|
|
52
|
+
- Merge (34109996+chiaramooney@users.noreply.github.com)
|
|
53
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.3
|
|
54
|
+
- Bump @rnw-scripts/eslint-config to v1.1.12
|
|
55
|
+
|
|
56
|
+
## 0.0.0-canary.31
|
|
57
|
+
|
|
58
|
+
Mon, 25 Apr 2022 22:50:30 GMT
|
|
59
|
+
|
|
60
|
+
### Changes
|
|
61
|
+
|
|
62
|
+
- Update globby of @react-native-windows/codegen to remove glob-parent 3.x dependency (dannyvv@microsoft.com)
|
|
63
|
+
|
|
64
|
+
## 0.0.0-canary.30
|
|
65
|
+
|
|
66
|
+
Tue, 12 Apr 2022 05:10:38 GMT
|
|
67
|
+
|
|
68
|
+
### Changes
|
|
69
|
+
|
|
70
|
+
- Fix Overrides (34109996+chiaramooney@users.noreply.github.com)
|
|
71
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.2
|
|
72
|
+
|
|
73
|
+
## 0.0.0-canary.29
|
|
74
|
+
|
|
75
|
+
Fri, 01 Apr 2022 05:09:56 GMT
|
|
76
|
+
|
|
77
|
+
### Changes
|
|
78
|
+
|
|
79
|
+
- Fix publish after promotion to preview (ngerlem@microsoft.com)
|
|
80
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.1
|
|
81
|
+
|
|
82
|
+
## 0.0.0-canary.28
|
|
83
|
+
|
|
84
|
+
Wed, 09 Mar 2022 06:09:07 GMT
|
|
85
|
+
|
|
86
|
+
### Changes
|
|
87
|
+
|
|
88
|
+
- Fix turbo module codegen to generate correct import statement and registration (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
89
|
+
|
|
90
|
+
## 0.0.0-canary.27
|
|
91
|
+
|
|
92
|
+
Fri, 04 Mar 2022 06:09:13 GMT
|
|
93
|
+
|
|
94
|
+
### Changes
|
|
95
|
+
|
|
96
|
+
- Generate fabric eventemitter.cpp (acoates@microsoft.com)
|
|
97
|
+
|
|
98
|
+
## 0.0.0-canary.26
|
|
99
|
+
|
|
100
|
+
Thu, 24 Feb 2022 06:07:47 GMT
|
|
101
|
+
|
|
102
|
+
### Changes
|
|
21
103
|
|
|
22
|
-
-
|
|
23
|
-
- Bump @react-native-windows/fs to v0.68.0
|
|
104
|
+
- Add TypeScript turbo module definition as new codegen traget (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
24
105
|
|
|
25
|
-
## 0.
|
|
106
|
+
## 0.0.0-canary.25
|
|
26
107
|
|
|
27
|
-
|
|
108
|
+
Wed, 09 Feb 2022 06:09:36 GMT
|
|
28
109
|
|
|
29
110
|
### Changes
|
|
30
111
|
|
|
31
|
-
-
|
|
32
|
-
- Bump @react-native-windows/fs to
|
|
112
|
+
- Bump minimum Node version to 14 (jthysell@microsoft.com)
|
|
113
|
+
- Bump @react-native-windows/fs to v1.0.2
|
|
114
|
+
- Bump @rnw-scripts/eslint-config to v1.1.11
|
|
115
|
+
- Bump @rnw-scripts/jest-unittest-config to v1.2.6
|
|
116
|
+
- Bump @rnw-scripts/just-task to v2.2.3
|
|
117
|
+
- Bump @rnw-scripts/ts-config to v2.0.2
|
|
33
118
|
|
|
34
|
-
## 0.
|
|
119
|
+
## 0.0.0-canary.24
|
|
35
120
|
|
|
36
|
-
|
|
121
|
+
Tue, 08 Feb 2022 18:21:23 GMT
|
|
37
122
|
|
|
38
123
|
### Changes
|
|
39
124
|
|
|
40
|
-
-
|
|
41
|
-
- Move @react-native-windows/fs to promoteRelease scheme (ngerlem@microsoft.com)
|
|
42
|
-
- Bump @react-native-windows/fs to v0.68.0-preview.1
|
|
125
|
+
- Fix TurboModule codegen document (53799235+ZihanChen-MSFT@users.noreply.github.com)
|
|
43
126
|
|
|
44
127
|
## 0.0.0-canary.23
|
|
45
128
|
|
package/lib-commonjs/Cli.js
CHANGED
|
@@ -14,6 +14,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
14
14
|
const fs_1 = __importDefault(require("@react-native-windows/fs"));
|
|
15
15
|
const globby_1 = __importDefault(require("globby"));
|
|
16
16
|
const GenerateNM2_1 = require("./generators/GenerateNM2");
|
|
17
|
+
const GenerateTypeScript_1 = require("./generators/GenerateTypeScript");
|
|
17
18
|
// @ts-ignore
|
|
18
19
|
const flow_1 = require("react-native-tscodegen/lib/rncodegen/src/parsers/flow");
|
|
19
20
|
// @ts-ignore
|
|
@@ -27,6 +28,21 @@ const argv = yargs_1.default.options({
|
|
|
27
28
|
type: 'array',
|
|
28
29
|
describe: 'glob patterns for files which contains specs',
|
|
29
30
|
},
|
|
31
|
+
ts: {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
describe: 'generate turbo module definition files in TypeScript',
|
|
34
|
+
default: false,
|
|
35
|
+
},
|
|
36
|
+
methodonly: {
|
|
37
|
+
type: 'boolean',
|
|
38
|
+
describe: 'generate only method metadata in C++ turbo module spec',
|
|
39
|
+
default: false,
|
|
40
|
+
},
|
|
41
|
+
outdir: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
describe: 'output directory',
|
|
44
|
+
default: 'codegen',
|
|
45
|
+
},
|
|
30
46
|
test: {
|
|
31
47
|
type: 'boolean',
|
|
32
48
|
describe: 'Verify that the generated output is unchanged',
|
|
@@ -76,11 +92,9 @@ function checkFilesForChanges(map, outputDir) {
|
|
|
76
92
|
let hasChanges = false;
|
|
77
93
|
const allExistingFiles = globby_1.default
|
|
78
94
|
.sync(`${outputDir}/**`)
|
|
79
|
-
.map(
|
|
80
|
-
.sort();
|
|
81
|
-
const allGeneratedFiles = [...map.keys()]
|
|
82
|
-
.map((_) => path_1.default.normalize(_))
|
|
95
|
+
.map(_ => path_1.default.normalize(_))
|
|
83
96
|
.sort();
|
|
97
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path_1.default.normalize(_)).sort();
|
|
84
98
|
if (allExistingFiles.length !== allGeneratedFiles.length ||
|
|
85
99
|
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index]))
|
|
86
100
|
return true;
|
|
@@ -102,7 +116,7 @@ function writeMapToFiles(map, outputDir) {
|
|
|
102
116
|
let success = true;
|
|
103
117
|
// This ensures that we delete any generated files from modules that have been deleted
|
|
104
118
|
const allExistingFiles = globby_1.default.sync(`${outputDir}/**`);
|
|
105
|
-
allExistingFiles.forEach(
|
|
119
|
+
allExistingFiles.forEach(existingFile => {
|
|
106
120
|
if (!map.has(path_1.default.normalize(existingFile))) {
|
|
107
121
|
fs_1.default.unlinkSync(existingFile);
|
|
108
122
|
}
|
|
@@ -128,7 +142,25 @@ function writeMapToFiles(map, outputDir) {
|
|
|
128
142
|
}
|
|
129
143
|
function parseFlowFile(filename) {
|
|
130
144
|
try {
|
|
131
|
-
|
|
145
|
+
const schema = (0, flow_1.parseFile)(filename);
|
|
146
|
+
// there will be at most one turbo module per file
|
|
147
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
148
|
+
if (moduleName) {
|
|
149
|
+
const spec = schema.modules[moduleName];
|
|
150
|
+
if (spec.type === 'NativeModule') {
|
|
151
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
152
|
+
if (contents) {
|
|
153
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
154
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
155
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, true);
|
|
156
|
+
}
|
|
157
|
+
else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
158
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, false);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return schema;
|
|
132
164
|
}
|
|
133
165
|
catch (e) {
|
|
134
166
|
if (e instanceof Error) {
|
|
@@ -154,15 +186,22 @@ function generate({ libraryName, schema, outputDirectory, moduleSpecName }, { /*
|
|
|
154
186
|
const componentOutputdir = path_1.default.join(outputDirectory, 'react/components', libraryName);
|
|
155
187
|
const generatedFiles = new Map();
|
|
156
188
|
generatedFiles.set(path_1.default.join(outputDirectory, '.clang-format'), 'DisableFormat: true\nSortIncludes: false');
|
|
157
|
-
const generateNM2 = (0, GenerateNM2_1.createNM2Generator)({
|
|
189
|
+
const generateNM2 = (0, GenerateNM2_1.createNM2Generator)({
|
|
190
|
+
namespace: argv.namespace,
|
|
191
|
+
methodonly: argv.methodonly,
|
|
192
|
+
});
|
|
158
193
|
const generatorPropsH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;
|
|
159
194
|
const generatorPropsCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP').generate;
|
|
160
195
|
const generatorShadowNodeH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH').generate;
|
|
161
196
|
const generatorShadowNodeCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;
|
|
162
197
|
const generatorComponentDescriptorH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;
|
|
163
198
|
const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;
|
|
199
|
+
const generatorEventEmitterCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterCpp').generate;
|
|
164
200
|
normalizeFileMap(generateNM2(libraryName, schema, moduleSpecName), outputDirectory, generatedFiles);
|
|
165
|
-
if (
|
|
201
|
+
if (argv.ts) {
|
|
202
|
+
normalizeFileMap((0, GenerateTypeScript_1.generateTypeScript)(libraryName, schema, moduleSpecName), outputDirectory, generatedFiles);
|
|
203
|
+
}
|
|
204
|
+
if (Object.keys(schema.modules).some(moduleName => schema.modules[moduleName].type === 'Component')) {
|
|
166
205
|
const componentGenerators = [
|
|
167
206
|
generatorPropsH,
|
|
168
207
|
generatorPropsCPP,
|
|
@@ -170,8 +209,9 @@ function generate({ libraryName, schema, outputDirectory, moduleSpecName }, { /*
|
|
|
170
209
|
generatorShadowNodeCPP,
|
|
171
210
|
generatorComponentDescriptorH,
|
|
172
211
|
generatorEventEmitterH,
|
|
212
|
+
generatorEventEmitterCPP,
|
|
173
213
|
];
|
|
174
|
-
componentGenerators.forEach(
|
|
214
|
+
componentGenerators.forEach(generator => {
|
|
175
215
|
const generated = generator(libraryName, schema, moduleSpecName);
|
|
176
216
|
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
177
217
|
});
|
|
@@ -194,6 +234,6 @@ else {
|
|
|
194
234
|
}
|
|
195
235
|
const libraryName = argv.libraryName;
|
|
196
236
|
const moduleSpecName = 'moduleSpecName';
|
|
197
|
-
const outputDirectory =
|
|
237
|
+
const outputDirectory = argv.outdir;
|
|
198
238
|
generate({ libraryName, schema, outputDirectory, moduleSpecName }, { generators: [], test: false });
|
|
199
239
|
//# sourceMappingURL=Cli.js.map
|
package/lib-commonjs/Cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,gDAAwB;AACxB,kEAA0C;AAC1C,oDAA4B;AAC5B,0DAA4D;AAC5D,aAAa;AACb,gFAAgF;AAChF,aAAa;AACb,+GAAuF;AAEvF,MAAM,IAAI,GAAG,eAAK,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,0BAA0B;KACrC;IACD,KAAK,EAAE;QACL,IAAI,EAAE,OAAO;QACb,QAAQ,EAAE,8CAA8C;KACzD;IACD,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,+CAA+C;KAC1D;IACD,SAAS,EAAE;QACT,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,qDAAqD;QAC/D,OAAO,EAAE,aAAa;KACvB;IACD,WAAW,EAAE;QACX,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,IAAI;QACd,QAAQ,EAAE,4DAA4D;KACvE;CACF,CAAC,CAAC,IAAI,CAAC;AAgBR;;;;;;;;;;;;;;;;;;;;;;;EAuBE;AAEF,SAAS,gBAAgB,CACvB,GAAwB,EACxB,S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AE;QACjB,OAAO,oBAAoB,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;KAC9D;IAED,OAAO,eAAe,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;AAC1D,CAAC;AAED,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;IAC5D,OAAO,CAAC,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAC5D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CACjB;AAED,IAAI,MAAkB,CAAC;AACvB,IAAI,IAAI,CAAC,IAAI,EAAE;IACb,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;CACnC;KAAM;IACL,MAAM,GAAG,cAAc,CAAC,gBAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAiB,CAAC,CAAC,CAAC;CAC9D;AAED,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;AACrC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AACxC,MAAM,eAAe,GAAG,SAAS,CAAC;AAClC,QAAQ,CACN,EAAC,WAAW,EAAE,MAAM,EAAE,eAAe,EAAE,cAAc,EAAC,EACtD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAC,CAC9B,CAAC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport {createNM2Generator} from './generators/GenerateNM2';\n// @ts-ignore\nimport {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';\n// @ts-ignore\nimport schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'array',\n describe: 'glob patterns for files which contains specs',\n },\n test: {\n type: 'boolean',\n describe: 'Verify that the generated output is unchanged',\n },\n namespace: {\n type: 'string',\n describe: 'C++/C# Namespace to put generated native modules in',\n default: 'MyNamespace',\n },\n libraryName: {\n type: 'string',\n required: true,\n describe: 'Used for part of the path generated within the codegen dir',\n },\n}).argv;\n\nimport {SchemaType} from 'react-native-tscodegen';\n\ninterface Options {\n libraryName: string;\n schema: SchemaType;\n outputDirectory: string;\n moduleSpecName: string;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\n}\n\n/*\nconst GENERATORS = {\n descriptors: [generateComponentDescriptorH.generate],\n events: [\n generateEventEmitterCpp.generate,\n generateEventEmitterH.generate,\n generateModuleHObjCpp.generate,\n generateModuleMm.generate,\n ],\n props: [\n generateComponentHObjCpp.generate,\n generatePropsCpp.generate,\n generatePropsH.generate,\n generatePropsJavaInterface.generate,\n generatePropsJavaDelegate.generate,\n ],\n modules: [generateModuleCpp.generate, generateModuleH.generate],\n tests: [generateTests.generate],\n 'shadow-nodes': [\n generateShadowNodeCpp.generate,\n generateShadowNodeH.generate,\n ],\n};\n*/\n\nfunction normalizeFileMap(\n map: Map<string, string>,\n outputDir: string,\n outMap: Map<string, string>,\n): void {\n for (const [fileName, contents] of map) {\n const location = path.join(outputDir, fileName);\n outMap.set(path.normalize(location), contents);\n }\n}\n\nfunction checkFilesForChanges(\n map: Map<string, string>,\n outputDir: string,\n): boolean {\n let hasChanges = false;\n\n const allExistingFiles = globby\n .sync(`${outputDir}/**`)\n .map((_) => path.normalize(_))\n .sort();\n const allGeneratedFiles = [...map.keys()]\n .map((_) => path.normalize(_))\n .sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])\n )\n return true;\n\n for (const [fileName, contents] of map) {\n if (!fs.existsSync(fileName)) {\n hasChanges = true;\n continue;\n }\n\n const currentContents = fs.readFileSync(fileName, 'utf8');\n if (currentContents !== contents) {\n console.error(`- ${fileName} has changed`);\n hasChanges = true;\n continue;\n }\n }\n\n return hasChanges;\n}\n\nfunction writeMapToFiles(map: Map<string, string>, outputDir: string) {\n let success = true;\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(`${outputDir}/**`);\n allExistingFiles.forEach((existingFile) => {\n if (!map.has(path.normalize(existingFile))) {\n fs.unlinkSync(existingFile);\n }\n });\n\n for (const [fileName, contents] of map) {\n try {\n fs.mkdirSync(path.dirname(fileName), {recursive: true});\n\n if (fs.existsSync(fileName)) {\n const currentContents = fs.readFileSync(fileName, 'utf8');\n // Don't update the files if there are no changes as this breaks incremental builds\n if (currentContents === contents) {\n continue;\n }\n }\n\n fs.writeFileSync(fileName, contents);\n } catch (error) {\n success = false;\n console.error(`Failed to write ${fileName} to ${fileName}`, error);\n }\n }\n\n return success;\n}\n\nfunction parseFlowFile(filename: string): SchemaType {\n try {\n return parseFile(filename);\n } catch (e) {\n if (e instanceof Error) {\n e.message = `(${filename}): ${e.message}`;\n }\n throw e;\n }\n}\n\nfunction combineSchemas(files: string[]): SchemaType {\n return files.reduce(\n (merged, filename) => {\n const contents = fs.readFileSync(filename, 'utf8');\n if (\n contents &&\n (/export\\s+default\\s+\\(?codegenNativeComponent</.test(contents) ||\n contents.includes('extends TurboModule'))\n ) {\n const schema = parseFlowFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nfunction generate(\n {libraryName, schema, outputDirectory, moduleSpecName}: Options,\n {/*generators,*/ test}: Config,\n): boolean {\n schemaValidator.validate(schema);\n\n const componentOutputdir = path.join(\n outputDirectory,\n 'react/components',\n libraryName,\n );\n\n const generatedFiles = new Map<string, string>();\n\n generatedFiles.set(\n path.join(outputDirectory, '.clang-format'),\n 'DisableFormat: true\\nSortIncludes: false',\n );\n\n const generateNM2 = createNM2Generator({namespace: argv.namespace});\n const generatorPropsH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;\n const generatorPropsCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP').generate;\n const generatorShadowNodeH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH').generate;\n const generatorShadowNodeCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;\n const generatorComponentDescriptorH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;\n const generatorEventEmitterH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;\n\n normalizeFileMap(\n generateNM2(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n\n if (\n Object.keys(schema.modules).some(\n (moduleName) => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorPropsH,\n generatorPropsCPP,\n generatorShadowNodeH,\n generatorShadowNodeCPP,\n generatorComponentDescriptorH,\n generatorEventEmitterH,\n ];\n\n componentGenerators.forEach((generator) => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, componentOutputdir, generatedFiles);\n });\n }\n\n if (test === true) {\n return checkFilesForChanges(generatedFiles, outputDirectory);\n }\n\n return writeMapToFiles(generatedFiles, outputDirectory);\n}\n\nif ((argv.file && argv.files) || (!argv.file && !argv.files)) {\n console.error('You must specify either --file or --files.');\n process.exit(1);\n}\n\nlet schema: SchemaType;\nif (argv.file) {\n schema = parseFlowFile(argv.file);\n} else {\n schema = combineSchemas(globby.sync(argv.files as string[]));\n}\n\nconst libraryName = argv.libraryName;\nconst moduleSpecName = 'moduleSpecName';\nconst outputDirectory = 'codegen';\ngenerate(\n {libraryName, schema, outputDirectory, moduleSpecName},\n {generators: [], test: false},\n);\n"]}
|
|
1
|
+
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAA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MAAM,GAAG,cAAc,CAAC,gBAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAiB,CAAC,CAAC,CAAC;CAC9D;AAED,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;AACrC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AACxC,MAAM,eAAe,GAAG,IAAI,CAAC,MAAM,CAAC;AACpC,QAAQ,CACN,EAAC,WAAW,EAAE,MAAM,EAAE,eAAe,EAAE,cAAc,EAAC,EACtD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAC,CAC9B,CAAC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport {createNM2Generator} from './generators/GenerateNM2';\nimport {\n generateTypeScript,\n setOptionalTurboModule,\n} from './generators/GenerateTypeScript';\n// @ts-ignore\nimport {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';\n// @ts-ignore\nimport schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'array',\n describe: 'glob patterns for files which contains specs',\n },\n ts: {\n type: 'boolean',\n describe: 'generate turbo module definition files in TypeScript',\n default: false,\n },\n methodonly: {\n type: 'boolean',\n describe: 'generate only method metadata in C++ turbo module spec',\n default: false,\n },\n outdir: {\n type: 'string',\n describe: 'output directory',\n default: 'codegen',\n },\n test: {\n type: 'boolean',\n describe: 'Verify that the generated output is unchanged',\n },\n namespace: {\n type: 'string',\n describe: 'C++/C# Namespace to put generated native modules in',\n default: 'MyNamespace',\n },\n libraryName: {\n type: 'string',\n required: true,\n describe: 'Used for part of the path generated within the codegen dir',\n },\n}).argv;\n\nimport {SchemaType} from 'react-native-tscodegen';\n\ninterface Options {\n libraryName: string;\n schema: SchemaType;\n outputDirectory: string;\n moduleSpecName: string;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\n}\n\n/*\nconst GENERATORS = {\n descriptors: [generateComponentDescriptorH.generate],\n events: [\n generateEventEmitterCpp.generate,\n generateEventEmitterH.generate,\n generateModuleHObjCpp.generate,\n generateModuleMm.generate,\n ],\n props: [\n generateComponentHObjCpp.generate,\n generatePropsCpp.generate,\n generatePropsH.generate,\n generatePropsJavaInterface.generate,\n generatePropsJavaDelegate.generate,\n ],\n modules: [generateModuleCpp.generate, generateModuleH.generate],\n tests: [generateTests.generate],\n 'shadow-nodes': [\n generateShadowNodeCpp.generate,\n generateShadowNodeH.generate,\n ],\n};\n*/\n\nfunction normalizeFileMap(\n map: Map<string, string>,\n outputDir: string,\n outMap: Map<string, string>,\n): void {\n for (const [fileName, contents] of map) {\n const location = path.join(outputDir, fileName);\n outMap.set(path.normalize(location), contents);\n }\n}\n\nfunction checkFilesForChanges(\n map: Map<string, string>,\n outputDir: string,\n): boolean {\n let hasChanges = false;\n\n const allExistingFiles = globby\n .sync(`${outputDir}/**`)\n .map(_ => path.normalize(_))\n .sort();\n const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])\n )\n return true;\n\n for (const [fileName, contents] of map) {\n if (!fs.existsSync(fileName)) {\n hasChanges = true;\n continue;\n }\n\n const currentContents = fs.readFileSync(fileName, 'utf8');\n if (currentContents !== contents) {\n console.error(`- ${fileName} has changed`);\n hasChanges = true;\n continue;\n }\n }\n\n return hasChanges;\n}\n\nfunction writeMapToFiles(map: Map<string, string>, outputDir: string) {\n let success = true;\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(`${outputDir}/**`);\n allExistingFiles.forEach(existingFile => {\n if (!map.has(path.normalize(existingFile))) {\n fs.unlinkSync(existingFile);\n }\n });\n\n for (const [fileName, contents] of map) {\n try {\n fs.mkdirSync(path.dirname(fileName), {recursive: true});\n\n if (fs.existsSync(fileName)) {\n const currentContents = fs.readFileSync(fileName, 'utf8');\n // Don't update the files if there are no changes as this breaks incremental builds\n if (currentContents === contents) {\n continue;\n }\n }\n\n fs.writeFileSync(fileName, contents);\n } catch (error) {\n success = false;\n console.error(`Failed to write ${fileName} to ${fileName}`, error);\n }\n }\n\n return success;\n}\n\nfunction parseFlowFile(filename: string): SchemaType {\n try {\n const schema = parseFile(filename);\n // there will be at most one turbo module per file\n const moduleName = Object.keys(schema.modules)[0];\n if (moduleName) {\n const spec = schema.modules[moduleName];\n if (spec.type === 'NativeModule') {\n const contents = fs.readFileSync(filename, 'utf8');\n if (contents) {\n // This is a temporary implementation until such information is added to the schema in facebook/react-native\n if (contents.includes('TurboModuleRegistry.get<')) {\n setOptionalTurboModule(spec, true);\n } else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {\n setOptionalTurboModule(spec, false);\n }\n }\n }\n }\n return schema;\n } catch (e) {\n if (e instanceof Error) {\n e.message = `(${filename}): ${e.message}`;\n }\n throw e;\n }\n}\n\nfunction combineSchemas(files: string[]): SchemaType {\n return files.reduce(\n (merged, filename) => {\n const contents = fs.readFileSync(filename, 'utf8');\n if (\n contents &&\n (/export\\s+default\\s+\\(?codegenNativeComponent</.test(contents) ||\n contents.includes('extends TurboModule'))\n ) {\n const schema = parseFlowFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nfunction generate(\n {libraryName, schema, outputDirectory, moduleSpecName}: Options,\n {/*generators,*/ test}: Config,\n): boolean {\n schemaValidator.validate(schema);\n\n const componentOutputdir = path.join(\n outputDirectory,\n 'react/components',\n libraryName,\n );\n\n const generatedFiles = new Map<string, string>();\n\n generatedFiles.set(\n path.join(outputDirectory, '.clang-format'),\n 'DisableFormat: true\\nSortIncludes: false',\n );\n\n const generateNM2 = createNM2Generator({\n namespace: argv.namespace,\n methodonly: argv.methodonly,\n });\n\n const generatorPropsH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;\n const generatorPropsCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP').generate;\n const generatorShadowNodeH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH').generate;\n const generatorShadowNodeCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;\n const generatorComponentDescriptorH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;\n const generatorEventEmitterH =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;\n const generatorEventEmitterCPP =\n require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterCpp').generate;\n\n normalizeFileMap(\n generateNM2(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n\n if (argv.ts) {\n normalizeFileMap(\n generateTypeScript(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n }\n\n if (\n Object.keys(schema.modules).some(\n moduleName => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorPropsH,\n generatorPropsCPP,\n generatorShadowNodeH,\n generatorShadowNodeCPP,\n generatorComponentDescriptorH,\n generatorEventEmitterH,\n generatorEventEmitterCPP,\n ];\n\n componentGenerators.forEach(generator => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, componentOutputdir, generatedFiles);\n });\n }\n\n if (test === true) {\n return checkFilesForChanges(generatedFiles, outputDirectory);\n }\n\n return writeMapToFiles(generatedFiles, outputDirectory);\n}\n\nif ((argv.file && argv.files) || (!argv.file && !argv.files)) {\n console.error('You must specify either --file or --files.');\n process.exit(1);\n}\n\nlet schema: SchemaType;\nif (argv.file) {\n schema = parseFlowFile(argv.file);\n} else {\n schema = combineSchemas(globby.sync(argv.files as string[]));\n}\n\nconst libraryName = argv.libraryName;\nconst moduleSpecName = 'moduleSpecName';\nconst outputDirectory = argv.outdir;\ngenerate(\n {libraryName, schema, outputDirectory, moduleSpecName},\n {generators: [], test: false},\n);\n"]}
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { SchemaType } from 'react-native-tscodegen';
|
|
7
7
|
declare type FilesOutput = Map<string, string>;
|
|
8
|
-
export declare function createNM2Generator({ namespace }: {
|
|
8
|
+
export declare function createNM2Generator({ namespace, methodonly, }: {
|
|
9
9
|
namespace: string;
|
|
10
|
+
methodonly: boolean;
|
|
10
11
|
}): (_libraryName: string, schema: SchemaType, _moduleSpecName: string) => FilesOutput;
|
|
11
12
|
export {};
|
|
@@ -38,7 +38,7 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
|
38
38
|
|
|
39
39
|
} // namespace ::_NAMESPACE_::
|
|
40
40
|
`;
|
|
41
|
-
function createNM2Generator({ namespace }) {
|
|
41
|
+
function createNM2Generator({ namespace, methodonly, }) {
|
|
42
42
|
return (_libraryName, schema, _moduleSpecName) => {
|
|
43
43
|
const files = new Map();
|
|
44
44
|
for (const moduleName of Object.keys(schema.modules)) {
|
|
@@ -65,7 +65,7 @@ ${methods[0]}
|
|
|
65
65
|
let errors = methods[1];
|
|
66
66
|
// prepare constants
|
|
67
67
|
const constants = (0, ValidateConstants_1.generateValidateConstants)(nativeModule, aliases);
|
|
68
|
-
if (constants !== undefined) {
|
|
68
|
+
if (constants !== undefined && !methodonly) {
|
|
69
69
|
tuples = `
|
|
70
70
|
static constexpr auto constants = std::tuple{
|
|
71
71
|
${constants[0]}
|
|
@@ -80,8 +80,8 @@ ${errors}`;
|
|
|
80
80
|
const traversedAliasedStructs = (0, AliasGen_1.generateAliases)(aliases);
|
|
81
81
|
files.set(`Native${preferredModuleName}Spec.g.h`, moduleTemplate
|
|
82
82
|
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)
|
|
83
|
-
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.
|
|
84
|
-
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.
|
|
83
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
|
|
84
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
|
|
85
85
|
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
86
86
|
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
87
87
|
.replace(/::_NAMESPACE_::/g, namespace));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenerateNM2.js","sourceRoot":"","sources":["../../src/generators/GenerateNM2.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAiE;AACjE,yCAA2D;AAC3D,2DAA8D;AAC9D,uDAA0D;AAI1D,MAAM,cAAc,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BtB,CAAC;AAEF,SAAgB,kBAAkB,CAAC,
|
|
1
|
+
{"version":3,"file":"GenerateNM2.js","sourceRoot":"","sources":["../../src/generators/GenerateNM2.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAiE;AACjE,yCAA2D;AAC3D,2DAA8D;AAC9D,uDAA0D;AAI1D,MAAM,cAAc,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BtB,CAAC;AAEF,SAAgB,kBAAkB,CAAC,EACjC,SAAS,EACT,UAAU,GAIX;IACC,OAAO,CACL,YAAoB,EACpB,MAAkB,EAClB,eAAuB,EACV,EAAE;QACf,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;QAExC,KAAK,MAAM,UAAU,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE;YACpD,MAAM,YAAY,GAAG,MAAM,CAA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qBACX,OAAO,CAAC,+BAA+B,EAAE,uBAAuB,CAAC;qBACjE,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;qBAC5D,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;qBAC5D,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC;qBAC/C,OAAO,CAAC,oBAAoB,EAAE,mBAAmB,CAAC;qBAClD,OAAO,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAC1C,CAAC;aACH;SACF;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAxED,gDAwEC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {SchemaType} from 'react-native-tscodegen';\nimport {AliasMap, setPreferredModuleName} from './AliasManaging';\nimport {createAliasMap, generateAliases} from './AliasGen';\nimport {generateValidateConstants} from './ValidateConstants';\nimport {generateValidateMethods} from './ValidateMethods';\n\ntype FilesOutput = Map<string, string>;\n\nconst moduleTemplate = `\n/*\n * This file is auto-generated from a NativeModule spec file in js.\n *\n * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules\n * in a way that also verifies at compile time that the native module matches the interface required\n * by the TurboModule JS spec.\n */\n#pragma once\n\n#include \"NativeModules.h\"\n#include <tuple>\n\nnamespace ::_NAMESPACE_:: {\n::_MODULE_ALIASED_STRUCTS_::\nstruct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {\n::_MODULE_MEMBERS_TUPLES_::\n\n template <class TModule>\n static constexpr void ValidateModule() noexcept {\n::_MODULE_MEMBERS_CHECKS_::\n\n::_MODULE_MEMBERS_ERRORS_::\n }\n};\n\n} // namespace ::_NAMESPACE_::\n`;\n\nexport function createNM2Generator({\n namespace,\n methodonly,\n}: {\n namespace: string;\n methodonly: boolean;\n}) {\n return (\n _libraryName: string,\n schema: SchemaType,\n _moduleSpecName: string,\n ): FilesOutput => {\n const files = new Map<string, string>();\n\n for (const moduleName of Object.keys(schema.modules)) {\n const nativeModule = schema.modules[moduleName];\n // from 0.65 facebook's react-native-codegen\n // the module name has the Native prefix comparing to 0.63\n // when reading files we provided\n const preferredModuleName = moduleName.startsWith('Native')\n ? moduleName.substr(6)\n : moduleName;\n setPreferredModuleName(preferredModuleName);\n\n if (nativeModule.type === 'NativeModule') {\n console.log(`Generating Native${preferredModuleName}Spec.g.h`);\n\n // copy all explicit to a map\n const aliases: AliasMap = createAliasMap(nativeModule.aliases);\n\n // prepare methods\n const methods = generateValidateMethods(nativeModule, aliases);\n let tuples = `\n static constexpr auto methods = std::tuple{\n${methods[0]}\n };`;\n let checks = `\n constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;\n let errors = methods[1];\n\n // prepare constants\n const constants = generateValidateConstants(nativeModule, aliases);\n if (constants !== undefined && !methodonly) {\n tuples = `\n static constexpr auto constants = std::tuple{\n${constants[0]}\n };${tuples}`;\n checks = `\n constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;\n errors = `${constants[1]}\n\n${errors}`;\n }\n\n // generate code for structs\n const traversedAliasedStructs = generateAliases(aliases);\n\n files.set(\n `Native${preferredModuleName}Spec.g.h`,\n moduleTemplate\n .replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)\n .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))\n .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))\n .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)\n .replace(/::_MODULE_NAME_::/g, preferredModuleName)\n .replace(/::_NAMESPACE_::/g, namespace),\n );\n }\n }\n\n return files;\n };\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import { NativeModuleSchema, SchemaType } from 'react-native-tscodegen';
|
|
7
|
+
export declare function setOptionalTurboModule(schema: NativeModuleSchema, optional: boolean): void;
|
|
8
|
+
export declare function getOptionalTurboModule(schema: NativeModuleSchema): boolean;
|
|
9
|
+
declare type FilesOutput = Map<string, string>;
|
|
10
|
+
export declare function generateTypeScript(_libraryName: string, schema: SchemaType, _moduleSpecName: string): FilesOutput;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.generateTypeScript = exports.getOptionalTurboModule = exports.setOptionalTurboModule = void 0;
|
|
9
|
+
function setOptionalTurboModule(schema, optional) {
|
|
10
|
+
const cs = schema;
|
|
11
|
+
cs.optionalTurboModule = optional;
|
|
12
|
+
}
|
|
13
|
+
exports.setOptionalTurboModule = setOptionalTurboModule;
|
|
14
|
+
function getOptionalTurboModule(schema) {
|
|
15
|
+
var _a;
|
|
16
|
+
return (_a = schema.optionalTurboModule) !== null && _a !== void 0 ? _a : false;
|
|
17
|
+
}
|
|
18
|
+
exports.getOptionalTurboModule = getOptionalTurboModule;
|
|
19
|
+
const moduleTemplate = `
|
|
20
|
+
/*
|
|
21
|
+
* This file is auto-generated from a NativeModule spec file in js.
|
|
22
|
+
*
|
|
23
|
+
* This is a TypeScript turbo module definition file.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import {TurboModule, TurboModuleRegistry} from 'react-native';
|
|
27
|
+
'use strict';
|
|
28
|
+
::_MODULE_ALIASED_STRUCTS_::
|
|
29
|
+
export interface Spec extends TurboModule {
|
|
30
|
+
::_MODULE_MEMBERS_::
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default TurboModuleRegistry.::_MODULE_GETTER_::<Spec>('::_MODULE_NAME_::');
|
|
34
|
+
`;
|
|
35
|
+
function optionalSign(obj) {
|
|
36
|
+
return obj.optional ? '?' : '';
|
|
37
|
+
}
|
|
38
|
+
function translateType(type) {
|
|
39
|
+
// avoid: Property 'type' does not exist on type 'never'
|
|
40
|
+
const returnType = type.type;
|
|
41
|
+
switch (type.type) {
|
|
42
|
+
case 'StringTypeAnnotation':
|
|
43
|
+
return 'string';
|
|
44
|
+
case 'NumberTypeAnnotation':
|
|
45
|
+
case 'FloatTypeAnnotation':
|
|
46
|
+
case 'DoubleTypeAnnotation':
|
|
47
|
+
case 'Int32TypeAnnotation':
|
|
48
|
+
return 'number';
|
|
49
|
+
case 'BooleanTypeAnnotation':
|
|
50
|
+
return 'boolean';
|
|
51
|
+
case 'ArrayTypeAnnotation':
|
|
52
|
+
if (type.elementType) {
|
|
53
|
+
return `${translateType(type.elementType)}[]`;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return `Array`;
|
|
57
|
+
}
|
|
58
|
+
case 'GenericObjectTypeAnnotation':
|
|
59
|
+
return 'object';
|
|
60
|
+
case 'ObjectTypeAnnotation':
|
|
61
|
+
return `{${type.properties
|
|
62
|
+
.map((prop) => {
|
|
63
|
+
return `${prop.name}${optionalSign(prop)}: ${translateType(prop.typeAnnotation)}`;
|
|
64
|
+
})
|
|
65
|
+
.join(', ')}}`;
|
|
66
|
+
case 'ReservedTypeAnnotation': {
|
|
67
|
+
// avoid: Property 'name' does not exist on type 'never'
|
|
68
|
+
const name = type.name;
|
|
69
|
+
// (#6597)
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
71
|
+
if (name !== 'RootTag')
|
|
72
|
+
throw new Error(`Unknown reserved function: ${name} in translateReturnType`);
|
|
73
|
+
return 'number';
|
|
74
|
+
}
|
|
75
|
+
case 'TypeAliasTypeAnnotation':
|
|
76
|
+
return type.name;
|
|
77
|
+
case 'NullableTypeAnnotation':
|
|
78
|
+
return `(${translateType(type.typeAnnotation)} | null | undefined)`;
|
|
79
|
+
case 'VoidTypeAnnotation':
|
|
80
|
+
return `void`;
|
|
81
|
+
case 'PromiseTypeAnnotation':
|
|
82
|
+
return `Promise`;
|
|
83
|
+
case `FunctionTypeAnnotation`:
|
|
84
|
+
return `((${type.params
|
|
85
|
+
.map((param) => {
|
|
86
|
+
return `${param.name}${optionalSign(param)}: ${translateType(param.typeAnnotation)}`;
|
|
87
|
+
})
|
|
88
|
+
.join(', ')}) => ${translateType(type.returnTypeAnnotation)})`;
|
|
89
|
+
default:
|
|
90
|
+
throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function translateAlias(name, type) {
|
|
94
|
+
return `
|
|
95
|
+
export interface ${name} {
|
|
96
|
+
${type.properties
|
|
97
|
+
.map((prop) => {
|
|
98
|
+
return ` ${prop.name}${optionalSign(prop)}: ${translateType(prop.typeAnnotation)};`;
|
|
99
|
+
})
|
|
100
|
+
.join('\n')}
|
|
101
|
+
}
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
function tryGetConstantType(nativeModule) {
|
|
105
|
+
const candidates = nativeModule.spec.properties.filter(prop => prop.name === 'getConstants');
|
|
106
|
+
if (candidates.length === 0) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const getConstant = candidates[0];
|
|
110
|
+
const funcType = getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
111
|
+
? getConstant.typeAnnotation.typeAnnotation
|
|
112
|
+
: getConstant.typeAnnotation;
|
|
113
|
+
if (funcType.params.length > 0 ||
|
|
114
|
+
funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation') {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
const constantType = funcType.returnTypeAnnotation;
|
|
118
|
+
if (constantType.properties.length === 0) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return constantType;
|
|
122
|
+
}
|
|
123
|
+
function translateMethod(func) {
|
|
124
|
+
const funcType = func.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
125
|
+
? func.typeAnnotation.typeAnnotation
|
|
126
|
+
: func.typeAnnotation;
|
|
127
|
+
return `
|
|
128
|
+
${func.name}(${funcType.params
|
|
129
|
+
.map((param) => {
|
|
130
|
+
return `${param.name}${optionalSign(param)}: ${translateType(param.typeAnnotation)}`;
|
|
131
|
+
})
|
|
132
|
+
.join(', ')})${optionalSign(func)}: ${translateType(funcType.returnTypeAnnotation)}${funcType.returnTypeAnnotation.type === 'ObjectTypeAnnotation' ? '' : ';'}`;
|
|
133
|
+
}
|
|
134
|
+
function generateTypeScript(_libraryName, schema, _moduleSpecName) {
|
|
135
|
+
const files = new Map();
|
|
136
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
137
|
+
const nativeModule = schema.modules[moduleName];
|
|
138
|
+
// from 0.65 facebook's react-native-codegen
|
|
139
|
+
// the module name has the Native prefix comparing to 0.63
|
|
140
|
+
// when reading files we provided
|
|
141
|
+
const nativePrefix = 'Native';
|
|
142
|
+
const preferredModuleName = moduleName.startsWith(nativePrefix)
|
|
143
|
+
? moduleName.substr(nativePrefix.length)
|
|
144
|
+
: moduleName;
|
|
145
|
+
if (nativeModule.type === 'NativeModule') {
|
|
146
|
+
console.log(`Generating ${preferredModuleName}Spec.g.ts`);
|
|
147
|
+
const aliasCode = Object.keys(nativeModule.aliases)
|
|
148
|
+
.map(name => translateAlias(name, nativeModule.aliases[name]))
|
|
149
|
+
.join('');
|
|
150
|
+
const constantType = tryGetConstantType(nativeModule);
|
|
151
|
+
const constantCode = constantType === undefined
|
|
152
|
+
? ''
|
|
153
|
+
: ` getConstants(): ${translateType(constantType)}`;
|
|
154
|
+
const methods = nativeModule.spec.properties.filter(prop => prop.name !== 'getConstants');
|
|
155
|
+
const membersCode = methods.map(translateMethod).join('');
|
|
156
|
+
files.set(`${preferredModuleName}Spec.g.ts`, moduleTemplate
|
|
157
|
+
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, aliasCode)
|
|
158
|
+
.replace(/::_MODULE_MEMBERS_::/g, constantCode + membersCode)
|
|
159
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
160
|
+
.replace(/::_MODULE_GETTER_::/g, getOptionalTurboModule(nativeModule) ? 'get' : 'getEnforcing'));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return files;
|
|
164
|
+
}
|
|
165
|
+
exports.generateTypeScript = generateTypeScript;
|
|
166
|
+
//# sourceMappingURL=GenerateTypeScript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenerateTypeScript.js","sourceRoot":"","sources":["../../src/generators/GenerateTypeScript.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAkBb,SAAgB,sBAAsB,CACpC,MAA0B,EAC1B,QAAiB;IAEjB,MAAM,EAAE,GAA8B,MAAM,CAAC;IAC7C,EAAE,CAAC,mBAAmB,GAAG,QAAQ,CAAC;AACpC,CAAC;AAND,wDAMC;AAED,SAAgB,sBAAsB,CAAC,MAA0B;;IAC/D,OAAO,MAA4B,MAAO,CAAC,mBAAmB,mCAAI,KAAK,CAAC;AAC1E,CAAC;AAFD,wDAEC;AAOD,MAAM,cAAc,GAAG;;;;;;;;;;;;;;;CAetB,CAAC;AAEF,SAAS,YAAY,CAAI,GAAkB;IACzC,OAAO,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;AACjC,CAAC;AAED,SAAS,aAAa,CACpB,IAIC;IAED,wDAAwD;IACxD,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC;IAC7B,QAAQ,IAAI,CAAC,IAAI,EAAE;QACjB,KAAK,sBAAsB;YACzB,OAAO,QAAQ,CAAC;QAClB,KAAK,sBAAsB,C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,CAAC;SACH;KACF;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAlDD,gDAkDC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport {\n NamedShape,\n NativeModuleBaseTypeAnnotation,\n NativeModuleFunctionTypeAnnotation,\n NativeModuleObjectTypeAnnotation,\n NativeModuleParamTypeAnnotation,\n NativeModuleReturnTypeAnnotation,\n NativeModuleSchema,\n Nullable,\n SchemaType,\n} from 'react-native-tscodegen';\n\ninterface CodegenNativeModuleSchema extends NativeModuleSchema {\n optionalTurboModule?: boolean;\n}\n\nexport function setOptionalTurboModule(\n schema: NativeModuleSchema,\n optional: boolean,\n): void {\n const cs = <CodegenNativeModuleSchema>schema;\n cs.optionalTurboModule = optional;\n}\n\nexport function getOptionalTurboModule(schema: NativeModuleSchema): boolean {\n return (<CodegenNativeModuleSchema>schema).optionalTurboModule ?? false;\n}\n\ntype ObjectProp = NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>;\ntype FunctionParam = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;\ntype FunctionDecl = NamedShape<Nullable<NativeModuleFunctionTypeAnnotation>>;\ntype FilesOutput = Map<string, string>;\n\nconst moduleTemplate = `\n/*\n * This file is auto-generated from a NativeModule spec file in js.\n *\n * This is a TypeScript turbo module definition file.\n */\n\nimport {TurboModule, TurboModuleRegistry} from 'react-native';\n'use strict';\n::_MODULE_ALIASED_STRUCTS_::\nexport interface Spec extends TurboModule {\n::_MODULE_MEMBERS_::\n}\n\nexport default TurboModuleRegistry.::_MODULE_GETTER_::<Spec>('::_MODULE_NAME_::');\n`;\n\nfunction optionalSign<T>(obj: NamedShape<T>): string {\n return obj.optional ? '?' : '';\n}\n\nfunction translateType(\n type: Nullable<\n | NativeModuleBaseTypeAnnotation\n | NativeModuleParamTypeAnnotation\n | NativeModuleReturnTypeAnnotation\n >,\n): string {\n // avoid: Property 'type' does not exist on type 'never'\n const returnType = type.type;\n switch (type.type) {\n case 'StringTypeAnnotation':\n return 'string';\n case 'NumberTypeAnnotation':\n case 'FloatTypeAnnotation':\n case 'DoubleTypeAnnotation':\n case 'Int32TypeAnnotation':\n return 'number';\n case 'BooleanTypeAnnotation':\n return 'boolean';\n case 'ArrayTypeAnnotation':\n if (type.elementType) {\n return `${translateType(type.elementType)}[]`;\n } else {\n return `Array`;\n }\n case 'GenericObjectTypeAnnotation':\n return 'object';\n case 'ObjectTypeAnnotation':\n return `{${type.properties\n .map((prop: ObjectProp) => {\n return `${prop.name}${optionalSign(prop)}: ${translateType(\n prop.typeAnnotation,\n )}`;\n })\n .join(', ')}}`;\n case 'ReservedTypeAnnotation': {\n // avoid: Property 'name' does not exist on type 'never'\n const name = type.name;\n // (#6597)\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (name !== 'RootTag')\n throw new Error(\n `Unknown reserved function: ${name} in translateReturnType`,\n );\n return 'number';\n }\n case 'TypeAliasTypeAnnotation':\n return type.name;\n case 'NullableTypeAnnotation':\n return `(${translateType(type.typeAnnotation)} | null | undefined)`;\n case 'VoidTypeAnnotation':\n return `void`;\n case 'PromiseTypeAnnotation':\n return `Promise`;\n case `FunctionTypeAnnotation`:\n return `((${type.params\n .map((param: FunctionParam) => {\n return `${param.name}${optionalSign(param)}: ${translateType(\n param.typeAnnotation,\n )}`;\n })\n .join(', ')}) => ${translateType(type.returnTypeAnnotation)})`;\n default:\n throw new Error(`Unhandled type in translateReturnType: ${returnType}`);\n }\n}\n\nfunction translateAlias(\n name: string,\n type: NativeModuleObjectTypeAnnotation,\n): string {\n return `\nexport interface ${name} {\n${type.properties\n .map((prop: ObjectProp) => {\n return ` ${prop.name}${optionalSign(prop)}: ${translateType(\n prop.typeAnnotation,\n )};`;\n })\n .join('\\n')}\n}\n`;\n}\n\nfunction tryGetConstantType(\n nativeModule: NativeModuleSchema,\n): NativeModuleObjectTypeAnnotation | undefined {\n const candidates = nativeModule.spec.properties.filter(\n prop => prop.name === 'getConstants',\n );\n if (candidates.length === 0) {\n return undefined;\n }\n\n const getConstant = candidates[0];\n const funcType =\n getConstant.typeAnnotation.type === 'NullableTypeAnnotation'\n ? getConstant.typeAnnotation.typeAnnotation\n : getConstant.typeAnnotation;\n if (\n funcType.params.length > 0 ||\n funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'\n ) {\n return undefined;\n }\n\n const constantType = funcType.returnTypeAnnotation;\n if (constantType.properties.length === 0) {\n return undefined;\n }\n\n return constantType;\n}\n\nfunction translateMethod(func: FunctionDecl): string {\n const funcType =\n func.typeAnnotation.type === 'NullableTypeAnnotation'\n ? func.typeAnnotation.typeAnnotation\n : func.typeAnnotation;\n\n return `\n ${func.name}(${funcType.params\n .map((param: FunctionParam) => {\n return `${param.name}${optionalSign(param)}: ${translateType(\n param.typeAnnotation,\n )}`;\n })\n .join(', ')})${optionalSign(func)}: ${translateType(\n funcType.returnTypeAnnotation,\n )}${\n funcType.returnTypeAnnotation.type === 'ObjectTypeAnnotation' ? '' : ';'\n }`;\n}\n\nexport function generateTypeScript(\n _libraryName: string,\n schema: SchemaType,\n _moduleSpecName: string,\n): FilesOutput {\n const files = new Map<string, string>();\n\n for (const moduleName of Object.keys(schema.modules)) {\n const nativeModule = schema.modules[moduleName];\n // from 0.65 facebook's react-native-codegen\n // the module name has the Native prefix comparing to 0.63\n // when reading files we provided\n const nativePrefix = 'Native';\n const preferredModuleName = moduleName.startsWith(nativePrefix)\n ? moduleName.substr(nativePrefix.length)\n : moduleName;\n\n if (nativeModule.type === 'NativeModule') {\n console.log(`Generating ${preferredModuleName}Spec.g.ts`);\n\n const aliasCode = Object.keys(nativeModule.aliases)\n .map(name => translateAlias(name, nativeModule.aliases[name]))\n .join('');\n\n const constantType = tryGetConstantType(nativeModule);\n const constantCode =\n constantType === undefined\n ? ''\n : ` getConstants(): ${translateType(constantType)}`;\n\n const methods = nativeModule.spec.properties.filter(\n prop => prop.name !== 'getConstants',\n );\n const membersCode = methods.map(translateMethod).join('');\n\n files.set(\n `${preferredModuleName}Spec.g.ts`,\n moduleTemplate\n .replace(/::_MODULE_ALIASED_STRUCTS_::/g, aliasCode)\n .replace(/::_MODULE_MEMBERS_::/g, constantCode + membersCode)\n .replace(/::_MODULE_NAME_::/g, preferredModuleName)\n .replace(\n /::_MODULE_GETTER_::/g,\n getOptionalTurboModule(nativeModule) ? 'get' : 'getEnforcing',\n ),\n );\n }\n }\n\n return files;\n}\n"]}
|
|
@@ -98,14 +98,14 @@ function translateFunctionParam(param, aliases, baseAliasName) {
|
|
|
98
98
|
return translateNullableParamType(param.typeAnnotation, aliases, baseAliasName, 'template', 'method-arg');
|
|
99
99
|
}
|
|
100
100
|
function translateSpecArgs(params, aliases, baseAliasName) {
|
|
101
|
-
return params.map(
|
|
101
|
+
return params.map(param => {
|
|
102
102
|
const translatedParam = translateSpecFunctionParam(param, aliases, `${baseAliasName}_${param.name}`);
|
|
103
103
|
return `${translatedParam}`;
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
106
|
exports.translateSpecArgs = translateSpecArgs;
|
|
107
107
|
function translateArgs(params, aliases, baseAliasName) {
|
|
108
|
-
return params.map(
|
|
108
|
+
return params.map(param => {
|
|
109
109
|
const translatedParam = translateFunctionParam(param, aliases, `${baseAliasName}_${param.name}`);
|
|
110
110
|
return `${translatedParam} ${param.name}`;
|
|
111
111
|
});
|