@napi-rs/cli 3.0.0-alpha.9 → 3.0.0-alpha.90

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 (163) hide show
  1. package/cli.mjs +1 -1
  2. package/dist/api/__tests__/new.spec.d.ts +1 -0
  3. package/dist/api/__tests__/new.spec.js +283 -0
  4. package/dist/api/__tests__/new.spec.js.map +1 -0
  5. package/dist/api/artifacts.js +28 -7
  6. package/dist/api/artifacts.js.map +1 -1
  7. package/dist/api/build.d.ts +1 -1
  8. package/dist/api/build.js +324 -115
  9. package/dist/api/build.js.map +1 -1
  10. package/dist/api/create-npm-dirs.js +46 -9
  11. package/dist/api/create-npm-dirs.js.map +1 -1
  12. package/dist/api/new.js +312 -107
  13. package/dist/api/new.js.map +1 -1
  14. package/dist/api/pre-publish.js +9 -7
  15. package/dist/api/pre-publish.js.map +1 -1
  16. package/dist/api/rename.js +74 -12
  17. package/dist/api/rename.js.map +1 -1
  18. package/dist/api/templates/cargo.toml.js +1 -0
  19. package/dist/api/templates/cargo.toml.js.map +1 -1
  20. package/dist/api/templates/ci-template.d.ts +3 -1
  21. package/dist/api/templates/ci-template.js +140 -125
  22. package/dist/api/templates/ci-template.js.map +1 -1
  23. package/dist/api/templates/ci.yml.d.ts +3 -1
  24. package/dist/api/templates/ci.yml.js +21 -5
  25. package/dist/api/templates/ci.yml.js.map +1 -1
  26. package/dist/api/templates/js-binding.d.ts +1 -0
  27. package/dist/api/templates/js-binding.js +142 -222
  28. package/dist/api/templates/js-binding.js.map +1 -1
  29. package/dist/api/templates/load-wasi-template.d.ts +2 -0
  30. package/dist/api/templates/load-wasi-template.js +168 -0
  31. package/dist/api/templates/load-wasi-template.js.map +1 -0
  32. package/dist/api/templates/package.json.d.ts +9 -2
  33. package/dist/api/templates/package.json.js +26 -4
  34. package/dist/api/templates/package.json.js.map +1 -1
  35. package/dist/api/templates/wasi-worker-template.d.ts +2 -0
  36. package/dist/api/templates/wasi-worker-template.js +122 -0
  37. package/dist/api/templates/wasi-worker-template.js.map +1 -0
  38. package/dist/api/universalize.js +5 -5
  39. package/dist/api/universalize.js.map +1 -1
  40. package/dist/api/version.js +2 -2
  41. package/dist/api/version.js.map +1 -1
  42. package/dist/commands/artifacts.js +2 -2
  43. package/dist/commands/artifacts.js.map +1 -1
  44. package/dist/commands/build.js +1 -1
  45. package/dist/commands/build.js.map +1 -1
  46. package/dist/commands/new.js +34 -34
  47. package/dist/commands/new.js.map +1 -1
  48. package/dist/def/artifacts.d.ts +21 -1
  49. package/dist/def/artifacts.js +10 -2
  50. package/dist/def/artifacts.js.map +1 -1
  51. package/dist/def/build.d.ts +149 -4
  52. package/dist/def/build.js +26 -4
  53. package/dist/def/build.js.map +1 -1
  54. package/dist/def/create-npm-dirs.d.ts +10 -0
  55. package/dist/def/create-npm-dirs.js +4 -0
  56. package/dist/def/create-npm-dirs.js.map +1 -1
  57. package/dist/def/new.d.ts +25 -7
  58. package/dist/def/new.js +11 -1
  59. package/dist/def/new.js.map +1 -1
  60. package/dist/def/pre-publish.d.ts +12 -2
  61. package/dist/def/pre-publish.js +7 -3
  62. package/dist/def/pre-publish.js.map +1 -1
  63. package/dist/def/rename.d.ts +16 -6
  64. package/dist/def/rename.js +5 -1
  65. package/dist/def/rename.js.map +1 -1
  66. package/dist/def/universalize.d.ts +10 -0
  67. package/dist/def/universalize.js +4 -0
  68. package/dist/def/universalize.js.map +1 -1
  69. package/dist/def/version.d.ts +10 -0
  70. package/dist/def/version.js +4 -0
  71. package/dist/def/version.js.map +1 -1
  72. package/dist/index.cjs +8743 -14849
  73. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  74. package/dist/utils/__tests__/config.spec.js +63 -0
  75. package/dist/utils/__tests__/config.spec.js.map +1 -0
  76. package/dist/utils/cargo.js +2 -2
  77. package/dist/utils/cargo.js.map +1 -1
  78. package/dist/utils/config.d.ts +48 -4
  79. package/dist/utils/config.js +36 -4
  80. package/dist/utils/config.js.map +1 -1
  81. package/dist/utils/index.d.ts +1 -1
  82. package/dist/utils/index.js +1 -1
  83. package/dist/utils/index.js.map +1 -1
  84. package/dist/utils/log.js +1 -1
  85. package/dist/utils/log.js.map +1 -1
  86. package/dist/utils/metadata.d.ts +13 -1
  87. package/dist/utils/metadata.js +31 -11
  88. package/dist/utils/metadata.js.map +1 -1
  89. package/dist/utils/misc.d.ts +3 -3
  90. package/dist/utils/misc.js +13 -11
  91. package/dist/utils/misc.js.map +1 -1
  92. package/dist/utils/read-config.d.ts +7 -0
  93. package/dist/utils/read-config.js +8 -0
  94. package/dist/utils/read-config.js.map +1 -0
  95. package/dist/utils/target.d.ts +7 -5
  96. package/dist/utils/target.js +17 -3
  97. package/dist/utils/target.js.map +1 -1
  98. package/dist/utils/typegen.d.ts +1 -1
  99. package/dist/utils/typegen.js +63 -32
  100. package/dist/utils/typegen.js.map +1 -1
  101. package/dist/utils/version.d.ts +2 -1
  102. package/dist/utils/version.js +10 -8
  103. package/dist/utils/version.js.map +1 -1
  104. package/package.json +34 -32
  105. package/src/api/__tests__/new.spec.ts +429 -0
  106. package/src/api/artifacts.ts +81 -9
  107. package/src/api/build.ts +498 -149
  108. package/src/api/create-npm-dirs.ts +57 -9
  109. package/src/api/new.ts +389 -122
  110. package/src/api/pre-publish.ts +13 -7
  111. package/src/api/rename.ts +103 -17
  112. package/src/api/templates/cargo.toml.ts +1 -0
  113. package/src/api/templates/ci-template.ts +150 -125
  114. package/src/api/templates/ci.yml.ts +27 -6
  115. package/src/api/templates/js-binding.ts +148 -220
  116. package/src/api/templates/load-wasi-template.ts +182 -0
  117. package/src/api/templates/package.json.ts +47 -5
  118. package/src/api/templates/wasi-worker-template.ts +122 -0
  119. package/src/api/universalize.ts +13 -8
  120. package/src/api/version.ts +5 -2
  121. package/src/commands/artifacts.ts +2 -2
  122. package/src/commands/build.ts +1 -1
  123. package/src/commands/new.ts +46 -45
  124. package/src/def/artifacts.ts +22 -3
  125. package/src/def/build.ts +55 -8
  126. package/src/def/create-npm-dirs.ts +9 -0
  127. package/src/def/new.ts +28 -3
  128. package/src/def/pre-publish.ts +12 -3
  129. package/src/def/rename.ts +11 -2
  130. package/src/def/universalize.ts +9 -0
  131. package/src/def/version.ts +9 -0
  132. package/src/utils/__tests__/__fixtures__/napi_type_def +130 -130
  133. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  134. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  135. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +28 -0
  136. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  137. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +316 -332
  138. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  139. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +8 -7
  140. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  141. package/src/utils/__tests__/config.spec.ts +81 -0
  142. package/src/utils/cargo.ts +2 -2
  143. package/src/utils/config.ts +110 -9
  144. package/src/utils/index.ts +1 -1
  145. package/src/utils/log.ts +1 -1
  146. package/src/utils/metadata.ts +52 -14
  147. package/src/utils/misc.ts +21 -12
  148. package/src/utils/read-config.ts +20 -0
  149. package/src/utils/target.ts +22 -4
  150. package/src/utils/typegen.ts +87 -52
  151. package/src/utils/version.ts +10 -8
  152. package/dist/api/load-wasi-template.d.ts +0 -1
  153. package/dist/api/load-wasi-template.js +0 -64
  154. package/dist/api/load-wasi-template.js.map +0 -1
  155. package/dist/api/wasi-worker-template.d.ts +0 -1
  156. package/dist/api/wasi-worker-template.js +0 -53
  157. package/dist/api/wasi-worker-template.js.map +0 -1
  158. package/dist/utils/ci.d.ts +0 -11
  159. package/dist/utils/ci.js +0 -96
  160. package/dist/utils/ci.js.map +0 -1
  161. package/src/api/load-wasi-template.ts +0 -66
  162. package/src/api/wasi-worker-template.ts +0 -52
  163. 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
+ })
@@ -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', 'pnpm'] 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
  */
@@ -29,6 +42,46 @@ interface UserNapiConfig {
29
42
  */
30
43
  constEnum?: boolean
31
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
+ * Whether to initialize wasm asynchronously
80
+ */
81
+ asyncInit?: boolean
82
+ }
83
+ }
84
+
32
85
  /**
33
86
  * @deprecated binaryName instead
34
87
  */
@@ -76,22 +129,34 @@ export interface CommonPackageJsonFields {
76
129
  main?: string
77
130
  module?: string
78
131
  types?: string
132
+ browser?: string
79
133
  exports?: any
80
134
 
81
135
  dependencies?: Record<string, string>
82
136
  devDependencies?: Record<string, string>
137
+
138
+ ava?: {
139
+ timeout?: string
140
+ }
83
141
  }
84
142
 
85
143
  export type NapiConfig = Required<
86
144
  Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
87
- > & {
88
- targets: Target[]
89
- packageJson: CommonPackageJsonFields
90
- }
145
+ > &
146
+ Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile' | 'constEnum'> & {
147
+ targets: Target[]
148
+ packageJson: CommonPackageJsonFields
149
+ }
91
150
 
92
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
151
+ export async function readNapiConfig(
152
+ path: string,
153
+ configPath?: string,
154
+ ): Promise<NapiConfig> {
155
+ if (configPath && !(await fileExists(configPath))) {
156
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
157
+ }
93
158
  if (!(await fileExists(path))) {
94
- throw new Error(`napi-rs config not found at ${path}`)
159
+ throw new Error(`package.json not found at ${path}`)
95
160
  }
96
161
  // May support multiple config sources later on.
97
162
  const content = await readFileAsync(path, 'utf8')
@@ -99,12 +164,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
99
164
  try {
100
165
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
101
166
  } catch (e) {
102
- throw new Error('Failed to parse napi-rs config', {
167
+ throw new Error(`Failed to parse package.json at ${path}`, {
103
168
  cause: e,
104
169
  })
105
170
  }
106
171
 
172
+ let separatedConfig: UserNapiConfig | undefined
173
+ if (configPath) {
174
+ const configContent = await readFileAsync(configPath, 'utf8')
175
+ try {
176
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
177
+ } catch (e) {
178
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
179
+ cause: e,
180
+ })
181
+ }
182
+ }
183
+
107
184
  const userNapiConfig = pkgJson.napi ?? {}
185
+ if (pkgJson.napi && separatedConfig) {
186
+ const pkgJsonPath = underline(path)
187
+ const configPathUnderline = underline(configPath!)
188
+ console.warn(
189
+ yellow(
190
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
191
+ ),
192
+ )
193
+ Object.assign(userNapiConfig, separatedConfig)
194
+ }
108
195
  const napiConfig: NapiConfig = merge(
109
196
  {
110
197
  binaryName: 'index',
@@ -120,16 +207,30 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
120
207
 
121
208
  // compatible with old config
122
209
  if (userNapiConfig?.name) {
123
- napiConfig.packageName = userNapiConfig.name
210
+ console.warn(
211
+ yellow(
212
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
213
+ ),
214
+ )
215
+ napiConfig.binaryName = userNapiConfig.name
124
216
  }
125
217
 
126
218
  if (!targets.length) {
219
+ let deprecatedWarned = false
220
+ const warning = yellow(
221
+ `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`,
222
+ )
127
223
  if (userNapiConfig.triples?.defaults) {
224
+ deprecatedWarned = true
225
+ console.warn(warning)
128
226
  targets = targets.concat(DEFAULT_TARGETS)
129
227
  }
130
228
 
131
229
  if (userNapiConfig.triples?.additional?.length) {
132
230
  targets = targets.concat(userNapiConfig.triples.additional)
231
+ if (!deprecatedWarned) {
232
+ console.warn(warning)
233
+ }
133
234
  }
134
235
  }
135
236
 
@@ -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'
@@ -7,3 +6,4 @@ export * from './metadata.js'
7
6
  export * from './config.js'
8
7
  export * from './cargo.js'
9
8
  export * from './typegen.js'
9
+ export * from './read-config.js'
package/src/utils/log.ts CHANGED
@@ -25,7 +25,7 @@ export const debugFactory = (namespace: string) => {
25
25
  console.error(
26
26
  colors.white(colors.bgRed(' ERROR ')),
27
27
  ...args.map((arg) =>
28
- arg instanceof Error ? arg.stack ?? arg.message : arg,
28
+ arg instanceof Error ? (arg.stack ?? arg.message) : arg,
29
29
  ),
30
30
  )
31
31
 
@@ -1,5 +1,5 @@
1
- import { execSync } from 'child_process'
2
- import fs from 'fs'
1
+ import { spawn } from 'node:child_process'
2
+ import fs from 'node:fs'
3
3
 
4
4
  export type CrateTargetKind =
5
5
  | 'bin'
@@ -26,6 +26,18 @@ export interface Crate {
26
26
  targets: CrateTarget[]
27
27
  features: Record<string, string[]>
28
28
  manifest_path: string
29
+ dependencies: Array<{
30
+ name: string
31
+ source: string
32
+ req: string
33
+ kind: string | null
34
+ rename: string | null
35
+ optional: boolean
36
+ uses_default_features: boolean
37
+ features: string[]
38
+ target: string | null
39
+ registry: string | null
40
+ }>
29
41
  }
30
42
 
31
43
  export interface CargoWorkspaceMetadata {
@@ -36,24 +48,50 @@ export interface CargoWorkspaceMetadata {
36
48
  workspace_root: string
37
49
  }
38
50
 
39
- export function parseMetadata(manifestPath: string) {
51
+ export async function parseMetadata(manifestPath: string) {
40
52
  if (!fs.existsSync(manifestPath)) {
41
53
  throw new Error(`No crate found in manifest: ${manifestPath}`)
42
54
  }
43
55
 
44
- const cmd = `cargo metadata --manifest-path ${manifestPath} --format-version 1 --no-deps`
56
+ const childProcess = spawn(
57
+ 'cargo',
58
+ ['metadata', '--manifest-path', manifestPath, '--format-version', '1'],
59
+ { stdio: 'pipe' },
60
+ )
45
61
 
46
- try {
47
- const output = execSync(cmd, {
48
- encoding: 'utf-8',
62
+ let stdout = ''
63
+ let stderr = ''
64
+ let status = 0
65
+ let error = null
66
+
67
+ childProcess.stdout.on('data', (data) => {
68
+ stdout += data
69
+ })
70
+
71
+ childProcess.stderr.on('data', (data) => {
72
+ stderr += data
73
+ })
74
+
75
+ await new Promise<void>((resolve) => {
76
+ childProcess.on('close', (code) => {
77
+ status = code ?? 0
78
+ resolve()
79
+ })
80
+ })
81
+
82
+ if (error) {
83
+ throw new Error('cargo metadata failed to run', { cause: error })
84
+ }
85
+ if (status !== 0) {
86
+ const simpleMessage = `cargo metadata exited with code ${status}`
87
+ throw new Error(`${simpleMessage} and error message:\n\n${stderr}`, {
88
+ cause: new Error(simpleMessage),
49
89
  })
50
- return JSON.parse(output) as CargoWorkspaceMetadata
90
+ }
91
+
92
+ try {
93
+ return JSON.parse(stdout) as CargoWorkspaceMetadata
51
94
  } catch (e) {
52
- throw new Error(
53
- `Failed to parse cargo metadata output by command: ${cmd}`,
54
- {
55
- cause: e,
56
- },
57
- )
95
+ throw new Error('Failed to parse cargo metadata JSON', { cause: e })
58
96
  }
59
97
  }
package/src/utils/misc.ts CHANGED
@@ -1,17 +1,17 @@
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 { promisify } from 'node:util'
4
11
 
12
+ import pkgJson from '../../package.json' with { type: 'json' }
5
13
  import { debug } from './log.js'
6
14
 
7
- const require = createRequire(import.meta.url)
8
- // NOTE:
9
- // import pkgJson from '@napi-rs/cli/package.json' assert { type: 'json' }
10
- // is experimental feature now, avoid using it.
11
- // see: https://nodejs.org/api/esm.html#import-assertions
12
- // eslint-disable-next-line import/no-extraneous-dependencies
13
- const pkgJson = require('@napi-rs/cli/package.json')
14
-
15
15
  export const readFileAsync = promisify(readFile)
16
16
  export const writeFileAsync = promisify(writeFile)
17
17
  export const unlinkAsync = promisify(unlink)
@@ -27,6 +27,15 @@ export async function fileExists(path: string) {
27
27
  return exists
28
28
  }
29
29
 
30
+ export async function dirExistsAsync(path: string) {
31
+ try {
32
+ const stats = await statAsync(path)
33
+ return stats.isDirectory()
34
+ } catch {
35
+ return false
36
+ }
37
+ }
38
+
30
39
  export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> {
31
40
  return keys.reduce((acc, key) => {
32
41
  acc[key] = o[key]
@@ -43,7 +52,7 @@ export async function updatePackageJson(
43
52
  debug(`File not exists ${path}`)
44
53
  return
45
54
  }
46
- const old = require(path)
55
+ const old = JSON.parse(await readFileAsync(path, 'utf8'))
47
56
  await writeFileAsync(path, JSON.stringify({ ...old, ...partial }, null, 2))
48
57
  }
49
58
 
@@ -0,0 +1,20 @@
1
+ import { resolve } from 'node:path'
2
+
3
+ import { readNapiConfig } from './config.js'
4
+
5
+ interface MinimalNapiOptions {
6
+ cwd: string
7
+ configPath?: string
8
+ packageJsonPath?: string
9
+ }
10
+
11
+ export async function readConfig(options: MinimalNapiOptions) {
12
+ const resolvePath = (...paths: string[]) => resolve(options.cwd, ...paths)
13
+ const config = await readNapiConfig(
14
+ resolvePath(
15
+ options.configPath ?? options.packageJsonPath ?? 'package.json',
16
+ ),
17
+ options.configPath ? resolvePath(options.configPath) : undefined,
18
+ )
19
+ return config
20
+ }
@@ -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',