@napi-rs/cli 3.0.0-alpha.81 → 3.0.0-alpha.82
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/build.js +70 -138
- package/dist/api/build.js.map +1 -1
- package/dist/index.cjs +226 -408
- package/dist/utils/misc.d.ts +2 -1
- package/dist/utils/misc.js +10 -8
- package/dist/utils/misc.js.map +1 -1
- package/dist/utils/typegen.d.ts +1 -1
- package/dist/utils/typegen.js +5 -22
- package/dist/utils/typegen.js.map +1 -1
- package/package.json +2 -2
- package/src/api/build.ts +90 -169
- package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +2 -16
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
- package/src/utils/misc.ts +10 -9
- package/src/utils/typegen.ts +10 -32
package/dist/utils/misc.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare const mkdirAsync: typeof mkdir.__promisify__;
|
|
|
7
7
|
export declare const statAsync: typeof stat.__promisify__;
|
|
8
8
|
export declare const readdirAsync: typeof readdir.__promisify__;
|
|
9
9
|
export declare function fileExists(path: string): Promise<boolean>;
|
|
10
|
+
export declare function dirExistsAsync(path: string): Promise<boolean>;
|
|
10
11
|
export declare function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K>;
|
|
11
12
|
export declare function updatePackageJson(path: string, partial: Record<string, any>): Promise<void>;
|
|
12
|
-
export declare const CLI_VERSION:
|
|
13
|
+
export declare const CLI_VERSION: string;
|
package/dist/utils/misc.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { readFile, writeFile, copyFile, mkdir, unlink, stat, readdir, } from 'node:fs';
|
|
2
|
-
import { createRequire } from 'node:module';
|
|
3
2
|
import { promisify } from 'node:util';
|
|
3
|
+
import pkgJson from '../../package.json' with { type: 'json' };
|
|
4
4
|
import { debug } from './log.js';
|
|
5
|
-
const require = createRequire(import.meta.url);
|
|
6
|
-
// NOTE:
|
|
7
|
-
// import pkgJson from '@napi-rs/cli/package.json' assert { type: 'json' }
|
|
8
|
-
// is experimental feature now, avoid using it.
|
|
9
|
-
// see: https://nodejs.org/api/esm.html#import-assertions
|
|
10
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
11
|
-
const pkgJson = require('@napi-rs/cli/package.json');
|
|
12
5
|
export const readFileAsync = promisify(readFile);
|
|
13
6
|
export const writeFileAsync = promisify(writeFile);
|
|
14
7
|
export const unlinkAsync = promisify(unlink);
|
|
@@ -22,6 +15,15 @@ export async function fileExists(path) {
|
|
|
22
15
|
.catch(() => false);
|
|
23
16
|
return exists;
|
|
24
17
|
}
|
|
18
|
+
export async function dirExistsAsync(path) {
|
|
19
|
+
try {
|
|
20
|
+
const stats = await statAsync(path);
|
|
21
|
+
return stats.isDirectory();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
25
27
|
export function pick(o, ...keys) {
|
|
26
28
|
return keys.reduce((acc, key) => {
|
|
27
29
|
acc[key] = o[key];
|
package/dist/utils/misc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../../src/utils/misc.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,KAAK,EACL,MAAM,EACN,IAAI,EACJ,OAAO,GACR,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../../src/utils/misc.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,KAAK,EACL,MAAM,EACN,IAAI,EACJ,OAAO,GACR,MAAM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AAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC9B,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;QACjB,OAAO,GAAG,CAAA;IACZ,CAAC,EAAE,EAAO,CAAC,CAAA;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAY,EACZ,OAA4B;IAE5B,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,IAAI,CAAC,CAAA;IACrC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,KAAK,CAAC,mBAAmB,IAAI,EAAE,CAAC,CAAA;QAChC,OAAM;IACR,CAAC;IACD,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACzB,MAAM,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,GAAG,EAAE,GAAG,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAA;AAC7E,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAA"}
|
package/dist/utils/typegen.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const DEFAULT_TYPE_DEF_HEADER = "/* auto-generated by NAPI-RS */\n/* eslint-disable */\n";
|
|
2
|
-
export declare function processTypeDef(intermediateTypeFile: string, constEnum: boolean
|
|
2
|
+
export declare function processTypeDef(intermediateTypeFile: string, constEnum: boolean): Promise<{
|
|
3
3
|
dts: string;
|
|
4
4
|
exports: string[];
|
|
5
5
|
}>;
|
package/dist/utils/typegen.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { sortBy
|
|
1
|
+
import { sortBy } from 'lodash-es';
|
|
2
2
|
import { readFileAsync } from './misc.js';
|
|
3
3
|
const TOP_LEVEL_NAMESPACE = '__TOP_LEVEL_MODULE__';
|
|
4
4
|
export const DEFAULT_TYPE_DEF_HEADER = `/* auto-generated by NAPI-RS */
|
|
@@ -56,11 +56,10 @@ function exportDeclare(ambient) {
|
|
|
56
56
|
}
|
|
57
57
|
return 'export declare';
|
|
58
58
|
}
|
|
59
|
-
export async function processTypeDef(intermediateTypeFile, constEnum
|
|
59
|
+
export async function processTypeDef(intermediateTypeFile, constEnum) {
|
|
60
60
|
const exports = [];
|
|
61
61
|
const defs = await readIntermediateTypeFile(intermediateTypeFile);
|
|
62
62
|
const groupedDefs = preprocessTypeDef(defs);
|
|
63
|
-
header = header ?? '';
|
|
64
63
|
const dts = sortBy(Array.from(groupedDefs), ([namespace]) => namespace)
|
|
65
64
|
.map(([namespace, defs]) => {
|
|
66
65
|
if (namespace === TOP_LEVEL_NAMESPACE) {
|
|
@@ -97,36 +96,20 @@ export async function processTypeDef(intermediateTypeFile, constEnum, header) {
|
|
|
97
96
|
}
|
|
98
97
|
})
|
|
99
98
|
.join('\n\n') + '\n';
|
|
100
|
-
if (dts.indexOf('ExternalObject<') > -1) {
|
|
101
|
-
header += `
|
|
102
|
-
export declare class ExternalObject<T> {
|
|
103
|
-
readonly '': {
|
|
104
|
-
readonly '': unique symbol
|
|
105
|
-
[K: symbol]: T
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
`;
|
|
109
|
-
}
|
|
110
99
|
return {
|
|
111
|
-
dts
|
|
100
|
+
dts,
|
|
112
101
|
exports,
|
|
113
102
|
};
|
|
114
103
|
}
|
|
115
104
|
async function readIntermediateTypeFile(file) {
|
|
116
105
|
const content = await readFileAsync(file, 'utf8');
|
|
117
|
-
const defs =
|
|
106
|
+
const defs = content
|
|
118
107
|
.split('\n')
|
|
119
108
|
.filter(Boolean)
|
|
120
109
|
.map((line) => {
|
|
121
110
|
line = line.trim();
|
|
122
|
-
if (!line.startsWith('{')) {
|
|
123
|
-
// crateName:{ "def": "", ... }
|
|
124
|
-
const start = line.indexOf(':') + 1;
|
|
125
|
-
line = line.slice(start);
|
|
126
|
-
}
|
|
127
111
|
return JSON.parse(line);
|
|
128
|
-
})
|
|
129
|
-
.filter((def) => !!def.kind), (a, b) => isEqual(a, b));
|
|
112
|
+
});
|
|
130
113
|
// move all `struct` def to the very top
|
|
131
114
|
// and order the rest alphabetically.
|
|
132
115
|
return defs.sort((a, b) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typegen.js","sourceRoot":"","sources":["../../src/utils/typegen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"typegen.js","sourceRoot":"","sources":["../../src/utils/typegen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAElC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAEzC,MAAM,mBAAmB,GAAG,sBAAsB,CAAA;AAClD,MAAM,CAAC,MAAM,uBAAuB,GAAG;;CAEtC,CAAA;AAED,IAAK,WASJ;AATD,WAAK,WAAW;IACd,8BAAe,CAAA;IACf,4BAAa,CAAA;IACb,yCAA0B,CAAA;IAC1B,sCAAuB,CAAA;IACvB,4BAAa,CAAA;IACb,wBAAS,CAAA;IACT,gCAAiB,CAAA;IACjB,4BAAa,CAAA;AACf,CAAC,EATI,WAAW,KAAX,WAAW,QASf;AAWD,SAAS,WAAW,CAClB,IAAiB,EACjB,SAAkB,EAClB,KAAa,EACb,OAAO,GAAG,KAAK;IAEf,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAA;IACzB,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,KAAK,WAAW,CAAC,SAAS;YACxB,CAAC,IAAI,oBAAoB,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,GAAG,KAAK,CAAA;YACtD,MAAK;QAEP,KAAK,WAAW,CAAC,IAAI;YACnB,CAAC,IAAI,eAAe,IAAI,CAAC,IAAI,QAAQ,IAAI,CAAC,GAAG,EAAE,CAAA;YAC/C,MAAK;QAEP,KAAK,WAAW,CAAC,IAAI;Y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AY,GAAG,CAAC,CAAA;QACjC,CAAC;QAED,IAAI,oBAAoB,EAAE,CAAC;YACzB,WAAW,IAAI,CAAC,CAAA;QAClB,CAAC;QAED,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,IAAI,EAAE,CAAA;QAE7C,OAAO,CAAC,CAAA;IACV,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAA;IAEb,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@napi-rs/cli",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.82",
|
|
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",
|
|
@@ -129,5 +129,5 @@
|
|
|
129
129
|
"**/__tests__/**/*.spec.ts"
|
|
130
130
|
]
|
|
131
131
|
},
|
|
132
|
-
"gitHead": "
|
|
132
|
+
"gitHead": "b3a477cac0284ff60a1c9f0e331993c02f8a8b8f"
|
|
133
133
|
}
|
package/src/api/build.ts
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process'
|
|
2
2
|
import { createHash } from 'node:crypto'
|
|
3
|
-
import {
|
|
4
|
-
existsSync,
|
|
5
|
-
mkdirSync,
|
|
6
|
-
unlinkSync,
|
|
7
|
-
readFileSync,
|
|
8
|
-
writeFileSync,
|
|
9
|
-
} from 'node:fs'
|
|
3
|
+
import { existsSync, mkdirSync, rmSync } from 'node:fs'
|
|
10
4
|
import { createRequire } from 'node:module'
|
|
11
|
-
import {
|
|
5
|
+
import { homedir } from 'node:os'
|
|
12
6
|
import { parse, join, resolve } from 'node:path'
|
|
13
7
|
|
|
14
8
|
import * as colors from 'colorette'
|
|
15
|
-
import { groupBy, split } from 'lodash-es'
|
|
16
9
|
import { include as setjmpInclude, lib as setjmpLib } from 'wasm-sjlj'
|
|
17
10
|
|
|
18
11
|
import { BuildOptions as RawBuildOptions } from '../def/build.js'
|
|
@@ -37,6 +30,8 @@ import {
|
|
|
37
30
|
tryInstallCargoBinary,
|
|
38
31
|
unlinkAsync,
|
|
39
32
|
writeFileAsync,
|
|
33
|
+
dirExistsAsync,
|
|
34
|
+
readdirAsync,
|
|
40
35
|
} from '../utils/index.js'
|
|
41
36
|
|
|
42
37
|
import { createCjsBinding, createEsmBinding } from './templates/index.js'
|
|
@@ -53,22 +48,14 @@ const debug = debugFactory('build')
|
|
|
53
48
|
const require = createRequire(import.meta.url)
|
|
54
49
|
|
|
55
50
|
type OutputKind = 'js' | 'dts' | 'node' | 'exe' | 'wasm'
|
|
56
|
-
type Output = {
|
|
57
|
-
kind: OutputKind
|
|
58
|
-
path: string
|
|
59
|
-
}
|
|
51
|
+
type Output = { kind: OutputKind; path: string }
|
|
60
52
|
|
|
61
|
-
type BuildOptions = RawBuildOptions & {
|
|
62
|
-
cargoOptions?: string[]
|
|
63
|
-
}
|
|
53
|
+
type BuildOptions = RawBuildOptions & { cargoOptions?: string[] }
|
|
64
54
|
|
|
65
55
|
export async function buildProject(options: BuildOptions) {
|
|
66
56
|
debug('napi build command receive options: %O', options)
|
|
67
57
|
|
|
68
|
-
options = {
|
|
69
|
-
dtsCache: true,
|
|
70
|
-
...options,
|
|
71
|
-
}
|
|
58
|
+
options = { dtsCache: true, ...options }
|
|
72
59
|
|
|
73
60
|
const cwd = options.cwd ?? process.cwd()
|
|
74
61
|
|
|
@@ -92,13 +79,6 @@ export async function buildProject(options: BuildOptions) {
|
|
|
92
79
|
)
|
|
93
80
|
}
|
|
94
81
|
|
|
95
|
-
const depsNapiPackages = pkg.dependencies
|
|
96
|
-
.map((p) => metadata.packages.find((c) => c.name === p.name))
|
|
97
|
-
.filter(
|
|
98
|
-
(c): c is Crate =>
|
|
99
|
-
c != null && c.dependencies.some((d) => d.name == 'napi-derive'),
|
|
100
|
-
)
|
|
101
|
-
|
|
102
82
|
const crateDir = parse(pkg.manifest_path).dir
|
|
103
83
|
|
|
104
84
|
const builder = new Builder(
|
|
@@ -121,7 +101,6 @@ export async function buildProject(options: BuildOptions) {
|
|
|
121
101
|
),
|
|
122
102
|
options.configPath ? resolvePath(options.configPath) : undefined,
|
|
123
103
|
),
|
|
124
|
-
depsNapiPackages,
|
|
125
104
|
)
|
|
126
105
|
|
|
127
106
|
return builder.build()
|
|
@@ -141,7 +120,6 @@ class Builder {
|
|
|
141
120
|
private readonly outputDir: string,
|
|
142
121
|
private readonly targetDir: string,
|
|
143
122
|
private readonly config: NapiConfig,
|
|
144
|
-
private readonly depsNapiPackages: Crate[],
|
|
145
123
|
) {}
|
|
146
124
|
|
|
147
125
|
get cdyLibName() {
|
|
@@ -323,10 +301,7 @@ class Builder {
|
|
|
323
301
|
const command =
|
|
324
302
|
process.env.CARGO ?? (this.options.useCross ? 'cross' : 'cargo')
|
|
325
303
|
const buildProcess = spawn(command, this.args, {
|
|
326
|
-
env: {
|
|
327
|
-
...process.env,
|
|
328
|
-
...this.envs,
|
|
329
|
-
},
|
|
304
|
+
env: { ...process.env, ...this.envs },
|
|
330
305
|
stdio: watch ? ['inherit', 'inherit', 'pipe'] : 'inherit',
|
|
331
306
|
cwd: this.cwd,
|
|
332
307
|
signal: controller.signal,
|
|
@@ -342,11 +317,7 @@ class Builder {
|
|
|
342
317
|
})
|
|
343
318
|
|
|
344
319
|
buildProcess.once('error', (e) => {
|
|
345
|
-
reject(
|
|
346
|
-
new Error(`Build failed with error: ${e.message}`, {
|
|
347
|
-
cause: e,
|
|
348
|
-
}),
|
|
349
|
-
)
|
|
320
|
+
reject(new Error(`Build failed with error: ${e.message}`, { cause: e }))
|
|
350
321
|
})
|
|
351
322
|
|
|
352
323
|
// watch mode only, they are piped through stderr
|
|
@@ -477,76 +448,8 @@ class Builder {
|
|
|
477
448
|
}
|
|
478
449
|
|
|
479
450
|
private setEnvs() {
|
|
480
|
-
// type definition
|
|
481
|
-
this.envs.
|
|
482
|
-
// Validate the packages and dependencies
|
|
483
|
-
if (existsSync(this.envs.TYPE_DEF_TMP_PATH)) {
|
|
484
|
-
const { depsNapiPackages, crate } = this
|
|
485
|
-
let shouldInvalidateSelf = false
|
|
486
|
-
const content = readFileSync(this.envs.TYPE_DEF_TMP_PATH, 'utf-8')
|
|
487
|
-
const typedefRaw = content
|
|
488
|
-
.split('\n')
|
|
489
|
-
.filter((line) => line.trim().length)
|
|
490
|
-
.map((line) => {
|
|
491
|
-
const [pkgName, ...jsonContents] = split(line, ':')
|
|
492
|
-
return {
|
|
493
|
-
pkgName,
|
|
494
|
-
jsonContent: jsonContents.join(':'),
|
|
495
|
-
}
|
|
496
|
-
})
|
|
497
|
-
const groupedTypedefRaw = groupBy(typedefRaw, ({ pkgName }) => pkgName)
|
|
498
|
-
const invalidPackages = new Set<string>()
|
|
499
|
-
for (const [pkgName, value] of Object.entries(groupedTypedefRaw)) {
|
|
500
|
-
const packageInvalidKey = `NAPI_PACKAGE_${pkgName.toUpperCase().replaceAll('-', '_')}_INVALID`
|
|
501
|
-
let done = false
|
|
502
|
-
for (const { jsonContent } of value) {
|
|
503
|
-
try {
|
|
504
|
-
// package_name: { "done": true } was written by napi-build in project build.rs
|
|
505
|
-
// if it exists, the package was successfully built and typedef was written
|
|
506
|
-
const json = JSON.parse(jsonContent)
|
|
507
|
-
if (json.done === true) {
|
|
508
|
-
done = true
|
|
509
|
-
break
|
|
510
|
-
}
|
|
511
|
-
} catch {
|
|
512
|
-
done = false
|
|
513
|
-
break
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
if (!done) {
|
|
517
|
-
process.env[packageInvalidKey] = `${Date.now()}`
|
|
518
|
-
shouldInvalidateSelf = true
|
|
519
|
-
invalidPackages.add(pkgName)
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
const typedefPackages = new Set(Object.keys(groupedTypedefRaw))
|
|
523
|
-
for (const crate of depsNapiPackages) {
|
|
524
|
-
if (!typedefPackages.has(crate.name)) {
|
|
525
|
-
debug('Set invalid package typedef: %i', crate.name)
|
|
526
|
-
shouldInvalidateSelf = true
|
|
527
|
-
process.env[
|
|
528
|
-
`NAPI_PACKAGE_${crate.name.toUpperCase().replaceAll('-', '_')}_INVALID`
|
|
529
|
-
] = `${Date.now()}`
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
if (shouldInvalidateSelf) {
|
|
533
|
-
debug('Set invalid package typedef: %i', crate.name)
|
|
534
|
-
process.env[
|
|
535
|
-
`NAPI_PACKAGE_${crate.name.toUpperCase().replaceAll('-', '_')}_INVALID`
|
|
536
|
-
] = `${Date.now()}`
|
|
537
|
-
}
|
|
538
|
-
// clean invalid packages typedef
|
|
539
|
-
if (invalidPackages.size) {
|
|
540
|
-
debug('Clean invalid packages typedef: %O', invalidPackages)
|
|
541
|
-
writeFileSync(
|
|
542
|
-
this.envs.TYPE_DEF_TMP_PATH,
|
|
543
|
-
typedefRaw
|
|
544
|
-
.filter(({ pkgName }) => !invalidPackages.has(pkgName))
|
|
545
|
-
.map(({ pkgName, jsonContent }) => `${pkgName}:${jsonContent}`)
|
|
546
|
-
.join('\n'),
|
|
547
|
-
)
|
|
548
|
-
}
|
|
549
|
-
}
|
|
451
|
+
// folder for intermediate type definition files
|
|
452
|
+
this.envs.TYPE_DEF_TMP_FOLDER = this.generateIntermediateTypeDefFolder()
|
|
550
453
|
|
|
551
454
|
// RUSTFLAGS
|
|
552
455
|
let rustflags =
|
|
@@ -725,22 +628,25 @@ class Builder {
|
|
|
725
628
|
return this
|
|
726
629
|
}
|
|
727
630
|
|
|
728
|
-
private
|
|
729
|
-
|
|
730
|
-
|
|
631
|
+
private generateIntermediateTypeDefFolder() {
|
|
632
|
+
let folder = join(
|
|
633
|
+
this.targetDir,
|
|
634
|
+
'napi-rs',
|
|
731
635
|
`${this.crate.name}-${createHash('sha256')
|
|
732
636
|
.update(this.crate.manifest_path)
|
|
733
637
|
.update(CLI_VERSION)
|
|
734
638
|
.digest('hex')
|
|
735
|
-
.substring(0, 8)}
|
|
639
|
+
.substring(0, 8)}`,
|
|
736
640
|
)
|
|
641
|
+
|
|
737
642
|
if (!this.options.dtsCache) {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
} catch {}
|
|
741
|
-
return `${dtsPath}_${Date.now()}.tmp`
|
|
643
|
+
rmSync(folder, { recursive: true, force: true })
|
|
644
|
+
folder += `_${Date.now()}`
|
|
742
645
|
}
|
|
743
|
-
|
|
646
|
+
|
|
647
|
+
mkdirAsync(folder, { recursive: true })
|
|
648
|
+
|
|
649
|
+
return folder
|
|
744
650
|
}
|
|
745
651
|
|
|
746
652
|
private async postBuild() {
|
|
@@ -838,9 +744,7 @@ class Builder {
|
|
|
838
744
|
})
|
|
839
745
|
return wasmBinaryName ? join(this.outputDir, wasmBinaryName) : null
|
|
840
746
|
} catch (e) {
|
|
841
|
-
throw new Error('Failed to copy artifact', {
|
|
842
|
-
cause: e,
|
|
843
|
-
})
|
|
747
|
+
throw new Error('Failed to copy artifact', { cause: e })
|
|
844
748
|
}
|
|
845
749
|
}
|
|
846
750
|
|
|
@@ -889,41 +793,76 @@ class Builder {
|
|
|
889
793
|
}
|
|
890
794
|
|
|
891
795
|
private async generateTypeDef() {
|
|
892
|
-
|
|
796
|
+
const typeDefDir = this.envs.TYPE_DEF_TMP_FOLDER
|
|
797
|
+
if (!(await dirExistsAsync(typeDefDir))) {
|
|
893
798
|
return []
|
|
894
799
|
}
|
|
895
800
|
|
|
896
801
|
const dest = join(this.outputDir, this.options.dts ?? 'index.d.ts')
|
|
897
802
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
803
|
+
let header = ''
|
|
804
|
+
let dts = ''
|
|
805
|
+
let exports: string[] = []
|
|
806
|
+
|
|
807
|
+
if (!this.options.noDtsHeader) {
|
|
808
|
+
if (this.options.dtsHeader) {
|
|
809
|
+
header = this.options.dtsHeader
|
|
810
|
+
} else if (this.config.dtsHeaderFile) {
|
|
811
|
+
try {
|
|
812
|
+
header = await readFileAsync(
|
|
813
|
+
join(this.cwd, this.config.dtsHeaderFile),
|
|
814
|
+
'utf-8',
|
|
815
|
+
)
|
|
816
|
+
} catch (e) {
|
|
817
|
+
debug.warn(
|
|
818
|
+
`Failed to read dts header file ${this.config.dtsHeaderFile}`,
|
|
819
|
+
e,
|
|
820
|
+
)
|
|
821
|
+
}
|
|
822
|
+
} else {
|
|
823
|
+
header = DEFAULT_TYPE_DEF_HEADER
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
const files = await readdirAsync(typeDefDir, { withFileTypes: true })
|
|
828
|
+
|
|
829
|
+
if (!files.length) {
|
|
830
|
+
debug('No type def files found. Skip generating dts file.')
|
|
831
|
+
return []
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
for (const file of files) {
|
|
835
|
+
if (!file.isFile()) {
|
|
836
|
+
continue
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const { dts: fileDts, exports: fileExports } = await processTypeDef(
|
|
840
|
+
join(typeDefDir, file.name),
|
|
841
|
+
this.options.constEnum ?? this.config.constEnum ?? true,
|
|
842
|
+
)
|
|
843
|
+
|
|
844
|
+
dts += fileDts
|
|
845
|
+
exports.push(...fileExports)
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
if (dts.indexOf('ExternalObject<') > -1) {
|
|
849
|
+
header += `
|
|
850
|
+
export declare class ExternalObject<T> {
|
|
851
|
+
readonly '': {
|
|
852
|
+
readonly '': unique symbol
|
|
853
|
+
[K: symbol]: T
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
`
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
dts = header + dts
|
|
918
860
|
|
|
919
861
|
try {
|
|
920
862
|
debug('Writing type def to:')
|
|
921
863
|
debug(' %i', dest)
|
|
922
864
|
await writeFileAsync(dest, dts, 'utf-8')
|
|
923
|
-
this.outputs.push({
|
|
924
|
-
kind: 'dts',
|
|
925
|
-
path: dest,
|
|
926
|
-
})
|
|
865
|
+
this.outputs.push({ kind: 'dts', path: dest })
|
|
927
866
|
} catch (e) {
|
|
928
867
|
debug.error('Failed to write type def file')
|
|
929
868
|
debug.error(e as Error)
|
|
@@ -956,10 +895,7 @@ class Builder {
|
|
|
956
895
|
debug('Writing js binding to:')
|
|
957
896
|
debug(' %i', dest)
|
|
958
897
|
await writeFileAsync(dest, binding, 'utf-8')
|
|
959
|
-
return {
|
|
960
|
-
kind: 'js',
|
|
961
|
-
path: dest,
|
|
962
|
-
} satisfies Output
|
|
898
|
+
return { kind: 'js', path: dest } satisfies Output
|
|
963
899
|
} catch (e) {
|
|
964
900
|
throw new Error('Failed to write js binding file', { cause: e })
|
|
965
901
|
}
|
|
@@ -1029,26 +965,11 @@ class Builder {
|
|
|
1029
965
|
`export * from '${this.config.packageName}-wasm32-wasi'\n`,
|
|
1030
966
|
)
|
|
1031
967
|
return [
|
|
1032
|
-
{
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
},
|
|
1036
|
-
{
|
|
1037
|
-
kind: 'js',
|
|
1038
|
-
path: browserBindingPath,
|
|
1039
|
-
},
|
|
1040
|
-
{
|
|
1041
|
-
kind: 'js',
|
|
1042
|
-
path: workerPath,
|
|
1043
|
-
},
|
|
1044
|
-
{
|
|
1045
|
-
kind: 'js',
|
|
1046
|
-
path: browserWorkerPath,
|
|
1047
|
-
},
|
|
1048
|
-
{
|
|
1049
|
-
kind: 'js',
|
|
1050
|
-
path: browserEntryPath,
|
|
1051
|
-
},
|
|
968
|
+
{ kind: 'js', path: bindingPath },
|
|
969
|
+
{ kind: 'js', path: browserBindingPath },
|
|
970
|
+
{ kind: 'js', path: workerPath },
|
|
971
|
+
{ kind: 'js', path: browserWorkerPath },
|
|
972
|
+
{ kind: 'js', path: browserEntryPath },
|
|
1052
973
|
] satisfies Output[]
|
|
1053
974
|
}
|
|
1054
975
|
return []
|
|
Binary file
|
|
Binary file
|
|
@@ -66,14 +66,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
66
66
|
|
|
67
67
|
> Snapshot 1
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
export declare class ExternalObject<T> {␊
|
|
71
|
-
readonly '': {␊
|
|
72
|
-
readonly '': unique symbol␊
|
|
73
|
-
[K: symbol]: T␊
|
|
74
|
-
}␊
|
|
75
|
-
}␊
|
|
76
|
-
/**␊
|
|
69
|
+
`/**␊
|
|
77
70
|
* \`constructor\` option for \`struct\` requires all fields to be public,␊
|
|
78
71
|
* otherwise tag impl fn as constructor␊
|
|
79
72
|
* #[napi(constructor)]␊
|
|
@@ -620,14 +613,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
620
613
|
|
|
621
614
|
> Snapshot 1
|
|
622
615
|
|
|
623
|
-
|
|
624
|
-
export declare class ExternalObject<T> {␊
|
|
625
|
-
readonly '': {␊
|
|
626
|
-
readonly '': unique symbol␊
|
|
627
|
-
[K: symbol]: T␊
|
|
628
|
-
}␊
|
|
629
|
-
}␊
|
|
630
|
-
/**␊
|
|
616
|
+
`/**␊
|
|
631
617
|
* \`constructor\` option for \`struct\` requires all fields to be public,␊
|
|
632
618
|
* otherwise tag impl fn as constructor␊
|
|
633
619
|
* #[napi(constructor)]␊
|
|
Binary file
|
|
Binary file
|
package/src/utils/misc.ts
CHANGED
|
@@ -7,19 +7,11 @@ import {
|
|
|
7
7
|
stat,
|
|
8
8
|
readdir,
|
|
9
9
|
} from 'node:fs'
|
|
10
|
-
import { createRequire } from 'node:module'
|
|
11
10
|
import { promisify } from 'node:util'
|
|
12
11
|
|
|
12
|
+
import pkgJson from '../../package.json' with { type: 'json' }
|
|
13
13
|
import { debug } from './log.js'
|
|
14
14
|
|
|
15
|
-
const require = createRequire(import.meta.url)
|
|
16
|
-
// NOTE:
|
|
17
|
-
// import pkgJson from '@napi-rs/cli/package.json' assert { type: 'json' }
|
|
18
|
-
// is experimental feature now, avoid using it.
|
|
19
|
-
// see: https://nodejs.org/api/esm.html#import-assertions
|
|
20
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
21
|
-
const pkgJson = require('@napi-rs/cli/package.json')
|
|
22
|
-
|
|
23
15
|
export const readFileAsync = promisify(readFile)
|
|
24
16
|
export const writeFileAsync = promisify(writeFile)
|
|
25
17
|
export const unlinkAsync = promisify(unlink)
|
|
@@ -35,6 +27,15 @@ export async function fileExists(path: string) {
|
|
|
35
27
|
return exists
|
|
36
28
|
}
|
|
37
29
|
|
|
30
|
+
export async function dirExistsAsync(path: string) {
|
|
31
|
+
try {
|
|
32
|
+
const stats = await statAsync(path)
|
|
33
|
+
return stats.isDirectory()
|
|
34
|
+
} catch {
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
38
39
|
export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> {
|
|
39
40
|
return keys.reduce((acc, key) => {
|
|
40
41
|
acc[key] = o[key]
|