@napi-rs/cli 3.0.3 → 3.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.
- package/dist/api/templates/js-binding.d.ts +2 -2
- package/dist/cli.js +43 -35
- package/dist/index.cjs +46 -38
- package/dist/index.js +43 -35
- package/package.json +3 -5
- package/src/api/build.ts +3 -4
- 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.0
|
|
3
|
+
"version": "3.1.0",
|
|
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",
|
|
@@ -79,8 +79,7 @@
|
|
|
79
79
|
"js-yaml": "^4.1.0",
|
|
80
80
|
"lodash-es": "^4.17.21",
|
|
81
81
|
"semver": "^7.7.1",
|
|
82
|
-
"typanion": "^3.14.0"
|
|
83
|
-
"wasm-sjlj": "^1.0.6"
|
|
82
|
+
"typanion": "^3.14.0"
|
|
84
83
|
},
|
|
85
84
|
"devDependencies": {
|
|
86
85
|
"@emnapi/core": "^1.4.0",
|
|
@@ -131,6 +130,5 @@
|
|
|
131
130
|
"**/__tests__/**/*.spec.ts",
|
|
132
131
|
"e2e/**/*.spec.ts"
|
|
133
132
|
]
|
|
134
|
-
}
|
|
135
|
-
"gitHead": "9f0dc581be12fb039cf4c337b6c5014ae54ae56c"
|
|
133
|
+
}
|
|
136
134
|
}
|
package/src/api/build.ts
CHANGED
|
@@ -6,7 +6,6 @@ import { homedir } from 'node:os'
|
|
|
6
6
|
import { parse, join, resolve } from 'node:path'
|
|
7
7
|
|
|
8
8
|
import * as colors from 'colorette'
|
|
9
|
-
import { include as setjmpInclude, lib as setjmpLib } from 'wasm-sjlj'
|
|
10
9
|
|
|
11
10
|
import { BuildOptions as RawBuildOptions } from '../def/build.js'
|
|
12
11
|
import {
|
|
@@ -591,7 +590,6 @@ class Builder {
|
|
|
591
590
|
'wasm32-wasi-threads',
|
|
592
591
|
)
|
|
593
592
|
this.envs.EMNAPI_LINK_DIR = emnapi
|
|
594
|
-
this.envs.SETJMP_LINK_DIR = setjmpLib
|
|
595
593
|
const { WASI_SDK_PATH } = process.env
|
|
596
594
|
|
|
597
595
|
if (WASI_SDK_PATH && existsSync(WASI_SDK_PATH)) {
|
|
@@ -627,11 +625,11 @@ class Builder {
|
|
|
627
625
|
)
|
|
628
626
|
this.setEnvIfNotExists(
|
|
629
627
|
'TARGET_CFLAGS',
|
|
630
|
-
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj
|
|
628
|
+
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj`,
|
|
631
629
|
)
|
|
632
630
|
this.setEnvIfNotExists(
|
|
633
631
|
'TARGET_CXXFLAGS',
|
|
634
|
-
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj
|
|
632
|
+
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj`,
|
|
635
633
|
)
|
|
636
634
|
this.setEnvIfNotExists(
|
|
637
635
|
`TARGET_LDFLAGS`,
|
|
@@ -1001,6 +999,7 @@ export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array
|
|
|
1001
999
|
this.config.binaryName,
|
|
1002
1000
|
this.config.packageName,
|
|
1003
1001
|
idents,
|
|
1002
|
+
this.config.packageJson.version,
|
|
1004
1003
|
)
|
|
1005
1004
|
|
|
1006
1005
|
try {
|
|
@@ -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}') {
|
|
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)
|