@napi-rs/cli 3.0.0-alpha.7 → 3.0.0-alpha.71

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 (160) hide show
  1. package/cli.mjs +1 -1
  2. package/dist/api/artifacts.js +28 -7
  3. package/dist/api/artifacts.js.map +1 -1
  4. package/dist/api/build.js +247 -47
  5. package/dist/api/build.js.map +1 -1
  6. package/dist/api/create-npm-dirs.js +46 -9
  7. package/dist/api/create-npm-dirs.js.map +1 -1
  8. package/dist/api/new.js +57 -20
  9. package/dist/api/new.js.map +1 -1
  10. package/dist/api/pre-publish.js +9 -7
  11. package/dist/api/pre-publish.js.map +1 -1
  12. package/dist/api/rename.js +9 -1
  13. package/dist/api/rename.js.map +1 -1
  14. package/dist/api/templates/cargo.toml.js +1 -0
  15. package/dist/api/templates/cargo.toml.js.map +1 -1
  16. package/dist/api/templates/ci-template.d.ts +3 -1
  17. package/dist/api/templates/ci-template.js +144 -131
  18. package/dist/api/templates/ci-template.js.map +1 -1
  19. package/dist/api/templates/ci.yml.d.ts +3 -1
  20. package/dist/api/templates/ci.yml.js +21 -5
  21. package/dist/api/templates/ci.yml.js.map +1 -1
  22. package/dist/api/templates/js-binding.d.ts +1 -0
  23. package/dist/api/templates/js-binding.js +135 -221
  24. package/dist/api/templates/js-binding.js.map +1 -1
  25. package/dist/api/templates/lib.rs.js +1 -4
  26. package/dist/api/templates/lib.rs.js.map +1 -1
  27. package/dist/api/templates/load-wasi-template.d.ts +2 -0
  28. package/dist/api/templates/load-wasi-template.js +166 -0
  29. package/dist/api/templates/load-wasi-template.js.map +1 -0
  30. package/dist/api/templates/package.json.d.ts +9 -2
  31. package/dist/api/templates/package.json.js +26 -4
  32. package/dist/api/templates/package.json.js.map +1 -1
  33. package/dist/api/templates/wasi-worker-template.d.ts +2 -0
  34. package/dist/api/templates/wasi-worker-template.js +122 -0
  35. package/dist/api/templates/wasi-worker-template.js.map +1 -0
  36. package/dist/api/universalize.js +5 -5
  37. package/dist/api/universalize.js.map +1 -1
  38. package/dist/api/version.js +2 -2
  39. package/dist/api/version.js.map +1 -1
  40. package/dist/commands/artifacts.js +2 -2
  41. package/dist/commands/artifacts.js.map +1 -1
  42. package/dist/commands/build.js +1 -1
  43. package/dist/commands/build.js.map +1 -1
  44. package/dist/commands/new.js +34 -34
  45. package/dist/commands/new.js.map +1 -1
  46. package/dist/def/artifacts.d.ts +21 -1
  47. package/dist/def/artifacts.js +10 -2
  48. package/dist/def/artifacts.js.map +1 -1
  49. package/dist/def/build.d.ts +155 -4
  50. package/dist/def/build.js +30 -4
  51. package/dist/def/build.js.map +1 -1
  52. package/dist/def/create-npm-dirs.d.ts +10 -0
  53. package/dist/def/create-npm-dirs.js +4 -0
  54. package/dist/def/create-npm-dirs.js.map +1 -1
  55. package/dist/def/new.d.ts +25 -7
  56. package/dist/def/new.js +11 -1
  57. package/dist/def/new.js.map +1 -1
  58. package/dist/def/pre-publish.d.ts +12 -2
  59. package/dist/def/pre-publish.js +7 -3
  60. package/dist/def/pre-publish.js.map +1 -1
  61. package/dist/def/rename.d.ts +16 -6
  62. package/dist/def/rename.js +5 -1
  63. package/dist/def/rename.js.map +1 -1
  64. package/dist/def/universalize.d.ts +10 -0
  65. package/dist/def/universalize.js +4 -0
  66. package/dist/def/universalize.js.map +1 -1
  67. package/dist/def/version.d.ts +10 -0
  68. package/dist/def/version.js +4 -0
  69. package/dist/def/version.js.map +1 -1
  70. package/dist/index.cjs +8146 -14672
  71. package/dist/index.d.ts +1 -0
  72. package/dist/index.js +1 -0
  73. package/dist/index.js.map +1 -1
  74. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  75. package/dist/utils/__tests__/config.spec.js +63 -0
  76. package/dist/utils/__tests__/config.spec.js.map +1 -0
  77. package/dist/utils/__tests__/typegen.spec.js +8 -4
  78. package/dist/utils/__tests__/typegen.spec.js.map +1 -1
  79. package/dist/utils/cargo.js +2 -2
  80. package/dist/utils/cargo.js.map +1 -1
  81. package/dist/utils/config.d.ts +48 -4
  82. package/dist/utils/config.js +37 -5
  83. package/dist/utils/config.js.map +1 -1
  84. package/dist/utils/index.d.ts +0 -1
  85. package/dist/utils/index.js +0 -1
  86. package/dist/utils/index.js.map +1 -1
  87. package/dist/utils/metadata.js +2 -2
  88. package/dist/utils/metadata.js.map +1 -1
  89. package/dist/utils/misc.d.ts +1 -2
  90. package/dist/utils/misc.js +3 -3
  91. package/dist/utils/misc.js.map +1 -1
  92. package/dist/utils/target.d.ts +7 -5
  93. package/dist/utils/target.js +17 -3
  94. package/dist/utils/target.js.map +1 -1
  95. package/dist/utils/typegen.d.ts +1 -1
  96. package/dist/utils/typegen.js +51 -17
  97. package/dist/utils/typegen.js.map +1 -1
  98. package/dist/utils/version.d.ts +2 -1
  99. package/dist/utils/version.js +10 -8
  100. package/dist/utils/version.js.map +1 -1
  101. package/package.json +27 -26
  102. package/src/api/artifacts.ts +81 -9
  103. package/src/api/build.ts +403 -66
  104. package/src/api/create-npm-dirs.ts +57 -9
  105. package/src/api/new.ts +89 -31
  106. package/src/api/pre-publish.ts +13 -7
  107. package/src/api/rename.ts +10 -1
  108. package/src/api/templates/cargo.toml.ts +1 -0
  109. package/src/api/templates/ci-template.ts +154 -131
  110. package/src/api/templates/ci.yml.ts +27 -6
  111. package/src/api/templates/js-binding.ts +141 -219
  112. package/src/api/templates/lib.rs.ts +1 -4
  113. package/src/api/templates/load-wasi-template.ts +180 -0
  114. package/src/api/templates/package.json.ts +47 -5
  115. package/src/api/templates/wasi-worker-template.ts +122 -0
  116. package/src/api/universalize.ts +13 -8
  117. package/src/api/version.ts +5 -2
  118. package/src/commands/artifacts.ts +2 -2
  119. package/src/commands/build.ts +1 -1
  120. package/src/commands/new.ts +46 -45
  121. package/src/def/artifacts.ts +22 -3
  122. package/src/def/build.ts +64 -8
  123. package/src/def/create-npm-dirs.ts +9 -0
  124. package/src/def/new.ts +28 -3
  125. package/src/def/pre-publish.ts +12 -3
  126. package/src/def/rename.ts +11 -2
  127. package/src/def/universalize.ts +9 -0
  128. package/src/def/version.ts +9 -0
  129. package/src/index.ts +2 -0
  130. package/src/utils/__tests__/__fixtures__/napi_type_def +130 -130
  131. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  132. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  133. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +28 -0
  134. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  135. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +711 -158
  136. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  137. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +8 -7
  138. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  139. package/src/utils/__tests__/config.spec.ts +81 -0
  140. package/src/utils/__tests__/typegen.spec.ts +18 -3
  141. package/src/utils/cargo.ts +2 -2
  142. package/src/utils/config.ts +112 -10
  143. package/src/utils/index.ts +0 -1
  144. package/src/utils/metadata.ts +2 -2
  145. package/src/utils/misc.ts +11 -3
  146. package/src/utils/target.ts +22 -4
  147. package/src/utils/typegen.ts +81 -36
  148. package/src/utils/version.ts +10 -8
  149. package/dist/api/load-wasi-template.d.ts +0 -1
  150. package/dist/api/load-wasi-template.js +0 -63
  151. package/dist/api/load-wasi-template.js.map +0 -1
  152. package/dist/api/wasi-worker-template.d.ts +0 -1
  153. package/dist/api/wasi-worker-template.js +0 -53
  154. package/dist/api/wasi-worker-template.js.map +0 -1
  155. package/dist/utils/ci.d.ts +0 -11
  156. package/dist/utils/ci.js +0 -96
  157. package/dist/utils/ci.js.map +0 -1
  158. package/src/api/load-wasi-template.ts +0 -65
  159. package/src/api/wasi-worker-template.ts +0 -52
  160. package/src/utils/ci.ts +0 -111
@@ -9,12 +9,13 @@ Generated by [AVA](https://avajs.dev).
9
9
  > Snapshot 1
10
10
 
11
11
  [
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',
12
+ '>= 8.6.0 < 9 || >= 9.0.0 < 10 || >= 10.0.0',
13
+ '>= 8.10.0 < 9 || >= 9.3.0 < 10 || >= 10.0.0',
14
+ '>= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0 < 10 || >= 10.0.0',
15
+ '>= 10.16.0 < 11 || >= 11.8.0 < 12 || >= 12.0.0',
16
+ '>= 10.17.0 < 11 || >= 12.11.0 < 13 || >= 13.0.0',
17
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',
18
+ '>= 10.23.0 < 11 || >= 12.19.0 < 13 || >= 14.12.0 < 15 || >= 15.0.0',
19
+ '>= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0',
20
+ '>= 18.17.0 < 19 || >= 20.3.0 < 21 || >= 21.1.0',
20
21
  ]
@@ -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
@@ -43,6 +43,21 @@ test('should process type def correctly', async (t) => {
43
43
  '__fixtures__',
44
44
  'napi_type_def',
45
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(
54
+ join(
55
+ fileURLToPath(import.meta.url),
56
+ '../',
57
+ '__fixtures__',
58
+ 'napi_type_def',
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
 
@@ -28,7 +28,7 @@ function detectCargoBinary(bin: string) {
28
28
  })
29
29
  debug('Cargo binary detected: %s', bin)
30
30
  return true
31
- } catch (e) {
31
+ } catch {
32
32
  debug('Cargo binary not detected: %s', bin)
33
33
  return false
34
34
  }
@@ -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,47 @@ interface UserNapiConfig {
24
37
  */
25
38
  npmClient?: string
26
39
 
40
+ /**
41
+ * Whether generate const enum for typescript bindings
42
+ */
43
+ constEnum?: boolean
44
+
45
+ /**
46
+ * dts header prepend to the generated dts file
47
+ */
48
+ dtsHeader?: string
49
+
50
+ /**
51
+ * dts header file path to be prepended to the generated dts file
52
+ * if both dtsHeader and dtsHeaderFile are provided, dtsHeaderFile will be used
53
+ */
54
+ dtsHeaderFile?: string
55
+
56
+ /**
57
+ * wasm compilation options
58
+ */
59
+ wasm?: {
60
+ /**
61
+ * https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory
62
+ * @default 4000 pages (256MiB)
63
+ */
64
+ initialMemory?: number
65
+ /**
66
+ * @default 65536 pages (4GiB)
67
+ */
68
+ maximumMemory?: number
69
+
70
+ /**
71
+ * Browser wasm binding configuration
72
+ */
73
+ browser: {
74
+ /**
75
+ * Whether to use fs module in browser
76
+ */
77
+ fs?: boolean
78
+ }
79
+ }
80
+
27
81
  /**
28
82
  * @deprecated binaryName instead
29
83
  */
@@ -71,22 +125,34 @@ export interface CommonPackageJsonFields {
71
125
  main?: string
72
126
  module?: string
73
127
  types?: string
128
+ browser?: string
74
129
  exports?: any
75
130
 
76
131
  dependencies?: Record<string, string>
77
132
  devDependencies?: Record<string, string>
133
+
134
+ ava?: {
135
+ timeout?: string
136
+ }
78
137
  }
79
138
 
80
139
  export type NapiConfig = Required<
81
140
  Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
82
- > & {
83
- targets: Target[]
84
- packageJson: CommonPackageJsonFields
85
- }
141
+ > &
142
+ Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile' | 'constEnum'> & {
143
+ targets: Target[]
144
+ packageJson: CommonPackageJsonFields
145
+ }
86
146
 
87
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
147
+ export async function readNapiConfig(
148
+ path: string,
149
+ configPath?: string,
150
+ ): Promise<NapiConfig> {
151
+ if (configPath && !(await fileExists(configPath))) {
152
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
153
+ }
88
154
  if (!(await fileExists(path))) {
89
- throw new Error(`napi-rs config not found at ${path}`)
155
+ throw new Error(`package.json not found at ${path}`)
90
156
  }
91
157
  // May support multiple config sources later on.
92
158
  const content = await readFileAsync(path, 'utf8')
@@ -94,12 +160,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
94
160
  try {
95
161
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
96
162
  } catch (e) {
97
- throw new Error('Failed to parse napi-rs config', {
163
+ throw new Error(`Failed to parse package.json at ${path}`, {
98
164
  cause: e,
99
165
  })
100
166
  }
101
167
 
168
+ let separatedConfig: UserNapiConfig | undefined
169
+ if (configPath) {
170
+ const configContent = await readFileAsync(configPath, 'utf8')
171
+ try {
172
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
173
+ } catch (e) {
174
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
175
+ cause: e,
176
+ })
177
+ }
178
+ }
179
+
102
180
  const userNapiConfig = pkgJson.napi ?? {}
181
+ if (pkgJson.napi && separatedConfig) {
182
+ const pkgJsonPath = underline(path)
183
+ const configPathUnderline = underline(configPath!)
184
+ console.warn(
185
+ yellow(
186
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
187
+ ),
188
+ )
189
+ Object.assign(userNapiConfig, separatedConfig)
190
+ }
103
191
  const napiConfig: NapiConfig = merge(
104
192
  {
105
193
  binaryName: 'index',
@@ -114,17 +202,31 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
114
202
  let targets: string[] = userNapiConfig.targets ?? []
115
203
 
116
204
  // compatible with old config
117
- if (userNapiConfig.package?.name) {
118
- napiConfig.packageName = userNapiConfig.package.name
205
+ if (userNapiConfig?.name) {
206
+ console.warn(
207
+ yellow(
208
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
209
+ ),
210
+ )
211
+ napiConfig.binaryName = userNapiConfig.name
119
212
  }
120
213
 
121
214
  if (!targets.length) {
215
+ let deprecatedWarned = false
216
+ const warning = yellow(
217
+ `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`,
218
+ )
122
219
  if (userNapiConfig.triples?.defaults) {
220
+ deprecatedWarned = true
221
+ console.warn(warning)
123
222
  targets = targets.concat(DEFAULT_TARGETS)
124
223
  }
125
224
 
126
225
  if (userNapiConfig.triples?.additional?.length) {
127
226
  targets = targets.concat(userNapiConfig.triples.additional)
227
+ if (!deprecatedWarned) {
228
+ console.warn(warning)
229
+ }
128
230
  }
129
231
  }
130
232
 
@@ -1,4 +1,3 @@
1
- export * from './ci.js'
2
1
  export * from './log.js'
3
2
  export * from './misc.js'
4
3
  export * from './target.js'
@@ -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,7 +1,11 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
2
 
3
3
  export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
4
4
 
5
+ export const UNIVERSAL_TARGETS = {
6
+ 'universal-apple-darwin': ['aarch64-apple-darwin', 'x86_64-apple-darwin'],
7
+ } as const
8
+
5
9
  export const AVAILABLE_TARGETS = [
6
10
  'aarch64-apple-darwin',
7
11
  'aarch64-linux-android',
@@ -15,15 +19,20 @@ export const AVAILABLE_TARGETS = [
15
19
  'x86_64-unknown-freebsd',
16
20
  'i686-pc-windows-msvc',
17
21
  'armv7-unknown-linux-gnueabihf',
22
+ 'armv7-unknown-linux-musleabihf',
18
23
  'armv7-linux-androideabi',
19
24
  'universal-apple-darwin',
20
25
  'riscv64gc-unknown-linux-gnu',
26
+ 'powerpc64le-unknown-linux-gnu',
27
+ 's390x-unknown-linux-gnu',
28
+ 'wasm32-wasi-preview1-threads',
21
29
  ] as const
22
30
 
23
31
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
24
32
 
25
33
  export const DEFAULT_TARGETS = [
26
34
  'x86_64-apple-darwin',
35
+ 'aarch64-apple-darwin',
27
36
  'x86_64-pc-windows-msvc',
28
37
  'x86_64-unknown-linux-gnu',
29
38
  ] as const
@@ -31,6 +40,8 @@ export const DEFAULT_TARGETS = [
31
40
  export const TARGET_LINKER: Record<string, string> = {
32
41
  'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
33
42
  'riscv64gc-unknown-linux-gnu': 'riscv64-linux-gnu-gcc',
43
+ 'powerpc64le-unknown-linux-gnu': 'powerpc64le-linux-gnu-gcc',
44
+ 's390x-unknown-linux-gnu': 's390x-linux-gnu-gcc',
34
45
  }
35
46
 
36
47
  // https://nodejs.org/api/process.html#process_process_arch
@@ -48,6 +59,7 @@ type NodeJSArch =
48
59
  | 'x32'
49
60
  | 'x64'
50
61
  | 'universal'
62
+ | 'wasm32'
51
63
 
52
64
  const CpuToNodeArch: Record<string, NodeJSArch> = {
53
65
  x86_64: 'x64',
@@ -55,6 +67,7 @@ const CpuToNodeArch: Record<string, NodeJSArch> = {
55
67
  i686: 'ia32',
56
68
  armv7: 'arm',
57
69
  riscv64gc: 'riscv64',
70
+ powerpc64le: 'ppc64',
58
71
  }
59
72
 
60
73
  export const NodeArchToCpu: Record<string, string> = {
@@ -63,6 +76,7 @@ export const NodeArchToCpu: Record<string, string> = {
63
76
  ia32: 'i686',
64
77
  arm: 'armv7',
65
78
  riscv64: 'riscv64gc',
79
+ ppc64: 'powerpc64le',
66
80
  }
67
81
 
68
82
  const SysToNodePlatform: Record<string, Platform> = {
@@ -80,7 +94,7 @@ export interface Target {
80
94
  triple: string
81
95
  platformArchABI: string
82
96
  platform: Platform
83
- arch: NodeJSArch | 'wasm32'
97
+ arch: NodeJSArch
84
98
  abi: string | null
85
99
  }
86
100
 
@@ -95,10 +109,14 @@ export interface Target {
95
109
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
96
110
  */
97
111
  export function parseTriple(rawTriple: string): Target {
98
- if (rawTriple === 'wasm32-wasi-preview1-threads') {
112
+ if (
113
+ rawTriple === 'wasm32-wasi' ||
114
+ rawTriple === 'wasm32-wasi-preview1-threads' ||
115
+ rawTriple.startsWith('wasm32-wasip')
116
+ ) {
99
117
  return {
100
118
  triple: rawTriple,
101
- platformArchABI: rawTriple,
119
+ platformArchABI: 'wasm32-wasi',
102
120
  platform: 'wasi',
103
121
  arch: 'wasm32',
104
122
  abi: 'wasi',
@@ -10,7 +10,9 @@ export const DEFAULT_TYPE_DEF_HEADER = `/* auto-generated by NAPI-RS */
10
10
  enum TypeDefKind {
11
11
  Const = 'const',
12
12
  Enum = 'enum',
13
+ StringEnum = 'string_enum',
13
14
  Interface = 'interface',
15
+ Type = 'type',
14
16
  Fn = 'fn',
15
17
  Struct = 'struct',
16
18
  Impl = 'impl',
@@ -25,24 +27,46 @@ interface TypeDefLine {
25
27
  js_mod?: string
26
28
  }
27
29
 
28
- function prettyPrint(line: TypeDefLine, ident: number): string {
30
+ function prettyPrint(
31
+ line: TypeDefLine,
32
+ constEnum: boolean,
33
+ ident: number,
34
+ ambient = false,
35
+ ): string {
29
36
  let s = line.js_doc ?? ''
30
37
  switch (line.kind) {
31
38
  case TypeDefKind.Interface:
32
39
  s += `export interface ${line.name} {\n${line.def}\n}`
33
40
  break
34
41
 
42
+ case TypeDefKind.Type:
43
+ s += `export type ${line.name} = \n${line.def}`
44
+ break
45
+
35
46
  case TypeDefKind.Enum:
36
- s += `export const enum ${line.name} {\n${line.def}\n}`
47
+ const enumName = constEnum ? 'const enum' : 'enum'
48
+ s += `${exportDeclare(ambient)} ${enumName} ${line.name} {\n${line.def}\n}`
49
+ break
50
+
51
+ case TypeDefKind.StringEnum:
52
+ if (constEnum) {
53
+ s += `${exportDeclare(ambient)} const enum ${line.name} {\n${line.def}\n}`
54
+ } else {
55
+ s += `export type ${line.name} = ${line.def.replaceAll(/.*=/g, '').replaceAll(',', '|')};`
56
+ }
37
57
  break
38
58
 
39
59
  case TypeDefKind.Struct:
40
- s += `export class ${line.name} {\n${line.def}\n}`
60
+ s += `${exportDeclare(ambient)} class ${line.name} {\n${line.def}\n}`
41
61
  if (line.original_name && line.original_name !== line.name) {
42
62
  s += `\nexport type ${line.original_name} = ${line.name}`
43
63
  }
44
64
  break
45
65
 
66
+ case TypeDefKind.Fn:
67
+ s += `${exportDeclare(ambient)} ${line.def}`
68
+ break
69
+
46
70
  default:
47
71
  s += line.def
48
72
  }
@@ -50,51 +74,65 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
50
74
  return correctStringIdent(s, ident)
51
75
  }
52
76
 
77
+ function exportDeclare(ambient: boolean): string {
78
+ if (ambient) {
79
+ return 'export'
80
+ }
81
+
82
+ return 'export declare'
83
+ }
84
+
53
85
  export async function processTypeDef(
54
86
  intermediateTypeFile: string,
87
+ constEnum: boolean,
55
88
  header?: string,
56
89
  ) {
57
90
  const exports: string[] = []
58
91
  const defs = await readIntermediateTypeFile(intermediateTypeFile)
59
92
  const groupedDefs = preprocessTypeDef(defs)
60
93
 
61
- header = header ? header + '\n' : ''
62
- let dts = ''
63
-
64
- sortBy(Array.from(groupedDefs), ([namespace]) => namespace).forEach(
65
- ([namespace, defs]) => {
66
- if (namespace === TOP_LEVEL_NAMESPACE) {
67
- for (const def of defs) {
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)
94
+ header = header ?? ''
95
+
96
+ const dts =
97
+ sortBy(Array.from(groupedDefs), ([namespace]) => namespace)
98
+ .map(([namespace, defs]) => {
99
+ if (namespace === TOP_LEVEL_NAMESPACE) {
100
+ return defs
101
+ .map((def) => {
102
+ switch (def.kind) {
103
+ case TypeDefKind.Const:
104
+ case TypeDefKind.Enum:
105
+ case TypeDefKind.StringEnum:
106
+ case TypeDefKind.Fn:
107
+ case TypeDefKind.Struct: {
108
+ exports.push(def.name)
109
+ if (def.original_name && def.original_name !== def.name) {
110
+ exports.push(def.original_name)
111
+ }
112
+ break
113
+ }
114
+ default:
115
+ break
77
116
  }
78
- break
79
- }
80
- default:
81
- break
117
+ return prettyPrint(def, constEnum, 0)
118
+ })
119
+ .join('\n\n')
120
+ } else {
121
+ exports.push(namespace)
122
+ let declaration = ''
123
+ declaration += `export declare namespace ${namespace} {\n`
124
+ for (const def of defs) {
125
+ declaration += prettyPrint(def, constEnum, 2, true) + '\n'
82
126
  }
127
+ declaration += '}'
128
+ return declaration
83
129
  }
84
- } else {
85
- exports.push(namespace)
86
- dts += `export namespace ${namespace} {\n`
87
- for (const def of defs) {
88
- dts += prettyPrint(def, 2) + '\n'
89
- }
90
- dts += '}\n\n'
91
- }
92
- },
93
- )
130
+ })
131
+ .join('\n\n') + '\n'
94
132
 
95
133
  if (dts.indexOf('ExternalObject<') > -1) {
96
134
  header += `
97
- export class ExternalObject<T> {
135
+ export declare class ExternalObject<T> {
98
136
  readonly '': {
99
137
  readonly '': unique symbol
100
138
  [K: symbol]: T
@@ -186,13 +224,20 @@ export function correctStringIdent(src: string, ident: number): string {
186
224
  const isInMultilineComment = line.startsWith('*')
187
225
  const isClosingBracket = line.endsWith('}')
188
226
  const isOpeningBracket = line.endsWith('{')
227
+ const isTypeDeclaration = line.endsWith('=')
228
+ const isTypeVariant = line.startsWith('|')
189
229
 
190
230
  let rightIndent = ident
191
- if (isOpeningBracket && !isInMultilineComment) {
231
+ if ((isOpeningBracket || isTypeDeclaration) && !isInMultilineComment) {
192
232
  bracketDepth += 1
193
233
  rightIndent += (bracketDepth - 1) * 2
194
234
  } else {
195
- if (isClosingBracket && bracketDepth > 0 && !isInMultilineComment) {
235
+ if (
236
+ isClosingBracket &&
237
+ bracketDepth > 0 &&
238
+ !isInMultilineComment &&
239
+ !isTypeVariant
240
+ ) {
196
241
  bracketDepth -= 1
197
242
  }
198
243
  rightIndent += bracketDepth * 2
@@ -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 {
@@ -1 +0,0 @@
1
- export declare const createWasiBinding: (wasmFileName: string, wasiRegisterFunctions: string[]) => string;