@napi-rs/cli 3.0.0-alpha.0 → 3.0.0-alpha.10

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 (180) hide show
  1. package/cli.mjs +6 -3
  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 +129 -35
  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 +60 -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 +69 -57
  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 +278 -109
  36. package/dist/api/templates/js-binding.js.map +1 -0
  37. package/dist/api/templates/lib.rs.js +2 -4
  38. package/dist/api/templates/lib.rs.js.map +1 -0
  39. package/dist/api/templates/package.json.js +30 -28
  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.d.ts +1 -0
  49. package/dist/cli.js +2 -0
  50. package/dist/cli.js.map +1 -0
  51. package/dist/commands/artifacts.js +1 -0
  52. package/dist/commands/artifacts.js.map +1 -0
  53. package/dist/commands/build.js +1 -0
  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 +2 -4
  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.js +1 -0
  70. package/dist/def/artifacts.js.map +1 -0
  71. package/dist/def/build.d.ts +18 -0
  72. package/dist/def/build.js +13 -0
  73. package/dist/def/build.js.map +1 -0
  74. package/dist/def/create-npm-dirs.js +1 -0
  75. package/dist/def/create-npm-dirs.js.map +1 -0
  76. package/dist/def/new.js +1 -0
  77. package/dist/def/new.js.map +1 -0
  78. package/dist/def/pre-publish.js +1 -0
  79. package/dist/def/pre-publish.js.map +1 -0
  80. package/dist/def/rename.js +1 -0
  81. package/dist/def/rename.js.map +1 -0
  82. package/dist/def/universalize.js +1 -0
  83. package/dist/def/universalize.js.map +1 -0
  84. package/dist/def/version.js +1 -0
  85. package/dist/def/version.js.map +1 -0
  86. package/dist/index.cjs +3205 -7977
  87. package/dist/index.d.ts +1 -0
  88. package/dist/index.js +2 -0
  89. package/dist/index.js.map +1 -0
  90. package/dist/utils/__tests__/target.spec.js +1 -0
  91. package/dist/utils/__tests__/target.spec.js.map +1 -0
  92. package/dist/utils/__tests__/typegen.spec.js +9 -4
  93. package/dist/utils/__tests__/typegen.spec.js.map +1 -0
  94. package/dist/utils/__tests__/version.spec.js +1 -0
  95. package/dist/utils/__tests__/version.spec.js.map +1 -0
  96. package/dist/utils/cargo.js +1 -0
  97. package/dist/utils/cargo.js.map +1 -0
  98. package/dist/utils/ci.js +10 -0
  99. package/dist/utils/ci.js.map +1 -0
  100. package/dist/utils/config.d.ts +14 -2
  101. package/dist/utils/config.js +4 -3
  102. package/dist/utils/config.js.map +1 -0
  103. package/dist/utils/index.js +1 -0
  104. package/dist/utils/index.js.map +1 -0
  105. package/dist/utils/log.js +1 -0
  106. package/dist/utils/log.js.map +1 -0
  107. package/dist/utils/metadata.js +1 -0
  108. package/dist/utils/metadata.js.map +1 -0
  109. package/dist/utils/misc.js +7 -1
  110. package/dist/utils/misc.js.map +1 -0
  111. package/dist/utils/target.d.ts +6 -5
  112. package/dist/utils/target.js +14 -2
  113. package/dist/utils/target.js.map +1 -0
  114. package/dist/utils/typegen.d.ts +4 -1
  115. package/dist/utils/typegen.js +27 -6
  116. package/dist/utils/typegen.js.map +1 -0
  117. package/dist/utils/version.js +3 -2
  118. package/dist/utils/version.js.map +1 -0
  119. package/package.json +47 -15
  120. package/src/api/artifacts.ts +7 -4
  121. package/src/api/build.ts +180 -44
  122. package/src/api/load-wasi-template.ts +62 -0
  123. package/src/api/new.ts +1 -4
  124. package/src/api/pre-publish.ts +8 -5
  125. package/src/api/templates/.gitignore.ts +1 -0
  126. package/src/api/templates/ci-template.ts +68 -57
  127. package/src/api/templates/ci.yml.ts +9 -6
  128. package/src/api/templates/js-binding.ts +270 -104
  129. package/src/api/templates/lib.rs.ts +1 -4
  130. package/src/api/templates/package.json.ts +31 -27
  131. package/src/api/wasi-worker-template.ts +52 -0
  132. package/src/cli.ts +2 -0
  133. package/src/commands/new.ts +1 -5
  134. package/src/def/build.ts +28 -0
  135. package/src/index.ts +2 -0
  136. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +7 -0
  137. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  138. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +555 -0
  139. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  140. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +7 -7
  141. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  142. package/src/utils/__tests__/typegen.spec.ts +19 -4
  143. package/src/utils/ci.ts +9 -0
  144. package/src/utils/config.ts +21 -5
  145. package/src/utils/misc.ts +6 -1
  146. package/src/utils/target.ts +21 -9
  147. package/src/utils/typegen.ts +30 -5
  148. package/src/utils/version.ts +1 -1
  149. package/dist/def/commands/artifacts.d.ts +0 -15
  150. package/dist/def/commands/artifacts.js +0 -29
  151. package/dist/def/commands/build.d.ts +0 -53
  152. package/dist/def/commands/build.js +0 -105
  153. package/dist/def/commands/create-npm-dirs.d.ts +0 -15
  154. package/dist/def/commands/create-npm-dirs.js +0 -29
  155. package/dist/def/commands/new.d.ts +0 -27
  156. package/dist/def/commands/new.js +0 -53
  157. package/dist/def/commands/pre-publish.d.ts +0 -23
  158. package/dist/def/commands/pre-publish.js +0 -45
  159. package/dist/def/commands/rename.d.ts +0 -25
  160. package/dist/def/commands/rename.js +0 -49
  161. package/dist/def/commands/universalize.d.ts +0 -13
  162. package/dist/def/commands/universalize.js +0 -25
  163. package/dist/def/commands/version.d.ts +0 -13
  164. package/dist/def/commands/version.js +0 -25
  165. package/dist/def/options/artifacts.d.ts +0 -35
  166. package/dist/def/options/artifacts.js +0 -11
  167. package/dist/def/options/build.d.ts +0 -97
  168. package/dist/def/options/build.js +0 -3
  169. package/dist/def/options/create-npm-dirs.d.ts +0 -35
  170. package/dist/def/options/create-npm-dirs.js +0 -11
  171. package/dist/def/options/new.d.ts +0 -79
  172. package/dist/def/options/new.js +0 -15
  173. package/dist/def/options/pre-publish.d.ts +0 -65
  174. package/dist/def/options/pre-publish.js +0 -13
  175. package/dist/def/options/rename.d.ts +0 -75
  176. package/dist/def/options/rename.js +0 -11
  177. package/dist/def/options/universalize.d.ts +0 -28
  178. package/dist/def/options/universalize.js +0 -10
  179. package/dist/def/options/version.d.ts +0 -28
  180. package/dist/def/options/version.js +0 -10
@@ -1,130 +1,296 @@
1
- /* eslint-disable @typescript-eslint/switch-exhaustiveness-check */
1
+ export function createCjsBinding(
2
+ localName: string,
3
+ pkgName: string,
4
+ idents: string[],
5
+ ): string {
6
+ return `// prettier-ignore
7
+ /* eslint-disable */
8
+ /* auto-generated by NAPI-RS */
2
9
 
3
- function loadNapiModule(binaryName: string, packageName: string) {
4
- const { existsSync, readFileSync } = require('fs')
5
- const { join } = require('path')
6
- const { platform, arch } = process
10
+ const { existsSync, readFileSync } = require('fs')
11
+ const { join } = require('path')
7
12
 
8
- const candidates: string[] = []
13
+ const { platform, arch } = process
9
14
 
10
- function isMusl() {
11
- // For Node 10
12
- if (!process.report || typeof process.report.getReport !== 'function') {
13
- try {
14
- const lddPath = require('child_process')
15
- .execSync('which ldd')
16
- .toString()
17
- .trim()
18
- return readFileSync(lddPath, 'utf8').includes('musl')
19
- } catch (e) {
20
- return true
21
- }
22
- } else {
23
- // @ts-expect-error
24
- const { glibcVersionRuntime } = process.report.getReport().header
25
- return !glibcVersionRuntime
15
+ let nativeBinding = null
16
+ let localFileExisted = false
17
+ let loadError = null
18
+
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()
26
28
  }
27
29
  }
30
+ return musl
31
+ }
28
32
 
29
- switch (platform) {
30
- case 'android':
31
- switch (arch) {
32
- case 'arm64':
33
- candidates.push('android-arm64')
34
- break
35
- case 'arm':
36
- candidates.push('android-arm-eabi')
37
- break
38
- }
39
- break
40
- case 'win32':
41
- switch (arch) {
42
- case 'x64':
43
- candidates.push('win32-x64-msvc')
44
- break
45
- case 'ia32':
46
- candidates.push('win32-ia32-msvc')
47
- break
48
- case 'arm64':
49
- candidates.push('win32-arm64-msvc')
50
- break
51
- }
52
- break
53
- case 'darwin':
54
- candidates.push('darwin-universal')
55
- switch (arch) {
56
- case 'x64':
57
- candidates.push('darwin-x64')
58
- break
59
- case 'arm64':
60
- candidates.push('darwin-arm64')
61
- break
62
- }
63
- break
64
- case 'freebsd':
65
- if (arch === 'x64') {
66
- candidates.push('freebsd-x64')
67
- }
68
- break
69
- case 'linux':
70
- switch (arch) {
71
- case 'x64':
72
- if (isMusl()) {
73
- candidates.push('linux-x64-musl')
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)) {
53
+ return true
54
+ }
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
65
+ }
66
+ }
67
+
68
+ switch (platform) {
69
+ case 'android':
70
+ switch (arch) {
71
+ case 'arm64':
72
+ localFileExisted = existsSync(join(__dirname, '${localName}.android-arm64.node'))
73
+ try {
74
+ if (localFileExisted) {
75
+ nativeBinding = require('./${localName}.android-arm64.node')
76
+ } else {
77
+ nativeBinding = require('${pkgName}-android-arm64')
78
+ }
79
+ } catch (e) {
80
+ loadError = e
81
+ }
82
+ break
83
+ case 'arm':
84
+ localFileExisted = existsSync(join(__dirname, '${localName}.android-arm-eabi.node'))
85
+ try {
86
+ if (localFileExisted) {
87
+ nativeBinding = require('./${localName}.android-arm-eabi.node')
88
+ } else {
89
+ nativeBinding = require('${pkgName}-android-arm-eabi')
90
+ }
91
+ } catch (e) {
92
+ loadError = e
93
+ }
94
+ break
95
+ default:
96
+ throw new Error(\`Unsupported architecture on Android \${arch}\`)
97
+ }
98
+ break
99
+ case 'win32':
100
+ switch (arch) {
101
+ case 'x64':
102
+ localFileExisted = existsSync(
103
+ join(__dirname, '${localName}.win32-x64-msvc.node')
104
+ )
105
+ try {
106
+ if (localFileExisted) {
107
+ nativeBinding = require('./${localName}.win32-x64-msvc.node')
74
108
  } else {
75
- candidates.push('linux-x64-gnu')
109
+ nativeBinding = require('${pkgName}-win32-x64-msvc')
76
110
  }
77
- break
78
- case 'arm64':
79
- if (isMusl()) {
80
- candidates.push('linux-arm64-musl')
111
+ } catch (e) {
112
+ loadError = e
113
+ }
114
+ break
115
+ case 'ia32':
116
+ localFileExisted = existsSync(
117
+ join(__dirname, '${localName}.win32-ia32-msvc.node')
118
+ )
119
+ try {
120
+ if (localFileExisted) {
121
+ nativeBinding = require('./${localName}.win32-ia32-msvc.node')
81
122
  } else {
82
- candidates.push('linux-arm64-gnu')
123
+ nativeBinding = require('${pkgName}-win32-ia32-msvc')
83
124
  }
84
- break
85
- case 'arm':
86
- candidates.push('linux-arm-gnueabihf')
87
- break
125
+ } catch (e) {
126
+ loadError = e
127
+ }
128
+ break
129
+ case 'arm64':
130
+ localFileExisted = existsSync(
131
+ join(__dirname, '${localName}.win32-arm64-msvc.node')
132
+ )
133
+ try {
134
+ if (localFileExisted) {
135
+ nativeBinding = require('./${localName}.win32-arm64-msvc.node')
136
+ } else {
137
+ nativeBinding = require('${pkgName}-win32-arm64-msvc')
138
+ }
139
+ } catch (e) {
140
+ loadError = e
141
+ }
142
+ break
143
+ default:
144
+ throw new Error(\`Unsupported architecture on Windows: \${arch}\`)
145
+ }
146
+ break
147
+ case 'darwin':
148
+ localFileExisted = existsSync(join(__dirname, '${localName}.darwin-universal.node'))
149
+ try {
150
+ if (localFileExisted) {
151
+ nativeBinding = require('./${localName}.darwin-universal.node')
152
+ } else {
153
+ nativeBinding = require('${pkgName}-darwin-universal')
88
154
  }
89
155
  break
90
- }
91
-
92
- let nativeBinding: any
93
- let loadError: any
94
-
95
- for (const suffix of candidates) {
96
- const localPath = join(__dirname, `${binaryName}.${suffix}.node`)
97
- const pkgPath = `${packageName}-${suffix}`
98
-
156
+ } catch {}
157
+ switch (arch) {
158
+ case 'x64':
159
+ localFileExisted = existsSync(join(__dirname, '${localName}.darwin-x64.node'))
160
+ try {
161
+ if (localFileExisted) {
162
+ nativeBinding = require('./${localName}.darwin-x64.node')
163
+ } else {
164
+ nativeBinding = require('${pkgName}-darwin-x64')
165
+ }
166
+ } catch (e) {
167
+ loadError = e
168
+ }
169
+ break
170
+ case 'arm64':
171
+ localFileExisted = existsSync(
172
+ join(__dirname, '${localName}.darwin-arm64.node')
173
+ )
174
+ try {
175
+ if (localFileExisted) {
176
+ nativeBinding = require('./${localName}.darwin-arm64.node')
177
+ } else {
178
+ nativeBinding = require('${pkgName}-darwin-arm64')
179
+ }
180
+ } catch (e) {
181
+ loadError = e
182
+ }
183
+ break
184
+ default:
185
+ throw new Error(\`Unsupported architecture on macOS: \${arch}\`)
186
+ }
187
+ break
188
+ case 'freebsd':
189
+ if (arch !== 'x64') {
190
+ throw new Error(\`Unsupported architecture on FreeBSD: \${arch}\`)
191
+ }
192
+ localFileExisted = existsSync(join(__dirname, '${localName}.freebsd-x64.node'))
99
193
  try {
100
- if (existsSync(localPath)) {
101
- nativeBinding = require(localPath)
194
+ if (localFileExisted) {
195
+ nativeBinding = require('./${localName}.freebsd-x64.node')
102
196
  } else {
103
- nativeBinding = require(pkgPath)
197
+ nativeBinding = require('${pkgName}-freebsd-x64')
104
198
  }
105
199
  } catch (e) {
106
200
  loadError = e
107
- continue
108
201
  }
109
-
110
- loadError = null
111
202
  break
112
- }
113
-
114
- if (!nativeBinding) {
115
- if (loadError) {
116
- throw loadError
203
+ case 'linux':
204
+ switch (arch) {
205
+ case 'x64':
206
+ if (isMusl()) {
207
+ localFileExisted = existsSync(
208
+ join(__dirname, '${localName}.linux-x64-musl.node')
209
+ )
210
+ try {
211
+ if (localFileExisted) {
212
+ nativeBinding = require('./${localName}.linux-x64-musl.node')
213
+ } else {
214
+ nativeBinding = require('${pkgName}-linux-x64-musl')
215
+ }
216
+ } catch (e) {
217
+ loadError = e
218
+ }
219
+ } else {
220
+ localFileExisted = existsSync(
221
+ join(__dirname, '${localName}.linux-x64-gnu.node')
222
+ )
223
+ try {
224
+ if (localFileExisted) {
225
+ nativeBinding = require('./${localName}.linux-x64-gnu.node')
226
+ } else {
227
+ nativeBinding = require('${pkgName}-linux-x64-gnu')
228
+ }
229
+ } catch (e) {
230
+ loadError = e
231
+ }
232
+ }
233
+ break
234
+ case 'arm64':
235
+ if (isMusl()) {
236
+ localFileExisted = existsSync(
237
+ join(__dirname, '${localName}.linux-arm64-musl.node')
238
+ )
239
+ try {
240
+ if (localFileExisted) {
241
+ nativeBinding = require('./${localName}.linux-arm64-musl.node')
242
+ } else {
243
+ nativeBinding = require('${pkgName}-linux-arm64-musl')
244
+ }
245
+ } catch (e) {
246
+ loadError = e
247
+ }
248
+ } else {
249
+ localFileExisted = existsSync(
250
+ join(__dirname, '${localName}.linux-arm64-gnu.node')
251
+ )
252
+ try {
253
+ if (localFileExisted) {
254
+ nativeBinding = require('./${localName}.linux-arm64-gnu.node')
255
+ } else {
256
+ nativeBinding = require('${pkgName}-linux-arm64-gnu')
257
+ }
258
+ } catch (e) {
259
+ loadError = e
260
+ }
261
+ }
262
+ break
263
+ case 'arm':
264
+ localFileExisted = existsSync(
265
+ join(__dirname, '${localName}.linux-arm-gnueabihf.node')
266
+ )
267
+ try {
268
+ if (localFileExisted) {
269
+ nativeBinding = require('./${localName}.linux-arm-gnueabihf.node')
270
+ } else {
271
+ nativeBinding = require('${pkgName}-linux-arm-gnueabihf')
272
+ }
273
+ } catch (e) {
274
+ loadError = e
275
+ }
276
+ break
277
+ default:
278
+ throw new Error(\`Unsupported architecture on Linux: \${arch}\`)
117
279
  }
280
+ break
281
+ default:
282
+ throw new Error(\`Unsupported OS: \${platform}, architecture: \${arch}\`)
283
+ }
118
284
 
119
- throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
285
+ if (!nativeBinding) {
286
+ if (loadError) {
287
+ throw loadError
120
288
  }
121
-
122
- return nativeBinding
289
+ throw new Error(\`Failed to load native binding\`)
123
290
  }
124
291
 
125
- export function createJsBinding(localName: string, pkgName: string): string {
126
- return `${loadNapiModule.toString()}
127
-
128
- module.exports = loadNapiModule('${localName}', '${pkgName}')
292
+ ${idents
293
+ .map((ident) => `module.exports.${ident} = nativeBinding.${ident}`)
294
+ .join('\n')}
129
295
  `
130
296
  }
@@ -1,7 +1,4 @@
1
- export const createLibRs = () => `#![deny(clippy::all)]
2
-
3
- #[macro_use]
4
- extern crate napi_derive;
1
+ export const createLibRs = () => `use napi_derive::napi;
5
2
 
6
3
  #[napi]
7
4
  pub fn sum(a: i32, b: i32) -> i32 {
@@ -1,3 +1,5 @@
1
+ import { CommonPackageJsonFields } from '../../utils/config.js'
2
+
1
3
  export const createPackageJson = ({
2
4
  name,
3
5
  binaryName,
@@ -13,32 +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
- "name": "${binaryName}",
27
- "targets": [
28
- ${targets.map((t) => `"${t}"`).join(',\n ')}
29
- ]
30
- },
31
- "scripts": {
32
- "test": "yarn build:debug --platform && 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
- "universal": "napi universal",
38
- "version": "napi version"
39
- },
40
- "devDependencies": {
41
- "@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
+ },
42
45
  }
43
- }`
46
+
47
+ return JSON.stringify(content, null, 2)
44
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
+ `
package/src/cli.ts CHANGED
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env node
2
+
1
3
  import { Cli } from 'clipanion'
2
4
 
3
5
  import { ArtifactsCommand } from './commands/artifacts.js'
@@ -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
 
package/src/def/build.ts CHANGED
@@ -47,6 +47,10 @@ export abstract class BaseBuildCommand extends Command {
47
47
  'Package name in generated js binding file. Only works with `--platform` flag',
48
48
  })
49
49
 
50
+ constEnum?: boolean = Option.Boolean('--const-enum', {
51
+ description: 'Whether generate const enum for typescript bindings',
52
+ })
53
+
50
54
  jsBinding?: string = Option.String('--js', {
51
55
  description:
52
56
  'Path and filename of generated JS binding file. Only works with `--platform` flag. Relative to `--output_dir`.',
@@ -92,11 +96,20 @@ export abstract class BaseBuildCommand extends Command {
92
96
  description: 'Build the specified library or the one at cwd',
93
97
  })
94
98
 
99
+ profile?: string = Option.String('--profile', {
100
+ description: 'Build artifacts with the specified profile',
101
+ })
102
+
95
103
  crossCompile?: boolean = Option.Boolean('--cross-compile,-x', {
96
104
  description:
97
105
  '[experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform',
98
106
  })
99
107
 
108
+ useCross?: boolean = Option.Boolean('--use-cross', {
109
+ description:
110
+ '[experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo`',
111
+ })
112
+
100
113
  watch?: boolean = Option.Boolean('--watch,-w', {
101
114
  description:
102
115
  'watch the crate changes and build continiously with `cargo-watch` crates',
@@ -124,6 +137,7 @@ export abstract class BaseBuildCommand extends Command {
124
137
  outputDir: this.outputDir,
125
138
  platform: this.platform,
126
139
  jsPackageName: this.jsPackageName,
140
+ constEnum: this.constEnum,
127
141
  jsBinding: this.jsBinding,
128
142
  noJsBinding: this.noJsBinding,
129
143
  dts: this.dts,
@@ -134,7 +148,9 @@ export abstract class BaseBuildCommand extends Command {
134
148
  verbose: this.verbose,
135
149
  bin: this.bin,
136
150
  package: this.package,
151
+ profile: this.profile,
137
152
  crossCompile: this.crossCompile,
153
+ useCross: this.useCross,
138
154
  watch: this.watch,
139
155
  features: this.features,
140
156
  allFeatures: this.allFeatures,
@@ -179,6 +195,10 @@ export interface BuildOptions {
179
195
  * Package name in generated js binding file. Only works with `--platform` flag
180
196
  */
181
197
  jsPackageName?: string
198
+ /**
199
+ * Whether generate const enum for typescript bindings
200
+ */
201
+ constEnum?: boolean
182
202
  /**
183
203
  * Path and filename of generated JS binding file. Only works with `--platform` flag. Relative to `--output_dir`.
184
204
  */
@@ -219,10 +239,18 @@ export interface BuildOptions {
219
239
  * Build the specified library or the one at cwd
220
240
  */
221
241
  package?: string
242
+ /**
243
+ * Build artifacts with the specified profile
244
+ */
245
+ profile?: string
222
246
  /**
223
247
  * [experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform
224
248
  */
225
249
  crossCompile?: boolean
250
+ /**
251
+ * [experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo`
252
+ */
253
+ useCross?: boolean
226
254
  /**
227
255
  * watch the crate changes and build continiously with `cargo-watch` crates
228
256
  */
package/src/index.ts CHANGED
@@ -29,3 +29,5 @@ export class NapiCli {
29
29
  universalize = universalizeBinaries
30
30
  version = version
31
31
  }
32
+
33
+ export { parseTriple } from './utils/target.js'
@@ -107,4 +107,11 @@ Generated by [AVA](https://avajs.dev).
107
107
  platformArchABI: 'darwin-universal',
108
108
  triple: 'universal-apple-darwin',
109
109
  },
110
+ {
111
+ abi: 'gnu',
112
+ arch: 'riscv64',
113
+ platform: 'linux',
114
+ platformArchABI: 'linux-riscv64-gnu',
115
+ triple: 'riscv64gc-unknown-linux-gnu',
116
+ },
110
117
  ]