@napi-rs/cli 3.0.4 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/templates/js-binding.d.ts +2 -2
- package/dist/cli.js +40 -29
- package/dist/index.cjs +43 -32
- package/dist/index.js +40 -29
- package/package.json +2 -3
- package/src/api/build.ts +1 -0
- package/src/api/templates/js-binding.ts +28 -9
- package/src/utils/__tests__/__snapshots__/target.spec.ts.md +2 -2
- package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +2 -2
- package/src/utils/target.ts +7 -10
- package/src/utils/typegen.ts +9 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@napi-rs/cli",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Cli tools for napi-rs",
|
|
5
5
|
"author": "LongYinan <lynweklm@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/napi-rs/napi-rs",
|
|
@@ -130,6 +130,5 @@
|
|
|
130
130
|
"**/__tests__/**/*.spec.ts",
|
|
131
131
|
"e2e/**/*.spec.ts"
|
|
132
132
|
]
|
|
133
|
-
}
|
|
134
|
-
"gitHead": "521b5774aa89f1a7c807a6ddba509ce43973f624"
|
|
133
|
+
}
|
|
135
134
|
}
|
package/src/api/build.ts
CHANGED
|
@@ -2,12 +2,13 @@ export function createCjsBinding(
|
|
|
2
2
|
localName: string,
|
|
3
3
|
pkgName: string,
|
|
4
4
|
idents: string[],
|
|
5
|
+
packageVersion?: string,
|
|
5
6
|
): string {
|
|
6
7
|
return `${bindingHeader}
|
|
7
8
|
const { createRequire } = require('node:module')
|
|
8
9
|
require = createRequire(__filename)
|
|
9
10
|
|
|
10
|
-
${createCommonBinding(localName, pkgName)}
|
|
11
|
+
${createCommonBinding(localName, pkgName, packageVersion)}
|
|
11
12
|
module.exports = nativeBinding
|
|
12
13
|
${idents
|
|
13
14
|
.map((ident) => `module.exports.${ident} = nativeBinding.${ident}`)
|
|
@@ -19,13 +20,14 @@ export function createEsmBinding(
|
|
|
19
20
|
localName: string,
|
|
20
21
|
pkgName: string,
|
|
21
22
|
idents: string[],
|
|
23
|
+
packageVersion?: string,
|
|
22
24
|
): string {
|
|
23
25
|
return `${bindingHeader}
|
|
24
26
|
import { createRequire } from 'node:module'
|
|
25
27
|
const require = createRequire(import.meta.url)
|
|
26
28
|
const __dirname = new URL('.', import.meta.url).pathname
|
|
27
29
|
|
|
28
|
-
${createCommonBinding(localName, pkgName)}
|
|
30
|
+
${createCommonBinding(localName, pkgName, packageVersion)}
|
|
29
31
|
const { ${idents.join(', ')} } = nativeBinding
|
|
30
32
|
${idents.map((ident) => `export { ${ident} }`).join('\n')}
|
|
31
33
|
`
|
|
@@ -37,20 +39,37 @@ const bindingHeader = `// prettier-ignore
|
|
|
37
39
|
/* auto-generated by NAPI-RS */
|
|
38
40
|
`
|
|
39
41
|
|
|
40
|
-
function createCommonBinding(
|
|
42
|
+
function createCommonBinding(
|
|
43
|
+
localName: string,
|
|
44
|
+
pkgName: string,
|
|
45
|
+
packageVersion?: string,
|
|
46
|
+
): string {
|
|
41
47
|
function requireTuple(tuple: string, identSize = 8) {
|
|
42
48
|
const identLow = ' '.repeat(identSize - 2)
|
|
43
49
|
const ident = ' '.repeat(identSize)
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
const versionCheck = packageVersion
|
|
51
|
+
? `
|
|
52
|
+
${identLow}try {
|
|
53
|
+
${ident}const binding = require('${pkgName}-${tuple}')
|
|
54
|
+
${ident}const bindingPackageVersion = require('${pkgName}-${tuple}/package.json').version
|
|
55
|
+
${ident}if (bindingPackageVersion !== '${packageVersion}' && (process.env.NAPI_RS_ENFORCE_VERSION_CHECK === '0' || !process.env.NAPI_RS_ENFORCE_VERSION_CHECK)) {
|
|
56
|
+
${ident} throw new Error(\`Native binding package version mismatch, expected ${packageVersion} but got \${bindingPackageVersion}. You can reinstall dependencies to fix this issue.\`)
|
|
57
|
+
${ident}}
|
|
58
|
+
${ident}return binding
|
|
46
59
|
${identLow}} catch (e) {
|
|
47
60
|
${ident}loadErrors.push(e)
|
|
48
|
-
${identLow}}
|
|
61
|
+
${identLow}}`
|
|
62
|
+
: `
|
|
49
63
|
${identLow}try {
|
|
50
64
|
${ident}return require('${pkgName}-${tuple}')
|
|
51
65
|
${identLow}} catch (e) {
|
|
52
66
|
${ident}loadErrors.push(e)
|
|
53
67
|
${identLow}}`
|
|
68
|
+
return `try {
|
|
69
|
+
${ident}return require('./${localName}.${tuple}.node')
|
|
70
|
+
${identLow}} catch (e) {
|
|
71
|
+
${ident}loadErrors.push(e)
|
|
72
|
+
${identLow}}${versionCheck}`
|
|
54
73
|
}
|
|
55
74
|
|
|
56
75
|
return `const { readFileSync } = require('node:fs')
|
|
@@ -186,11 +205,11 @@ function requireNative() {
|
|
|
186
205
|
}
|
|
187
206
|
} else if (process.platform === 'openharmony') {
|
|
188
207
|
if (process.arch === 'arm64') {
|
|
189
|
-
${requireTuple('
|
|
208
|
+
${requireTuple('openharmony-arm64')}
|
|
190
209
|
} else if (process.arch === 'x64') {
|
|
191
|
-
${requireTuple('
|
|
210
|
+
${requireTuple('openharmony-x64')}
|
|
192
211
|
} else if (process.arch === 'arm') {
|
|
193
|
-
${requireTuple('
|
|
212
|
+
${requireTuple('openharmony-arm')}
|
|
194
213
|
} else {
|
|
195
214
|
loadErrors.push(new Error(\`Unsupported architecture on OpenHarmony: \${process.arch}\`))
|
|
196
215
|
}
|
|
@@ -41,7 +41,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
41
41
|
abi: null,
|
|
42
42
|
arch: 'arm64',
|
|
43
43
|
platform: 'openharmony',
|
|
44
|
-
platformArchABI: '
|
|
44
|
+
platformArchABI: 'openharmony-arm64',
|
|
45
45
|
triple: 'aarch64-unknown-linux-ohos',
|
|
46
46
|
},
|
|
47
47
|
{
|
|
@@ -83,7 +83,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
83
83
|
abi: null,
|
|
84
84
|
arch: 'x64',
|
|
85
85
|
platform: 'openharmony',
|
|
86
|
-
platformArchABI: '
|
|
86
|
+
platformArchABI: 'openharmony-x64',
|
|
87
87
|
triple: 'x86_64-unknown-linux-ohos',
|
|
88
88
|
},
|
|
89
89
|
{
|
|
Binary file
|
|
@@ -197,8 +197,8 @@ Generated by [AVA](https://avajs.dev).
|
|
|
197
197
|
␊
|
|
198
198
|
export declare class Fib3 {␊
|
|
199
199
|
current: number␊
|
|
200
|
-
|
|
201
|
-
constructor(current: number,
|
|
200
|
+
nextNum: number␊
|
|
201
|
+
constructor(current: number, nextNum: number)␊
|
|
202
202
|
[Symbol.iterator](): Iterator<number, void, number>␊
|
|
203
203
|
}␊
|
|
204
204
|
␊
|
package/src/utils/target.ts
CHANGED
|
@@ -6,6 +6,8 @@ export const UNIVERSAL_TARGETS = {
|
|
|
6
6
|
'universal-apple-darwin': ['aarch64-apple-darwin', 'x86_64-apple-darwin'],
|
|
7
7
|
} as const
|
|
8
8
|
|
|
9
|
+
const SUB_SYSTEMS = new Set(['android', 'ohos'])
|
|
10
|
+
|
|
9
11
|
export const AVAILABLE_TARGETS = [
|
|
10
12
|
'aarch64-apple-darwin',
|
|
11
13
|
'aarch64-linux-android',
|
|
@@ -139,24 +141,19 @@ export function parseTriple(rawTriple: string): Target {
|
|
|
139
141
|
;[cpu, sys] = triples
|
|
140
142
|
} else {
|
|
141
143
|
// aarch64-unknown-linux-musl
|
|
142
|
-
// ^ cpu
|
|
144
|
+
// ^ cpu ^vendor ^ sys ^ abi
|
|
143
145
|
// aarch64-apple-darwin
|
|
144
146
|
// ^ cpu ^ sys (abi is None)
|
|
145
147
|
;[cpu, , sys, abi = null] = triples
|
|
146
148
|
}
|
|
147
149
|
|
|
150
|
+
if (abi && SUB_SYSTEMS.has(abi)) {
|
|
151
|
+
sys = abi
|
|
152
|
+
abi = null
|
|
153
|
+
}
|
|
148
154
|
const platform = SysToNodePlatform[sys] ?? (sys as Platform)
|
|
149
155
|
const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
|
|
150
156
|
|
|
151
|
-
if (rawTriple.includes('ohos')) {
|
|
152
|
-
return {
|
|
153
|
-
triple: rawTriple,
|
|
154
|
-
platformArchABI: `linux-${arch}-ohos`,
|
|
155
|
-
platform: 'openharmony',
|
|
156
|
-
arch,
|
|
157
|
-
abi: null,
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
157
|
return {
|
|
161
158
|
triple: rawTriple,
|
|
162
159
|
platformArchABI: abi ? `${platform}-${arch}-${abi}` : `${platform}-${arch}`,
|
package/src/utils/typegen.ts
CHANGED
|
@@ -15,6 +15,7 @@ enum TypeDefKind {
|
|
|
15
15
|
Type = 'type',
|
|
16
16
|
Fn = 'fn',
|
|
17
17
|
Struct = 'struct',
|
|
18
|
+
Extends = 'extends',
|
|
18
19
|
Impl = 'impl',
|
|
19
20
|
}
|
|
20
21
|
|
|
@@ -23,6 +24,7 @@ interface TypeDefLine {
|
|
|
23
24
|
name: string
|
|
24
25
|
original_name?: string
|
|
25
26
|
def: string
|
|
27
|
+
extends?: string
|
|
26
28
|
js_doc?: string
|
|
27
29
|
js_mod?: string
|
|
28
30
|
}
|
|
@@ -57,7 +59,8 @@ function prettyPrint(
|
|
|
57
59
|
break
|
|
58
60
|
|
|
59
61
|
case TypeDefKind.Struct:
|
|
60
|
-
|
|
62
|
+
const extendsDef = line.extends ? ` extends ${line.extends}` : ''
|
|
63
|
+
s += `${exportDeclare(ambient)} class ${line.name}${extendsDef} {\n${line.def}\n}`
|
|
61
64
|
if (line.original_name && line.original_name !== line.name) {
|
|
62
65
|
s += `\nexport type ${line.original_name} = ${line.name}`
|
|
63
66
|
}
|
|
@@ -185,6 +188,11 @@ function preprocessTypeDef(defs: TypeDefLine[]): Map<string, TypeDefLine[]> {
|
|
|
185
188
|
if (def.kind === TypeDefKind.Struct) {
|
|
186
189
|
group.push(def)
|
|
187
190
|
classDefs.set(def.name, def)
|
|
191
|
+
} else if (def.kind === TypeDefKind.Extends) {
|
|
192
|
+
const classDef = classDefs.get(def.name)
|
|
193
|
+
if (classDef) {
|
|
194
|
+
classDef.extends = def.def
|
|
195
|
+
}
|
|
188
196
|
} else if (def.kind === TypeDefKind.Impl) {
|
|
189
197
|
// merge `impl` into class definition
|
|
190
198
|
const classDef = classDefs.get(def.name)
|