nitrogen 0.2.23 → 0.29.4
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/README.md +15 -109
- package/lib/Logger.d.ts +10 -0
- package/lib/Logger.js +56 -0
- package/lib/autolinking/Autolinking.d.ts +8 -0
- package/lib/autolinking/Autolinking.js +1 -0
- package/lib/autolinking/android/createCMakeExtension.d.ts +6 -0
- package/lib/autolinking/android/createCMakeExtension.js +106 -0
- package/lib/autolinking/android/createGradleExtension.d.ts +5 -0
- package/lib/autolinking/android/createGradleExtension.js +36 -0
- package/lib/autolinking/android/createHybridObjectInitializer.d.ts +2 -0
- package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
- package/lib/autolinking/createAndroidAutolinking.d.ts +11 -0
- package/lib/autolinking/createAndroidAutolinking.js +13 -0
- package/lib/autolinking/createIOSAutolinking.d.ts +5 -0
- package/lib/autolinking/createIOSAutolinking.js +19 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.d.ts +9 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.d.ts +5 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.d.ts +2 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.d.ts +3 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
- package/lib/config/NitroConfig.d.ts +52 -0
- package/lib/config/NitroConfig.js +112 -0
- package/lib/config/NitroUserConfig.d.ts +22 -0
- package/lib/config/NitroUserConfig.js +88 -0
- package/lib/config/getConfig.d.ts +2 -0
- package/lib/config/getConfig.js +84 -0
- package/lib/createGitAttributes.d.ts +1 -0
- package/lib/createGitAttributes.js +11 -0
- package/lib/createPlatformSpec.d.ts +4 -0
- package/lib/createPlatformSpec.js +127 -0
- package/lib/getFiles.d.ts +1 -0
- package/lib/getFiles.js +28 -0
- package/lib/getPlatformSpecs.d.ts +17 -0
- package/lib/getPlatformSpecs.js +153 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +113 -10
- package/lib/init.d.ts +1 -0
- package/lib/init.js +123 -0
- package/lib/nitrogen.d.ts +11 -0
- package/lib/nitrogen.js +165 -0
- package/lib/prettifyDirectory.d.ts +1 -0
- package/lib/prettifyDirectory.js +27 -0
- package/lib/syntax/BridgedType.d.ts +41 -0
- package/lib/syntax/BridgedType.js +1 -0
- package/lib/syntax/CodeNode.d.ts +23 -0
- package/lib/syntax/CodeNode.js +1 -0
- package/lib/syntax/HybridObjectSpec.d.ts +13 -0
- package/lib/syntax/HybridObjectSpec.js +1 -0
- package/lib/syntax/Method.d.ts +41 -0
- package/lib/syntax/Method.js +108 -0
- package/lib/syntax/Parameter.d.ts +15 -0
- package/lib/syntax/Parameter.js +65 -0
- package/lib/syntax/Property.d.ts +50 -0
- package/lib/syntax/Property.js +147 -0
- package/lib/syntax/SourceFile.d.ts +70 -0
- package/lib/syntax/SourceFile.js +7 -0
- package/lib/syntax/c++/CppEnum.d.ts +6 -0
- package/lib/syntax/c++/CppEnum.js +110 -0
- package/lib/syntax/c++/CppHybridObject.d.ts +3 -0
- package/lib/syntax/c++/CppHybridObject.js +146 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
- package/lib/syntax/c++/CppStruct.d.ts +3 -0
- package/lib/syntax/c++/CppStruct.js +108 -0
- package/lib/syntax/c++/CppUnion.d.ts +6 -0
- package/lib/syntax/c++/CppUnion.js +88 -0
- package/lib/syntax/c++/getForwardDeclaration.d.ts +3 -0
- package/lib/syntax/c++/getForwardDeclaration.js +14 -0
- package/lib/syntax/c++/includeNitroHeader.d.ts +7 -0
- package/lib/syntax/c++/includeNitroHeader.js +34 -0
- package/lib/syntax/createType.d.ts +15 -0
- package/lib/syntax/createType.js +303 -0
- package/lib/syntax/getAllTypes.d.ts +3 -0
- package/lib/syntax/getAllTypes.js +11 -0
- package/lib/syntax/getCustomTypeConfig.d.ts +8 -0
- package/lib/syntax/getCustomTypeConfig.js +53 -0
- package/lib/syntax/getHybridObjectName.d.ts +36 -0
- package/lib/syntax/getHybridObjectName.js +10 -0
- package/lib/syntax/getInterfaceProperties.d.ts +4 -0
- package/lib/syntax/getInterfaceProperties.js +9 -0
- package/lib/syntax/getReferencedTypes.d.ts +2 -0
- package/lib/syntax/getReferencedTypes.js +47 -0
- package/lib/syntax/helpers.d.ts +13 -0
- package/lib/syntax/helpers.js +53 -0
- package/lib/syntax/isCoreType.d.ts +9 -0
- package/lib/syntax/isCoreType.js +47 -0
- package/lib/syntax/kotlin/FbjniHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.d.ts +8 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.d.ts +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.d.ts +19 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
- package/lib/syntax/kotlin/KotlinEnum.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinEnum.js +113 -0
- package/lib/syntax/kotlin/KotlinFunction.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinFunction.js +256 -0
- package/lib/syntax/kotlin/KotlinHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
- package/lib/syntax/kotlin/KotlinStruct.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinStruct.js +172 -0
- package/lib/syntax/kotlin/KotlinVariant.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinVariant.js +191 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.d.ts +21 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.d.ts +17 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
- package/lib/syntax/swift/SwiftEnum.d.ts +3 -0
- package/lib/syntax/swift/SwiftEnum.js +52 -0
- package/lib/syntax/swift/SwiftFunction.d.ts +3 -0
- package/lib/syntax/swift/SwiftFunction.js +83 -0
- package/lib/syntax/swift/SwiftHybridObject.d.ts +3 -0
- package/lib/syntax/swift/SwiftHybridObject.js +103 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.d.ts +11 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.d.ts +19 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
- package/lib/syntax/swift/SwiftStruct.d.ts +3 -0
- package/lib/syntax/swift/SwiftStruct.js +75 -0
- package/lib/syntax/swift/SwiftVariant.d.ts +3 -0
- package/lib/syntax/swift/SwiftVariant.js +58 -0
- package/lib/syntax/types/ArrayBufferType.d.ts +10 -0
- package/lib/syntax/types/ArrayBufferType.js +37 -0
- package/lib/syntax/types/ArrayType.d.ts +12 -0
- package/lib/syntax/types/ArrayType.js +52 -0
- package/lib/syntax/types/BigIntType.d.ts +10 -0
- package/lib/syntax/types/BigIntType.js +27 -0
- package/lib/syntax/types/BooleanType.d.ts +10 -0
- package/lib/syntax/types/BooleanType.js +27 -0
- package/lib/syntax/types/CustomType.d.ts +14 -0
- package/lib/syntax/types/CustomType.js +36 -0
- package/lib/syntax/types/DateType.d.ts +10 -0
- package/lib/syntax/types/DateType.js +35 -0
- package/lib/syntax/types/EnumType.d.ts +23 -0
- package/lib/syntax/types/EnumType.js +101 -0
- package/lib/syntax/types/ErrorType.d.ts +11 -0
- package/lib/syntax/types/ErrorType.js +37 -0
- package/lib/syntax/types/FunctionType.d.ts +29 -0
- package/lib/syntax/types/FunctionType.js +147 -0
- package/lib/syntax/types/HybridObjectBaseType.d.ts +11 -0
- package/lib/syntax/types/HybridObjectBaseType.js +38 -0
- package/lib/syntax/types/HybridObjectType.d.ts +23 -0
- package/lib/syntax/types/HybridObjectType.js +131 -0
- package/lib/syntax/types/MapType.d.ts +10 -0
- package/lib/syntax/types/MapType.js +37 -0
- package/lib/syntax/types/NamedWrappingType.d.ts +14 -0
- package/lib/syntax/types/NamedWrappingType.js +27 -0
- package/lib/syntax/types/NullType.d.ts +10 -0
- package/lib/syntax/types/NullType.js +23 -0
- package/lib/syntax/types/NumberType.d.ts +10 -0
- package/lib/syntax/types/NumberType.js +27 -0
- package/lib/syntax/types/OptionalType.d.ts +13 -0
- package/lib/syntax/types/OptionalType.js +59 -0
- package/lib/syntax/types/PromiseType.d.ts +16 -0
- package/lib/syntax/types/PromiseType.js +62 -0
- package/lib/syntax/types/RecordType.d.ts +13 -0
- package/lib/syntax/types/RecordType.js +47 -0
- package/lib/syntax/types/ResultWrappingType.d.ts +13 -0
- package/lib/syntax/types/ResultWrappingType.js +44 -0
- package/lib/syntax/types/StringType.d.ts +10 -0
- package/lib/syntax/types/StringType.js +35 -0
- package/lib/syntax/types/StructType.d.ts +14 -0
- package/lib/syntax/types/StructType.js +61 -0
- package/lib/syntax/types/TupleType.d.ts +12 -0
- package/lib/syntax/types/TupleType.js +39 -0
- package/lib/syntax/types/Type.d.ts +55 -0
- package/lib/syntax/types/Type.js +1 -0
- package/lib/syntax/types/VariantType.d.ts +19 -0
- package/lib/syntax/types/VariantType.js +75 -0
- package/lib/syntax/types/VoidType.d.ts +10 -0
- package/lib/syntax/types/VoidType.js +27 -0
- package/lib/syntax/types/getTypeAs.d.ts +2 -0
- package/lib/syntax/types/getTypeAs.js +12 -0
- package/lib/utils.d.ts +22 -0
- package/lib/utils.js +126 -0
- package/lib/views/CppHybridViewComponent.d.ts +14 -0
- package/lib/views/CppHybridViewComponent.js +254 -0
- package/lib/views/createHostComponentJs.d.ts +3 -0
- package/lib/views/createHostComponentJs.js +27 -0
- package/lib/views/kotlin/KotlinHybridViewManager.d.ts +3 -0
- package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
- package/lib/views/swift/SwiftHybridViewManager.d.ts +3 -0
- package/lib/views/swift/SwiftHybridViewManager.js +131 -0
- package/lib/writeFile.d.ts +5 -0
- package/lib/writeFile.js +19 -0
- package/package.json +58 -29
- package/src/Logger.ts +63 -0
- package/src/autolinking/Autolinking.ts +9 -0
- package/src/autolinking/android/createCMakeExtension.ts +123 -0
- package/src/autolinking/android/createGradleExtension.ts +43 -0
- package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
- package/src/autolinking/createAndroidAutolinking.ts +28 -0
- package/src/autolinking/createIOSAutolinking.ts +24 -0
- package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
- package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
- package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
- package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
- package/src/config/NitroConfig.ts +139 -0
- package/src/config/NitroUserConfig.ts +105 -0
- package/src/config/getConfig.ts +91 -0
- package/src/createGitAttributes.ts +15 -0
- package/src/createPlatformSpec.ts +176 -0
- package/src/getFiles.ts +31 -0
- package/src/getPlatformSpecs.ts +202 -0
- package/src/index.ts +146 -0
- package/src/init.ts +186 -0
- package/src/nitrogen.ts +246 -0
- package/src/prettifyDirectory.ts +32 -0
- package/src/syntax/BridgedType.ts +59 -0
- package/src/syntax/CodeNode.ts +24 -0
- package/src/syntax/HybridObjectSpec.ts +14 -0
- package/src/syntax/Method.ts +154 -0
- package/src/syntax/Parameter.ts +81 -0
- package/src/syntax/Property.ts +203 -0
- package/src/syntax/SourceFile.ts +80 -0
- package/src/syntax/c++/CppEnum.ts +128 -0
- package/src/syntax/c++/CppHybridObject.ts +165 -0
- package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
- package/src/syntax/c++/CppStruct.ts +129 -0
- package/src/syntax/c++/CppUnion.ts +105 -0
- package/src/syntax/c++/getForwardDeclaration.ts +19 -0
- package/src/syntax/c++/includeNitroHeader.ts +40 -0
- package/src/syntax/createType.ts +365 -0
- package/src/syntax/getAllTypes.ts +18 -0
- package/src/syntax/getCustomTypeConfig.ts +71 -0
- package/src/syntax/getHybridObjectName.ts +48 -0
- package/src/syntax/getInterfaceProperties.ts +21 -0
- package/src/syntax/getReferencedTypes.ts +57 -0
- package/src/syntax/helpers.ts +79 -0
- package/src/syntax/isCoreType.ts +60 -0
- package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
- package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
- package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
- package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
- package/src/syntax/kotlin/KotlinEnum.ts +130 -0
- package/src/syntax/kotlin/KotlinFunction.ts +277 -0
- package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
- package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
- package/src/syntax/kotlin/KotlinStruct.ts +198 -0
- package/src/syntax/kotlin/KotlinVariant.ts +212 -0
- package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
- package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
- package/src/syntax/swift/SwiftEnum.ts +65 -0
- package/src/syntax/swift/SwiftFunction.ts +91 -0
- package/src/syntax/swift/SwiftHybridObject.ts +121 -0
- package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
- package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
- package/src/syntax/swift/SwiftStruct.ts +85 -0
- package/src/syntax/swift/SwiftVariant.ts +67 -0
- package/src/syntax/types/ArrayBufferType.ts +49 -0
- package/src/syntax/types/ArrayType.ts +62 -0
- package/src/syntax/types/BigIntType.ts +35 -0
- package/src/syntax/types/BooleanType.ts +35 -0
- package/src/syntax/types/CustomType.ts +47 -0
- package/src/syntax/types/DateType.ts +43 -0
- package/src/syntax/types/EnumType.ts +130 -0
- package/src/syntax/types/ErrorType.ts +44 -0
- package/src/syntax/types/FunctionType.ts +167 -0
- package/src/syntax/types/HybridObjectBaseType.ts +54 -0
- package/src/syntax/types/HybridObjectType.ts +198 -0
- package/src/syntax/types/MapType.ts +49 -0
- package/src/syntax/types/NamedWrappingType.ts +33 -0
- package/src/syntax/types/NullType.ts +30 -0
- package/src/syntax/types/NumberType.ts +34 -0
- package/src/syntax/types/OptionalType.ts +66 -0
- package/src/syntax/types/PromiseType.ts +72 -0
- package/src/syntax/types/RecordType.ts +56 -0
- package/src/syntax/types/ResultWrappingType.ts +53 -0
- package/src/syntax/types/StringType.ts +44 -0
- package/src/syntax/types/StructType.ts +83 -0
- package/src/syntax/types/TupleType.ts +53 -0
- package/src/syntax/types/Type.ts +82 -0
- package/src/syntax/types/VariantType.ts +92 -0
- package/src/syntax/types/VoidType.ts +34 -0
- package/src/syntax/types/getTypeAs.ts +15 -0
- package/src/utils.ts +162 -0
- package/src/views/CppHybridViewComponent.ts +301 -0
- package/src/views/createHostComponentJs.ts +34 -0
- package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
- package/src/views/swift/SwiftHybridViewManager.ts +153 -0
- package/src/writeFile.ts +27 -0
- package/.jshintignore +0 -6
- package/.jshintrc +0 -3
- package/.npmignore +0 -3
- package/.travis.yml +0 -13
- package/LICENSE +0 -13
- package/browser/nitrogen-min.js +0 -3
- package/browser/nitrogen.js +0 -6369
- package/lib/apiKey.js +0 -67
- package/lib/blob.js +0 -57
- package/lib/commandManager.js +0 -350
- package/lib/device.js +0 -19
- package/lib/memoryStore.js +0 -24
- package/lib/message.js +0 -298
- package/lib/permission.js +0 -121
- package/lib/principal.js +0 -330
- package/lib/service.js +0 -349
- package/lib/session.js +0 -494
- package/lib/user.js +0 -20
- package/publish +0 -2
- package/scripts/build-documentation +0 -4
- package/scripts/build-module +0 -27
- package/scripts/module.js +0 -12
- package/scripts/postamble.js +0 -1
- package/scripts/preamble.js +0 -2
- package/scripts/run-test-server +0 -9
- package/test/config.js +0 -12
- package/test/fixtures/images/image.jpg +0 -0
- package/test/fixtures/images/motion0.jpg +0 -0
- package/test/fixtures/images/motion1.jpg +0 -0
- package/test/fixtures/images/motion2.jpg +0 -0
- package/test/fixtures/index.js +0 -76
- package/test/main.js +0 -5
- package/test/memoryStore.js +0 -22
- package/test/mocha.opts +0 -3
- package/test/units/apiKey.js +0 -46
- package/test/units/blob.js +0 -35
- package/test/units/commandManager.js +0 -67
- package/test/units/device.js +0 -26
- package/test/units/heartbeat.js +0 -28
- package/test/units/message.js +0 -79
- package/test/units/permissions.js +0 -43
- package/test/units/principal.js +0 -116
- package/test/units/service.js +0 -92
- package/test/units/session.js +0 -97
- package/test/units/user.js +0 -48
- package/yuidoc.json +0 -8
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ZodError } from 'zod';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { NitroUserConfigSchema, } from './NitroUserConfig.js';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
function readFile(configPath) {
|
|
6
|
+
try {
|
|
7
|
+
return fs.readFileSync(configPath, 'utf8');
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
if (typeof error === 'object' && error != null && 'code' in error) {
|
|
11
|
+
switch (error.code) {
|
|
12
|
+
case 'ENOENT':
|
|
13
|
+
case 'ENOTDIR':
|
|
14
|
+
console.error(`❌ The path ${chalk.underline(configPath)} does not exist! Create a ${chalk.underline('nitro.json')} file and try again.`);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
break;
|
|
18
|
+
default:
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
// different kind of error, throw it.
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function propPathToString(propPath) {
|
|
29
|
+
if (propPath.length === 0)
|
|
30
|
+
return '';
|
|
31
|
+
const prop = propPath.reduce((prev, curr) => {
|
|
32
|
+
if (typeof curr === 'string') {
|
|
33
|
+
return `${prev}.${curr}`;
|
|
34
|
+
}
|
|
35
|
+
else if (typeof curr === 'number') {
|
|
36
|
+
return `${prev}[${curr}]`;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return prev;
|
|
40
|
+
}
|
|
41
|
+
}, '');
|
|
42
|
+
return prop.slice(1);
|
|
43
|
+
}
|
|
44
|
+
function parseConfig(json) {
|
|
45
|
+
let object;
|
|
46
|
+
try {
|
|
47
|
+
object = JSON.parse(json);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw new Error(`Failed to parse nitro.json config as JSON! Make sure it has a valid JSON syntax. JSON: ${json}`, {
|
|
51
|
+
cause: error,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
return NitroUserConfigSchema.parse(object);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error instanceof ZodError) {
|
|
59
|
+
const issues = error.issues.map((i) => {
|
|
60
|
+
const prop = propPathToString(i.path);
|
|
61
|
+
switch (i.code) {
|
|
62
|
+
case 'invalid_type':
|
|
63
|
+
return `\`${prop}\` must be ${i.expected}, but is ${i.input}.`;
|
|
64
|
+
case 'invalid_value':
|
|
65
|
+
case 'invalid_union':
|
|
66
|
+
case 'invalid_element':
|
|
67
|
+
return `\`${prop}\` is not a valid & safe string. It must only contain alphanumeric characters and must not start with a number.`;
|
|
68
|
+
default:
|
|
69
|
+
return `\`${prop}\`: ${i.message} (${i.code})`;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
console.error(`❌ Invalid nitro.json config file! ${issues.join(' - also, ')}`);
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export function readUserConfig(configPath) {
|
|
81
|
+
const json = readFile(configPath);
|
|
82
|
+
const config = parseConfig(json);
|
|
83
|
+
return config;
|
|
84
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createGitAttributes(markAsGenerated: boolean, folder: string): Promise<string>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export async function createGitAttributes(markAsGenerated, folder) {
|
|
4
|
+
const file = path.join(folder, '.gitattributes');
|
|
5
|
+
// Marks all files in this current folder as "generated"
|
|
6
|
+
const content = `
|
|
7
|
+
** linguist-generated=${markAsGenerated}
|
|
8
|
+
`.trim();
|
|
9
|
+
await fs.writeFile(file, content + '\n', 'utf-8');
|
|
10
|
+
return file;
|
|
11
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Node, Type } from 'ts-morph';
|
|
2
|
+
import { createCppHybridObject } from './syntax/c++/CppHybridObject.js';
|
|
3
|
+
import { extendsHybridObject, isHybridView, isAnyHybridSubclass, isDirectlyHybridObject, isHybridViewProps, isHybridViewMethods, } from './getPlatformSpecs.js';
|
|
4
|
+
import { Property } from './syntax/Property.js';
|
|
5
|
+
import { Method } from './syntax/Method.js';
|
|
6
|
+
import { createSwiftHybridObject } from './syntax/swift/SwiftHybridObject.js';
|
|
7
|
+
import { createKotlinHybridObject } from './syntax/kotlin/KotlinHybridObject.js';
|
|
8
|
+
import { createType } from './syntax/createType.js';
|
|
9
|
+
import { Parameter } from './syntax/Parameter.js';
|
|
10
|
+
import { getBaseTypes, getHybridObjectNitroModuleConfig } from './utils.js';
|
|
11
|
+
import { NitroConfig } from './config/NitroConfig.js';
|
|
12
|
+
export function generatePlatformFiles(interfaceType, language) {
|
|
13
|
+
const spec = getHybridObjectSpec(interfaceType, language);
|
|
14
|
+
// TODO: We currently just call this so the HybridObject itself is a "known type".
|
|
15
|
+
// This causes the Swift Umbrella header to properly forward-declare it.
|
|
16
|
+
// Without this, only Hybrid Objects that are actually used in public APIs will be forward-declared.
|
|
17
|
+
createType(language, interfaceType, false);
|
|
18
|
+
switch (language) {
|
|
19
|
+
case 'c++':
|
|
20
|
+
return generateCppFiles(spec);
|
|
21
|
+
case 'swift':
|
|
22
|
+
return generateSwiftFiles(spec);
|
|
23
|
+
case 'kotlin':
|
|
24
|
+
return generateKotlinFiles(spec);
|
|
25
|
+
default:
|
|
26
|
+
throw new Error(`Language "${language}" is not supported!`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function getHybridObjectSpec(type, language) {
|
|
30
|
+
const config = getHybridObjectNitroModuleConfig(type) ?? NitroConfig.current;
|
|
31
|
+
if (isHybridView(type)) {
|
|
32
|
+
const symbol = type.getAliasSymbolOrThrow();
|
|
33
|
+
const name = symbol.getEscapedName();
|
|
34
|
+
// It's a Hybrid View - the `Props & Methods` types are just intersected together.
|
|
35
|
+
const unions = type.getIntersectionTypes();
|
|
36
|
+
const props = unions.find((t) => isHybridViewProps(t));
|
|
37
|
+
const methods = unions.find((t) => isHybridViewMethods(t));
|
|
38
|
+
if (props == null)
|
|
39
|
+
throw new Error(`Props cannot be null! ${name}<...> (HybridView) requires type arguments.`);
|
|
40
|
+
const propsSpec = getHybridObjectSpec(props, language);
|
|
41
|
+
const methodsSpec = methods != null ? getHybridObjectSpec(methods, language) : undefined;
|
|
42
|
+
return {
|
|
43
|
+
baseTypes: [],
|
|
44
|
+
isHybridView: true,
|
|
45
|
+
language: language,
|
|
46
|
+
methods: methodsSpec?.methods ?? [],
|
|
47
|
+
properties: propsSpec.properties,
|
|
48
|
+
name: name,
|
|
49
|
+
config: config,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const symbol = type.getSymbolOrThrow();
|
|
53
|
+
const name = symbol.getEscapedName();
|
|
54
|
+
const properties = [];
|
|
55
|
+
const methods = [];
|
|
56
|
+
for (const prop of type.getProperties()) {
|
|
57
|
+
const declarations = prop.getDeclarations();
|
|
58
|
+
if (declarations.length > 1) {
|
|
59
|
+
throw new Error(`${name}: Function overloading is not supported! (In "${prop.getName()}")`);
|
|
60
|
+
}
|
|
61
|
+
let declaration = declarations[0];
|
|
62
|
+
if (declaration == null) {
|
|
63
|
+
throw new Error(`${name}: Property "${prop.getName()}" does not have a type declaration!`);
|
|
64
|
+
}
|
|
65
|
+
const parent = declaration.getParentOrThrow().getType();
|
|
66
|
+
if (parent === type) {
|
|
67
|
+
// it's an own property. declared literally here. fine.
|
|
68
|
+
}
|
|
69
|
+
else if (extendsHybridObject(parent, true) ||
|
|
70
|
+
isDirectlyHybridObject(parent)) {
|
|
71
|
+
// it's coming from a base class that is already a HybridObject. We can grab this via inheritance.
|
|
72
|
+
// don't generate this property natively.
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// it's coming from any TypeScript type that is not a HybridObject.
|
|
77
|
+
// Maybe just a literal interface, then we copy over the props.
|
|
78
|
+
}
|
|
79
|
+
if (Node.isPropertySignature(declaration)) {
|
|
80
|
+
const t = declaration.getType();
|
|
81
|
+
const propType = createType(language, t, prop.isOptional() || t.isNullable());
|
|
82
|
+
properties.push(new Property(prop.getName(), propType, declaration.isReadonly()));
|
|
83
|
+
}
|
|
84
|
+
else if (Node.isMethodSignature(declaration)) {
|
|
85
|
+
const returnType = declaration.getReturnType();
|
|
86
|
+
const methodReturnType = createType(language, returnType, returnType.isNullable());
|
|
87
|
+
const methodParameters = declaration
|
|
88
|
+
.getParameters()
|
|
89
|
+
.map((p) => new Parameter(p, language));
|
|
90
|
+
methods.push(new Method(prop.getName(), methodReturnType, methodParameters));
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error(`${name}: Property "${prop.getName()}" is neither a property, nor a method!`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const bases = getBaseTypes(type)
|
|
97
|
+
.filter((t) => isAnyHybridSubclass(t))
|
|
98
|
+
.map((t) => getHybridObjectSpec(t, language));
|
|
99
|
+
const spec = {
|
|
100
|
+
language: language,
|
|
101
|
+
name: name,
|
|
102
|
+
properties: properties,
|
|
103
|
+
methods: methods,
|
|
104
|
+
baseTypes: bases,
|
|
105
|
+
isHybridView: isHybridView(type),
|
|
106
|
+
config: config,
|
|
107
|
+
};
|
|
108
|
+
return spec;
|
|
109
|
+
}
|
|
110
|
+
function generateCppFiles(spec) {
|
|
111
|
+
const cppFiles = createCppHybridObject(spec);
|
|
112
|
+
return cppFiles;
|
|
113
|
+
}
|
|
114
|
+
function generateSwiftFiles(spec) {
|
|
115
|
+
// 1. Always generate a C++ spec for the shared layer and type declarations (enums, interfaces, ...)
|
|
116
|
+
const cppFiles = generateCppFiles(spec);
|
|
117
|
+
// 2. Generate Swift specific files and potentially a C++ binding layer
|
|
118
|
+
const swiftFiles = createSwiftHybridObject(spec);
|
|
119
|
+
return [...cppFiles, ...swiftFiles];
|
|
120
|
+
}
|
|
121
|
+
function generateKotlinFiles(spec) {
|
|
122
|
+
// 1. Always generate a C++ spec for the shared layer and type declarations (enums, interfaces, ...)
|
|
123
|
+
const cppFiles = generateCppFiles(spec);
|
|
124
|
+
// 2. Generate Kotlin specific files and potentially a C++ binding layer
|
|
125
|
+
const kotlinFiles = createKotlinHybridObject(spec);
|
|
126
|
+
return [...cppFiles, ...kotlinFiles];
|
|
127
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getFiles(directory: string): Promise<string[]>;
|
package/lib/getFiles.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Dirent, promises as fs } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
function getFilePath(file, rootDir) {
|
|
4
|
+
const dir = file.parentPath ?? file.path ?? rootDir;
|
|
5
|
+
return path.join(dir, file.name);
|
|
6
|
+
}
|
|
7
|
+
export async function getFiles(directory) {
|
|
8
|
+
try {
|
|
9
|
+
const files = await fs.readdir(directory, {
|
|
10
|
+
recursive: true,
|
|
11
|
+
withFileTypes: true,
|
|
12
|
+
});
|
|
13
|
+
return files.filter((f) => f.isFile()).map((f) => getFilePath(f, directory));
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (typeof error === 'object' &&
|
|
17
|
+
error != null &&
|
|
18
|
+
'code' in error &&
|
|
19
|
+
error.code === 'ENOENT') {
|
|
20
|
+
// directory does not exist
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
// different error
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PlatformSpec } from 'react-native-nitro-modules';
|
|
2
|
+
import type { InterfaceDeclaration, Type, TypeAliasDeclaration } from 'ts-morph';
|
|
3
|
+
export type Platform = keyof Required<PlatformSpec>;
|
|
4
|
+
export type Language = Required<PlatformSpec>[keyof PlatformSpec];
|
|
5
|
+
export declare function isDirectlyHybridObject(type: Type): boolean;
|
|
6
|
+
export declare function extendsHybridObject(type: Type, recursive: boolean): boolean;
|
|
7
|
+
export declare function isHybridViewProps(type: Type): boolean;
|
|
8
|
+
export declare function isHybridViewMethods(type: Type): boolean;
|
|
9
|
+
export declare function isHybridView(type: Type): boolean;
|
|
10
|
+
export declare function isAnyHybridSubclass(type: Type): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If the given interface ({@linkcode declaration}) extends `HybridObject`,
|
|
13
|
+
* this method returns the platforms it exists on.
|
|
14
|
+
* If it doesn't extend `HybridObject`, this returns `undefined`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getHybridObjectPlatforms(declaration: InterfaceDeclaration | TypeAliasDeclaration): PlatformSpec | undefined;
|
|
17
|
+
export declare function getHybridViewPlatforms(view: InterfaceDeclaration | TypeAliasDeclaration): PlatformSpec | undefined;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { Node, Symbol } from 'ts-morph';
|
|
2
|
+
import { getBaseTypes } from './utils.js';
|
|
3
|
+
const platformLanguages = {
|
|
4
|
+
ios: ['swift', 'c++'],
|
|
5
|
+
android: ['kotlin', 'c++'],
|
|
6
|
+
};
|
|
7
|
+
const allPlatforms = Object.keys(platformLanguages);
|
|
8
|
+
const allLanguages = Object.values(platformLanguages).flatMap((l) => l);
|
|
9
|
+
function isValidLanguage(language) {
|
|
10
|
+
if (language == null) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return allLanguages.includes(language);
|
|
14
|
+
}
|
|
15
|
+
function isValidPlatform(platform) {
|
|
16
|
+
return allPlatforms.includes(platform);
|
|
17
|
+
}
|
|
18
|
+
function getLiteralValue(symbol) {
|
|
19
|
+
const value = symbol.getValueDeclaration();
|
|
20
|
+
if (value == null) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const type = value.getType();
|
|
24
|
+
const literal = type.getLiteralValue();
|
|
25
|
+
if (typeof literal === 'string') {
|
|
26
|
+
return literal;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
// TODO: The type casting result here doesn't really work in TS.
|
|
31
|
+
function isValidLanguageForPlatform(language, platform) {
|
|
32
|
+
return platformLanguages[platform].includes(language);
|
|
33
|
+
}
|
|
34
|
+
function getPlatformSpec(typeName, platformSpecs) {
|
|
35
|
+
const result = {};
|
|
36
|
+
// Properties (ios, android)
|
|
37
|
+
const properties = platformSpecs.getProperties();
|
|
38
|
+
for (const property of properties) {
|
|
39
|
+
// Property name (ios, android)
|
|
40
|
+
const platform = property.getName();
|
|
41
|
+
if (!isValidPlatform(platform)) {
|
|
42
|
+
console.warn(` ⚠️ ${typeName} does not properly extend HybridObject<T> - "${platform}" is not a valid Platform! ` +
|
|
43
|
+
`Valid platforms are: [${allPlatforms.join(', ')}]`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// Value (swift, kotlin, c++)
|
|
47
|
+
const language = getLiteralValue(property);
|
|
48
|
+
if (!isValidLanguage(language)) {
|
|
49
|
+
console.warn(` ⚠️ ${typeName}: Language ${language} is not a valid language for ${platform}! ` +
|
|
50
|
+
`Valid languages are: [${platformLanguages[platform].join(', ')}]`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
// Double-check that language works on this platform (android: kotlin/c++, ios: swift/c++)
|
|
54
|
+
if (!isValidLanguageForPlatform(language, platform)) {
|
|
55
|
+
console.warn(` ⚠️ ${typeName}: Language ${language} is not a valid language for ${platform}! ` +
|
|
56
|
+
`Valid languages are: [${platformLanguages[platform].join(', ')}]`);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
// @ts-expect-error because TypeScript isn't smart enough yet to correctly cast after the `isValidLanguageForPlatform` check.
|
|
60
|
+
result[platform] = language;
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
function isDirectlyType(type, name) {
|
|
65
|
+
const symbol = type.getSymbol() ?? type.getAliasSymbol();
|
|
66
|
+
if (symbol?.getName() === name) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
function extendsType(type, name, recursive) {
|
|
72
|
+
for (const base of getBaseTypes(type)) {
|
|
73
|
+
const isHybrid = isDirectlyType(base, name);
|
|
74
|
+
if (isHybrid) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
if (recursive) {
|
|
78
|
+
const baseExtends = extendsType(base, name, recursive);
|
|
79
|
+
if (baseExtends) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
export function isDirectlyHybridObject(type) {
|
|
87
|
+
return isDirectlyType(type, 'HybridObject');
|
|
88
|
+
}
|
|
89
|
+
export function extendsHybridObject(type, recursive) {
|
|
90
|
+
return extendsType(type, 'HybridObject', recursive);
|
|
91
|
+
}
|
|
92
|
+
export function isHybridViewProps(type) {
|
|
93
|
+
return extendsType(type, 'HybridViewProps', true);
|
|
94
|
+
}
|
|
95
|
+
export function isHybridViewMethods(type) {
|
|
96
|
+
return extendsType(type, 'HybridViewMethods', true);
|
|
97
|
+
}
|
|
98
|
+
export function isHybridView(type) {
|
|
99
|
+
// HybridViews are type aliases for `HybridView`, and `Props & Methods` are just intersected together.
|
|
100
|
+
const unionTypes = type.getIntersectionTypes();
|
|
101
|
+
for (const union of unionTypes) {
|
|
102
|
+
const symbol = union.getSymbol();
|
|
103
|
+
if (symbol == null)
|
|
104
|
+
return false;
|
|
105
|
+
return symbol.getName() === 'HybridViewTag';
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
export function isAnyHybridSubclass(type) {
|
|
110
|
+
if (isDirectlyHybridObject(type))
|
|
111
|
+
return false;
|
|
112
|
+
if (isHybridView(type))
|
|
113
|
+
return true;
|
|
114
|
+
if (extendsHybridObject(type, true))
|
|
115
|
+
return true;
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* If the given interface ({@linkcode declaration}) extends `HybridObject`,
|
|
120
|
+
* this method returns the platforms it exists on.
|
|
121
|
+
* If it doesn't extend `HybridObject`, this returns `undefined`.
|
|
122
|
+
*/
|
|
123
|
+
export function getHybridObjectPlatforms(declaration) {
|
|
124
|
+
const base = getBaseTypes(declaration.getType()).find((t) => isDirectlyHybridObject(t));
|
|
125
|
+
if (base == null) {
|
|
126
|
+
// this type does not extend `HybridObject`.
|
|
127
|
+
throw new Error(`Couldn't find HybridObject<..> base for ${declaration.getName()}! (${declaration.getText()})`);
|
|
128
|
+
}
|
|
129
|
+
const genericArguments = base.getTypeArguments();
|
|
130
|
+
const platformSpecsArgument = genericArguments[0];
|
|
131
|
+
if (platformSpecsArgument == null) {
|
|
132
|
+
// it uses `HybridObject` without generic arguments. This defaults to C++
|
|
133
|
+
return { android: 'c++', ios: 'c++' };
|
|
134
|
+
}
|
|
135
|
+
return getPlatformSpec(declaration.getName(), platformSpecsArgument);
|
|
136
|
+
}
|
|
137
|
+
export function getHybridViewPlatforms(view) {
|
|
138
|
+
if (Node.isTypeAliasDeclaration(view)) {
|
|
139
|
+
const hybridViewTypeNode = view.getTypeNode();
|
|
140
|
+
const isHybridViewType = Node.isTypeReference(hybridViewTypeNode) &&
|
|
141
|
+
hybridViewTypeNode.getTypeName().getText() === 'HybridView';
|
|
142
|
+
if (!isHybridViewType) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const genericArguments = hybridViewTypeNode.getTypeArguments();
|
|
146
|
+
const platformSpecArg = genericArguments[2];
|
|
147
|
+
if (platformSpecArg != null) {
|
|
148
|
+
return getPlatformSpec(view.getName(), platformSpecArg.getType());
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// it uses `HybridObject` without generic arguments. This defaults to platform native languages
|
|
152
|
+
return { ios: 'swift', android: 'kotlin' };
|
|
153
|
+
}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
CHANGED
|
@@ -1,10 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import yargs from 'yargs/yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import { prettifyDirectory } from './prettifyDirectory.js';
|
|
6
|
+
import { getFiles } from './getFiles.js';
|
|
7
|
+
import { runNitrogen } from './nitrogen.js';
|
|
8
|
+
import { promises as fs } from 'fs';
|
|
9
|
+
import { isValidLogLevel, setLogLevel } from './Logger.js';
|
|
10
|
+
import { initNewNitroModule } from './init.js';
|
|
11
|
+
import { NITROGEN_VERSION } from './utils.js';
|
|
12
|
+
const commandName = 'nitrogen';
|
|
13
|
+
// Maximum of 100 col width
|
|
14
|
+
const cliWidth = Math.min(process.stdout.columns * 0.9, 100);
|
|
15
|
+
// Set up yargs CLI app
|
|
16
|
+
await yargs(hideBin(process.argv))
|
|
17
|
+
.option('log-level', {
|
|
18
|
+
type: 'string',
|
|
19
|
+
description: 'Configures the log-level of nitrogen.',
|
|
20
|
+
default: 'info',
|
|
21
|
+
choices: ['debug', 'info', 'warning', 'error'],
|
|
22
|
+
})
|
|
23
|
+
.middleware((args) => {
|
|
24
|
+
if (!isValidLogLevel(args.logLevel)) {
|
|
25
|
+
throw new Error(`Invalid log-level ("${args.logLevel}")!`);
|
|
26
|
+
}
|
|
27
|
+
setLogLevel(args.logLevel);
|
|
28
|
+
})
|
|
29
|
+
// 🔥 nitrogen [path]
|
|
30
|
+
.command('$0 [basePath]', `Usage: ${chalk.bold(`${commandName} <basePath> [options]`)}\n` +
|
|
31
|
+
`Run the nitro code-generator on all ${chalk.underline('**/*.nitro.ts')} files found ` +
|
|
32
|
+
`in the current directory and generate C++, Swift or Kotlin outputs in ${chalk.underline('./nitrogen/generated')}.`, (y) => y
|
|
33
|
+
.positional('basePath', {
|
|
34
|
+
type: 'string',
|
|
35
|
+
description: `The base path of where Nitrogen will start looking for ${chalk.underline('**/*.nitro.ts')} specs.`,
|
|
36
|
+
default: process.cwd(),
|
|
37
|
+
})
|
|
38
|
+
.option('out', {
|
|
39
|
+
type: 'string',
|
|
40
|
+
description: 'Configures the output path of the generated C++, Swift or Kotlin files.',
|
|
41
|
+
default: './nitrogen/generated',
|
|
42
|
+
})
|
|
43
|
+
.option('config', {
|
|
44
|
+
type: 'string',
|
|
45
|
+
description: `A custom path to a ${chalk.underline('nitro.json')} config file.`,
|
|
46
|
+
default: './nitro.json',
|
|
47
|
+
}), async (argv) => {
|
|
48
|
+
const basePath = argv.basePath;
|
|
49
|
+
const outputDirectory = argv.out;
|
|
50
|
+
await runNitrogenCommand(basePath, outputDirectory);
|
|
51
|
+
})
|
|
52
|
+
// 🔥 nitrogen init <moduleName>
|
|
53
|
+
.command('init <moduleName>', `Usage: ${chalk.bold(`${commandName} init <moduleName> [options]`)}\n` +
|
|
54
|
+
`Create a new Nitro Module.`, (y) => y
|
|
55
|
+
.positional('moduleName', {
|
|
56
|
+
type: 'string',
|
|
57
|
+
description: 'The name of the Nitro Module that will be created.',
|
|
58
|
+
demandOption: true,
|
|
59
|
+
})
|
|
60
|
+
.option('path', {
|
|
61
|
+
type: 'string',
|
|
62
|
+
description: `A custom path to create the new Nitro Module in - instead of the current working directory.`,
|
|
63
|
+
default: process.cwd(),
|
|
64
|
+
}), async (argv) => {
|
|
65
|
+
await initNewNitroModule(argv.path, argv.moduleName);
|
|
66
|
+
})
|
|
67
|
+
.usage(`Usage: ${chalk.bold('$0 [options]')}\n` +
|
|
68
|
+
`$0 is a code-generater for Nitro Modules (${chalk.underline('https://github.com/mrousavy/nitro')})\n` +
|
|
69
|
+
`It converts all TypeScript specs found in ${chalk.underline('**/*.nitro.ts')} to C++, Swift or Kotlin specs.\n` +
|
|
70
|
+
`Each library/module must have a ${chalk.underline('nitro.json')} configuration file in it's root directory.\n` +
|
|
71
|
+
`$Nitrogen Version: ${chalk.bold(NITROGEN_VERSION)}`)
|
|
72
|
+
.help()
|
|
73
|
+
.strict()
|
|
74
|
+
.wrap(cliWidth).argv;
|
|
75
|
+
async function runNitrogenCommand(baseDirectory, outputDirectory) {
|
|
76
|
+
// 1. Prepare output folders
|
|
77
|
+
const filesBefore = await getFiles(outputDirectory);
|
|
78
|
+
const start = performance.now();
|
|
79
|
+
// 2. Run Nitrogen
|
|
80
|
+
const { generatedFiles, generatedSpecsCount, targetSpecsCount } = await runNitrogen({
|
|
81
|
+
baseDirectory: baseDirectory,
|
|
82
|
+
outputDirectory: outputDirectory,
|
|
83
|
+
});
|
|
84
|
+
const end = performance.now();
|
|
85
|
+
const timeS = ((end - start) / 1000).toFixed(1);
|
|
86
|
+
console.log(`🎉 Generated ${generatedSpecsCount}/${targetSpecsCount} HybridObject${generatedSpecsCount === 1 ? '' : 's'} in ${timeS}s!`);
|
|
87
|
+
console.log(`💡 Your code is in ${chalk.underline(prettifyDirectory(outputDirectory))}`);
|
|
88
|
+
// 3. Delete all old dangling files
|
|
89
|
+
const addedFiles = generatedFiles.filter((f) => !filesBefore.includes(f));
|
|
90
|
+
const removedFiles = filesBefore.filter((f) => !generatedFiles.includes(f));
|
|
91
|
+
if (addedFiles.length > 0 || removedFiles.length > 0) {
|
|
92
|
+
let text = '';
|
|
93
|
+
const as = addedFiles.length > 1 ? 's' : '';
|
|
94
|
+
const rs = removedFiles.length > 1 ? 's' : '';
|
|
95
|
+
if (addedFiles.length > 0 && removedFiles.length === 0) {
|
|
96
|
+
text = `Added ${addedFiles.length} file${as}`;
|
|
97
|
+
}
|
|
98
|
+
else if (addedFiles.length === 0 && removedFiles.length > 0) {
|
|
99
|
+
text = `Removed ${removedFiles.length} file${rs}`;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
text = `Added ${addedFiles.length} file${as} and removed ${removedFiles.length} file${rs}`;
|
|
103
|
+
}
|
|
104
|
+
console.log(`‼️ ${text} - ${chalk.bold('you need to run `pod install`/sync gradle to update files!')}`);
|
|
105
|
+
}
|
|
106
|
+
const promises = removedFiles.map(async (file) => {
|
|
107
|
+
const stat = await fs.stat(file);
|
|
108
|
+
if (stat.isFile()) {
|
|
109
|
+
await fs.rm(file);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
await Promise.all(promises);
|
|
113
|
+
}
|
package/lib/init.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initNewNitroModule(baseDirectory: string, moduleName: string, ref?: string): Promise<void>;
|