@react-native-windows/codegen 0.0.0-canary.4 → 0.0.0-canary.41
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 +359 -4
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +60 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +11 -0
- package/lib-commonjs/generators/AliasGen.js +72 -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 +12 -0
- package/lib-commonjs/generators/GenerateNM2.js +94 -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 +8 -0
- package/lib-commonjs/generators/ObjectTypes.js +53 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
- package/lib-commonjs/generators/ParamTypes.js +114 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
- package/lib-commonjs/generators/ReturnTypes.js +63 -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 +8 -0
- package/lib-commonjs/generators/ValidateMethods.js +70 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +35 -0
- package/lib-commonjs/index.js +190 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +32 -18
- package/src/Cli.ts +26 -152
- package/src/generators/AliasGen.ts +105 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +69 -297
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +73 -0
- package/src/generators/ParamTypes.ts +220 -0
- package/src/generators/ReturnTypes.ts +92 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +135 -0
- package/src/index.ts +321 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -462
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -0,0 +1,38 @@
|
|
|
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.generateValidateConstants = void 0;
|
|
9
|
+
const AliasManaging_1 = require("./AliasManaging");
|
|
10
|
+
function generateValidateConstants(nativeModule, aliases) {
|
|
11
|
+
const candidates = nativeModule.spec.properties.filter(prop => prop.name === 'getConstants');
|
|
12
|
+
if (candidates.length === 0) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
const getConstant = candidates[0];
|
|
16
|
+
const funcType = getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
17
|
+
? getConstant.typeAnnotation.typeAnnotation
|
|
18
|
+
: getConstant.typeAnnotation;
|
|
19
|
+
if (funcType.params.length > 0 ||
|
|
20
|
+
funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation') {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const constantType = funcType.returnTypeAnnotation;
|
|
24
|
+
if (constantType.properties.length === 0) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const cppName = (0, AliasManaging_1.getAnonymousAliasCppName)(aliases, 'Constants', constantType);
|
|
28
|
+
return [
|
|
29
|
+
` TypedConstant<${cppName}>{0},`,
|
|
30
|
+
` REACT_SHOW_CONSTANT_SPEC_ERRORS(
|
|
31
|
+
0,
|
|
32
|
+
"${cppName}",
|
|
33
|
+
" REACT_GET_CONSTANTS(GetConstants) ${cppName} GetConstants() noexcept {/*implementation*/}\\n"
|
|
34
|
+
" REACT_GET_CONSTANTS(GetConstants) static ${cppName} GetConstants() noexcept {/*implementation*/}\\n");`,
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
exports.generateValidateConstants = generateValidateConstants;
|
|
38
|
+
//# sourceMappingURL=ValidateConstants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidateConstants.js","sourceRoot":"","sources":["../../src/generators/ValidateConstants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAmE;AAEnE,SAAgB,yBAAyB,CACvC,YAAgC,EAChC,OAAiB;IAEjB,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CACpD,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,CACrC,CAAC;IACF,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;QAC3B,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,WAAW,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,QAAQ,GACZ,WAAW,CAAC,cAAc,CAAC,IAAI,KAAK,wBAAwB;QAC1D,CAAC,CAAC,WAAW,CAAC,cAAc,CAAC,cAAc;QAC3C,CAAC,CAAC,WAAW,CAAC,cAAc,CAAC;IACjC,IACE,QAAQ,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC;QAC1B,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,sBAAsB,EAC7D;QACA,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,YAAY,GAAG,QAAQ,CAAC,oBAAoB,CAAC;IACnD,IAAI,YAAY,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;QACxC,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,OAAO,GAAG,IAAA,wCAAwB,EAAC,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IAE7E,OAAO;QACL,uBAAuB,OAAO,OAAO;QACrC;;aAES,OAAO;mDAC+B,OAAO;0DACA,OAAO,qDAAqD;KACnH,CAAC;AACJ,CAAC;AAtCD,8DAsCC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {NativeModuleSchema} from 'react-native-tscodegen';\nimport {AliasMap, getAnonymousAliasCppName} from './AliasManaging';\n\nexport function generateValidateConstants(\n nativeModule: NativeModuleSchema,\n aliases: AliasMap,\n): [string, string] | 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 const cppName = getAnonymousAliasCppName(aliases, 'Constants', constantType);\n\n return [\n ` TypedConstant<${cppName}>{0},`,\n ` REACT_SHOW_CONSTANT_SPEC_ERRORS(\n 0,\n \"${cppName}\",\n \" REACT_GET_CONSTANTS(GetConstants) ${cppName} GetConstants() noexcept {/*implementation*/}\\\\n\"\n \" REACT_GET_CONSTANTS(GetConstants) static ${cppName} GetConstants() noexcept {/*implementation*/}\\\\n\");`,\n ];\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
import type { NativeModuleSchema } from 'react-native-tscodegen';
|
|
7
|
+
import { AliasMap } from './AliasManaging';
|
|
8
|
+
export declare function generateValidateMethods(nativeModule: NativeModuleSchema, aliases: AliasMap): [string, string];
|
|
@@ -0,0 +1,70 @@
|
|
|
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 isMethodReturnPromise(funcType) {
|
|
16
|
+
return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';
|
|
17
|
+
}
|
|
18
|
+
function getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName) {
|
|
19
|
+
const args = (0, ParamTypes_1.translateArgs)(funcType.params, aliases, baseAliasName);
|
|
20
|
+
if (isMethodReturnPromise(funcType)) {
|
|
21
|
+
// TODO: type of the promise could be provided in the future
|
|
22
|
+
args.push('::React::ReactPromise<::React::JSValue> &&result');
|
|
23
|
+
}
|
|
24
|
+
// TODO: be much more exhastive on the possible method signatures that can be used..
|
|
25
|
+
const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${prop.name}) ${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation, aliases, baseAliasName)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
|
|
26
|
+
const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${prop.name}) static ${(0, ReturnTypes_1.translateImplReturnType)(funcType.returnTypeAnnotation, aliases, baseAliasName)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
|
|
27
|
+
return [sig, staticsig];
|
|
28
|
+
}
|
|
29
|
+
function translatePossibleMethodSignatures(prop, funcType, aliases, baseAliasName) {
|
|
30
|
+
return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
|
|
31
|
+
.map(sig => `" ${sig}\\n"`)
|
|
32
|
+
.join('\n ');
|
|
33
|
+
}
|
|
34
|
+
function renderProperties(properties, aliases, tuple) {
|
|
35
|
+
// TODO: generate code for constants
|
|
36
|
+
return properties
|
|
37
|
+
.filter(prop => prop.name !== 'getConstants')
|
|
38
|
+
.map((prop, index) => {
|
|
39
|
+
// TODO: prop.optional === true
|
|
40
|
+
// TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
41
|
+
const propAliasName = prop.name;
|
|
42
|
+
const funcType = prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
43
|
+
? prop.typeAnnotation.typeAnnotation
|
|
44
|
+
: prop.typeAnnotation;
|
|
45
|
+
const traversedArgs = (0, ParamTypes_1.translateSpecArgs)(funcType.params, aliases, propAliasName);
|
|
46
|
+
const translatedReturnParam = (0, ReturnTypes_1.translateSpecReturnType)(funcType.returnTypeAnnotation, aliases, propAliasName);
|
|
47
|
+
if (isMethodReturnPromise(funcType)) {
|
|
48
|
+
// TODO: type of the promise could be provided in the future
|
|
49
|
+
traversedArgs.push('Promise<::React::JSValue>');
|
|
50
|
+
}
|
|
51
|
+
if (tuple) {
|
|
52
|
+
return ` ${isMethodSync(funcType) ? 'Sync' : ''}Method<${translatedReturnParam}(${traversedArgs.join(', ')}) noexcept>{${index}, L"${prop.name}"},`;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return ` REACT_SHOW_METHOD_SPEC_ERRORS(
|
|
56
|
+
${index},
|
|
57
|
+
"${prop.name}",
|
|
58
|
+
${translatePossibleMethodSignatures(prop, funcType, aliases, propAliasName)});`;
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.join('\n');
|
|
62
|
+
}
|
|
63
|
+
function generateValidateMethods(nativeModule, aliases) {
|
|
64
|
+
const properties = nativeModule.spec.properties;
|
|
65
|
+
const traversedProperties = renderProperties(properties, aliases, false);
|
|
66
|
+
const traversedPropertyTuples = renderProperties(properties, aliases, true);
|
|
67
|
+
return [traversedPropertyTuples, traversedProperties];
|
|
68
|
+
}
|
|
69
|
+
exports.generateValidateMethods = generateValidateMethods;
|
|
70
|
+
//# 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;;;AAQb,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,qBAAqB,CAAC,QAA4C;IACzE,OAAO,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,uBAAuB,CAAC;AACxE,CAAC;AAED,SAAS,2BAA2B,CAClC,IAA+B,EAC/B,QAA4C,EAC5C,OAAiB,EACjB,aAAqB;IAErB,MAAM,IAAI,GAAG,IAAA,0BAAa,EAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;IACpE,IAAI,qB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,OAAO,EAAE,IAAI,CAAC,CAAC;IAC5E,OAAO,CAAC,uBAAuB,EAAE,mBAAmB,CAAC,CAAC;AACxD,CAAC;AARD,0DAQC","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-tscodegen';\nimport {AliasMap} from './AliasManaging';\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 isMethodReturnPromise(funcType: NativeModuleFunctionTypeAnnotation) {\n return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';\n}\n\nfunction getPossibleMethodSignatures(\n prop: NativeModulePropertyShape,\n funcType: NativeModuleFunctionTypeAnnotation,\n aliases: AliasMap,\n baseAliasName: string,\n): string[] {\n const args = translateArgs(funcType.params, aliases, baseAliasName);\n if (isMethodReturnPromise(funcType)) {\n // TODO: type of the promise could be provided in the future\n args.push('::React::ReactPromise<::React::JSValue> &&result');\n }\n\n // TODO: be much more exhastive 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 )} ${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 )} ${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): string {\n return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)\n .map(sig => `\" ${sig}\\\\n\"`)\n .join('\\n ');\n}\n\nfunction renderProperties(\n properties: ReadonlyArray<NativeModulePropertyShape>,\n aliases: AliasMap,\n tuple: boolean,\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 );\n\n const translatedReturnParam = translateSpecReturnType(\n funcType.returnTypeAnnotation,\n aliases,\n propAliasName,\n );\n\n if (isMethodReturnPromise(funcType)) {\n // TODO: type of the promise could be provided in the future\n traversedArgs.push('Promise<::React::JSValue>');\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 )});`;\n }\n })\n .join('\\n');\n}\n\nexport function generateValidateMethods(\n nativeModule: NativeModuleSchema,\n aliases: AliasMap,\n): [string, string] {\n const properties = nativeModule.spec.properties;\n const traversedProperties = renderProperties(properties, aliases, false);\n const traversedPropertyTuples = renderProperties(properties, aliases, true);\n return [traversedPropertyTuples, traversedProperties];\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*
|
|
5
|
+
* @format
|
|
6
|
+
*/
|
|
7
|
+
import type { SchemaType } from 'react-native-tscodegen';
|
|
8
|
+
interface Options {
|
|
9
|
+
libraryName: string;
|
|
10
|
+
schema: SchemaType;
|
|
11
|
+
outputDirectory: string;
|
|
12
|
+
moduleSpecName: string;
|
|
13
|
+
namespace: string;
|
|
14
|
+
methodonly: boolean;
|
|
15
|
+
ts: boolean;
|
|
16
|
+
}
|
|
17
|
+
interface Config {
|
|
18
|
+
generators: any[];
|
|
19
|
+
test?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function parseFile(filename: string): SchemaType;
|
|
22
|
+
export declare function combineSchemas(files: string[]): SchemaType;
|
|
23
|
+
export declare function generate({ libraryName, schema, outputDirectory, moduleSpecName, namespace, methodonly, ts, }: Options, { /*generators,*/ test }: Config): boolean;
|
|
24
|
+
export declare type CodeGenOptions = {
|
|
25
|
+
file?: string;
|
|
26
|
+
files?: string[];
|
|
27
|
+
libraryName: string;
|
|
28
|
+
outdir: string;
|
|
29
|
+
namespace: string;
|
|
30
|
+
methodonly: boolean;
|
|
31
|
+
ts: boolean;
|
|
32
|
+
test: boolean;
|
|
33
|
+
};
|
|
34
|
+
export declare function runCodeGen(options: CodeGenOptions): boolean;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
const FlowParser = require(path_1.default.resolve(rncodegenPath, 'lib/parsers/flow'));
|
|
22
|
+
const TypeScriptParser = require(path_1.default.resolve(rncodegenPath, 'lib/parsers/typescript'));
|
|
23
|
+
const schemaValidator = require(path_1.default.resolve(rncodegenPath, 'lib/schemaValidator'));
|
|
24
|
+
function normalizeFileMap(map, outputDir, outMap) {
|
|
25
|
+
for (const [fileName, contents] of map) {
|
|
26
|
+
const location = path_1.default.join(outputDir, fileName);
|
|
27
|
+
outMap.set(path_1.default.normalize(location), contents);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function checkFilesForChanges(map, outputDir) {
|
|
31
|
+
let hasChanges = false;
|
|
32
|
+
const allExistingFiles = globby_1.default
|
|
33
|
+
.sync(`${outputDir}/**`)
|
|
34
|
+
.map(_ => path_1.default.normalize(_))
|
|
35
|
+
.sort();
|
|
36
|
+
const allGeneratedFiles = [...map.keys()].map(_ => path_1.default.normalize(_)).sort();
|
|
37
|
+
if (allExistingFiles.length !== allGeneratedFiles.length ||
|
|
38
|
+
!allExistingFiles.every((val, index) => val === allGeneratedFiles[index]))
|
|
39
|
+
return true;
|
|
40
|
+
for (const [fileName, contents] of map) {
|
|
41
|
+
if (!fs_1.default.existsSync(fileName)) {
|
|
42
|
+
hasChanges = true;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const currentContents = fs_1.default.readFileSync(fileName, 'utf8');
|
|
46
|
+
if (currentContents !== contents) {
|
|
47
|
+
console.log(`- ${fileName} has changed`);
|
|
48
|
+
hasChanges = true;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return hasChanges;
|
|
53
|
+
}
|
|
54
|
+
function writeMapToFiles(map, outputDir) {
|
|
55
|
+
let success = true;
|
|
56
|
+
// This ensures that we delete any generated files from modules that have been deleted
|
|
57
|
+
const allExistingFiles = globby_1.default.sync(`${outputDir}/**`);
|
|
58
|
+
allExistingFiles.forEach(existingFile => {
|
|
59
|
+
if (!map.has(path_1.default.normalize(existingFile))) {
|
|
60
|
+
fs_1.default.unlinkSync(existingFile);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
for (const [fileName, contents] of map) {
|
|
64
|
+
try {
|
|
65
|
+
fs_1.default.mkdirSync(path_1.default.dirname(fileName), { recursive: true });
|
|
66
|
+
if (fs_1.default.existsSync(fileName)) {
|
|
67
|
+
const currentContents = fs_1.default.readFileSync(fileName, 'utf8');
|
|
68
|
+
// Don't update the files if there are no changes as this breaks incremental builds
|
|
69
|
+
if (currentContents === contents) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
fs_1.default.writeFileSync(fileName, contents);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
success = false;
|
|
77
|
+
console.error(`Failed to write ${fileName} to ${fileName}`, error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return success;
|
|
81
|
+
}
|
|
82
|
+
function parseFile(filename) {
|
|
83
|
+
try {
|
|
84
|
+
const isTypeScript = path_1.default.extname(filename) === '.ts' || path_1.default.extname(filename) === '.tsx';
|
|
85
|
+
const schema = isTypeScript
|
|
86
|
+
? TypeScriptParser.parseFile(filename)
|
|
87
|
+
: FlowParser.parseFile(filename);
|
|
88
|
+
// there will be at most one turbo module per file
|
|
89
|
+
const moduleName = Object.keys(schema.modules)[0];
|
|
90
|
+
if (moduleName) {
|
|
91
|
+
const spec = schema.modules[moduleName];
|
|
92
|
+
if (spec.type === 'NativeModule') {
|
|
93
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
94
|
+
if (contents) {
|
|
95
|
+
// This is a temporary implementation until such information is added to the schema in facebook/react-native
|
|
96
|
+
if (contents.includes('TurboModuleRegistry.get<')) {
|
|
97
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, true);
|
|
98
|
+
}
|
|
99
|
+
else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {
|
|
100
|
+
(0, GenerateTypeScript_1.setOptionalTurboModule)(spec, false);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return schema;
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
if (e instanceof Error) {
|
|
109
|
+
e.message = `(${filename}): ${e.message}`;
|
|
110
|
+
}
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.parseFile = parseFile;
|
|
115
|
+
function combineSchemas(files) {
|
|
116
|
+
return files.reduce((merged, filename) => {
|
|
117
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
118
|
+
if (contents &&
|
|
119
|
+
(/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
|
|
120
|
+
contents.includes('extends TurboModule'))) {
|
|
121
|
+
const schema = parseFile(filename);
|
|
122
|
+
merged.modules = { ...merged.modules, ...schema.modules };
|
|
123
|
+
}
|
|
124
|
+
return merged;
|
|
125
|
+
}, { modules: {} });
|
|
126
|
+
}
|
|
127
|
+
exports.combineSchemas = combineSchemas;
|
|
128
|
+
function generate({ libraryName, schema, outputDirectory, moduleSpecName, namespace, methodonly, ts, }, { /*generators,*/ test }) {
|
|
129
|
+
schemaValidator.validate(schema);
|
|
130
|
+
const componentOutputdir = path_1.default.join(outputDirectory, 'react/components', libraryName);
|
|
131
|
+
const generatedFiles = new Map();
|
|
132
|
+
generatedFiles.set(path_1.default.join(outputDirectory, '.clang-format'), 'DisableFormat: true\nSortIncludes: false');
|
|
133
|
+
const generateNM2 = (0, GenerateNM2_1.createNM2Generator)({
|
|
134
|
+
namespace,
|
|
135
|
+
methodonly,
|
|
136
|
+
});
|
|
137
|
+
const generatorPropsH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GeneratePropsH')).generate;
|
|
138
|
+
const generatorPropsCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GeneratePropsCPP')).generate;
|
|
139
|
+
const generatorShadowNodeH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateShadowNodeH')).generate;
|
|
140
|
+
const generatorShadowNodeCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateShadowNodeCPP')).generate;
|
|
141
|
+
const generatorComponentDescriptorH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateComponentDescriptorH')).generate;
|
|
142
|
+
const generatorEventEmitterH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateEventEmitterH')).generate;
|
|
143
|
+
const generatorEventEmitterCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateEventEmitterCpp')).generate;
|
|
144
|
+
normalizeFileMap(generateNM2(libraryName, schema, moduleSpecName), outputDirectory, generatedFiles);
|
|
145
|
+
if (ts) {
|
|
146
|
+
normalizeFileMap((0, GenerateTypeScript_1.generateTypeScript)(libraryName, schema, moduleSpecName), outputDirectory, generatedFiles);
|
|
147
|
+
}
|
|
148
|
+
if (Object.keys(schema.modules).some(moduleName => schema.modules[moduleName].type === 'Component')) {
|
|
149
|
+
const componentGenerators = [
|
|
150
|
+
generatorPropsH,
|
|
151
|
+
generatorPropsCPP,
|
|
152
|
+
generatorShadowNodeH,
|
|
153
|
+
generatorShadowNodeCPP,
|
|
154
|
+
generatorComponentDescriptorH,
|
|
155
|
+
generatorEventEmitterH,
|
|
156
|
+
generatorEventEmitterCPP,
|
|
157
|
+
];
|
|
158
|
+
componentGenerators.forEach(generator => {
|
|
159
|
+
const generated = generator(libraryName, schema, moduleSpecName);
|
|
160
|
+
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (test === true) {
|
|
164
|
+
return checkFilesForChanges(generatedFiles, outputDirectory);
|
|
165
|
+
}
|
|
166
|
+
return writeMapToFiles(generatedFiles, outputDirectory);
|
|
167
|
+
}
|
|
168
|
+
exports.generate = generate;
|
|
169
|
+
function runCodeGen(options) {
|
|
170
|
+
if (!options.file && !options.files)
|
|
171
|
+
throw new Error('Must specify file or files option');
|
|
172
|
+
const schema = options.file
|
|
173
|
+
? parseFile(options.file)
|
|
174
|
+
: combineSchemas(globby_1.default.sync(options.files));
|
|
175
|
+
const libraryName = options.libraryName;
|
|
176
|
+
const moduleSpecName = 'moduleSpecName';
|
|
177
|
+
const outputDirectory = options.outdir;
|
|
178
|
+
const { namespace, methodonly, ts } = options;
|
|
179
|
+
return generate({
|
|
180
|
+
libraryName,
|
|
181
|
+
schema,
|
|
182
|
+
outputDirectory,
|
|
183
|
+
moduleSpecName,
|
|
184
|
+
namespace,
|
|
185
|
+
methodonly,
|
|
186
|
+
ts,
|
|
187
|
+
}, { generators: [], test: options.test });
|
|
188
|
+
}
|
|
189
|
+
exports.runCodeGen = runCodeGen;
|
|
190
|
+
//# 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;AAC5B,0DAA4D;AAC5D,wEAGyC;AAGzC,8CAA8C;AAC9C,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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,CAAC;IACvC,MAAM,EAAC,SAAS,EAAE,UAAU,EAAE,EAAE,EAAC,GAAG,OAAO,CAAC;IAC5C,OAAO,QAAQ,CACb;QACE,WAAW;QACX,MAAM;QACN,eAAe;QACf,cAAc;QACd,SAAS;QACT,UAAU;QACV,EAAE;KACH,EACD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAC,CACrC,CAAC;AACJ,CAAC;AAxBD,gCAwBC","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 {createNM2Generator} from './generators/GenerateNM2';\nimport {\n generateTypeScript,\n setOptionalTurboModule,\n} from './generators/GenerateTypeScript';\nimport type {SchemaType} from 'react-native-tscodegen';\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);\nconst FlowParser = require(path.resolve(rncodegenPath, 'lib/parsers/flow'));\nconst TypeScriptParser = require(path.resolve(\n rncodegenPath,\n 'lib/parsers/typescript',\n));\n\nconst schemaValidator = require(path.resolve(\n rncodegenPath,\n 'lib/schemaValidator',\n));\n\ninterface Options {\n libraryName: string;\n schema: SchemaType;\n outputDirectory: string;\n moduleSpecName: string;\n namespace: string;\n methodonly: boolean;\n ts: boolean;\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 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.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 // 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\nexport function parseFile(filename: string): SchemaType {\n try {\n const isTypeScript =\n path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';\n\n const schema = isTypeScript\n ? TypeScriptParser.parseFile(filename)\n : FlowParser.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\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 schema,\n outputDirectory,\n moduleSpecName,\n namespace,\n methodonly,\n ts,\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 namespace,\n methodonly,\n });\n\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\n normalizeFileMap(\n generateNM2(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n\n if (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\nexport type CodeGenOptions = {\n file?: string;\n files?: string[];\n libraryName: string;\n outdir: string;\n namespace: string;\n methodonly: boolean;\n ts: boolean;\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 outputDirectory = options.outdir;\n const {namespace, methodonly, ts} = options;\n return generate(\n {\n libraryName,\n schema,\n outputDirectory,\n moduleSpecName,\n namespace,\n methodonly,\n ts,\n },\n {generators: [], test: options.test},\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-windows/codegen",
|
|
3
|
-
"version": "0.0.0-canary.
|
|
3
|
+
"version": "0.0.0-canary.41",
|
|
4
4
|
"description": "Generators for react-native-codegen targeting react-native-windows",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "lib-commonjs/index.js",
|
|
6
6
|
"repository": "https://github.com/microsoft/react-native-windows",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"private": false,
|
|
@@ -15,33 +15,44 @@
|
|
|
15
15
|
"watch": "rnw-scripts watch"
|
|
16
16
|
},
|
|
17
17
|
"bin": {
|
|
18
|
-
"codegen": "./bin.js"
|
|
18
|
+
"react-native-windows-codegen": "./bin.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@react-native-windows/fs": "^0.0.0-canary.8",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^5.30.5",
|
|
23
|
+
"@typescript-eslint/parser": "^5.30.5",
|
|
21
24
|
"chalk": "^4.1.0",
|
|
22
|
-
"globby": "^
|
|
25
|
+
"globby": "^11.0.4",
|
|
23
26
|
"mustache": "^4.0.1",
|
|
24
|
-
"react-native-tscodegen": "0.66.1",
|
|
25
27
|
"source-map-support": "^0.5.19",
|
|
26
28
|
"yargs": "^16.2.0"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
|
-
"@rnw-scripts/eslint-config": "1.1.
|
|
30
|
-
"@rnw-scripts/jest-unittest-config": "1.
|
|
31
|
-
"@rnw-scripts/just-task": "2.
|
|
32
|
-
"@rnw-scripts/ts-config": "
|
|
31
|
+
"@rnw-scripts/eslint-config": "1.1.14",
|
|
32
|
+
"@rnw-scripts/jest-unittest-config": "1.4.0",
|
|
33
|
+
"@rnw-scripts/just-task": "2.3.0",
|
|
34
|
+
"@rnw-scripts/ts-config": "2.0.2",
|
|
33
35
|
"@types/chalk": "^2.2.0",
|
|
34
|
-
"@types/
|
|
35
|
-
"@types/jest": "^26.0.20",
|
|
36
|
+
"@types/jest": "^29.2.2",
|
|
36
37
|
"@types/node": "^14.14.22",
|
|
37
38
|
"@types/yargs": "16.0.0",
|
|
38
|
-
"babel-jest": "^
|
|
39
|
-
"eslint": "
|
|
40
|
-
"jest": "^
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"typescript": "^
|
|
39
|
+
"babel-jest": "^29.3.0",
|
|
40
|
+
"eslint": "^8.19.0",
|
|
41
|
+
"jest": "^29.0.3",
|
|
42
|
+
"prettier": "^2.4.1",
|
|
43
|
+
"react-native-tscodegen": "0.68.4",
|
|
44
|
+
"typescript": "^4.4.4"
|
|
44
45
|
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"react-native": "*"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"bin.js",
|
|
51
|
+
"CHANGELOG.md",
|
|
52
|
+
"README.md",
|
|
53
|
+
"lib-commonjs",
|
|
54
|
+
"src"
|
|
55
|
+
],
|
|
45
56
|
"beachball": {
|
|
46
57
|
"defaultNpmTag": "canary",
|
|
47
58
|
"disallowedChangeTypes": [
|
|
@@ -50,5 +61,8 @@
|
|
|
50
61
|
"patch"
|
|
51
62
|
]
|
|
52
63
|
},
|
|
53
|
-
"promoteRelease": true
|
|
64
|
+
"promoteRelease": true,
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": ">= 14"
|
|
67
|
+
}
|
|
54
68
|
}
|