@napi-rs/cli 3.0.0-alpha.4 → 3.0.0-alpha.6

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 (135) hide show
  1. package/cli.mjs +0 -0
  2. package/dist/api/artifacts.js +5 -2
  3. package/dist/api/artifacts.js.map +1 -0
  4. package/dist/api/build.d.ts +1 -1
  5. package/dist/api/build.js +98 -27
  6. package/dist/api/build.js.map +1 -0
  7. package/dist/api/create-npm-dirs.js +1 -0
  8. package/dist/api/create-npm-dirs.js.map +1 -0
  9. package/dist/api/load-wasi-template.d.ts +1 -0
  10. package/dist/api/load-wasi-template.js +63 -0
  11. package/dist/api/load-wasi-template.js.map +1 -0
  12. package/dist/api/new.js +2 -1
  13. package/dist/api/new.js.map +1 -0
  14. package/dist/api/pre-publish.js +1 -0
  15. package/dist/api/pre-publish.js.map +1 -0
  16. package/dist/api/rename.js +1 -0
  17. package/dist/api/rename.js.map +1 -0
  18. package/dist/api/templates/.gitignore.d.ts +1 -1
  19. package/dist/api/templates/.gitignore.js +2 -0
  20. package/dist/api/templates/.gitignore.js.map +1 -0
  21. package/dist/api/templates/.npmignore.js +1 -0
  22. package/dist/api/templates/.npmignore.js.map +1 -0
  23. package/dist/api/templates/build.rs.js +1 -0
  24. package/dist/api/templates/build.rs.js.map +1 -0
  25. package/dist/api/templates/cargo.toml.js +1 -0
  26. package/dist/api/templates/cargo.toml.js.map +1 -0
  27. package/dist/api/templates/ci-template.js +48 -35
  28. package/dist/api/templates/ci-template.js.map +1 -0
  29. package/dist/api/templates/ci.yml.d.ts +1 -1
  30. package/dist/api/templates/ci.yml.js +10 -3
  31. package/dist/api/templates/ci.yml.js.map +1 -0
  32. package/dist/api/templates/index.js +1 -0
  33. package/dist/api/templates/index.js.map +1 -0
  34. package/dist/api/templates/js-binding.d.ts +1 -1
  35. package/dist/api/templates/js-binding.js +49 -13
  36. package/dist/api/templates/js-binding.js.map +1 -0
  37. package/dist/api/templates/lib.rs.js +1 -0
  38. package/dist/api/templates/lib.rs.js.map +1 -0
  39. package/dist/api/templates/package.json.js +30 -27
  40. package/dist/api/templates/package.json.js.map +1 -0
  41. package/dist/api/universalize.js +1 -0
  42. package/dist/api/universalize.js.map +1 -0
  43. package/dist/api/version.js +1 -0
  44. package/dist/api/version.js.map +1 -0
  45. package/dist/api/wasi-worker-template.d.ts +1 -0
  46. package/dist/api/wasi-worker-template.js +53 -0
  47. package/dist/api/wasi-worker-template.js.map +1 -0
  48. package/dist/cli.js +1 -0
  49. package/dist/cli.js.map +1 -0
  50. package/dist/commands/artifacts.js +2 -2
  51. package/dist/commands/artifacts.js.map +1 -0
  52. package/dist/commands/build.js +1 -0
  53. package/dist/commands/build.js.map +1 -0
  54. package/dist/commands/create-npm-dirs.js +1 -0
  55. package/dist/commands/create-npm-dirs.js.map +1 -0
  56. package/dist/commands/help.js +2 -2
  57. package/dist/commands/help.js.map +1 -0
  58. package/dist/commands/new.js +2 -4
  59. package/dist/commands/new.js.map +1 -0
  60. package/dist/commands/pre-publish.js +1 -0
  61. package/dist/commands/pre-publish.js.map +1 -0
  62. package/dist/commands/rename.js +1 -0
  63. package/dist/commands/rename.js.map +1 -0
  64. package/dist/commands/universalize.js +1 -0
  65. package/dist/commands/universalize.js.map +1 -0
  66. package/dist/commands/version.js +1 -0
  67. package/dist/commands/version.js.map +1 -0
  68. package/dist/def/artifacts.js +2 -2
  69. package/dist/def/artifacts.js.map +1 -0
  70. package/dist/def/build.js +2 -2
  71. package/dist/def/build.js.map +1 -0
  72. package/dist/def/create-npm-dirs.js +2 -2
  73. package/dist/def/create-npm-dirs.js.map +1 -0
  74. package/dist/def/new.js +2 -2
  75. package/dist/def/new.js.map +1 -0
  76. package/dist/def/pre-publish.js +2 -2
  77. package/dist/def/pre-publish.js.map +1 -0
  78. package/dist/def/rename.js +2 -2
  79. package/dist/def/rename.js.map +1 -0
  80. package/dist/def/universalize.js +2 -2
  81. package/dist/def/universalize.js.map +1 -0
  82. package/dist/def/version.js +2 -2
  83. package/dist/def/version.js.map +1 -0
  84. package/dist/index.cjs +2921 -7847
  85. package/dist/index.js +1 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/utils/__tests__/target.spec.js +1 -0
  88. package/dist/utils/__tests__/target.spec.js.map +1 -0
  89. package/dist/utils/__tests__/typegen.spec.js +2 -1
  90. package/dist/utils/__tests__/typegen.spec.js.map +1 -0
  91. package/dist/utils/__tests__/version.spec.js +1 -0
  92. package/dist/utils/__tests__/version.spec.js.map +1 -0
  93. package/dist/utils/cargo.js +1 -0
  94. package/dist/utils/cargo.js.map +1 -0
  95. package/dist/utils/ci.js +1 -0
  96. package/dist/utils/ci.js.map +1 -0
  97. package/dist/utils/config.d.ts +9 -1
  98. package/dist/utils/config.js +1 -0
  99. package/dist/utils/config.js.map +1 -0
  100. package/dist/utils/index.js +1 -0
  101. package/dist/utils/index.js.map +1 -0
  102. package/dist/utils/log.js +1 -0
  103. package/dist/utils/log.js.map +1 -0
  104. package/dist/utils/metadata.js +1 -0
  105. package/dist/utils/metadata.js.map +1 -0
  106. package/dist/utils/misc.js +1 -0
  107. package/dist/utils/misc.js.map +1 -0
  108. package/dist/utils/target.d.ts +4 -3
  109. package/dist/utils/target.js +10 -0
  110. package/dist/utils/target.js.map +1 -0
  111. package/dist/utils/typegen.d.ts +4 -1
  112. package/dist/utils/typegen.js +21 -1
  113. package/dist/utils/typegen.js.map +1 -0
  114. package/dist/utils/version.js +3 -2
  115. package/dist/utils/version.js.map +1 -0
  116. package/package.json +47 -15
  117. package/src/api/artifacts.ts +7 -4
  118. package/src/api/build.ts +133 -25
  119. package/src/api/load-wasi-template.ts +65 -0
  120. package/src/api/new.ts +1 -4
  121. package/src/api/pre-publish.ts +8 -5
  122. package/src/api/templates/.gitignore.ts +1 -0
  123. package/src/api/templates/ci-template.ts +47 -35
  124. package/src/api/templates/ci.yml.ts +9 -6
  125. package/src/api/templates/js-binding.ts +52 -13
  126. package/src/api/templates/package.json.ts +31 -26
  127. package/src/api/wasi-worker-template.ts +52 -0
  128. package/src/commands/new.ts +1 -5
  129. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +7 -7
  130. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  131. package/src/utils/__tests__/typegen.spec.ts +1 -1
  132. package/src/utils/config.ts +12 -1
  133. package/src/utils/target.ts +16 -7
  134. package/src/utils/typegen.ts +20 -1
  135. package/src/utils/version.ts +1 -1
@@ -1,8 +1,10 @@
1
- export function createJsBinding(localName: string, pkgName: string): string {
1
+ export function createCjsBinding(
2
+ localName: string,
3
+ pkgName: string,
4
+ idents: string[],
5
+ ): string {
2
6
  return `// prettier-ignore
3
- /* tslint:disable */
4
7
  /* eslint-disable */
5
-
6
8
  /* auto-generated by NAPI-RS */
7
9
 
8
10
  const { existsSync, readFileSync } = require('fs')
@@ -14,18 +16,52 @@ let nativeBinding = null
14
16
  let localFileExisted = false
15
17
  let loadError = null
16
18
 
17
- function isMusl() {
18
- // For Node 10
19
- if (!process.report || typeof process.report.getReport !== 'function') {
20
- try {
21
- const lddPath = require('child_process').execSync('which ldd').toString().trim()
22
- return readFileSync(lddPath, 'utf8').includes('musl')
23
- } catch (e) {
19
+ const isMusl = () => {
20
+ let musl = false
21
+ if (process.platform === 'linux') {
22
+ musl = isMuslFromFilesystem()
23
+ if (musl === null) {
24
+ musl = isMuslFromReport()
25
+ }
26
+ if (musl === null) {
27
+ musl = isMuslFromChildProcess()
28
+ }
29
+ }
30
+ return musl
31
+ }
32
+
33
+ const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')
34
+
35
+ const isMuslFromFilesystem = () => {
36
+ try {
37
+ return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
38
+ } catch {
39
+ return null
40
+ }
41
+ }
42
+
43
+ const isMuslFromReport = () => {
44
+ const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null
45
+ if (!report) {
46
+ return null
47
+ }
48
+ if (report.header && report.header.glibcVersionRuntime) {
49
+ return false
50
+ }
51
+ if (Array.isArray(report.sharedObjects)) {
52
+ if (report.sharedObjects.some(isFileMusl)) {
24
53
  return true
25
54
  }
26
- } else {
27
- const { glibcVersionRuntime } = process.report.getReport().header
28
- return !glibcVersionRuntime
55
+ }
56
+ return false
57
+ }
58
+
59
+ const isMuslFromChildProcess = () => {
60
+ try {
61
+ return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
62
+ } catch (e) {
63
+ // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
64
+ return false
29
65
  }
30
66
  }
31
67
 
@@ -253,5 +289,8 @@ if (!nativeBinding) {
253
289
  throw new Error(\`Failed to load native binding\`)
254
290
  }
255
291
 
292
+ ${idents
293
+ .map((ident) => `module.exports.${ident} = nativeBinding.${ident}`)
294
+ .join('\n')}
256
295
  `
257
296
  }
@@ -1,3 +1,5 @@
1
+ import { CommonPackageJsonFields } from '../../utils/config.js'
2
+
1
3
  export const createPackageJson = ({
2
4
  name,
3
5
  binaryName,
@@ -13,31 +15,34 @@ export const createPackageJson = ({
13
15
  engineRequirement: string
14
16
  cliVersion: string
15
17
  }) => {
16
- return `{
17
- "name": "${name}",
18
- "version": "1.0.0",
19
- "main": "index.js",
20
- "types": "index.d.ts",
21
- "license": "${license}",
22
- "engines": {
23
- "node": "${engineRequirement}"
24
- },
25
- "napi": {
26
- "binaryName": "${binaryName}",
27
- "targets": [
28
- ${targets.map((t) => `"${t}"`).join(',\n ')}
29
- ]
30
- },
31
- "scripts": {
32
- "test": "node -e \\"assert(require('.').sum(1, 2) === 3)\\"",
33
- "build": "napi build --release --platform --strip",
34
- "build:debug": "napi build",
35
- "prepublishOnly": "napi prepublish -t npm",
36
- "artifacts": "napi artifacts",
37
- "version": "napi version"
38
- },
39
- "devDependencies": {
40
- "@napi-rs/cli": "^${cliVersion}"
18
+ const content: CommonPackageJsonFields = {
19
+ name,
20
+ version: '1.0.0',
21
+ license,
22
+ engines: {
23
+ node: engineRequirement,
24
+ },
25
+ type: 'commonjs',
26
+ main: 'index.js',
27
+ types: 'index.d.ts',
28
+ module: undefined,
29
+ exports: undefined,
30
+ napi: {
31
+ binaryName,
32
+ targets,
33
+ },
34
+ scripts: {
35
+ test: 'node -e "assert(require(\'.\').sum(1, 2) === 3)"',
36
+ build: 'napi build --release --platform --strip',
37
+ 'build:debug': 'napi build',
38
+ prepublishOnly: 'napi prepublish -t npm',
39
+ artifacts: 'napi artifacts',
40
+ version: 'napi version',
41
+ },
42
+ devDependencies: {
43
+ '@napi-rs/cli': `^${cliVersion}`,
44
+ },
41
45
  }
42
- }`
46
+
47
+ return JSON.stringify(content, null, 2)
43
48
  }
@@ -0,0 +1,52 @@
1
+ export const WASI_WORKER_TEMPLATE = `import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parentPort, Worker } from "node:worker_threads";
4
+
5
+ import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core";
6
+ import { WASI } from "@tybys/wasm-util";
7
+
8
+ const require = createRequire(import.meta.url);
9
+
10
+ if (parentPort) {
11
+ parentPort.on("message", (data) => {
12
+ globalThis.onmessage({ data });
13
+ });
14
+ }
15
+
16
+ Object.assign(globalThis, {
17
+ self: globalThis,
18
+ require,
19
+ Worker,
20
+ importScripts: function (f) {
21
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
22
+ },
23
+ postMessage: function (msg) {
24
+ if (parentPort) {
25
+ parentPort.postMessage(msg);
26
+ }
27
+ },
28
+ });
29
+
30
+ const handler = new MessageHandler({
31
+ onLoad({ wasmModule, wasmMemory }) {
32
+ const wasi = new WASI({ fs });
33
+
34
+ return instantiateNapiModuleSync(wasmModule, {
35
+ childThread: true,
36
+ wasi,
37
+ overwriteImports(importObject) {
38
+ importObject.env = {
39
+ ...importObject.env,
40
+ ...importObject.napi,
41
+ ...importObject.emnapi,
42
+ memory: wasmMemory
43
+ };
44
+ },
45
+ });
46
+ },
47
+ });
48
+
49
+ globalThis.onmessage = function (e) {
50
+ handler.handle(e);
51
+ };
52
+ `
@@ -94,10 +94,6 @@ export class NewCommand extends BaseNewCommand {
94
94
  }
95
95
 
96
96
  private async fetchTargets(): Promise<TargetTriple[]> {
97
- if (this.enableDefaultTargets) {
98
- return DEFAULT_TARGETS.concat()
99
- }
100
-
101
97
  if (this.enableAllTargets) {
102
98
  return AVAILABLE_TARGETS.concat()
103
99
  }
@@ -107,7 +103,7 @@ export class NewCommand extends BaseNewCommand {
107
103
  type: 'checkbox',
108
104
  loop: false,
109
105
  message: 'Choose target(s) your crate will be compiled to',
110
- default: DEFAULT_TARGETS,
106
+ default: this.enableDefaultTargets ? DEFAULT_TARGETS : [],
111
107
  choices: AVAILABLE_TARGETS,
112
108
  })
113
109
 
@@ -10,11 +10,11 @@ Generated by [AVA](https://avajs.dev).
10
10
 
11
11
  [
12
12
  '>= 8.6.0',
13
- '>= 8.10.0 && < 9 || >= 9.3.0',
14
- '>= 6.14.2 && < 7 || >= 8.11.2 && < 9 || >= 9.11.0',
15
- '>= 10.16.0 && < 11 || >= 11.8.0',
16
- '>= 10.17.0 && < 11 || >= 12.11.0',
17
- '>= 10.20.0 && < 11 || >= 12.17.0 && < 13 || >= 14.0.0',
18
- '>= 10.23.0 && < 11 || >= 12.19.0 && < 13 || >= 14.12.0',
19
- '>= 12.22.0 && < 13 || >= 14.17.0 && < 15 || >= 15.12.0',
13
+ '>= 8.10.0 < 9 || >= 9.3.0',
14
+ '>= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0',
15
+ '>= 10.16.0 < 11 || >= 11.8.0',
16
+ '>= 10.17.0 < 11 || >= 12.11.0',
17
+ '>= 10.20.0 < 11 || >= 12.17.0 < 13 || >= 14.0.0',
18
+ '>= 10.23.0 < 11 || >= 12.19.0 < 13 || >= 14.12.0',
19
+ '>= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0',
20
20
  ]
@@ -36,7 +36,7 @@ test('should ident string correctly', (t) => {
36
36
  })
37
37
 
38
38
  test('should process type def correctly', async (t) => {
39
- const dts = await processTypeDef(
39
+ const { dts } = await processTypeDef(
40
40
  join(
41
41
  fileURLToPath(import.meta.url),
42
42
  '../',
@@ -49,7 +49,7 @@ interface UserNapiConfig {
49
49
  }
50
50
  }
51
51
 
52
- interface CommonPackageJsonFields {
52
+ export interface CommonPackageJsonFields {
53
53
  name: string
54
54
  version: string
55
55
  description?: string
@@ -64,6 +64,17 @@ interface CommonPackageJsonFields {
64
64
  bugs?: any
65
65
  // eslint-disable-next-line no-use-before-define
66
66
  napi?: UserNapiConfig
67
+ type?: 'module' | 'commonjs'
68
+ scripts?: Record<string, string>
69
+
70
+ // modules
71
+ main?: string
72
+ module?: string
73
+ types?: string
74
+ exports?: any
75
+
76
+ dependencies?: Record<string, string>
77
+ devDependencies?: Record<string, string>
67
78
  }
68
79
 
69
80
  export type NapiConfig = Required<
@@ -1,5 +1,7 @@
1
1
  import { execSync } from 'child_process'
2
2
 
3
+ export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
4
+
3
5
  export const AVAILABLE_TARGETS = [
4
6
  'aarch64-apple-darwin',
5
7
  'aarch64-linux-android',
@@ -63,24 +65,22 @@ export const NodeArchToCpu: Record<string, string> = {
63
65
  riscv64: 'riscv64gc',
64
66
  }
65
67
 
66
- const SysToNodePlatform: Record<string, NodeJS.Platform> = {
68
+ const SysToNodePlatform: Record<string, Platform> = {
67
69
  linux: 'linux',
68
70
  freebsd: 'freebsd',
69
71
  darwin: 'darwin',
70
72
  windows: 'win32',
71
73
  }
72
74
 
73
- export const UniArchsByPlatform: Partial<
74
- Record<NodeJS.Platform, NodeJSArch[]>
75
- > = {
75
+ export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
76
76
  darwin: ['x64', 'arm64'],
77
77
  }
78
78
 
79
79
  export interface Target {
80
80
  triple: string
81
81
  platformArchABI: string
82
- platform: NodeJS.Platform
83
- arch: NodeJSArch
82
+ platform: Platform
83
+ arch: NodeJSArch | 'wasm32'
84
84
  abi: string | null
85
85
  }
86
86
 
@@ -95,6 +95,15 @@ export interface Target {
95
95
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
96
96
  */
97
97
  export function parseTriple(rawTriple: string): Target {
98
+ if (rawTriple === 'wasm32-wasi-preview1-threads') {
99
+ return {
100
+ triple: rawTriple,
101
+ platformArchABI: rawTriple,
102
+ platform: 'wasi',
103
+ arch: 'wasm32',
104
+ abi: 'wasi',
105
+ }
106
+ }
98
107
  const triple = rawTriple.endsWith('eabi')
99
108
  ? `${rawTriple.slice(0, -4)}-eabi`
100
109
  : rawTriple
@@ -114,7 +123,7 @@ export function parseTriple(rawTriple: string): Target {
114
123
  ;[cpu, , sys, abi = null] = triples
115
124
  }
116
125
 
117
- const platform = SysToNodePlatform[sys] ?? (sys as NodeJS.Platform)
126
+ const platform = SysToNodePlatform[sys] ?? (sys as Platform)
118
127
  const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
119
128
  return {
120
129
  triple: rawTriple,
@@ -54,6 +54,7 @@ export async function processTypeDef(
54
54
  intermediateTypeFile: string,
55
55
  header?: string,
56
56
  ) {
57
+ const exports: string[] = []
57
58
  const defs = await readIntermediateTypeFile(intermediateTypeFile)
58
59
  const groupedDefs = preprocessTypeDef(defs)
59
60
 
@@ -65,8 +66,23 @@ export async function processTypeDef(
65
66
  if (namespace === TOP_LEVEL_NAMESPACE) {
66
67
  for (const def of defs) {
67
68
  dts += prettyPrint(def, 0) + '\n\n'
69
+ switch (def.kind) {
70
+ case TypeDefKind.Const:
71
+ case TypeDefKind.Enum:
72
+ case TypeDefKind.Fn:
73
+ case TypeDefKind.Struct: {
74
+ exports.push(def.name)
75
+ if (def.original_name && def.original_name !== def.name) {
76
+ exports.push(def.original_name)
77
+ }
78
+ break
79
+ }
80
+ default:
81
+ break
82
+ }
68
83
  }
69
84
  } else {
85
+ exports.push(namespace)
70
86
  dts += `export namespace ${namespace} {\n`
71
87
  for (const def of defs) {
72
88
  dts += prettyPrint(def, 2) + '\n'
@@ -87,7 +103,10 @@ export class ExternalObject<T> {
87
103
  `
88
104
  }
89
105
 
90
- return header + dts
106
+ return {
107
+ dts: header + dts,
108
+ exports,
109
+ }
91
110
  }
92
111
 
93
112
  async function readIntermediateTypeFile(file: string) {
@@ -68,7 +68,7 @@ function toEngineRequirement(versions: NodeVersion[]): string {
68
68
  requirements.push(req)
69
69
  })
70
70
 
71
- return requirements.join(' && ')
71
+ return requirements.join(' ')
72
72
  }
73
73
 
74
74
  export function napiEngineRequirement(napiVersion: NapiVersion): string {