@react-native-windows/codegen 0.0.0-canary.7 → 0.0.0-canary.71
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 +620 -11
- package/README.md +1 -1
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +92 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +12 -0
- package/lib-commonjs/generators/AliasGen.js +88 -0
- package/lib-commonjs/generators/AliasGen.js.map +1 -0
- package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
- package/lib-commonjs/generators/AliasManaging.js +49 -0
- package/lib-commonjs/generators/AliasManaging.js.map +1 -0
- package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
- package/lib-commonjs/generators/GenerateNM2.js +142 -0
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
- 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/ObjectTypes.d.ts +13 -0
- package/lib-commonjs/generators/ObjectTypes.js +75 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +12 -0
- package/lib-commonjs/generators/ParamTypes.js +137 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
- package/lib-commonjs/generators/ReturnTypes.js +29 -0
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
- package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
- package/lib-commonjs/generators/ValidateConstants.js +38 -0
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
- package/lib-commonjs/generators/ValidateMethods.d.ts +9 -0
- package/lib-commonjs/generators/ValidateMethods.js +75 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +37 -0
- package/lib-commonjs/index.js +224 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +37 -20
- package/src/Cli.ts +57 -190
- package/src/generators/AliasGen.ts +149 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +138 -295
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +130 -0
- package/src/generators/ParamTypes.ts +298 -0
- package/src/generators/ReturnTypes.ts +70 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +177 -0
- package/src/index.ts +393 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -547
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
* Licensed under the MIT License.
|
|
5
|
+
*
|
|
6
|
+
* @format
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.runCodeGen = exports.generate = exports.combineSchemas = exports.parseFile = void 0;
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
const fs_1 = __importDefault(require("@react-native-windows/fs"));
|
|
15
|
+
const globby_1 = __importDefault(require("globby"));
|
|
16
|
+
const GenerateNM2_1 = require("./generators/GenerateNM2");
|
|
17
|
+
const GenerateTypeScript_1 = require("./generators/GenerateTypeScript");
|
|
18
|
+
// Load @react-native/codegen from react-native
|
|
19
|
+
const rnPath = path_1.default.dirname(require.resolve('react-native/package.json'));
|
|
20
|
+
const rncodegenPath = path_1.default.dirname(require.resolve('@react-native/codegen/package.json', { paths: [rnPath] }));
|
|
21
|
+
function getParser(isTypeScript) {
|
|
22
|
+
if (isTypeScript) {
|
|
23
|
+
const fp = require(path_1.default.resolve(rncodegenPath, 'lib/parsers/typescript/parser'));
|
|
24
|
+
return new fp.TypeScriptParser();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const fp = require(path_1.default.resolve(rncodegenPath, 'lib/parsers/flow/parser'));
|
|
28
|
+
return new fp.FlowParser();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const schemaValidator = require(path_1.default.resolve(rncodegenPath, 'lib/SchemaValidator'));
|
|
32
|
+
function normalizeFileMap(map, outputDir, outMap) {
|
|
33
|
+
for (const [fileName, contents] of map) {
|
|
34
|
+
const location = path_1.default.join(outputDir, fileName);
|
|
35
|
+
outMap.set(path_1.default.normalize(location), contents);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function checkFilesForChanges(map, outputDir) {
|
|
39
|
+
let hasChanges = false;
|
|
40
|
+
outputDir = path_1.default.resolve(outputDir);
|
|
41
|
+
const globbyDir = outputDir.replace(/\\/g, '/');
|
|
42
|
+
const allExistingFiles = globby_1.default
|
|
43
|
+
.sync([`${globbyDir}/**`, `${globbyDir}/**/.*`], { absolute: true })
|
|
44
|
+
.map(_ => path_1.default.normalize(_));
|
|
45
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path_1.default.normalize(_)).sort();
|
|
46
|
+
if (allExistingFiles.length !== allGeneratedFiles.length ||
|
|
47
|
+
!allGeneratedFiles.every(filepath => allExistingFiles.includes(path_1.default.normalize(path_1.default.resolve(process.cwd(), filepath)))))
|
|
48
|
+
return true;
|
|
49
|
+
for (const [fileName, contents] of map) {
|
|
50
|
+
if (!fs_1.default.existsSync(fileName)) {
|
|
51
|
+
hasChanges = true;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const currentContents = fs_1.default.readFileSync(fileName, 'utf8');
|
|
55
|
+
if (currentContents !== contents) {
|
|
56
|
+
console.log(`- ${fileName} has changed`);
|
|
57
|
+
hasChanges = true;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return hasChanges;
|
|
62
|
+
}
|
|
63
|
+
function writeMapToFiles(map, outputDir) {
|
|
64
|
+
let success = true;
|
|
65
|
+
outputDir = path_1.default.resolve(outputDir);
|
|
66
|
+
const globbyDir = outputDir.replace(/\\/g, '/');
|
|
67
|
+
// This ensures that we delete any generated files from modules that have been deleted
|
|
68
|
+
const allExistingFiles = globby_1.default.sync([`${globbyDir}/**`, `${globbyDir}/**/.*`], { absolute: true });
|
|
69
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path_1.default.normalize(_)).sort();
|
|
70
|
+
allExistingFiles.forEach(existingFile => {
|
|
71
|
+
if (!allGeneratedFiles.includes(path_1.default.normalize(existingFile))) {
|
|
72
|
+
console.log('Deleting ', existingFile);
|
|
73
|
+
fs_1.default.unlinkSync(existingFile);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
for (const [fileName, contents] of map) {
|
|
77
|
+
try {
|
|
78
|
+
fs_1.default.mkdirSync(path_1.default.dirname(fileName), { recursive: true });
|
|
79
|
+
if (fs_1.default.existsSync(fileName)) {
|
|
80
|
+
const currentContents = fs_1.default.readFileSync(fileName, 'utf8');
|
|
81
|
+
// Don't update the files if there are no changes as this breaks incremental builds
|
|
82
|
+
if (currentContents === contents) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
console.log('Writing ', fileName);
|
|
87
|
+
fs_1.default.writeFileSync(fileName, contents);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
success = false;
|
|
91
|
+
console.error(`Failed to write ${fileName} to ${fileName}`, error);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return success;
|
|
95
|
+
}
|
|
96
|
+
function parseFile(filename) {
|
|
97
|
+
try {
|
|
98
|
+
const isTypeScript = path_1.default.extname(filename) === '.ts' || path_1.default.extname(filename) === '.tsx';
|
|
99
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
100
|
+
const schema = getParser(isTypeScript).parseString(contents, filename);
|
|
101
|
+
// there will be at most one turbo module per file
|
|
102
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
103
|
+
if (moduleName) {
|
|
104
|
+
const spec = schema.modules[moduleName];
|
|
105
|
+
if (spec.type === 'NativeModule') {
|
|
106
|
+
if (contents) {
|
|
107
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
108
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
109
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, true);
|
|
110
|
+
}
|
|
111
|
+
else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
112
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, false);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return schema;
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
if (e instanceof Error) {
|
|
121
|
+
e.message = `(${filename}): ${e.message}`;
|
|
122
|
+
}
|
|
123
|
+
throw e;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.parseFile = parseFile;
|
|
127
|
+
function combineSchemas(files) {
|
|
128
|
+
return files.reduce((merged, filename) => {
|
|
129
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
130
|
+
if (contents &&
|
|
131
|
+
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
|
|
132
|
+
contents.includes('extends TurboModule'))) {
|
|
133
|
+
const schema = parseFile(filename);
|
|
134
|
+
merged.modules = { ...merged.modules, ...schema.modules };
|
|
135
|
+
}
|
|
136
|
+
return merged;
|
|
137
|
+
}, { modules: {} });
|
|
138
|
+
}
|
|
139
|
+
exports.combineSchemas = combineSchemas;
|
|
140
|
+
function generate({ libraryName, methodOnly, modulesCxx, modulesTypeScriptTypes, modulesWindows, namespace, outputDirectory, cppStringType, separateDataTypes, moduleSpecName, schema, }, { /*generators,*/ test }) {
|
|
141
|
+
schemaValidator.validate(schema);
|
|
142
|
+
const componentOutputdir = path_1.default.join(outputDirectory, 'react/components', libraryName);
|
|
143
|
+
const generatedFiles = new Map();
|
|
144
|
+
generatedFiles.set(path_1.default.join(outputDirectory, '.clang-format'), 'DisableFormat: true\nSortIncludes: false');
|
|
145
|
+
const generateNM2 = (0, GenerateNM2_1.createNM2Generator)({
|
|
146
|
+
methodOnly,
|
|
147
|
+
namespace,
|
|
148
|
+
cppStringType,
|
|
149
|
+
separateDataTypes,
|
|
150
|
+
});
|
|
151
|
+
const generateJsiModuleH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/modules/GenerateModuleH')).generate;
|
|
152
|
+
const generateJsiModuleCpp = require(path_1.default.resolve(rncodegenPath, 'lib/generators/modules/GenerateModuleCpp')).generate;
|
|
153
|
+
const generatorPropsH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GeneratePropsH')).generate;
|
|
154
|
+
const generatorPropsCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GeneratePropsCpp')).generate;
|
|
155
|
+
const generatorShadowNodeH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateShadowNodeH')).generate;
|
|
156
|
+
const generatorShadowNodeCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateShadowNodeCpp')).generate;
|
|
157
|
+
const generatorComponentDescriptorH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateComponentDescriptorH')).generate;
|
|
158
|
+
const generatorEventEmitterH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateEventEmitterH')).generate;
|
|
159
|
+
const generatorEventEmitterCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateEventEmitterCpp')).generate;
|
|
160
|
+
const generatorStateCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateStateCpp')).generate;
|
|
161
|
+
const generatorStateH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateStateH')).generate;
|
|
162
|
+
const moduleGenerators = [];
|
|
163
|
+
if (modulesWindows) {
|
|
164
|
+
moduleGenerators.push(generateNM2);
|
|
165
|
+
}
|
|
166
|
+
if (modulesCxx) {
|
|
167
|
+
moduleGenerators.push(generateJsiModuleH);
|
|
168
|
+
moduleGenerators.push(generateJsiModuleCpp);
|
|
169
|
+
}
|
|
170
|
+
if (modulesTypeScriptTypes) {
|
|
171
|
+
moduleGenerators.push(GenerateTypeScript_1.generateTypeScript);
|
|
172
|
+
}
|
|
173
|
+
moduleGenerators.forEach(generator => {
|
|
174
|
+
const generated = generator(libraryName, schema, moduleSpecName);
|
|
175
|
+
normalizeFileMap(generated, outputDirectory, generatedFiles);
|
|
176
|
+
});
|
|
177
|
+
if (Object.keys(schema.modules).some(moduleName => schema.modules[moduleName].type === 'Component')) {
|
|
178
|
+
const componentGenerators = [
|
|
179
|
+
generatorComponentDescriptorH,
|
|
180
|
+
generatorEventEmitterCPP,
|
|
181
|
+
generatorEventEmitterH,
|
|
182
|
+
generatorPropsCPP,
|
|
183
|
+
generatorPropsH,
|
|
184
|
+
generatorShadowNodeCPP,
|
|
185
|
+
generatorShadowNodeH,
|
|
186
|
+
generatorStateCPP,
|
|
187
|
+
generatorStateH,
|
|
188
|
+
];
|
|
189
|
+
componentGenerators.forEach(generator => {
|
|
190
|
+
const generated = generator(libraryName, schema, moduleSpecName);
|
|
191
|
+
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (test === true) {
|
|
195
|
+
return checkFilesForChanges(generatedFiles, outputDirectory);
|
|
196
|
+
}
|
|
197
|
+
return writeMapToFiles(generatedFiles, outputDirectory);
|
|
198
|
+
}
|
|
199
|
+
exports.generate = generate;
|
|
200
|
+
function runCodeGen(options) {
|
|
201
|
+
if (!options.file && !options.files)
|
|
202
|
+
throw new Error('Must specify file or files option');
|
|
203
|
+
const schema = options.file
|
|
204
|
+
? parseFile(options.file)
|
|
205
|
+
: combineSchemas(globby_1.default.sync(options.files));
|
|
206
|
+
const libraryName = options.libraryName;
|
|
207
|
+
const moduleSpecName = 'moduleSpecName';
|
|
208
|
+
const { methodOnly, modulesCxx, modulesTypeScriptTypes, modulesWindows, namespace, outputDirectory, cppStringType, separateDataTypes, } = options;
|
|
209
|
+
return generate({
|
|
210
|
+
libraryName,
|
|
211
|
+
methodOnly,
|
|
212
|
+
modulesCxx,
|
|
213
|
+
modulesTypeScriptTypes,
|
|
214
|
+
modulesWindows,
|
|
215
|
+
namespace,
|
|
216
|
+
outputDirectory,
|
|
217
|
+
cppStringType,
|
|
218
|
+
separateDataTypes,
|
|
219
|
+
moduleSpecName,
|
|
220
|
+
schema,
|
|
221
|
+
}, { generators: [], test: options.test });
|
|
222
|
+
}
|
|
223
|
+
exports.runCodeGen = runCodeGen;
|
|
224
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;AAEH,gDAAwB;AACxB,kEAA0C;AAC1C,oDAA4B;AAE5B,0DAA4D;AAC5D,wEAGyC;AAMzC,+CAA+C;AAC/C,MAAM,MAAM,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC,CAAC;AAC1E,MAAM,aAAa,GAAG,cAAI,CAAC,OAAO,CAChC,OAAO,CAAC,OAAO,CAAC,oCAAoC,EAAE,EAAC,KAAK,EAAE,CAAC,MAAM,CAAC,EAAC,CAAC,CACzE,CAAC;AAEF,SAAS,SAAS,CAAC,YAAqB;IACtC,IAAI,YAAY,EAAE;QAChB,MAAM,EAAE,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAC7B,aAAa,EACb,+BAA+B,CAChC,CAAC,C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CAAC;IAEvD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI;QACzB,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QACzB,CAAC,CAAC,cAAc,CAAC,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAM,CAAC,CAAC,CAAC;IAEhD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACxC,MAAM,cAAc,GAAG,gBAAgB,CAAC;IACxC,MAAM,EACJ,UAAU,EACV,UAAU,EACV,sBAAsB,EACtB,cAAc,EACd,SAAS,EACT,eAAe,EACf,aAAa,EACb,iBAAiB,GAClB,GAAG,OAAO,CAAC;IACZ,OAAO,QAAQ,CACb;QACE,WAAW;QACX,UAAU;QACV,UAAU;QACV,sBAAsB;QACtB,cAAc;QACd,SAAS;QACT,eAAe;QACf,aAAa;QACb,iBAAiB;QACjB,cAAc;QACd,MAAM;KACP,EACD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAC,CACrC,CAAC;AACJ,CAAC;AApCD,gCAoCC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport type {CppStringTypes} from './generators/GenerateNM2';\nimport {createNM2Generator} from './generators/GenerateNM2';\nimport {\n generateTypeScript,\n setOptionalTurboModule,\n} from './generators/GenerateTypeScript';\nimport type {SchemaType} from '@react-native/codegen/lib/CodegenSchema';\nimport type {Parser} from '@react-native/codegen/lib/parsers/parser';\n\nexport type {CppStringTypes} from './generators/GenerateNM2';\n\n// Load @react-native/codegen from react-native\nconst rnPath = path.dirname(require.resolve('react-native/package.json'));\nconst rncodegenPath = path.dirname(\n require.resolve('@react-native/codegen/package.json', {paths: [rnPath]}),\n);\n\nfunction getParser(isTypeScript: boolean): Parser {\n if (isTypeScript) {\n const fp = require(path.resolve(\n rncodegenPath,\n 'lib/parsers/typescript/parser',\n ));\n return new fp.TypeScriptParser();\n } else {\n const fp = require(path.resolve(rncodegenPath, 'lib/parsers/flow/parser'));\n return new fp.FlowParser();\n }\n}\n\nconst schemaValidator = require(path.resolve(\n rncodegenPath,\n 'lib/SchemaValidator',\n));\n\nexport interface SharedOptions {\n libraryName: string;\n methodOnly: boolean;\n modulesCxx: boolean;\n modulesTypeScriptTypes: boolean;\n modulesWindows: boolean;\n namespace: string;\n outputDirectory: string;\n cppStringType: CppStringTypes;\n separateDataTypes: boolean;\n}\n\ninterface Options extends SharedOptions {\n moduleSpecName: string;\n schema: SchemaType;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\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 outputDir = path.resolve(outputDir);\n const globbyDir = outputDir.replace(/\\\\/g, '/');\n const allExistingFiles = globby\n .sync([`${globbyDir}/**`, `${globbyDir}/**/.*`], {absolute: true})\n .map(_ => path.normalize(_));\n const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allGeneratedFiles.every(filepath =>\n allExistingFiles.includes(\n path.normalize(path.resolve(process.cwd(), filepath)),\n ),\n )\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.log(`- ${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 outputDir = path.resolve(outputDir);\n const globbyDir = outputDir.replace(/\\\\/g, '/');\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(\n [`${globbyDir}/**`, `${globbyDir}/**/.*`],\n {absolute: true},\n );\n\n const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();\n allExistingFiles.forEach(existingFile => {\n if (!allGeneratedFiles.includes(path.normalize(existingFile))) {\n console.log('Deleting ', 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 console.log('Writing ', fileName);\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\nexport function parseFile(filename: string): SchemaType {\n try {\n const isTypeScript =\n path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';\n const contents = fs.readFileSync(filename, 'utf8');\n const schema = getParser(isTypeScript).parseString(contents, 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 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\nexport function 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 = parseFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nexport function generate(\n {\n libraryName,\n methodOnly,\n modulesCxx,\n modulesTypeScriptTypes,\n modulesWindows,\n namespace,\n outputDirectory,\n cppStringType,\n separateDataTypes,\n moduleSpecName,\n schema,\n }: 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 methodOnly,\n namespace,\n cppStringType,\n separateDataTypes,\n });\n\n const generateJsiModuleH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/modules/GenerateModuleH',\n )).generate;\n const generateJsiModuleCpp = require(path.resolve(\n rncodegenPath,\n 'lib/generators/modules/GenerateModuleCpp',\n )).generate;\n const generatorPropsH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GeneratePropsH',\n )).generate;\n const generatorPropsCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GeneratePropsCpp',\n )).generate;\n const generatorShadowNodeH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateShadowNodeH',\n )).generate;\n const generatorShadowNodeCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateShadowNodeCpp',\n )).generate;\n const generatorComponentDescriptorH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateComponentDescriptorH',\n )).generate;\n const generatorEventEmitterH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateEventEmitterH',\n )).generate;\n const generatorEventEmitterCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateEventEmitterCpp',\n )).generate;\n const generatorStateCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateStateCpp',\n )).generate;\n const generatorStateH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateStateH',\n )).generate;\n\n const moduleGenerators = [];\n\n if (modulesWindows) {\n moduleGenerators.push(generateNM2);\n }\n\n if (modulesCxx) {\n moduleGenerators.push(generateJsiModuleH);\n moduleGenerators.push(generateJsiModuleCpp);\n }\n\n if (modulesTypeScriptTypes) {\n moduleGenerators.push(generateTypeScript);\n }\n\n moduleGenerators.forEach(generator => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, outputDirectory, generatedFiles);\n });\n\n if (\n Object.keys(schema.modules).some(\n moduleName => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorComponentDescriptorH,\n generatorEventEmitterCPP,\n generatorEventEmitterH,\n generatorPropsCPP,\n generatorPropsH,\n generatorShadowNodeCPP,\n generatorShadowNodeH,\n generatorStateCPP,\n generatorStateH,\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\nexport interface CodeGenOptions extends SharedOptions {\n file?: string;\n files?: string[];\n test: boolean;\n}\n\nexport function runCodeGen(options: CodeGenOptions): boolean {\n if (!options.file && !options.files)\n throw new Error('Must specify file or files option');\n\n const schema = options.file\n ? parseFile(options.file)\n : combineSchemas(globby.sync(options.files!));\n\n const libraryName = options.libraryName;\n const moduleSpecName = 'moduleSpecName';\n const {\n methodOnly,\n modulesCxx,\n modulesTypeScriptTypes,\n modulesWindows,\n namespace,\n outputDirectory,\n cppStringType,\n separateDataTypes,\n } = options;\n return generate(\n {\n libraryName,\n methodOnly,\n modulesCxx,\n modulesTypeScriptTypes,\n modulesWindows,\n namespace,\n outputDirectory,\n cppStringType,\n separateDataTypes,\n moduleSpecName,\n schema,\n },\n {generators: [], test: options.test},\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-windows/codegen",
|
|
3
|
-
"version": "0.0.0-canary.
|
|
3
|
+
"version": "0.0.0-canary.71",
|
|
4
4
|
"description": "Generators for react-native-codegen targeting react-native-windows",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"repository":
|
|
5
|
+
"main": "lib-commonjs/index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/microsoft/react-native-windows",
|
|
9
|
+
"directory": "packages/@react-native-windows/codegen"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"private": false,
|
|
9
13
|
"scripts": {
|
|
@@ -15,33 +19,43 @@
|
|
|
15
19
|
"watch": "rnw-scripts watch"
|
|
16
20
|
},
|
|
17
21
|
"bin": {
|
|
18
|
-
"codegen": "./bin.js"
|
|
22
|
+
"react-native-windows-codegen": "./bin.js"
|
|
19
23
|
},
|
|
20
24
|
"dependencies": {
|
|
25
|
+
"@react-native-windows/fs": "^0.0.0-canary.29",
|
|
21
26
|
"chalk": "^4.1.0",
|
|
22
|
-
"globby": "^
|
|
27
|
+
"globby": "^11.0.4",
|
|
23
28
|
"mustache": "^4.0.1",
|
|
24
|
-
"react-native-tscodegen": "0.66.1",
|
|
25
29
|
"source-map-support": "^0.5.19",
|
|
26
30
|
"yargs": "^16.2.0"
|
|
27
31
|
},
|
|
28
32
|
"devDependencies": {
|
|
29
|
-
"@rnw-scripts/eslint-config": "1.
|
|
30
|
-
"@rnw-scripts/jest-unittest-config": "1.
|
|
31
|
-
"@rnw-scripts/just-task": "2.
|
|
32
|
-
"@rnw-scripts/ts-config": "2.0.
|
|
33
|
+
"@rnw-scripts/eslint-config": "1.2.3",
|
|
34
|
+
"@rnw-scripts/jest-unittest-config": "1.5.6",
|
|
35
|
+
"@rnw-scripts/just-task": "2.3.17",
|
|
36
|
+
"@rnw-scripts/ts-config": "2.0.5",
|
|
33
37
|
"@types/chalk": "^2.2.0",
|
|
34
|
-
"@types/
|
|
35
|
-
"@types/
|
|
36
|
-
"@types/node": "^14.14.22",
|
|
38
|
+
"@types/jest": "^29.2.2",
|
|
39
|
+
"@types/node": "^18.0.0",
|
|
37
40
|
"@types/yargs": "16.0.0",
|
|
38
|
-
"
|
|
39
|
-
"eslint": "
|
|
40
|
-
"jest": "^
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^5.30.5",
|
|
42
|
+
"@typescript-eslint/parser": "^5.57.1",
|
|
43
|
+
"babel-jest": "^29.3.0",
|
|
44
|
+
"eslint": "^8.19.0",
|
|
45
|
+
"jest": "^29.2.1",
|
|
46
|
+
"prettier": "^2.4.1",
|
|
47
|
+
"typescript": "^4.9.5"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react-native": "*"
|
|
44
51
|
},
|
|
52
|
+
"files": [
|
|
53
|
+
"bin.js",
|
|
54
|
+
"CHANGELOG.md",
|
|
55
|
+
"README.md",
|
|
56
|
+
"lib-commonjs",
|
|
57
|
+
"src"
|
|
58
|
+
],
|
|
45
59
|
"beachball": {
|
|
46
60
|
"defaultNpmTag": "canary",
|
|
47
61
|
"disallowedChangeTypes": [
|
|
@@ -50,5 +64,8 @@
|
|
|
50
64
|
"patch"
|
|
51
65
|
]
|
|
52
66
|
},
|
|
53
|
-
"promoteRelease": true
|
|
67
|
+
"promoteRelease": true,
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">= 18"
|
|
70
|
+
}
|
|
54
71
|
}
|
package/src/Cli.ts
CHANGED
|
@@ -6,14 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import yargs from 'yargs';
|
|
9
|
-
import
|
|
10
|
-
import fs from 'fs';
|
|
11
|
-
import globby from 'globby';
|
|
12
|
-
import {createNM2Generator} from './generators/GenerateNM2';
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
import {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
import schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
|
|
9
|
+
import {CodeGenOptions, runCodeGen} from './index';
|
|
17
10
|
|
|
18
11
|
const argv = yargs.options({
|
|
19
12
|
file: {
|
|
@@ -21,12 +14,39 @@ const argv = yargs.options({
|
|
|
21
14
|
describe: 'file which contains spec',
|
|
22
15
|
},
|
|
23
16
|
files: {
|
|
24
|
-
type: '
|
|
17
|
+
type: 'string',
|
|
18
|
+
array: true,
|
|
25
19
|
describe: 'glob patterns for files which contains specs',
|
|
26
20
|
},
|
|
21
|
+
modulesTypeScriptTypes: {
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
describe: 'generate turbo module definition files in TypeScript',
|
|
24
|
+
default: false,
|
|
25
|
+
},
|
|
26
|
+
modulesCxx: {
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
describe: 'generate C++ JSI turbo module spec files',
|
|
29
|
+
default: false,
|
|
30
|
+
},
|
|
31
|
+
modulesWindows: {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
describe: 'generate turbo module spec files for REACT_MODULE',
|
|
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
|
+
outputDirectory: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
describe: 'output directory',
|
|
44
|
+
default: 'codegen',
|
|
45
|
+
},
|
|
27
46
|
test: {
|
|
28
47
|
type: 'boolean',
|
|
29
48
|
describe: 'Verify that the generated output is unchanged',
|
|
49
|
+
default: false,
|
|
30
50
|
},
|
|
31
51
|
namespace: {
|
|
32
52
|
type: 'string',
|
|
@@ -38,193 +58,40 @@ const argv = yargs.options({
|
|
|
38
58
|
required: true,
|
|
39
59
|
describe: 'Used for part of the path generated within the codegen dir',
|
|
40
60
|
},
|
|
61
|
+
cppStringType: {
|
|
62
|
+
choices: ['std::string', 'std::wstring'],
|
|
63
|
+
describe:
|
|
64
|
+
'C++ string type in generated code, should be "std::string" or "std::wstring"',
|
|
65
|
+
default: 'std::string',
|
|
66
|
+
},
|
|
67
|
+
separateDataTypes: {
|
|
68
|
+
type: 'boolean',
|
|
69
|
+
describe: 'generate data types in a separate file',
|
|
70
|
+
default: false,
|
|
71
|
+
},
|
|
41
72
|
}).argv;
|
|
42
73
|
|
|
43
|
-
import {SchemaType} from 'react-native-tscodegen';
|
|
44
|
-
|
|
45
|
-
interface Options {
|
|
46
|
-
libraryName: string;
|
|
47
|
-
schema: SchemaType;
|
|
48
|
-
outputDirectory: string;
|
|
49
|
-
moduleSpecName: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
interface Config {
|
|
53
|
-
generators: any[] /*Generators[]*/;
|
|
54
|
-
test?: boolean;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/*
|
|
58
|
-
const GENERATORS = {
|
|
59
|
-
descriptors: [generateComponentDescriptorH.generate],
|
|
60
|
-
events: [
|
|
61
|
-
generateEventEmitterCpp.generate,
|
|
62
|
-
generateEventEmitterH.generate,
|
|
63
|
-
generateModuleHObjCpp.generate,
|
|
64
|
-
generateModuleMm.generate,
|
|
65
|
-
],
|
|
66
|
-
props: [
|
|
67
|
-
generateComponentHObjCpp.generate,
|
|
68
|
-
generatePropsCpp.generate,
|
|
69
|
-
generatePropsH.generate,
|
|
70
|
-
generatePropsJavaInterface.generate,
|
|
71
|
-
generatePropsJavaDelegate.generate,
|
|
72
|
-
],
|
|
73
|
-
modules: [generateModuleCpp.generate, generateModuleH.generate],
|
|
74
|
-
tests: [generateTests.generate],
|
|
75
|
-
'shadow-nodes': [
|
|
76
|
-
generateShadowNodeCpp.generate,
|
|
77
|
-
generateShadowNodeH.generate,
|
|
78
|
-
],
|
|
79
|
-
};
|
|
80
|
-
*/
|
|
81
|
-
|
|
82
|
-
function checkFilesForChanges(
|
|
83
|
-
map: Map<string, string>,
|
|
84
|
-
outputDir: string,
|
|
85
|
-
): boolean {
|
|
86
|
-
let hasChanges = false;
|
|
87
|
-
|
|
88
|
-
for (const [contents, fileName] of map) {
|
|
89
|
-
const location = path.join(outputDir, fileName);
|
|
90
|
-
if (!fs.existsSync(location)) {
|
|
91
|
-
hasChanges = true;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const currentContents = fs.readFileSync(location, 'utf8');
|
|
96
|
-
if (currentContents !== contents) {
|
|
97
|
-
console.error(`- ${fileName} has changed`);
|
|
98
|
-
hasChanges = true;
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return hasChanges;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
107
|
-
let success = true;
|
|
108
|
-
map.forEach((contents: string, fileName: string) => {
|
|
109
|
-
try {
|
|
110
|
-
const location = path.join(outputDir, fileName);
|
|
111
|
-
fs.mkdirSync(path.dirname(location), {recursive: true});
|
|
112
|
-
fs.writeFileSync(location, contents);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
success = false;
|
|
115
|
-
console.error(`Failed to write ${fileName} to ${outputDir}`, error);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
return success;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function combineSchemas(files: string[]): SchemaType {
|
|
123
|
-
return files.reduce(
|
|
124
|
-
(merged, filename) => {
|
|
125
|
-
const contents = fs.readFileSync(filename, 'utf8');
|
|
126
|
-
if (
|
|
127
|
-
contents &&
|
|
128
|
-
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
|
|
129
|
-
contents.includes('extends TurboModule'))
|
|
130
|
-
) {
|
|
131
|
-
const schema = parseFile(filename);
|
|
132
|
-
|
|
133
|
-
if (schema && schema.modules) {
|
|
134
|
-
merged.modules = {...merged.modules, ...schema.modules};
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return merged;
|
|
138
|
-
},
|
|
139
|
-
{modules: {}},
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function generate(
|
|
144
|
-
{libraryName, schema, outputDirectory, moduleSpecName}: Options,
|
|
145
|
-
{/*generators,*/ test}: Config,
|
|
146
|
-
): boolean {
|
|
147
|
-
schemaValidator.validate(schema);
|
|
148
|
-
|
|
149
|
-
const componentOutputdir = path.join(
|
|
150
|
-
outputDirectory,
|
|
151
|
-
'react/components',
|
|
152
|
-
libraryName,
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const generatedModuleFiles = [];
|
|
156
|
-
const generatedComponentFiles = [];
|
|
157
|
-
/*
|
|
158
|
-
for (const name of generators) {
|
|
159
|
-
for (const generator of GENERATORS[name]) {
|
|
160
|
-
generatedFiles.push(...generator(libraryName, schema, moduleSpecName));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
*/
|
|
164
|
-
|
|
165
|
-
const generateNM2 = createNM2Generator({namespace: argv.namespace});
|
|
166
|
-
const generatorPropsH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH')
|
|
167
|
-
.generate;
|
|
168
|
-
const generatorPropsCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP')
|
|
169
|
-
.generate;
|
|
170
|
-
const generatorShadowNodeH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH')
|
|
171
|
-
.generate;
|
|
172
|
-
const generatorShadowNodeCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP')
|
|
173
|
-
.generate;
|
|
174
|
-
const generatorComponentDescriptorH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH')
|
|
175
|
-
.generate;
|
|
176
|
-
const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH')
|
|
177
|
-
.generate;
|
|
178
|
-
|
|
179
|
-
generatedModuleFiles.push(
|
|
180
|
-
...generateNM2(libraryName, schema, moduleSpecName),
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
generatedComponentFiles.push(
|
|
184
|
-
...generatorPropsH(libraryName, schema, moduleSpecName),
|
|
185
|
-
...generatorPropsCPP(libraryName, schema, moduleSpecName),
|
|
186
|
-
...generatorShadowNodeH(libraryName, schema, moduleSpecName),
|
|
187
|
-
...generatorShadowNodeCPP(libraryName, schema, moduleSpecName),
|
|
188
|
-
...generatorComponentDescriptorH(libraryName, schema, moduleSpecName),
|
|
189
|
-
...generatorEventEmitterH(libraryName, schema, moduleSpecName),
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
const moduleFilesToUpdate = new Map<string, string>([
|
|
193
|
-
...generatedModuleFiles,
|
|
194
|
-
]);
|
|
195
|
-
const componentFilesToUpdate = new Map<string, string>([
|
|
196
|
-
...generatedComponentFiles,
|
|
197
|
-
]);
|
|
198
|
-
|
|
199
|
-
if (test === true) {
|
|
200
|
-
return (
|
|
201
|
-
checkFilesForChanges(moduleFilesToUpdate, outputDirectory) &&
|
|
202
|
-
checkFilesForChanges(componentFilesToUpdate, componentOutputdir)
|
|
203
|
-
);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return (
|
|
207
|
-
writeMapToFiles(moduleFilesToUpdate, outputDirectory) &&
|
|
208
|
-
writeMapToFiles(componentFilesToUpdate, componentOutputdir)
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
74
|
if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
|
|
213
75
|
console.error('You must specify either --file or --files.');
|
|
214
76
|
process.exit(1);
|
|
215
77
|
}
|
|
216
78
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
79
|
+
if (
|
|
80
|
+
argv.cppStringType !== 'std::string' &&
|
|
81
|
+
argv.cppStringType !== 'std::wstring'
|
|
82
|
+
) {
|
|
83
|
+
console.error('cppStringType should be "std::string" or "std::wstring".');
|
|
84
|
+
process.exit(1);
|
|
222
85
|
}
|
|
223
86
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
87
|
+
// type casting is necessary here because
|
|
88
|
+
// cppStringType does not become union of string literals
|
|
89
|
+
// until yargs.options get improved in the future
|
|
90
|
+
const changesNecessary = runCodeGen(<CodeGenOptions>argv);
|
|
91
|
+
|
|
92
|
+
if (argv.test && changesNecessary) {
|
|
93
|
+
console.error(
|
|
94
|
+
'There is a change in the output of codegen. Rerun "react-native codegen-windows" to regenerate.',
|
|
95
|
+
);
|
|
96
|
+
process.exit(2);
|
|
97
|
+
}
|