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

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 (177) hide show
  1. package/cli.mjs +0 -0
  2. package/dist/api/artifacts.js +32 -8
  3. package/dist/api/artifacts.js.map +1 -0
  4. package/dist/api/build.d.ts +1 -1
  5. package/dist/api/build.js +231 -30
  6. package/dist/api/build.js.map +1 -0
  7. package/dist/api/create-npm-dirs.js +46 -8
  8. package/dist/api/create-npm-dirs.js.map +1 -0
  9. package/dist/api/new.js +47 -20
  10. package/dist/api/new.js.map +1 -0
  11. package/dist/api/pre-publish.js +10 -7
  12. package/dist/api/pre-publish.js.map +1 -0
  13. package/dist/api/rename.js +10 -1
  14. package/dist/api/rename.js.map +1 -0
  15. package/dist/api/templates/.gitignore.d.ts +1 -1
  16. package/dist/api/templates/.gitignore.js +2 -0
  17. package/dist/api/templates/.gitignore.js.map +1 -0
  18. package/dist/api/templates/.npmignore.js +1 -0
  19. package/dist/api/templates/.npmignore.js.map +1 -0
  20. package/dist/api/templates/build.rs.js +1 -0
  21. package/dist/api/templates/build.rs.js.map +1 -0
  22. package/dist/api/templates/cargo.toml.js +1 -0
  23. package/dist/api/templates/cargo.toml.js.map +1 -0
  24. package/dist/api/templates/ci-template.d.ts +2 -1
  25. package/dist/api/templates/ci-template.js +150 -152
  26. package/dist/api/templates/ci-template.js.map +1 -0
  27. package/dist/api/templates/ci.yml.d.ts +2 -1
  28. package/dist/api/templates/ci.yml.js +19 -4
  29. package/dist/api/templates/ci.yml.js.map +1 -0
  30. package/dist/api/templates/index.js +1 -0
  31. package/dist/api/templates/index.js.map +1 -0
  32. package/dist/api/templates/js-binding.d.ts +1 -1
  33. package/dist/api/templates/js-binding.js +143 -30
  34. package/dist/api/templates/js-binding.js.map +1 -0
  35. package/dist/api/templates/lib.rs.js +2 -4
  36. package/dist/api/templates/lib.rs.js.map +1 -0
  37. package/dist/api/templates/load-wasi-template.d.ts +2 -0
  38. package/dist/api/templates/load-wasi-template.js +143 -0
  39. package/dist/api/templates/load-wasi-template.js.map +1 -0
  40. package/dist/api/templates/package.json.d.ts +9 -2
  41. package/dist/api/templates/package.json.js +53 -28
  42. package/dist/api/templates/package.json.js.map +1 -0
  43. package/dist/api/templates/wasi-worker-template.d.ts +2 -0
  44. package/dist/api/templates/wasi-worker-template.js +93 -0
  45. package/dist/api/templates/wasi-worker-template.js.map +1 -0
  46. package/dist/api/universalize.js +6 -5
  47. package/dist/api/universalize.js.map +1 -0
  48. package/dist/api/version.js +3 -2
  49. package/dist/api/version.js.map +1 -0
  50. package/dist/cli.js +1 -0
  51. package/dist/cli.js.map +1 -0
  52. package/dist/commands/artifacts.js +4 -4
  53. package/dist/commands/artifacts.js.map +1 -0
  54. package/dist/commands/build.js +2 -1
  55. package/dist/commands/build.js.map +1 -0
  56. package/dist/commands/create-npm-dirs.js +1 -0
  57. package/dist/commands/create-npm-dirs.js.map +1 -0
  58. package/dist/commands/help.js +2 -2
  59. package/dist/commands/help.js.map +1 -0
  60. package/dist/commands/new.js +15 -7
  61. package/dist/commands/new.js.map +1 -0
  62. package/dist/commands/pre-publish.js +1 -0
  63. package/dist/commands/pre-publish.js.map +1 -0
  64. package/dist/commands/rename.js +1 -0
  65. package/dist/commands/rename.js.map +1 -0
  66. package/dist/commands/universalize.js +1 -0
  67. package/dist/commands/universalize.js.map +1 -0
  68. package/dist/commands/version.js +1 -0
  69. package/dist/commands/version.js.map +1 -0
  70. package/dist/def/artifacts.d.ts +21 -1
  71. package/dist/def/artifacts.js +12 -4
  72. package/dist/def/artifacts.js.map +1 -0
  73. package/dist/def/build.d.ts +20 -2
  74. package/dist/def/build.js +16 -4
  75. package/dist/def/build.js.map +1 -0
  76. package/dist/def/create-npm-dirs.d.ts +10 -0
  77. package/dist/def/create-npm-dirs.js +6 -2
  78. package/dist/def/create-npm-dirs.js.map +1 -0
  79. package/dist/def/new.d.ts +24 -6
  80. package/dist/def/new.js +13 -3
  81. package/dist/def/new.js.map +1 -0
  82. package/dist/def/pre-publish.d.ts +10 -0
  83. package/dist/def/pre-publish.js +9 -5
  84. package/dist/def/pre-publish.js.map +1 -0
  85. package/dist/def/rename.d.ts +11 -1
  86. package/dist/def/rename.js +7 -3
  87. package/dist/def/rename.js.map +1 -0
  88. package/dist/def/universalize.d.ts +10 -0
  89. package/dist/def/universalize.js +6 -2
  90. package/dist/def/universalize.js.map +1 -0
  91. package/dist/def/version.d.ts +10 -0
  92. package/dist/def/version.js +6 -2
  93. package/dist/def/version.js.map +1 -0
  94. package/dist/index.cjs +6022 -8033
  95. package/dist/index.d.ts +1 -0
  96. package/dist/index.js +2 -0
  97. package/dist/index.js.map +1 -0
  98. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  99. package/dist/utils/__tests__/config.spec.js +63 -0
  100. package/dist/utils/__tests__/config.spec.js.map +1 -0
  101. package/dist/utils/__tests__/target.spec.js +1 -0
  102. package/dist/utils/__tests__/target.spec.js.map +1 -0
  103. package/dist/utils/__tests__/typegen.spec.js +9 -4
  104. package/dist/utils/__tests__/typegen.spec.js.map +1 -0
  105. package/dist/utils/__tests__/version.spec.js +1 -0
  106. package/dist/utils/__tests__/version.spec.js.map +1 -0
  107. package/dist/utils/cargo.js +2 -1
  108. package/dist/utils/cargo.js.map +1 -0
  109. package/dist/utils/ci.js +1 -0
  110. package/dist/utils/ci.js.map +1 -0
  111. package/dist/utils/config.d.ts +25 -5
  112. package/dist/utils/config.js +39 -6
  113. package/dist/utils/config.js.map +1 -0
  114. package/dist/utils/index.js +1 -0
  115. package/dist/utils/index.js.map +1 -0
  116. package/dist/utils/log.js +1 -0
  117. package/dist/utils/log.js.map +1 -0
  118. package/dist/utils/metadata.js +3 -2
  119. package/dist/utils/metadata.js.map +1 -0
  120. package/dist/utils/misc.d.ts +1 -1
  121. package/dist/utils/misc.js +4 -3
  122. package/dist/utils/misc.js.map +1 -0
  123. package/dist/utils/target.d.ts +9 -5
  124. package/dist/utils/target.js +16 -1
  125. package/dist/utils/target.js.map +1 -0
  126. package/dist/utils/typegen.d.ts +4 -1
  127. package/dist/utils/typegen.js +27 -6
  128. package/dist/utils/typegen.js.map +1 -0
  129. package/dist/utils/version.d.ts +2 -1
  130. package/dist/utils/version.js +13 -10
  131. package/dist/utils/version.js.map +1 -0
  132. package/package.json +50 -16
  133. package/src/api/artifacts.ts +85 -10
  134. package/src/api/build.ts +381 -32
  135. package/src/api/create-npm-dirs.ts +56 -8
  136. package/src/api/new.ts +69 -33
  137. package/src/api/pre-publish.ts +21 -12
  138. package/src/api/rename.ts +10 -1
  139. package/src/api/templates/.gitignore.ts +1 -0
  140. package/src/api/templates/ci-template.ts +151 -152
  141. package/src/api/templates/ci.yml.ts +19 -4
  142. package/src/api/templates/js-binding.ts +146 -30
  143. package/src/api/templates/lib.rs.ts +1 -4
  144. package/src/api/templates/load-wasi-template.ts +150 -0
  145. package/src/api/templates/package.json.ts +74 -27
  146. package/src/api/templates/wasi-worker-template.ts +93 -0
  147. package/src/api/universalize.ts +8 -5
  148. package/src/api/version.ts +5 -2
  149. package/src/commands/artifacts.ts +2 -2
  150. package/src/commands/build.ts +1 -1
  151. package/src/commands/new.ts +14 -8
  152. package/src/def/artifacts.ts +22 -3
  153. package/src/def/build.ts +32 -4
  154. package/src/def/create-npm-dirs.ts +9 -0
  155. package/src/def/new.ts +28 -3
  156. package/src/def/pre-publish.ts +12 -3
  157. package/src/def/rename.ts +11 -2
  158. package/src/def/universalize.ts +9 -0
  159. package/src/def/version.ts +9 -0
  160. package/src/index.ts +2 -0
  161. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  162. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  163. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +7 -0
  164. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  165. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +555 -0
  166. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  167. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +9 -8
  168. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  169. package/src/utils/__tests__/config.spec.ts +81 -0
  170. package/src/utils/__tests__/typegen.spec.ts +19 -4
  171. package/src/utils/cargo.ts +1 -1
  172. package/src/utils/config.ts +85 -9
  173. package/src/utils/metadata.ts +2 -2
  174. package/src/utils/misc.ts +11 -3
  175. package/src/utils/target.ts +23 -7
  176. package/src/utils/typegen.ts +30 -5
  177. package/src/utils/version.ts +11 -9
@@ -0,0 +1,81 @@
1
+ import { unlink, writeFile } from 'node:fs/promises'
2
+ import { tmpdir } from 'node:os'
3
+ import { join } from 'node:path'
4
+
5
+ import ava, { TestFn } from 'ava'
6
+
7
+ import {
8
+ CommonPackageJsonFields,
9
+ UserNapiConfig,
10
+ readNapiConfig,
11
+ } from '../config.js'
12
+
13
+ const NON_EXISTENT_FILE = 'non-existent-file'
14
+
15
+ const test = ava as TestFn<{
16
+ configPath: string
17
+ packageJson: string
18
+ pkgJson: CommonPackageJsonFields
19
+ config: UserNapiConfig
20
+ }>
21
+
22
+ test.before(async (t) => {
23
+ const tmp = tmpdir()
24
+ const configPath = join(tmp, 'napi.json')
25
+ const packageJson = join(tmp, 'package.json')
26
+ const pkgJson = {
27
+ name: '@napi-rs/testing',
28
+ version: '0.0.0',
29
+ napi: {
30
+ binaryName: 'testing',
31
+ packageName: '@napi-rs/testing',
32
+ targets: [
33
+ 'x86_64-unknown-linux-gnu',
34
+ 'x86_64-pc-windows-msvc',
35
+ 'x86_64-apple-darwin',
36
+ ],
37
+ },
38
+ }
39
+ await writeFile(packageJson, JSON.stringify(pkgJson, null, 2))
40
+ const config = {
41
+ binaryName: 'testing',
42
+ packageName: '@node-rs/testing',
43
+ targets: [
44
+ 'x86_64-unknown-linux-gnu',
45
+ 'x86_64-apple-darwin',
46
+ 'aarch64-apple-darwin',
47
+ ],
48
+ }
49
+ await writeFile(configPath, JSON.stringify(config, null, 2))
50
+ t.context = { configPath, config, packageJson, pkgJson }
51
+ })
52
+
53
+ test.after(async (t) => {
54
+ await unlink(t.context.configPath)
55
+ await unlink(t.context.packageJson)
56
+ })
57
+
58
+ test('should throw if package.json not found', async (t) => {
59
+ await t.throwsAsync(() => readNapiConfig(NON_EXISTENT_FILE), {
60
+ message: `package.json not found at ${NON_EXISTENT_FILE}`,
61
+ })
62
+ })
63
+
64
+ test('should throw if napi.json not found', async (t) => {
65
+ const { packageJson } = t.context
66
+ await t.throwsAsync(() => readNapiConfig(packageJson, NON_EXISTENT_FILE), {
67
+ message: `NAPI-RS config not found at ${NON_EXISTENT_FILE}`,
68
+ })
69
+ })
70
+
71
+ test('should be able to read config from package.json', async (t) => {
72
+ const { packageJson } = t.context
73
+ const config = await readNapiConfig(packageJson)
74
+ t.snapshot(config)
75
+ })
76
+
77
+ test('should be able to read config from napi.json', async (t) => {
78
+ const { packageJson, configPath } = t.context
79
+ const config = await readNapiConfig(packageJson, configPath)
80
+ t.snapshot(config)
81
+ })
@@ -18,13 +18,13 @@ test('should ident string correctly', (t) => {
18
18
  foo() {
19
19
  a = b
20
20
  }
21
-
21
+
22
22
  bar = () => {
23
-
23
+
24
24
  }
25
25
  boz = 1
26
26
  }
27
-
27
+
28
28
  namespace B {
29
29
  namespace C {
30
30
  type D = A
@@ -36,13 +36,28 @@ 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
+ join(
41
+ fileURLToPath(import.meta.url),
42
+ '../',
43
+ '__fixtures__',
44
+ 'napi_type_def',
45
+ ),
46
+ true,
47
+ )
48
+
49
+ t.snapshot(dts)
50
+ })
51
+
52
+ test('should process type def with noConstEnum correctly', async (t) => {
53
+ const { dts } = await processTypeDef(
40
54
  join(
41
55
  fileURLToPath(import.meta.url),
42
56
  '../',
43
57
  '__fixtures__',
44
58
  'napi_type_def',
45
59
  ),
60
+ false,
46
61
  )
47
62
 
48
63
  t.snapshot(dts)
@@ -1,4 +1,4 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
2
 
3
3
  import { debug } from './log.js'
4
4
 
@@ -1,9 +1,22 @@
1
+ import { underline, yellow } from 'colorette'
1
2
  import { merge, omit } from 'lodash-es'
2
3
 
3
4
  import { fileExists, readFileAsync } from './misc.js'
4
5
  import { DEFAULT_TARGETS, parseTriple, Target } from './target.js'
5
6
 
6
- interface UserNapiConfig {
7
+ export type ValueOfConstArray<T> = T[Exclude<keyof T, keyof Array<any>>]
8
+
9
+ export const SupportedPackageManagers = ['yarn'] as const
10
+ export const SupportedTestFrameworks = ['ava'] as const
11
+
12
+ export type SupportedPackageManager = ValueOfConstArray<
13
+ typeof SupportedPackageManagers
14
+ >
15
+ export type SupportedTestFramework = ValueOfConstArray<
16
+ typeof SupportedTestFrameworks
17
+ >
18
+
19
+ export interface UserNapiConfig {
7
20
  /**
8
21
  * Name of the binary to be generated, default to `index`
9
22
  */
@@ -24,6 +37,11 @@ interface UserNapiConfig {
24
37
  */
25
38
  npmClient?: string
26
39
 
40
+ /**
41
+ * Whether generate const enum for typescript bindings
42
+ */
43
+ constEnum?: boolean
44
+
27
45
  /**
28
46
  * @deprecated binaryName instead
29
47
  */
@@ -41,7 +59,7 @@ interface UserNapiConfig {
41
59
  /**
42
60
  * Whether enable default targets
43
61
  */
44
- default: boolean
62
+ defaults: boolean
45
63
  /**
46
64
  * Additional targets to be compiled for
47
65
  */
@@ -49,7 +67,7 @@ interface UserNapiConfig {
49
67
  }
50
68
  }
51
69
 
52
- interface CommonPackageJsonFields {
70
+ export interface CommonPackageJsonFields {
53
71
  name: string
54
72
  version: string
55
73
  description?: string
@@ -64,6 +82,22 @@ interface CommonPackageJsonFields {
64
82
  bugs?: any
65
83
  // eslint-disable-next-line no-use-before-define
66
84
  napi?: UserNapiConfig
85
+ type?: 'module' | 'commonjs'
86
+ scripts?: Record<string, string>
87
+
88
+ // modules
89
+ main?: string
90
+ module?: string
91
+ types?: string
92
+ browser?: string
93
+ exports?: any
94
+
95
+ dependencies?: Record<string, string>
96
+ devDependencies?: Record<string, string>
97
+
98
+ ava?: {
99
+ timeout?: string
100
+ }
67
101
  }
68
102
 
69
103
  export type NapiConfig = Required<
@@ -73,9 +107,15 @@ export type NapiConfig = Required<
73
107
  packageJson: CommonPackageJsonFields
74
108
  }
75
109
 
76
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
110
+ export async function readNapiConfig(
111
+ path: string,
112
+ configPath?: string,
113
+ ): Promise<NapiConfig> {
114
+ if (configPath && !(await fileExists(configPath))) {
115
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
116
+ }
77
117
  if (!(await fileExists(path))) {
78
- throw new Error(`napi-rs config not found at ${path}`)
118
+ throw new Error(`package.json not found at ${path}`)
79
119
  }
80
120
  // May support multiple config sources later on.
81
121
  const content = await readFileAsync(path, 'utf8')
@@ -83,12 +123,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
83
123
  try {
84
124
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
85
125
  } catch (e) {
86
- throw new Error('Failed to parse napi-rs config', {
126
+ throw new Error(`Failed to parse package.json at ${path}`, {
87
127
  cause: e,
88
128
  })
89
129
  }
90
130
 
131
+ let separatedConfig: UserNapiConfig | undefined
132
+ if (configPath) {
133
+ const configContent = await readFileAsync(configPath, 'utf8')
134
+ try {
135
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
136
+ } catch (e) {
137
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
138
+ cause: e,
139
+ })
140
+ }
141
+ }
142
+
91
143
  const userNapiConfig = pkgJson.napi ?? {}
144
+ if (pkgJson.napi && separatedConfig) {
145
+ const pkgJsonPath = underline(path)
146
+ const configPathUnderline = underline(configPath!)
147
+ console.warn(
148
+ yellow(
149
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
150
+ ),
151
+ )
152
+ Object.assign(userNapiConfig, separatedConfig)
153
+ }
92
154
  const napiConfig: NapiConfig = merge(
93
155
  {
94
156
  binaryName: 'index',
@@ -103,17 +165,31 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
103
165
  let targets: string[] = userNapiConfig.targets ?? []
104
166
 
105
167
  // compatible with old config
106
- if (userNapiConfig.package?.name) {
107
- napiConfig.packageName = userNapiConfig.package.name
168
+ if (userNapiConfig?.name) {
169
+ console.warn(
170
+ yellow(
171
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
172
+ ),
173
+ )
174
+ napiConfig.binaryName = userNapiConfig.name
108
175
  }
109
176
 
110
177
  if (!targets.length) {
111
- if (userNapiConfig.triples?.default) {
178
+ let deprecatedWarned = false
179
+ const warning = yellow(
180
+ `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`,
181
+ )
182
+ if (userNapiConfig.triples?.defaults) {
183
+ deprecatedWarned = true
184
+ console.warn(warning)
112
185
  targets = targets.concat(DEFAULT_TARGETS)
113
186
  }
114
187
 
115
188
  if (userNapiConfig.triples?.additional?.length) {
116
189
  targets = targets.concat(userNapiConfig.triples.additional)
190
+ if (!deprecatedWarned) {
191
+ console.warn(warning)
192
+ }
117
193
  }
118
194
  }
119
195
 
@@ -1,5 +1,5 @@
1
- import { execSync } from 'child_process'
2
- import fs from 'fs'
1
+ import { execSync } from 'node:child_process'
2
+ import fs from 'node:fs'
3
3
 
4
4
  export type CrateTargetKind =
5
5
  | 'bin'
package/src/utils/misc.ts CHANGED
@@ -1,6 +1,14 @@
1
- import { readFile, writeFile, copyFile, mkdir, unlink, stat, readdir } from 'fs'
2
- import { createRequire } from 'module'
3
- import { promisify } from 'util'
1
+ import {
2
+ readFile,
3
+ writeFile,
4
+ copyFile,
5
+ mkdir,
6
+ unlink,
7
+ stat,
8
+ readdir,
9
+ } from 'node:fs'
10
+ import { createRequire } from 'node:module'
11
+ import { promisify } from 'node:util'
4
12
 
5
13
  import { debug } from './log.js'
6
14
 
@@ -1,4 +1,10 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
+
3
+ export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
4
+
5
+ export const UNIVERSAL_TARGETS = {
6
+ 'universal-apple-darwin': ['aarch64-apple-darwin', 'x86_64-apple-darwin'],
7
+ } as const
2
8
 
3
9
  export const AVAILABLE_TARGETS = [
4
10
  'aarch64-apple-darwin',
@@ -16,12 +22,14 @@ export const AVAILABLE_TARGETS = [
16
22
  'armv7-linux-androideabi',
17
23
  'universal-apple-darwin',
18
24
  'riscv64gc-unknown-linux-gnu',
25
+ 'wasm32-wasi-preview1-threads',
19
26
  ] as const
20
27
 
21
28
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
22
29
 
23
30
  export const DEFAULT_TARGETS = [
24
31
  'x86_64-apple-darwin',
32
+ 'aarch64-apple-darwin',
25
33
  'x86_64-pc-windows-msvc',
26
34
  'x86_64-unknown-linux-gnu',
27
35
  ] as const
@@ -46,6 +54,7 @@ type NodeJSArch =
46
54
  | 'x32'
47
55
  | 'x64'
48
56
  | 'universal'
57
+ | 'wasm32'
49
58
 
50
59
  const CpuToNodeArch: Record<string, NodeJSArch> = {
51
60
  x86_64: 'x64',
@@ -63,23 +72,21 @@ export const NodeArchToCpu: Record<string, string> = {
63
72
  riscv64: 'riscv64gc',
64
73
  }
65
74
 
66
- const SysToNodePlatform: Record<string, NodeJS.Platform> = {
75
+ const SysToNodePlatform: Record<string, Platform> = {
67
76
  linux: 'linux',
68
77
  freebsd: 'freebsd',
69
78
  darwin: 'darwin',
70
79
  windows: 'win32',
71
80
  }
72
81
 
73
- export const UniArchsByPlatform: Partial<
74
- Record<NodeJS.Platform, NodeJSArch[]>
75
- > = {
82
+ export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
76
83
  darwin: ['x64', 'arm64'],
77
84
  }
78
85
 
79
86
  export interface Target {
80
87
  triple: string
81
88
  platformArchABI: string
82
- platform: NodeJS.Platform
89
+ platform: Platform
83
90
  arch: NodeJSArch
84
91
  abi: string | null
85
92
  }
@@ -95,6 +102,15 @@ export interface Target {
95
102
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
96
103
  */
97
104
  export function parseTriple(rawTriple: string): Target {
105
+ if (rawTriple === 'wasm32-wasi-preview1-threads') {
106
+ return {
107
+ triple: rawTriple,
108
+ platformArchABI: 'wasm32-wasi',
109
+ platform: 'wasi',
110
+ arch: 'wasm32',
111
+ abi: 'wasi',
112
+ }
113
+ }
98
114
  const triple = rawTriple.endsWith('eabi')
99
115
  ? `${rawTriple.slice(0, -4)}-eabi`
100
116
  : rawTriple
@@ -114,7 +130,7 @@ export function parseTriple(rawTriple: string): Target {
114
130
  ;[cpu, , sys, abi = null] = triples
115
131
  }
116
132
 
117
- const platform = SysToNodePlatform[sys] ?? (sys as NodeJS.Platform)
133
+ const platform = SysToNodePlatform[sys] ?? (sys as Platform)
118
134
  const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
119
135
  return {
120
136
  triple: rawTriple,
@@ -25,7 +25,11 @@ interface TypeDefLine {
25
25
  js_mod?: string
26
26
  }
27
27
 
28
- function prettyPrint(line: TypeDefLine, ident: number): string {
28
+ function prettyPrint(
29
+ line: TypeDefLine,
30
+ constEnum: boolean,
31
+ ident: number,
32
+ ): string {
29
33
  let s = line.js_doc ?? ''
30
34
  switch (line.kind) {
31
35
  case TypeDefKind.Interface:
@@ -33,7 +37,8 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
33
37
  break
34
38
 
35
39
  case TypeDefKind.Enum:
36
- s += `export const enum ${line.name} {\n${line.def}\n}`
40
+ const enumName = constEnum ? 'const enum' : 'enum'
41
+ s += `export ${enumName} ${line.name} {\n${line.def}\n}`
37
42
  break
38
43
 
39
44
  case TypeDefKind.Struct:
@@ -52,8 +57,10 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
52
57
 
53
58
  export async function processTypeDef(
54
59
  intermediateTypeFile: string,
60
+ constEnum: boolean,
55
61
  header?: string,
56
62
  ) {
63
+ const exports: string[] = []
57
64
  const defs = await readIntermediateTypeFile(intermediateTypeFile)
58
65
  const groupedDefs = preprocessTypeDef(defs)
59
66
 
@@ -64,12 +71,27 @@ export async function processTypeDef(
64
71
  ([namespace, defs]) => {
65
72
  if (namespace === TOP_LEVEL_NAMESPACE) {
66
73
  for (const def of defs) {
67
- dts += prettyPrint(def, 0) + '\n\n'
74
+ dts += prettyPrint(def, constEnum, 0) + '\n\n'
75
+ switch (def.kind) {
76
+ case TypeDefKind.Const:
77
+ case TypeDefKind.Enum:
78
+ case TypeDefKind.Fn:
79
+ case TypeDefKind.Struct: {
80
+ exports.push(def.name)
81
+ if (def.original_name && def.original_name !== def.name) {
82
+ exports.push(def.original_name)
83
+ }
84
+ break
85
+ }
86
+ default:
87
+ break
88
+ }
68
89
  }
69
90
  } else {
91
+ exports.push(namespace)
70
92
  dts += `export namespace ${namespace} {\n`
71
93
  for (const def of defs) {
72
- dts += prettyPrint(def, 2) + '\n'
94
+ dts += prettyPrint(def, constEnum, 2) + '\n'
73
95
  }
74
96
  dts += '}\n\n'
75
97
  }
@@ -87,7 +109,10 @@ export class ExternalObject<T> {
87
109
  `
88
110
  }
89
111
 
90
- return header + dts
112
+ return {
113
+ dts: header + dts,
114
+ exports,
115
+ }
91
116
  }
92
117
 
93
118
  async function readIntermediateTypeFile(file: string) {
@@ -7,20 +7,22 @@ export enum NapiVersion {
7
7
  Napi6,
8
8
  Napi7,
9
9
  Napi8,
10
+ Napi9,
10
11
  }
11
12
 
12
13
  /// because node support new napi version in some minor version updates, so we might meet such situation:
13
14
  /// `node v10.20.0` supports `napi5` and `napi6`, but `node v12.0.0` only support `napi4`,
14
- /// by which, we can not tell directly napi version supportness from node version directly.
15
+ /// by which, we can not tell directly napi version supportless from node version directly.
15
16
  const NAPI_VERSION_MATRIX = new Map<NapiVersion, string>([
16
- [NapiVersion.Napi1, '8.6.0'],
17
- [NapiVersion.Napi2, '8.10.0 | 9.3.0'],
18
- [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0'],
19
- [NapiVersion.Napi4, '10.16.0 | 11.8.0'],
20
- [NapiVersion.Napi5, '10.17.0 | 12.11.0'],
17
+ [NapiVersion.Napi1, '8.6.0 | 9.0.0 | 10.0.0'],
18
+ [NapiVersion.Napi2, '8.10.0 | 9.3.0 | 10.0.0'],
19
+ [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0 | 10.0.0'],
20
+ [NapiVersion.Napi4, '10.16.0 | 11.8.0 | 12.0.0'],
21
+ [NapiVersion.Napi5, '10.17.0 | 12.11.0 | 13.0.0'],
21
22
  [NapiVersion.Napi6, '10.20.0 | 12.17.0 | 14.0.0'],
22
- [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0'],
23
- [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0'],
23
+ [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0 | 15.0.0'],
24
+ [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0 | 16.0.0'],
25
+ [NapiVersion.Napi9, '18.17.0 | 20.3.0 | 21.1.0'],
24
26
  ])
25
27
 
26
28
  interface NodeVersion {
@@ -68,7 +70,7 @@ function toEngineRequirement(versions: NodeVersion[]): string {
68
70
  requirements.push(req)
69
71
  })
70
72
 
71
- return requirements.join(' && ')
73
+ return requirements.join(' ')
72
74
  }
73
75
 
74
76
  export function napiEngineRequirement(napiVersion: NapiVersion): string {