@react-native-windows/codegen 0.0.0-canary.9 → 0.0.1-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +726 -15
  2. package/README.md +1 -1
  3. package/bin.js +0 -0
  4. package/lib-commonjs/Cli.d.ts +7 -0
  5. package/lib-commonjs/Cli.js +92 -0
  6. package/lib-commonjs/Cli.js.map +1 -0
  7. package/lib-commonjs/generators/AliasGen.d.ts +12 -0
  8. package/lib-commonjs/generators/AliasGen.js +88 -0
  9. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  10. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  11. package/lib-commonjs/generators/AliasManaging.js +49 -0
  12. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  13. package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
  14. package/lib-commonjs/generators/GenerateNM2.js +142 -0
  15. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  16. package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
  17. package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
  18. package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
  19. package/lib-commonjs/generators/ObjectTypes.d.ts +13 -0
  20. package/lib-commonjs/generators/ObjectTypes.js +75 -0
  21. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  22. package/lib-commonjs/generators/ParamTypes.d.ts +12 -0
  23. package/lib-commonjs/generators/ParamTypes.js +137 -0
  24. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  25. package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
  26. package/lib-commonjs/generators/ReturnTypes.js +29 -0
  27. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  28. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  29. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  30. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  31. package/lib-commonjs/generators/ValidateMethods.d.ts +9 -0
  32. package/lib-commonjs/generators/ValidateMethods.js +75 -0
  33. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  34. package/lib-commonjs/index.d.ts +37 -0
  35. package/lib-commonjs/index.js +224 -0
  36. package/lib-commonjs/index.js.map +1 -0
  37. package/package.json +37 -20
  38. package/src/Cli.ts +57 -190
  39. package/src/generators/AliasGen.ts +149 -0
  40. package/src/generators/AliasManaging.ts +75 -0
  41. package/src/generators/GenerateNM2.ts +138 -295
  42. package/src/generators/GenerateTypeScript.ts +247 -0
  43. package/src/generators/ObjectTypes.ts +130 -0
  44. package/src/generators/ParamTypes.ts +298 -0
  45. package/src/generators/ReturnTypes.ts +70 -0
  46. package/src/generators/ValidateConstants.ts +50 -0
  47. package/src/generators/ValidateMethods.ts +177 -0
  48. package/src/index.ts +393 -0
  49. package/.eslintrc.js +0 -4
  50. package/.vscode/launch.json +0 -23
  51. package/CHANGELOG.json +0 -583
  52. package/jest.config.js +0 -1
  53. package/tsconfig.json +0 -5
@@ -0,0 +1,75 @@
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.generateValidateMethods = void 0;
9
+ const ParamTypes_1 = require("./ParamTypes");
10
+ const ReturnTypes_1 = require("./ReturnTypes");
11
+ function isMethodSync(funcType) {
12
+ return (funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
13
+ funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation');
14
+ }
15
+ function getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName, options) {
16
+ const args = (0, ParamTypes_1.translateArgs)(funcType.params, aliases, baseAliasName, options);
17
+ if (funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation') {
18
+ if (funcType.returnTypeAnnotation.elementType) {
19
+ args.push(`::React::ReactPromise<${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation.elementType, aliases, baseAliasName, options)}> &&result`);
20
+ }
21
+ else {
22
+ args.push('::React::ReactPromise<::React::JSValue> &&result');
23
+ }
24
+ }
25
+ // TODO: be much more exhaustive on the possible method signatures that can be used..
26
+ const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${prop.name}) ${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation, aliases, baseAliasName, options)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
27
+ const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${prop.name}) static ${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation, aliases, baseAliasName, options)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
28
+ return [sig, staticsig];
29
+ }
30
+ function translatePossibleMethodSignatures(prop, funcType, aliases, baseAliasName, options) {
31
+ return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName, options)
32
+ .map(sig => `" ${sig}\\n"`)
33
+ .join('\n ');
34
+ }
35
+ function renderProperties(properties, aliases, tuple, options) {
36
+ // TODO: generate code for constants
37
+ return properties
38
+ .filter(prop => prop.name !== 'getConstants')
39
+ .map((prop, index) => {
40
+ // TODO: prop.optional === true
41
+ // TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
42
+ const propAliasName = prop.name;
43
+ const funcType = prop.typeAnnotation.type === 'NullableTypeAnnotation'
44
+ ? prop.typeAnnotation.typeAnnotation
45
+ : prop.typeAnnotation;
46
+ const traversedArgs = (0, ParamTypes_1.translateSpecArgs)(funcType.params, aliases, propAliasName, options);
47
+ const translatedReturnParam = (0, ReturnTypes_1.translateSpecReturnType)(funcType.returnTypeAnnotation, aliases, propAliasName, options);
48
+ if (funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation') {
49
+ if (funcType.returnTypeAnnotation.elementType) {
50
+ traversedArgs.push(`Promise<${(0, ReturnTypes_1.translateSpecReturnType)(funcType.returnTypeAnnotation.elementType, aliases, propAliasName, options)}>`);
51
+ }
52
+ else {
53
+ traversedArgs.push('Promise<::React::JSValue>');
54
+ }
55
+ }
56
+ if (tuple) {
57
+ return ` ${isMethodSync(funcType) ? 'Sync' : ''}Method<${translatedReturnParam}(${traversedArgs.join(', ')}) noexcept>{${index}, L"${prop.name}"},`;
58
+ }
59
+ else {
60
+ return ` REACT_SHOW_METHOD_SPEC_ERRORS(
61
+ ${index},
62
+ "${prop.name}",
63
+ ${translatePossibleMethodSignatures(prop, funcType, aliases, propAliasName, options)});`;
64
+ }
65
+ })
66
+ .join('\n');
67
+ }
68
+ function generateValidateMethods(nativeModule, aliases, options) {
69
+ const properties = nativeModule.spec.properties;
70
+ const traversedProperties = renderProperties(properties, aliases, false, options);
71
+ const traversedPropertyTuples = renderProperties(properties, aliases, true, options);
72
+ return [traversedPropertyTuples, traversedProperties];
73
+ }
74
+ exports.generateValidateMethods = generateValidateMethods;
75
+ //# sourceMappingURL=ValidateMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ValidateMethods.js","sourceRoot":"","sources":["../../src/generators/ValidateMethods.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AASb,6CAA8D;AAC9D,+CAA+E;AAE/E,SAAS,YAAY,CAAC,QAA4C;IAChE,OAAO,CACL,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,oBAAoB;QAC3D,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,uBAAuB,CAC/D,CAAC;AACJ,CAAC;AAED,SAAS,2BAA2B,CAClC,IAA+B,EAC/B,QAA4C,EAC5C,OAAiB,EACjB,aAAqB,EACrB,OAA0B;IAE1B,MAAM,IAAI,GAAG,IAAA,0BAAa,EAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;IAC7E,IAAI,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,uBAAuB,EAA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AC,IAAI,KAAK,CAAC;SAC5C;aAAM;YACL,OAAO;YACH,KAAK;aACJ,IAAI,CAAC,IAAI;YACV,iCAAiC,CACjC,IAAI,EACJ,QAAQ,EACR,OAAO,EACP,aAAa,EACb,OAAO,CACR,IAAI,CAAC;SACT;IACH,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,SAAgB,uBAAuB,CACrC,YAAgC,EAChC,OAAiB,EACjB,OAA0B;IAE1B,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC;IAChD,MAAM,mBAAmB,GAAG,gBAAgB,CAC1C,UAAU,EACV,OAAO,EACP,KAAK,EACL,OAAO,CACR,CAAC;IACF,MAAM,uBAAuB,GAAG,gBAAgB,CAC9C,UAAU,EACV,OAAO,EACP,IAAI,EACJ,OAAO,CACR,CAAC;IACF,OAAO,CAAC,uBAAuB,EAAE,mBAAmB,CAAC,CAAC;AACxD,CAAC;AAnBD,0DAmBC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {\n NativeModuleFunctionTypeAnnotation,\n NativeModulePropertyShape,\n NativeModuleSchema,\n} from '@react-native/codegen/lib/CodegenSchema';\nimport {AliasMap} from './AliasManaging';\nimport type {CppCodegenOptions} from './ObjectTypes';\nimport {translateArgs, translateSpecArgs} from './ParamTypes';\nimport {translateImplReturnType, translateSpecReturnType} from './ReturnTypes';\n\nfunction isMethodSync(funcType: NativeModuleFunctionTypeAnnotation) {\n return (\n funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&\n funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation'\n );\n}\n\nfunction getPossibleMethodSignatures(\n prop: NativeModulePropertyShape,\n funcType: NativeModuleFunctionTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n options: CppCodegenOptions,\n): string[] {\n const args = translateArgs(funcType.params, aliases, baseAliasName, options);\n if (funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation') {\n if (funcType.returnTypeAnnotation.elementType) {\n args.push(\n `::React::ReactPromise<${translateImplReturnType(\n funcType.returnTypeAnnotation.elementType,\n aliases,\n baseAliasName,\n options,\n )}> &&result`,\n );\n } else {\n args.push('::React::ReactPromise<::React::JSValue> &&result');\n }\n }\n\n // TODO: be much more exhaustive on the possible method signatures that can be used..\n const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${\n prop.name\n }) ${translateImplReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n baseAliasName,\n options,\n )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;\n\n const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${\n prop.name\n }) static ${translateImplReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n baseAliasName,\n options,\n )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;\n\n return [sig, staticsig];\n}\n\nfunction translatePossibleMethodSignatures(\n prop: NativeModulePropertyShape,\n funcType: NativeModuleFunctionTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n options: CppCodegenOptions,\n): string {\n return getPossibleMethodSignatures(\n prop,\n funcType,\n aliases,\n baseAliasName,\n options,\n )\n .map(sig => `\" ${sig}\\\\n\"`)\n .join('\\n ');\n}\n\nfunction renderProperties(\n properties: ReadonlyArray<NativeModulePropertyShape>,\n aliases: AliasMap,\n tuple: boolean,\n options: CppCodegenOptions,\n): string {\n // TODO: generate code for constants\n return properties\n .filter(prop => prop.name !== 'getConstants')\n .map((prop, index) => {\n // TODO: prop.optional === true\n // TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'\n const propAliasName = prop.name;\n const funcType =\n prop.typeAnnotation.type === 'NullableTypeAnnotation'\n ? prop.typeAnnotation.typeAnnotation\n : prop.typeAnnotation;\n\n const traversedArgs = translateSpecArgs(\n funcType.params,\n aliases,\n propAliasName,\n options,\n );\n\n const translatedReturnParam = translateSpecReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n propAliasName,\n options,\n );\n\n if (funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation') {\n if (funcType.returnTypeAnnotation.elementType) {\n traversedArgs.push(\n `Promise<${translateSpecReturnType(\n funcType.returnTypeAnnotation.elementType,\n aliases,\n propAliasName,\n options,\n )}>`,\n );\n } else {\n traversedArgs.push('Promise<::React::JSValue>');\n }\n }\n\n if (tuple) {\n return ` ${\n isMethodSync(funcType) ? 'Sync' : ''\n }Method<${translatedReturnParam}(${traversedArgs.join(\n ', ',\n )}) noexcept>{${index}, L\"${prop.name}\"},`;\n } else {\n return ` REACT_SHOW_METHOD_SPEC_ERRORS(\n ${index},\n \"${prop.name}\",\n ${translatePossibleMethodSignatures(\n prop,\n funcType,\n aliases,\n propAliasName,\n options,\n )});`;\n }\n })\n .join('\\n');\n}\n\nexport function generateValidateMethods(\n nativeModule: NativeModuleSchema,\n aliases: AliasMap,\n options: CppCodegenOptions,\n): [string, string] {\n const properties = nativeModule.spec.properties;\n const traversedProperties = renderProperties(\n properties,\n aliases,\n false,\n options,\n );\n const traversedPropertyTuples = renderProperties(\n properties,\n aliases,\n true,\n options,\n );\n return [traversedPropertyTuples, traversedProperties];\n}\n"]}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ *
5
+ * @format
6
+ */
7
+ import type { CppStringTypes } from './generators/GenerateNM2';
8
+ import type { SchemaType } from '@react-native/codegen/lib/CodegenSchema';
9
+ export type { CppStringTypes } from './generators/GenerateNM2';
10
+ export interface SharedOptions {
11
+ libraryName: string;
12
+ methodOnly: boolean;
13
+ modulesCxx: boolean;
14
+ modulesTypeScriptTypes: boolean;
15
+ modulesWindows: boolean;
16
+ namespace: string;
17
+ outputDirectory: string;
18
+ cppStringType: CppStringTypes;
19
+ separateDataTypes: boolean;
20
+ }
21
+ interface Options extends SharedOptions {
22
+ moduleSpecName: string;
23
+ schema: SchemaType;
24
+ }
25
+ interface Config {
26
+ generators: any[];
27
+ test?: boolean;
28
+ }
29
+ export declare function parseFile(filename: string): SchemaType;
30
+ export declare function combineSchemas(files: string[]): SchemaType;
31
+ export declare function generate({ libraryName, methodOnly, modulesCxx, modulesTypeScriptTypes, modulesWindows, namespace, outputDirectory, cppStringType, separateDataTypes, moduleSpecName, schema, }: Options, { /*generators,*/ test }: Config): boolean;
32
+ export interface CodeGenOptions extends SharedOptions {
33
+ file?: string;
34
+ files?: string[];
35
+ test: boolean;
36
+ }
37
+ export declare function runCodeGen(options: CodeGenOptions): boolean;
@@ -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,CAAC;QACH,OAAO,IAAI,EAAE,CAAC,gBAAgB,EAAE,CAAC;KAClC;SAAM;QACL,MAAM,EAAE,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAAC,aAAa,EAAE,yBAAyB,CAAC,CAAC,CAAC;QAC3E,OAAO,IAAI,EAAE,CAAC,UAAU,EAAE,CAAC;KAC5B;AACH,CAAC;AAED,MAAM,eAAe,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAC1C,aAAa,EACb,qBAAqB,CACtB,CAAC,CAAC;AAwBH,SAAS,gBAAgB,CACvB,GAAwB,EACxB,SAAiB,EACjB,MAA2B;IAE3B,KAAK,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,GAAG,EAAE;QACtC,MAAM,QAAQ,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,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ACb,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.9",
3
+ "version": "0.0.1-0",
4
4
  "description": "Generators for react-native-codegen targeting react-native-windows",
5
- "main": "index.js",
6
- "repository": "https://github.com/microsoft/react-native-windows",
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.1-0",
21
26
  "chalk": "^4.1.0",
22
- "globby": "^9.2.0",
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.1.7",
30
- "@rnw-scripts/jest-unittest-config": "1.2.3",
31
- "@rnw-scripts/just-task": "2.2.0",
32
- "@rnw-scripts/ts-config": "2.0.0",
33
+ "@rnw-scripts/eslint-config": "1.2.12",
34
+ "@rnw-scripts/jest-unittest-config": "1.5.8",
35
+ "@rnw-scripts/just-task": "2.3.28",
36
+ "@rnw-scripts/ts-config": "2.0.5",
33
37
  "@types/chalk": "^2.2.0",
34
- "@types/globby": "^9.1.0",
35
- "@types/jest": "^26.0.20",
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
- "babel-jest": "^26.3.0",
39
- "eslint": "7.12.0",
40
- "jest": "^26.6.3",
41
- "just-scripts": "^1.3.3",
42
- "prettier": "1.19.1",
43
- "typescript": "^3.8.3"
41
+ "@typescript-eslint/eslint-plugin": "^5.30.5",
42
+ "@typescript-eslint/parser": "^5.57.1",
43
+ "babel-jest": "^29.6.3",
44
+ "eslint": "^8.19.0",
45
+ "jest": "^29.6.3",
46
+ "prettier": "2.8.8",
47
+ "typescript": "5.0.4"
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
  }