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

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 (209) hide show
  1. package/dist/api/artifacts.js +29 -7
  2. package/dist/api/artifacts.js.map +1 -0
  3. package/dist/api/build.d.ts +1 -1
  4. package/dist/api/build.js +206 -32
  5. package/dist/api/build.js.map +1 -0
  6. package/dist/api/create-npm-dirs.js +50 -8
  7. package/dist/api/create-npm-dirs.js.map +1 -0
  8. package/dist/api/new.js +47 -20
  9. package/dist/api/new.js.map +1 -0
  10. package/dist/api/pre-publish.js +10 -7
  11. package/dist/api/pre-publish.js.map +1 -0
  12. package/dist/api/rename.js +10 -1
  13. package/dist/api/rename.js.map +1 -0
  14. package/dist/api/templates/.gitignore.d.ts +1 -1
  15. package/dist/api/templates/.gitignore.js +2 -0
  16. package/dist/api/templates/.gitignore.js.map +1 -0
  17. package/dist/api/templates/.npmignore.js +1 -0
  18. package/dist/api/templates/.npmignore.js.map +1 -0
  19. package/dist/api/templates/build.rs.js +1 -0
  20. package/dist/api/templates/build.rs.js.map +1 -0
  21. package/dist/api/templates/cargo.toml.js +1 -0
  22. package/dist/api/templates/cargo.toml.js.map +1 -0
  23. package/dist/api/templates/ci-template.d.ts +2 -1
  24. package/dist/api/templates/ci-template.js +156 -157
  25. package/dist/api/templates/ci-template.js.map +1 -0
  26. package/dist/api/templates/ci.yml.d.ts +2 -1
  27. package/dist/api/templates/ci.yml.js +19 -4
  28. package/dist/api/templates/ci.yml.js.map +1 -0
  29. package/dist/api/templates/index.js +1 -0
  30. package/dist/api/templates/index.js.map +1 -0
  31. package/dist/api/templates/js-binding.d.ts +1 -1
  32. package/dist/api/templates/js-binding.js +320 -107
  33. package/dist/api/templates/js-binding.js.map +1 -0
  34. package/dist/api/templates/lib.rs.js +2 -4
  35. package/dist/api/templates/lib.rs.js.map +1 -0
  36. package/dist/api/templates/load-wasi-template.d.ts +2 -0
  37. package/dist/api/templates/load-wasi-template.js +139 -0
  38. package/dist/api/templates/load-wasi-template.js.map +1 -0
  39. package/dist/api/templates/package.json.d.ts +9 -2
  40. package/dist/api/templates/package.json.js +55 -28
  41. package/dist/api/templates/package.json.js.map +1 -0
  42. package/dist/api/templates/wasi-worker-template.d.ts +2 -0
  43. package/dist/api/templates/wasi-worker-template.js +86 -0
  44. package/dist/api/templates/wasi-worker-template.js.map +1 -0
  45. package/dist/api/universalize.js +4 -3
  46. package/dist/api/universalize.js.map +1 -0
  47. package/dist/api/version.js +3 -2
  48. package/dist/api/version.js.map +1 -0
  49. package/dist/cli.js +1 -0
  50. package/dist/cli.js.map +1 -0
  51. package/dist/commands/artifacts.js +3 -2
  52. package/dist/commands/artifacts.js.map +1 -0
  53. package/dist/commands/build.js +2 -1
  54. package/dist/commands/build.js.map +1 -0
  55. package/dist/commands/create-npm-dirs.js +1 -0
  56. package/dist/commands/create-npm-dirs.js.map +1 -0
  57. package/dist/commands/help.js +1 -0
  58. package/dist/commands/help.js.map +1 -0
  59. package/dist/commands/new.js +15 -7
  60. package/dist/commands/new.js.map +1 -0
  61. package/dist/commands/pre-publish.js +1 -0
  62. package/dist/commands/pre-publish.js.map +1 -0
  63. package/dist/commands/rename.js +1 -0
  64. package/dist/commands/rename.js.map +1 -0
  65. package/dist/commands/universalize.js +1 -0
  66. package/dist/commands/universalize.js.map +1 -0
  67. package/dist/commands/version.js +1 -0
  68. package/dist/commands/version.js.map +1 -0
  69. package/dist/def/artifacts.d.ts +21 -1
  70. package/dist/def/artifacts.js +11 -2
  71. package/dist/def/artifacts.js.map +1 -0
  72. package/dist/def/build.d.ts +32 -2
  73. package/dist/def/build.js +23 -2
  74. package/dist/def/build.js.map +1 -0
  75. package/dist/def/create-npm-dirs.d.ts +10 -0
  76. package/dist/def/create-npm-dirs.js +5 -0
  77. package/dist/def/create-npm-dirs.js.map +1 -0
  78. package/dist/def/new.d.ts +24 -6
  79. package/dist/def/new.js +12 -1
  80. package/dist/def/new.js.map +1 -0
  81. package/dist/def/pre-publish.d.ts +10 -0
  82. package/dist/def/pre-publish.js +8 -3
  83. package/dist/def/pre-publish.js.map +1 -0
  84. package/dist/def/rename.d.ts +11 -1
  85. package/dist/def/rename.js +6 -1
  86. package/dist/def/rename.js.map +1 -0
  87. package/dist/def/universalize.d.ts +10 -0
  88. package/dist/def/universalize.js +5 -0
  89. package/dist/def/universalize.js.map +1 -0
  90. package/dist/def/version.d.ts +10 -0
  91. package/dist/def/version.js +5 -0
  92. package/dist/def/version.js.map +1 -0
  93. package/dist/index.cjs +6156 -8108
  94. package/dist/index.d.ts +1 -0
  95. package/dist/index.js +2 -0
  96. package/dist/index.js.map +1 -0
  97. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  98. package/dist/utils/__tests__/config.spec.js +63 -0
  99. package/dist/utils/__tests__/config.spec.js.map +1 -0
  100. package/dist/utils/__tests__/target.spec.js +1 -0
  101. package/dist/utils/__tests__/target.spec.js.map +1 -0
  102. package/dist/utils/__tests__/typegen.spec.js +9 -4
  103. package/dist/utils/__tests__/typegen.spec.js.map +1 -0
  104. package/dist/utils/__tests__/version.spec.js +1 -0
  105. package/dist/utils/__tests__/version.spec.js.map +1 -0
  106. package/dist/utils/cargo.js +2 -1
  107. package/dist/utils/cargo.js.map +1 -0
  108. package/dist/utils/ci.js +10 -0
  109. package/dist/utils/ci.js.map +1 -0
  110. package/dist/utils/config.d.ts +24 -5
  111. package/dist/utils/config.js +39 -6
  112. package/dist/utils/config.js.map +1 -0
  113. package/dist/utils/index.js +1 -0
  114. package/dist/utils/index.js.map +1 -0
  115. package/dist/utils/log.js +1 -0
  116. package/dist/utils/log.js.map +1 -0
  117. package/dist/utils/metadata.js +3 -2
  118. package/dist/utils/metadata.js.map +1 -0
  119. package/dist/utils/misc.d.ts +1 -1
  120. package/dist/utils/misc.js +4 -3
  121. package/dist/utils/misc.js.map +1 -0
  122. package/dist/utils/target.d.ts +9 -5
  123. package/dist/utils/target.js +20 -1
  124. package/dist/utils/target.js.map +1 -0
  125. package/dist/utils/typegen.d.ts +4 -1
  126. package/dist/utils/typegen.js +27 -6
  127. package/dist/utils/typegen.js.map +1 -0
  128. package/dist/utils/version.d.ts +2 -1
  129. package/dist/utils/version.js +13 -10
  130. package/dist/utils/version.js.map +1 -0
  131. package/package.json +50 -15
  132. package/src/api/artifacts.ts +79 -9
  133. package/src/api/build.ts +331 -39
  134. package/src/api/create-npm-dirs.ts +62 -8
  135. package/src/api/new.ts +69 -33
  136. package/src/api/pre-publish.ts +21 -12
  137. package/src/api/rename.ts +10 -1
  138. package/src/api/templates/.gitignore.ts +1 -0
  139. package/src/api/templates/ci-template.ts +157 -157
  140. package/src/api/templates/ci.yml.ts +19 -4
  141. package/src/api/templates/js-binding.ts +312 -102
  142. package/src/api/templates/lib.rs.ts +1 -4
  143. package/src/api/templates/load-wasi-template.ts +146 -0
  144. package/src/api/templates/package.json.ts +78 -27
  145. package/src/api/templates/wasi-worker-template.ts +86 -0
  146. package/src/api/universalize.ts +6 -3
  147. package/src/api/version.ts +5 -2
  148. package/src/commands/artifacts.ts +2 -2
  149. package/src/commands/build.ts +1 -1
  150. package/src/commands/new.ts +14 -8
  151. package/src/def/artifacts.ts +22 -3
  152. package/src/def/build.ts +51 -4
  153. package/src/def/create-npm-dirs.ts +9 -0
  154. package/src/def/new.ts +28 -3
  155. package/src/def/pre-publish.ts +12 -3
  156. package/src/def/rename.ts +11 -2
  157. package/src/def/universalize.ts +9 -0
  158. package/src/def/version.ts +9 -0
  159. package/src/index.ts +2 -0
  160. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  161. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  162. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +14 -0
  163. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  164. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +555 -0
  165. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  166. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +9 -8
  167. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  168. package/src/utils/__tests__/config.spec.ts +81 -0
  169. package/src/utils/__tests__/typegen.spec.ts +19 -4
  170. package/src/utils/cargo.ts +1 -1
  171. package/src/utils/ci.ts +9 -0
  172. package/src/utils/config.ts +84 -9
  173. package/src/utils/metadata.ts +2 -2
  174. package/src/utils/misc.ts +11 -3
  175. package/src/utils/target.ts +28 -7
  176. package/src/utils/typegen.ts +30 -5
  177. package/src/utils/version.ts +11 -9
  178. package/dist/def/commands/artifacts.d.ts +0 -15
  179. package/dist/def/commands/artifacts.js +0 -29
  180. package/dist/def/commands/build.d.ts +0 -53
  181. package/dist/def/commands/build.js +0 -105
  182. package/dist/def/commands/create-npm-dirs.d.ts +0 -15
  183. package/dist/def/commands/create-npm-dirs.js +0 -29
  184. package/dist/def/commands/new.d.ts +0 -27
  185. package/dist/def/commands/new.js +0 -53
  186. package/dist/def/commands/pre-publish.d.ts +0 -23
  187. package/dist/def/commands/pre-publish.js +0 -45
  188. package/dist/def/commands/rename.d.ts +0 -25
  189. package/dist/def/commands/rename.js +0 -49
  190. package/dist/def/commands/universalize.d.ts +0 -13
  191. package/dist/def/commands/universalize.js +0 -25
  192. package/dist/def/commands/version.d.ts +0 -13
  193. package/dist/def/commands/version.js +0 -25
  194. package/dist/def/options/artifacts.d.ts +0 -35
  195. package/dist/def/options/artifacts.js +0 -11
  196. package/dist/def/options/build.d.ts +0 -97
  197. package/dist/def/options/build.js +0 -3
  198. package/dist/def/options/create-npm-dirs.d.ts +0 -35
  199. package/dist/def/options/create-npm-dirs.js +0 -11
  200. package/dist/def/options/new.d.ts +0 -79
  201. package/dist/def/options/new.js +0 -15
  202. package/dist/def/options/pre-publish.d.ts +0 -65
  203. package/dist/def/options/pre-publish.js +0 -13
  204. package/dist/def/options/rename.d.ts +0 -75
  205. package/dist/def/options/rename.js +0 -11
  206. package/dist/def/options/universalize.d.ts +0 -28
  207. package/dist/def/options/universalize.js +0 -10
  208. package/dist/def/options/version.d.ts +0 -28
  209. package/dist/def/options/version.js +0 -10
@@ -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
 
package/src/utils/ci.ts CHANGED
@@ -99,4 +99,13 @@ export const CIConfig: Partial<
99
99
  ],
100
100
  test: false,
101
101
  },
102
+ 'riscv64gc-unknown-linux-gnu': {
103
+ host: 'ubuntu-latest',
104
+ build_setup: [
105
+ 'sudo apt-get update',
106
+ 'sudo apt-get install g++-riscv64-linux-gnu gcc-riscv64-linux-gnu -y',
107
+ ],
108
+ // No official nodejs docker image for riscv64
109
+ test: false,
110
+ },
102
111
  }
@@ -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,21 @@ 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
+ exports?: any
93
+
94
+ dependencies?: Record<string, string>
95
+ devDependencies?: Record<string, string>
96
+
97
+ ava?: {
98
+ timeout?: string
99
+ }
67
100
  }
68
101
 
69
102
  export type NapiConfig = Required<
@@ -73,9 +106,15 @@ export type NapiConfig = Required<
73
106
  packageJson: CommonPackageJsonFields
74
107
  }
75
108
 
76
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
109
+ export async function readNapiConfig(
110
+ path: string,
111
+ configPath?: string,
112
+ ): Promise<NapiConfig> {
113
+ if (configPath && !(await fileExists(configPath))) {
114
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
115
+ }
77
116
  if (!(await fileExists(path))) {
78
- throw new Error(`napi-rs config not found at ${path}`)
117
+ throw new Error(`package.json not found at ${path}`)
79
118
  }
80
119
  // May support multiple config sources later on.
81
120
  const content = await readFileAsync(path, 'utf8')
@@ -83,12 +122,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
83
122
  try {
84
123
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
85
124
  } catch (e) {
86
- throw new Error('Failed to parse napi-rs config', {
125
+ throw new Error(`Failed to parse package.json at ${path}`, {
87
126
  cause: e,
88
127
  })
89
128
  }
90
129
 
130
+ let separatedConfig: UserNapiConfig | undefined
131
+ if (configPath) {
132
+ const configContent = await readFileAsync(configPath, 'utf8')
133
+ try {
134
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
135
+ } catch (e) {
136
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
137
+ cause: e,
138
+ })
139
+ }
140
+ }
141
+
91
142
  const userNapiConfig = pkgJson.napi ?? {}
143
+ if (pkgJson.napi && separatedConfig) {
144
+ const pkgJsonPath = underline(path)
145
+ const configPathUnderline = underline(configPath!)
146
+ console.warn(
147
+ yellow(
148
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
149
+ ),
150
+ )
151
+ Object.assign(userNapiConfig, separatedConfig)
152
+ }
92
153
  const napiConfig: NapiConfig = merge(
93
154
  {
94
155
  binaryName: 'index',
@@ -103,17 +164,31 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
103
164
  let targets: string[] = userNapiConfig.targets ?? []
104
165
 
105
166
  // compatible with old config
106
- if (userNapiConfig.package?.name) {
107
- napiConfig.packageName = userNapiConfig.package.name
167
+ if (userNapiConfig?.name) {
168
+ console.warn(
169
+ yellow(
170
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
171
+ ),
172
+ )
173
+ napiConfig.binaryName = userNapiConfig.name
108
174
  }
109
175
 
110
176
  if (!targets.length) {
111
- if (userNapiConfig.triples?.default) {
177
+ let deprecatedWarned = false
178
+ const warning = yellow(
179
+ `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`,
180
+ )
181
+ if (userNapiConfig.triples?.defaults) {
182
+ deprecatedWarned = true
183
+ console.warn(warning)
112
184
  targets = targets.concat(DEFAULT_TARGETS)
113
185
  }
114
186
 
115
187
  if (userNapiConfig.triples?.additional?.length) {
116
188
  targets = targets.concat(userNapiConfig.triples.additional)
189
+ if (!deprecatedWarned) {
190
+ console.warn(warning)
191
+ }
117
192
  }
118
193
  }
119
194
 
@@ -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',
@@ -15,18 +21,22 @@ export const AVAILABLE_TARGETS = [
15
21
  'armv7-unknown-linux-gnueabihf',
16
22
  'armv7-linux-androideabi',
17
23
  'universal-apple-darwin',
24
+ 'riscv64gc-unknown-linux-gnu',
25
+ 'wasm32-wasi-preview1-threads',
18
26
  ] as const
19
27
 
20
28
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
21
29
 
22
30
  export const DEFAULT_TARGETS = [
23
31
  'x86_64-apple-darwin',
32
+ 'aarch64-apple-darwin',
24
33
  'x86_64-pc-windows-msvc',
25
34
  'x86_64-unknown-linux-gnu',
26
35
  ] as const
27
36
 
28
37
  export const TARGET_LINKER: Record<string, string> = {
29
38
  'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
39
+ 'riscv64gc-unknown-linux-gnu': 'riscv64-linux-gnu-gcc',
30
40
  }
31
41
 
32
42
  // https://nodejs.org/api/process.html#process_process_arch
@@ -38,17 +48,20 @@ type NodeJSArch =
38
48
  | 'mipsel'
39
49
  | 'ppc'
40
50
  | 'ppc64'
51
+ | 'riscv64'
41
52
  | 's390'
42
53
  | 's390x'
43
54
  | 'x32'
44
55
  | 'x64'
45
56
  | 'universal'
57
+ | 'wasm32'
46
58
 
47
59
  const CpuToNodeArch: Record<string, NodeJSArch> = {
48
60
  x86_64: 'x64',
49
61
  aarch64: 'arm64',
50
62
  i686: 'ia32',
51
63
  armv7: 'arm',
64
+ riscv64gc: 'riscv64',
52
65
  }
53
66
 
54
67
  export const NodeArchToCpu: Record<string, string> = {
@@ -56,25 +69,24 @@ export const NodeArchToCpu: Record<string, string> = {
56
69
  arm64: 'aarch64',
57
70
  ia32: 'i686',
58
71
  arm: 'armv7',
72
+ riscv64: 'riscv64gc',
59
73
  }
60
74
 
61
- const SysToNodePlatform: Record<string, NodeJS.Platform> = {
75
+ const SysToNodePlatform: Record<string, Platform> = {
62
76
  linux: 'linux',
63
77
  freebsd: 'freebsd',
64
78
  darwin: 'darwin',
65
79
  windows: 'win32',
66
80
  }
67
81
 
68
- export const UniArchsByPlatform: Partial<
69
- Record<NodeJS.Platform, NodeJSArch[]>
70
- > = {
82
+ export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
71
83
  darwin: ['x64', 'arm64'],
72
84
  }
73
85
 
74
86
  export interface Target {
75
87
  triple: string
76
88
  platformArchABI: string
77
- platform: NodeJS.Platform
89
+ platform: Platform
78
90
  arch: NodeJSArch
79
91
  abi: string | null
80
92
  }
@@ -90,6 +102,15 @@ export interface Target {
90
102
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
91
103
  */
92
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
+ }
93
114
  const triple = rawTriple.endsWith('eabi')
94
115
  ? `${rawTriple.slice(0, -4)}-eabi`
95
116
  : rawTriple
@@ -109,7 +130,7 @@ export function parseTriple(rawTriple: string): Target {
109
130
  ;[cpu, , sys, abi = null] = triples
110
131
  }
111
132
 
112
- const platform = SysToNodePlatform[sys] ?? (sys as NodeJS.Platform)
133
+ const platform = SysToNodePlatform[sys] ?? (sys as Platform)
113
134
  const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
114
135
  return {
115
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 {
@@ -1,15 +0,0 @@
1
- import { Command } from 'clipanion';
2
- export declare abstract class BaseArtifactsCommand extends Command {
3
- static paths: string[][];
4
- static usage: import("clipanion").Usage;
5
- cwd: string;
6
- packageJsonPath: string;
7
- outputDir: string;
8
- npmDir: string;
9
- getOptions(): {
10
- cwd: string;
11
- packageJsonPath: string;
12
- outputDir: string;
13
- npmDir: string;
14
- };
15
- }
@@ -1,29 +0,0 @@
1
- // This file is generated by codegen/index.ts
2
- // Do not edit this file manually
3
- import { Command, Option } from 'clipanion';
4
- export class BaseArtifactsCommand extends Command {
5
- static paths = [['artifacts']];
6
- static usage = Command.Usage({
7
- description: 'Copy artifacts from Github Actions into npm packages and ready to publish',
8
- });
9
- cwd = Option.String('--cwd', process.cwd(), {
10
- description: 'The working directory of where napi command will be executed in, all other paths options are relative to this path',
11
- });
12
- packageJsonPath = Option.String('--package-json-path', 'package.json', {
13
- description: 'Path to `package.json`',
14
- });
15
- outputDir = Option.String('--output-dir,-o', './', {
16
- description: 'Path to the folder where all built `.node` files put, same as `--output-dir` of build command',
17
- });
18
- npmDir = Option.String('--npm-dir', 'npm', {
19
- description: 'Path to the folder where the npm packages put',
20
- });
21
- getOptions() {
22
- return {
23
- cwd: this.cwd,
24
- packageJsonPath: this.packageJsonPath,
25
- outputDir: this.outputDir,
26
- npmDir: this.npmDir,
27
- };
28
- }
29
- }
@@ -1,53 +0,0 @@
1
- import { Command } from 'clipanion';
2
- export declare abstract class BaseBuildCommand extends Command {
3
- static paths: string[][];
4
- static usage: import("clipanion").Usage;
5
- target?: string;
6
- cwd?: string;
7
- manifestPath?: string;
8
- packageJsonPath?: string;
9
- targetDir?: string;
10
- outputDir?: string;
11
- platform?: boolean;
12
- jsPackageName?: string;
13
- jsBinding?: string;
14
- noJsBinding?: boolean;
15
- dts?: string;
16
- dtsHeader?: string;
17
- noDtsHeader?: boolean;
18
- strip?: boolean;
19
- release?: boolean;
20
- verbose?: boolean;
21
- bin?: string;
22
- package?: string;
23
- crossCompile?: boolean;
24
- watch?: boolean;
25
- features?: string[];
26
- allFeatures?: boolean;
27
- noDefaultFeatures?: boolean;
28
- getOptions(): {
29
- target: string | undefined;
30
- cwd: string | undefined;
31
- manifestPath: string | undefined;
32
- packageJsonPath: string | undefined;
33
- targetDir: string | undefined;
34
- outputDir: string | undefined;
35
- platform: boolean | undefined;
36
- jsPackageName: string | undefined;
37
- jsBinding: string | undefined;
38
- noJsBinding: boolean | undefined;
39
- dts: string | undefined;
40
- dtsHeader: string | undefined;
41
- noDtsHeader: boolean | undefined;
42
- strip: boolean | undefined;
43
- release: boolean | undefined;
44
- verbose: boolean | undefined;
45
- bin: string | undefined;
46
- package: string | undefined;
47
- crossCompile: boolean | undefined;
48
- watch: boolean | undefined;
49
- features: string[] | undefined;
50
- allFeatures: boolean | undefined;
51
- noDefaultFeatures: boolean | undefined;
52
- };
53
- }
@@ -1,105 +0,0 @@
1
- // This file is generated by codegen/index.ts
2
- // Do not edit this file manually
3
- import { Command, Option } from 'clipanion';
4
- export class BaseBuildCommand extends Command {
5
- static paths = [['build']];
6
- static usage = Command.Usage({
7
- description: 'Build the napi-rs project',
8
- });
9
- target = Option.String('--target,-t', {
10
- description: 'Build for the target triple, bypassed to `cargo build --target`',
11
- });
12
- cwd = Option.String('--cwd', {
13
- description: 'The working directory of where napi command will be executed in, all other paths options are relative to this path',
14
- });
15
- manifestPath = Option.String('--manifest-path', {
16
- description: 'Path to `Cargo.toml`',
17
- });
18
- packageJsonPath = Option.String('--package-json-path', {
19
- description: 'Path to `package.json`',
20
- });
21
- targetDir = Option.String('--target-dir', {
22
- description: 'Directory for all crate generated artifacts, see `cargo build --target-dir`',
23
- });
24
- outputDir = Option.String('--output-dir,-o', {
25
- description: 'Path to where all the built files would be put. Default to the crate folder',
26
- });
27
- platform = Option.Boolean('--platform', {
28
- description: 'Add platform triple to the generated nodejs binding file, eg: `[name].linux-x64-gnu.node`',
29
- });
30
- jsPackageName = Option.String('--js-package-name', {
31
- description: 'Package name in generated js binding file. Only works with `--platform` flag',
32
- });
33
- jsBinding = Option.String('--js', {
34
- description: 'Path and filename of generated JS binding file. Only works with `--platform` flag. Relative to `--output_dir`.',
35
- });
36
- noJsBinding = Option.Boolean('--no-js', {
37
- description: 'Whether to disable the generation JS binding file. Only works with `--platform` flag.',
38
- });
39
- dts = Option.String('--dts', {
40
- description: 'Path and filename of generated type def file. Relative to `--output_dir`',
41
- });
42
- dtsHeader = Option.String('--dts-header', {
43
- description: 'Custom file header for generated type def file. Only works when `typedef` feature enabled.',
44
- });
45
- noDtsHeader = Option.Boolean('--no-dts-header', {
46
- description: 'Whether to disable the default file header for generated type def file. Only works when `typedef` feature enabled.',
47
- });
48
- strip = Option.Boolean('--strip,-s', {
49
- description: 'Whether strip the library to achieve the minimum file size',
50
- });
51
- release = Option.Boolean('--release,-r', {
52
- description: 'Build in release mode',
53
- });
54
- verbose = Option.Boolean('--verbose,-v', {
55
- description: 'Verbosely log build command trace',
56
- });
57
- bin = Option.String('--bin', {
58
- description: 'Build only the specified binary',
59
- });
60
- package = Option.String('--package,-p', {
61
- description: 'Build the specified library or the one at cwd',
62
- });
63
- crossCompile = Option.Boolean('--cross-compile', {
64
- description: '[experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform',
65
- });
66
- watch = Option.Boolean('--watch,-w', {
67
- description: 'watch the crate changes and build continiously with `cargo-watch` crates',
68
- });
69
- features = Option.Array('--features,-F', {
70
- description: 'Space-separated list of features to activate',
71
- });
72
- allFeatures = Option.Boolean('--all-features', {
73
- description: 'Activate all available features',
74
- });
75
- noDefaultFeatures = Option.Boolean('--no-default-features', {
76
- description: 'Do not activate the `default` feature',
77
- });
78
- getOptions() {
79
- return {
80
- target: this.target,
81
- cwd: this.cwd,
82
- manifestPath: this.manifestPath,
83
- packageJsonPath: this.packageJsonPath,
84
- targetDir: this.targetDir,
85
- outputDir: this.outputDir,
86
- platform: this.platform,
87
- jsPackageName: this.jsPackageName,
88
- jsBinding: this.jsBinding,
89
- noJsBinding: this.noJsBinding,
90
- dts: this.dts,
91
- dtsHeader: this.dtsHeader,
92
- noDtsHeader: this.noDtsHeader,
93
- strip: this.strip,
94
- release: this.release,
95
- verbose: this.verbose,
96
- bin: this.bin,
97
- package: this.package,
98
- crossCompile: this.crossCompile,
99
- watch: this.watch,
100
- features: this.features,
101
- allFeatures: this.allFeatures,
102
- noDefaultFeatures: this.noDefaultFeatures,
103
- };
104
- }
105
- }