@napi-rs/cli 3.0.0-alpha.3 → 3.0.0-alpha.5

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.
Files changed (71) hide show
  1. package/dist/api/build.js +34 -14
  2. package/dist/api/templates/ci-template.js +10 -11
  3. package/dist/api/templates/ci.yml.js +8 -2
  4. package/dist/api/templates/js-binding.d.ts +1 -1
  5. package/dist/api/templates/js-binding.js +243 -109
  6. package/dist/api/templates/package.json.js +29 -27
  7. package/dist/cli.js +0 -0
  8. package/dist/commands/new.js +1 -4
  9. package/dist/def/build.d.ts +12 -0
  10. package/dist/def/build.js +8 -0
  11. package/dist/index.cjs +2734 -7836
  12. package/dist/utils/__tests__/typegen.spec.js +1 -1
  13. package/dist/utils/ci.js +9 -0
  14. package/dist/utils/config.d.ts +9 -1
  15. package/dist/utils/target.d.ts +2 -2
  16. package/dist/utils/target.js +4 -0
  17. package/dist/utils/typegen.d.ts +4 -1
  18. package/dist/utils/typegen.js +20 -1
  19. package/dist/utils/version.js +2 -2
  20. package/package.json +13 -13
  21. package/src/api/artifacts.ts +2 -2
  22. package/src/api/build.ts +45 -18
  23. package/src/api/pre-publish.ts +8 -5
  24. package/src/api/templates/ci-template.ts +10 -11
  25. package/src/api/templates/ci.yml.ts +8 -2
  26. package/src/api/templates/js-binding.ts +236 -104
  27. package/src/api/templates/package.json.ts +31 -26
  28. package/src/commands/new.ts +1 -5
  29. package/src/def/build.ts +19 -0
  30. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +7 -0
  31. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  32. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +7 -7
  33. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  34. package/src/utils/__tests__/typegen.spec.ts +1 -1
  35. package/src/utils/ci.ts +9 -0
  36. package/src/utils/config.ts +12 -1
  37. package/src/utils/target.ts +5 -0
  38. package/src/utils/typegen.ts +20 -1
  39. package/src/utils/version.ts +1 -1
  40. package/dist/def/commands/artifacts.d.ts +0 -15
  41. package/dist/def/commands/artifacts.js +0 -29
  42. package/dist/def/commands/build.d.ts +0 -53
  43. package/dist/def/commands/build.js +0 -105
  44. package/dist/def/commands/create-npm-dirs.d.ts +0 -15
  45. package/dist/def/commands/create-npm-dirs.js +0 -29
  46. package/dist/def/commands/new.d.ts +0 -27
  47. package/dist/def/commands/new.js +0 -53
  48. package/dist/def/commands/pre-publish.d.ts +0 -23
  49. package/dist/def/commands/pre-publish.js +0 -45
  50. package/dist/def/commands/rename.d.ts +0 -25
  51. package/dist/def/commands/rename.js +0 -49
  52. package/dist/def/commands/universalize.d.ts +0 -13
  53. package/dist/def/commands/universalize.js +0 -25
  54. package/dist/def/commands/version.d.ts +0 -13
  55. package/dist/def/commands/version.js +0 -25
  56. package/dist/def/options/artifacts.d.ts +0 -35
  57. package/dist/def/options/artifacts.js +0 -11
  58. package/dist/def/options/build.d.ts +0 -97
  59. package/dist/def/options/build.js +0 -3
  60. package/dist/def/options/create-npm-dirs.d.ts +0 -35
  61. package/dist/def/options/create-npm-dirs.js +0 -11
  62. package/dist/def/options/new.d.ts +0 -79
  63. package/dist/def/options/new.js +0 -15
  64. package/dist/def/options/pre-publish.d.ts +0 -65
  65. package/dist/def/options/pre-publish.js +0 -13
  66. package/dist/def/options/rename.d.ts +0 -75
  67. package/dist/def/options/rename.js +0 -11
  68. package/dist/def/options/universalize.d.ts +0 -28
  69. package/dist/def/options/universalize.js +0 -10
  70. package/dist/def/options/version.d.ts +0 -28
  71. package/dist/def/options/version.js +0 -10
package/dist/api/build.js CHANGED
@@ -4,7 +4,7 @@ import { tmpdir } from 'os';
4
4
  import { parse, join, resolve } from 'path';
5
5
  import * as colors from 'colorette';
6
6
  import { CLI_VERSION, copyFileAsync, debugFactory, DEFAULT_TYPE_DEF_HEADER, fileExists, getSystemDefaultTarget, getTargetLinker, mkdirAsync, parseMetadata, parseTriple, processTypeDef, readNapiConfig, targetToEnvVar, tryInstallCargoBinary, unlinkAsync, writeFileAsync, } from '../utils/index.js';
7
- import { createJsBinding } from './templates/index.js';
7
+ import { createCjsBinding } from './templates/index.js';
8
8
  const debug = debugFactory('build');
9
9
  export async function buildProject(options) {
10
10
  debug('napi build command receive options: %O', options);
@@ -89,13 +89,15 @@ class Builder {
89
89
  debug(`Start building crate: ${this.crate.name}`);
90
90
  debug(' %i', `cargo ${this.args.join(' ')}`);
91
91
  const controller = new AbortController();
92
+ const watch = this.options.watch;
92
93
  const buildTask = new Promise((resolve, reject) => {
93
- const buildProcess = spawn('cargo', this.args, {
94
+ const command = process.env.CARGO ?? (this.options.useCross ? 'cross' : 'cargo');
95
+ const buildProcess = spawn(command, this.args, {
94
96
  env: {
95
97
  ...process.env,
96
98
  ...this.envs,
97
99
  },
98
- stdio: 'inherit',
100
+ stdio: watch ? ['inherit', 'inherit', 'pipe'] : 'inherit',
99
101
  cwd: this.cwd,
100
102
  signal: controller.signal,
101
103
  });
@@ -113,6 +115,14 @@ class Builder {
113
115
  cause: e,
114
116
  }));
115
117
  });
118
+ // watch mode only, they are piped through stderr
119
+ buildProcess.stderr?.on('data', (data) => {
120
+ const output = data.toString();
121
+ console.error(output);
122
+ if (/Finished\s(dev|release)/.test(output)) {
123
+ this.postBuild().catch(() => { });
124
+ }
125
+ });
116
126
  });
117
127
  return {
118
128
  task: buildTask.then(() => this.postBuild()),
@@ -132,7 +142,7 @@ class Builder {
132
142
  // ===>
133
143
  // cargo watch [...] -- build --target x86_64-unknown-linux-gnu
134
144
  // cargo watch [...] -- zigbuild --target x86_64-unknown-linux-gnu
135
- this.args.push('watch', '--why', '-i', '*.{js,ts,node}', '-w', this.crateDir, '--', 'cargo');
145
+ this.args.push('watch', '--why', '-i', '*.{js,ts,node}', '-w', this.crateDir, '--', 'cargo', 'build');
136
146
  set = true;
137
147
  }
138
148
  }
@@ -287,6 +297,9 @@ class Builder {
287
297
  if (this.options.targetDir) {
288
298
  this.args.push('--target-dir', this.options.targetDir);
289
299
  }
300
+ if (this.options.profile) {
301
+ this.args.push('--profile', this.options.profile);
302
+ }
290
303
  if (this.options.cargoOptions?.length) {
291
304
  this.args.push(...this.options.cargoOptions);
292
305
  }
@@ -321,8 +334,8 @@ class Builder {
321
334
  await this.copyArtifact();
322
335
  // only for cdylib
323
336
  if (this.cdyLibName) {
324
- await this.generateTypeDef();
325
- await this.writeJsBinding();
337
+ const idents = await this.generateTypeDef();
338
+ await this.writeJsBinding(idents);
326
339
  }
327
340
  return this.outputs;
328
341
  }
@@ -331,7 +344,9 @@ class Builder {
331
344
  if (!srcName || !destName) {
332
345
  return;
333
346
  }
334
- const src = join(this.targetDir, this.target.triple, this.options.release ? 'release' : 'debug', srcName);
347
+ const profile = this.options.profile ?? (this.options.release ? 'release' : 'debug');
348
+ const src = join(this.targetDir, this.target.triple, profile, srcName);
349
+ debug(`Copy artifact from: [${src}]`);
335
350
  const dest = join(this.outputDir, destName);
336
351
  try {
337
352
  if (await fileExists(dest)) {
@@ -378,10 +393,10 @@ class Builder {
378
393
  }
379
394
  async generateTypeDef() {
380
395
  if (!(await fileExists(this.envs.TYPE_DEF_TMP_PATH))) {
381
- return;
396
+ return [];
382
397
  }
383
398
  const dest = join(this.outputDir, this.options.dts ?? 'index.d.ts');
384
- const dts = await processTypeDef(this.envs.TYPE_DEF_TMP_PATH, !this.options.noDtsHeader
399
+ const { dts, exports } = await processTypeDef(this.envs.TYPE_DEF_TMP_PATH, !this.options.noDtsHeader
385
400
  ? this.options.dtsHeader ?? DEFAULT_TYPE_DEF_HEADER
386
401
  : '');
387
402
  try {
@@ -397,17 +412,22 @@ class Builder {
397
412
  debug.error('Failed to write type def file');
398
413
  debug.error(e);
399
414
  }
415
+ return exports;
400
416
  }
401
- async writeJsBinding() {
402
- if (!this.options.platform || this.options.noJsBinding) {
417
+ async writeJsBinding(idents) {
418
+ if (!this.options.platform ||
419
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
420
+ this.options.noJsBinding ||
421
+ idents.length === 0) {
403
422
  return;
404
423
  }
405
- const dest = join(this.outputDir, this.options.jsBinding ?? 'index.js');
406
- const js = createJsBinding(this.config.binaryName, this.config.packageName);
424
+ const name = parse(this.options.jsBinding ?? 'index.js').name;
425
+ const cjs = createCjsBinding(this.config.binaryName, this.config.packageName, idents);
407
426
  try {
427
+ const dest = join(this.outputDir, `${name}.js`);
408
428
  debug('Writing js binding to:');
409
429
  debug(' %i', dest);
410
- await writeFileAsync(dest, js, 'utf-8');
430
+ await writeFileAsync(dest, cjs, 'utf-8');
411
431
  this.outputs.push({
412
432
  kind: 'js',
413
433
  path: dest,
@@ -94,6 +94,14 @@ jobs:
94
94
  - host: windows-latest
95
95
  target: 'aarch64-pc-windows-msvc'
96
96
  build: yarn build --platform --target aarch64-pc-windows-msvc
97
+ - host: ubuntu-latest
98
+ target: 'riscv64gc-unknown-linux-gnu'
99
+ setup: |
100
+ sudo apt-get update
101
+ sudo apt-get install gcc-riscv64-linux-gnu -y
102
+ build: |
103
+ yarn build --platform --target riscv64gc-unknown-linux-gnu
104
+ riscv64-linux-gnu-strip *.node
97
105
 
98
106
  name: stable - \${{ matrix.settings.target }} - node@18
99
107
  runs-on: \${{ matrix.settings.host }}
@@ -106,7 +114,6 @@ jobs:
106
114
  if: \${{ !matrix.settings.docker }}
107
115
  with:
108
116
  node-version: 18
109
- check-latest: true
110
117
  cache: yarn
111
118
 
112
119
  - name: Install
@@ -150,7 +157,6 @@ jobs:
150
157
  if: matrix.settings.target == 'i686-pc-windows-msvc'
151
158
  with:
152
159
  node-version: 18
153
- check-latest: true
154
160
  cache: yarn
155
161
  architecture: x86
156
162
 
@@ -192,9 +198,7 @@ jobs:
192
198
  usesh: true
193
199
  mem: 3000
194
200
  prepare: |
195
- pkg install -y -f curl node libnghttp2
196
- curl -qL https://www.npmjs.com/install.sh | sh
197
- npm install --location=global --ignore-scripts yarn
201
+ pkg install -y -f curl node libnghttp2 npm yarn
198
202
  curl https://sh.rustup.rs -sSf --output rustup.sh
199
203
  sh rustup.sh -y --profile minimal --default-toolchain beta
200
204
  export PATH="/usr/local/cargo/bin:$PATH"
@@ -247,7 +251,6 @@ jobs:
247
251
  uses: actions/setup-node@v3
248
252
  with:
249
253
  node-version: \${{ matrix.node }}
250
- check-latest: true
251
254
  cache: 'yarn'
252
255
 
253
256
  - name: 'Install dependencies'
@@ -283,7 +286,6 @@ jobs:
283
286
  uses: actions/setup-node@v3
284
287
  with:
285
288
  node-version: \${{ matrix.node }}
286
- check-latest: true
287
289
  cache: 'yarn'
288
290
 
289
291
  - name: 'Install dependencies'
@@ -319,7 +321,6 @@ jobs:
319
321
  uses: actions/setup-node@v3
320
322
  with:
321
323
  node-version: \${{ matrix.node }}
322
- check-latest: true
323
324
  cache: 'yarn'
324
325
 
325
326
  - name: 'Install dependencies'
@@ -368,7 +369,7 @@ jobs:
368
369
  yarn config set supportedArchitectures.cpu "arm64"
369
370
  yarn config set supportedArchitectures.libc "glibc"
370
371
  yarn install
371
-
372
+
372
373
  - name: Set up QEMU
373
374
  uses: docker/setup-qemu-action@v2
374
375
  with:
@@ -483,7 +484,6 @@ jobs:
483
484
  uses: actions/setup-node@v3
484
485
  with:
485
486
  node-version: 18
486
- check-latest: true
487
487
  cache: yarn
488
488
 
489
489
  - name: 'Install dependencies'
@@ -529,7 +529,6 @@ jobs:
529
529
  uses: actions/setup-node@v3
530
530
  with:
531
531
  node-version: 18
532
- check-latest: true
533
532
  cache: 'yarn'
534
533
 
535
534
  - name: 'Install dependencies'
@@ -44,9 +44,15 @@ export const createGithubActionsCIYml = (binaryName, targets) => {
44
44
  delete fullTemplate.jobs[TEST_MACOS_WINDOWS];
45
45
  }
46
46
  else {
47
- const filterTarget = enableWindowsX86 ? 'macos-latest' : 'windows-latest';
47
+ const filterTargets = new Set();
48
+ if (enableWindowsX86) {
49
+ filterTargets.add('windows-latest');
50
+ }
51
+ if (enableMacOSUni || enableMacOSX86) {
52
+ filterTargets.add('macos-latest');
53
+ }
48
54
  fullTemplate.jobs[TEST_MACOS_WINDOWS].strategy.matrix.settings =
49
- fullTemplate.jobs[TEST_MACOS_WINDOWS].strategy.matrix.settings.filter(({ host }) => host !== filterTarget);
55
+ fullTemplate.jobs[TEST_MACOS_WINDOWS].strategy.matrix.settings.filter(({ host }) => filterTargets.has(host));
50
56
  requiredSteps.push(TEST_MACOS_WINDOWS);
51
57
  }
52
58
  if (!enableLinuxX86Gnu) {
@@ -1 +1 @@
1
- export declare function createJsBinding(localName: string, pkgName: string): string;
1
+ export declare function createCjsBinding(localName: string, pkgName: string, idents: string[]): string;
@@ -1,124 +1,258 @@
1
- /* eslint-disable @typescript-eslint/switch-exhaustiveness-check */
2
- function loadNapiModule(binaryName, packageName) {
3
- const { existsSync, readFileSync } = require('fs');
4
- const { join } = require('path');
5
- const { platform, arch } = process;
6
- const candidates = [];
7
- function isMusl() {
8
- // For Node 10
9
- if (!process.report || typeof process.report.getReport !== 'function') {
10
- try {
11
- const lddPath = require('child_process')
12
- .execSync('which ldd')
13
- .toString()
14
- .trim();
15
- return readFileSync(lddPath, 'utf8').includes('musl');
16
- }
17
- catch (e) {
18
- return true;
19
- }
1
+ export function createCjsBinding(localName, pkgName, idents) {
2
+ return `// prettier-ignore
3
+ /* eslint-disable */
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const { existsSync, readFileSync } = require('fs')
7
+ const { join } = require('path')
8
+
9
+ const { platform, arch } = process
10
+
11
+ let nativeBinding = null
12
+ let localFileExisted = false
13
+ let loadError = null
14
+
15
+ function isMusl() {
16
+ // For Node 10
17
+ if (!process.report || typeof process.report.getReport !== 'function') {
18
+ try {
19
+ const lddPath = require('child_process').execSync('which ldd').toString().trim()
20
+ return readFileSync(lddPath, 'utf8').includes('musl')
21
+ } catch (e) {
22
+ return true
23
+ }
24
+ } else {
25
+ const { glibcVersionRuntime } = process.report.getReport().header
26
+ return !glibcVersionRuntime
27
+ }
28
+ }
29
+
30
+ switch (platform) {
31
+ case 'android':
32
+ switch (arch) {
33
+ case 'arm64':
34
+ localFileExisted = existsSync(join(__dirname, '${localName}.android-arm64.node'))
35
+ try {
36
+ if (localFileExisted) {
37
+ nativeBinding = require('./${localName}.android-arm64.node')
38
+ } else {
39
+ nativeBinding = require('${pkgName}-android-arm64')
40
+ }
41
+ } catch (e) {
42
+ loadError = e
20
43
  }
21
- else {
22
- // @ts-expect-error
23
- const { glibcVersionRuntime } = process.report.getReport().header;
24
- return !glibcVersionRuntime;
44
+ break
45
+ case 'arm':
46
+ localFileExisted = existsSync(join(__dirname, '${localName}.android-arm-eabi.node'))
47
+ try {
48
+ if (localFileExisted) {
49
+ nativeBinding = require('./${localName}.android-arm-eabi.node')
50
+ } else {
51
+ nativeBinding = require('${pkgName}-android-arm-eabi')
52
+ }
53
+ } catch (e) {
54
+ loadError = e
25
55
  }
56
+ break
57
+ default:
58
+ throw new Error(\`Unsupported architecture on Android \${arch}\`)
26
59
  }
27
- switch (platform) {
28
- case 'android':
29
- switch (arch) {
30
- case 'arm64':
31
- candidates.push('android-arm64');
32
- break;
33
- case 'arm':
34
- candidates.push('android-arm-eabi');
35
- break;
36
- }
37
- break;
38
- case 'win32':
39
- switch (arch) {
40
- case 'x64':
41
- candidates.push('win32-x64-msvc');
42
- break;
43
- case 'ia32':
44
- candidates.push('win32-ia32-msvc');
45
- break;
46
- case 'arm64':
47
- candidates.push('win32-arm64-msvc');
48
- break;
49
- }
50
- break;
51
- case 'darwin':
52
- candidates.push('darwin-universal');
53
- switch (arch) {
54
- case 'x64':
55
- candidates.push('darwin-x64');
56
- break;
57
- case 'arm64':
58
- candidates.push('darwin-arm64');
59
- break;
60
- }
61
- break;
62
- case 'freebsd':
63
- if (arch === 'x64') {
64
- candidates.push('freebsd-x64');
65
- }
66
- break;
67
- case 'linux':
68
- switch (arch) {
69
- case 'x64':
70
- if (isMusl()) {
71
- candidates.push('linux-x64-musl');
72
- }
73
- else {
74
- candidates.push('linux-x64-gnu');
75
- }
76
- break;
77
- case 'arm64':
78
- if (isMusl()) {
79
- candidates.push('linux-arm64-musl');
80
- }
81
- else {
82
- candidates.push('linux-arm64-gnu');
83
- }
84
- break;
85
- case 'arm':
86
- candidates.push('linux-arm-gnueabihf');
87
- break;
88
- }
89
- break;
60
+ break
61
+ case 'win32':
62
+ switch (arch) {
63
+ case 'x64':
64
+ localFileExisted = existsSync(
65
+ join(__dirname, '${localName}.win32-x64-msvc.node')
66
+ )
67
+ try {
68
+ if (localFileExisted) {
69
+ nativeBinding = require('./${localName}.win32-x64-msvc.node')
70
+ } else {
71
+ nativeBinding = require('${pkgName}-win32-x64-msvc')
72
+ }
73
+ } catch (e) {
74
+ loadError = e
75
+ }
76
+ break
77
+ case 'ia32':
78
+ localFileExisted = existsSync(
79
+ join(__dirname, '${localName}.win32-ia32-msvc.node')
80
+ )
81
+ try {
82
+ if (localFileExisted) {
83
+ nativeBinding = require('./${localName}.win32-ia32-msvc.node')
84
+ } else {
85
+ nativeBinding = require('${pkgName}-win32-ia32-msvc')
86
+ }
87
+ } catch (e) {
88
+ loadError = e
89
+ }
90
+ break
91
+ case 'arm64':
92
+ localFileExisted = existsSync(
93
+ join(__dirname, '${localName}.win32-arm64-msvc.node')
94
+ )
95
+ try {
96
+ if (localFileExisted) {
97
+ nativeBinding = require('./${localName}.win32-arm64-msvc.node')
98
+ } else {
99
+ nativeBinding = require('${pkgName}-win32-arm64-msvc')
100
+ }
101
+ } catch (e) {
102
+ loadError = e
103
+ }
104
+ break
105
+ default:
106
+ throw new Error(\`Unsupported architecture on Windows: \${arch}\`)
90
107
  }
91
- let nativeBinding;
92
- let loadError;
93
- for (const suffix of candidates) {
94
- const localPath = join(__dirname, `${binaryName}.${suffix}.node`);
95
- const pkgPath = `${packageName}-${suffix}`;
108
+ break
109
+ case 'darwin':
110
+ localFileExisted = existsSync(join(__dirname, '${localName}.darwin-universal.node'))
111
+ try {
112
+ if (localFileExisted) {
113
+ nativeBinding = require('./${localName}.darwin-universal.node')
114
+ } else {
115
+ nativeBinding = require('${pkgName}-darwin-universal')
116
+ }
117
+ break
118
+ } catch {}
119
+ switch (arch) {
120
+ case 'x64':
121
+ localFileExisted = existsSync(join(__dirname, '${localName}.darwin-x64.node'))
96
122
  try {
97
- if (existsSync(localPath)) {
98
- nativeBinding = require(localPath);
123
+ if (localFileExisted) {
124
+ nativeBinding = require('./${localName}.darwin-x64.node')
125
+ } else {
126
+ nativeBinding = require('${pkgName}-darwin-x64')
127
+ }
128
+ } catch (e) {
129
+ loadError = e
130
+ }
131
+ break
132
+ case 'arm64':
133
+ localFileExisted = existsSync(
134
+ join(__dirname, '${localName}.darwin-arm64.node')
135
+ )
136
+ try {
137
+ if (localFileExisted) {
138
+ nativeBinding = require('./${localName}.darwin-arm64.node')
139
+ } else {
140
+ nativeBinding = require('${pkgName}-darwin-arm64')
141
+ }
142
+ } catch (e) {
143
+ loadError = e
144
+ }
145
+ break
146
+ default:
147
+ throw new Error(\`Unsupported architecture on macOS: \${arch}\`)
148
+ }
149
+ break
150
+ case 'freebsd':
151
+ if (arch !== 'x64') {
152
+ throw new Error(\`Unsupported architecture on FreeBSD: \${arch}\`)
153
+ }
154
+ localFileExisted = existsSync(join(__dirname, '${localName}.freebsd-x64.node'))
155
+ try {
156
+ if (localFileExisted) {
157
+ nativeBinding = require('./${localName}.freebsd-x64.node')
158
+ } else {
159
+ nativeBinding = require('${pkgName}-freebsd-x64')
160
+ }
161
+ } catch (e) {
162
+ loadError = e
163
+ }
164
+ break
165
+ case 'linux':
166
+ switch (arch) {
167
+ case 'x64':
168
+ if (isMusl()) {
169
+ localFileExisted = existsSync(
170
+ join(__dirname, '${localName}.linux-x64-musl.node')
171
+ )
172
+ try {
173
+ if (localFileExisted) {
174
+ nativeBinding = require('./${localName}.linux-x64-musl.node')
175
+ } else {
176
+ nativeBinding = require('${pkgName}-linux-x64-musl')
99
177
  }
100
- else {
101
- nativeBinding = require(pkgPath);
178
+ } catch (e) {
179
+ loadError = e
180
+ }
181
+ } else {
182
+ localFileExisted = existsSync(
183
+ join(__dirname, '${localName}.linux-x64-gnu.node')
184
+ )
185
+ try {
186
+ if (localFileExisted) {
187
+ nativeBinding = require('./${localName}.linux-x64-gnu.node')
188
+ } else {
189
+ nativeBinding = require('${pkgName}-linux-x64-gnu')
102
190
  }
191
+ } catch (e) {
192
+ loadError = e
193
+ }
103
194
  }
104
- catch (e) {
105
- loadError = e;
106
- continue;
195
+ break
196
+ case 'arm64':
197
+ if (isMusl()) {
198
+ localFileExisted = existsSync(
199
+ join(__dirname, '${localName}.linux-arm64-musl.node')
200
+ )
201
+ try {
202
+ if (localFileExisted) {
203
+ nativeBinding = require('./${localName}.linux-arm64-musl.node')
204
+ } else {
205
+ nativeBinding = require('${pkgName}-linux-arm64-musl')
206
+ }
207
+ } catch (e) {
208
+ loadError = e
209
+ }
210
+ } else {
211
+ localFileExisted = existsSync(
212
+ join(__dirname, '${localName}.linux-arm64-gnu.node')
213
+ )
214
+ try {
215
+ if (localFileExisted) {
216
+ nativeBinding = require('./${localName}.linux-arm64-gnu.node')
217
+ } else {
218
+ nativeBinding = require('${pkgName}-linux-arm64-gnu')
219
+ }
220
+ } catch (e) {
221
+ loadError = e
222
+ }
107
223
  }
108
- loadError = null;
109
- break;
110
- }
111
- if (!nativeBinding) {
112
- if (loadError) {
113
- throw loadError;
224
+ break
225
+ case 'arm':
226
+ localFileExisted = existsSync(
227
+ join(__dirname, '${localName}.linux-arm-gnueabihf.node')
228
+ )
229
+ try {
230
+ if (localFileExisted) {
231
+ nativeBinding = require('./${localName}.linux-arm-gnueabihf.node')
232
+ } else {
233
+ nativeBinding = require('${pkgName}-linux-arm-gnueabihf')
234
+ }
235
+ } catch (e) {
236
+ loadError = e
114
237
  }
115
- throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
238
+ break
239
+ default:
240
+ throw new Error(\`Unsupported architecture on Linux: \${arch}\`)
116
241
  }
117
- return nativeBinding;
242
+ break
243
+ default:
244
+ throw new Error(\`Unsupported OS: \${platform}, architecture: \${arch}\`)
245
+ }
246
+
247
+ if (!nativeBinding) {
248
+ if (loadError) {
249
+ throw loadError
250
+ }
251
+ throw new Error(\`Failed to load native binding\`)
118
252
  }
119
- export function createJsBinding(localName, pkgName) {
120
- return `${loadNapiModule.toString()}
121
253
 
122
- module.exports = loadNapiModule('${localName}', '${pkgName}')
254
+ ${idents
255
+ .map((ident) => `module.exports.${ident} = binding.${ident}`)
256
+ .join('\n')}
123
257
  `;
124
258
  }
@@ -1,29 +1,31 @@
1
1
  export const createPackageJson = ({ name, binaryName, targets, license, engineRequirement, cliVersion, }) => {
2
- return `{
3
- "name": "${name}",
4
- "version": "1.0.0",
5
- "main": "index.js",
6
- "types": "index.d.ts",
7
- "license": "${license}",
8
- "engines": {
9
- "node": "${engineRequirement}"
10
- },
11
- "napi": {
12
- "binaryName": "${binaryName}",
13
- "targets": [
14
- ${targets.map((t) => `"${t}"`).join(',\n ')}
15
- ]
16
- },
17
- "scripts": {
18
- "test": "node -e \\"assert(require('.').sum(1, 2) === 3)\\"",
19
- "build": "napi build --release --platform --strip",
20
- "build:debug": "napi build",
21
- "prepublishOnly": "napi prepublish -t npm",
22
- "artifacts": "napi artifacts",
23
- "version": "napi version"
24
- },
25
- "devDependencies": {
26
- "@napi-rs/cli": "^${cliVersion}"
27
- }
28
- }`;
2
+ const content = {
3
+ name,
4
+ version: '1.0.0',
5
+ license,
6
+ engines: {
7
+ node: engineRequirement,
8
+ },
9
+ type: 'commonjs',
10
+ main: 'index.js',
11
+ types: 'index.d.ts',
12
+ module: undefined,
13
+ exports: undefined,
14
+ napi: {
15
+ binaryName,
16
+ targets,
17
+ },
18
+ scripts: {
19
+ test: 'node -e "assert(require(\'.\').sum(1, 2) === 3)"',
20
+ build: 'napi build --release --platform --strip',
21
+ 'build:debug': 'napi build',
22
+ prepublishOnly: 'napi prepublish -t npm',
23
+ artifacts: 'napi artifacts',
24
+ version: 'napi version',
25
+ },
26
+ devDependencies: {
27
+ '@napi-rs/cli': `^${cliVersion}`,
28
+ },
29
+ };
30
+ return JSON.stringify(content, null, 2);
29
31
  };
package/dist/cli.js CHANGED
File without changes