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

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 (199) hide show
  1. package/cli.mjs +1 -1
  2. package/dist/api/__tests__/create-npm-dirs.spec.d.ts +1 -0
  3. package/dist/api/__tests__/new.spec.d.ts +1 -0
  4. package/dist/api/build.d.ts +1 -1
  5. package/dist/api/create-npm-dirs.d.ts +5 -0
  6. package/dist/api/templates/index.d.ts +0 -7
  7. package/dist/api/templates/js-binding.d.ts +1 -0
  8. package/dist/api/templates/load-wasi-template.d.ts +2 -0
  9. package/dist/api/templates/wasi-worker-template.d.ts +2 -0
  10. package/dist/cli.js +3961 -15
  11. package/dist/def/artifacts.d.ts +21 -1
  12. package/dist/def/build.d.ts +149 -4
  13. package/dist/def/create-npm-dirs.d.ts +10 -0
  14. package/dist/def/new.d.ts +25 -7
  15. package/dist/def/pre-publish.d.ts +21 -2
  16. package/dist/def/rename.d.ts +16 -6
  17. package/dist/def/universalize.d.ts +10 -0
  18. package/dist/def/version.d.ts +10 -0
  19. package/dist/index.cjs +10052 -15662
  20. package/dist/index.js +3712 -32
  21. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  22. package/dist/utils/config.d.ts +56 -4
  23. package/dist/utils/index.d.ts +1 -1
  24. package/dist/utils/metadata.d.ts +13 -1
  25. package/dist/utils/misc.d.ts +3 -3
  26. package/dist/utils/read-config.d.ts +7 -0
  27. package/dist/utils/target.d.ts +8 -6
  28. package/dist/utils/typegen.d.ts +1 -1
  29. package/dist/utils/version.d.ts +2 -1
  30. package/package.json +38 -34
  31. package/src/api/__tests__/create-npm-dirs.spec.ts +210 -0
  32. package/src/api/__tests__/new.spec.ts +429 -0
  33. package/src/api/artifacts.ts +81 -9
  34. package/src/api/build.ts +595 -183
  35. package/src/api/create-npm-dirs.ts +64 -13
  36. package/src/api/new.ts +389 -122
  37. package/src/api/pre-publish.ts +19 -11
  38. package/src/api/rename.ts +103 -17
  39. package/src/api/templates/index.ts +0 -7
  40. package/src/api/templates/js-binding.ts +159 -220
  41. package/src/api/templates/load-wasi-template.ts +216 -0
  42. package/src/api/templates/wasi-worker-template.ts +122 -0
  43. package/src/api/universalize.ts +13 -8
  44. package/src/api/version.ts +5 -2
  45. package/src/commands/artifacts.ts +2 -2
  46. package/src/commands/build.ts +1 -1
  47. package/src/commands/new.ts +46 -45
  48. package/src/def/artifacts.ts +22 -3
  49. package/src/def/build.ts +55 -8
  50. package/src/def/create-npm-dirs.ts +9 -0
  51. package/src/def/new.ts +28 -3
  52. package/src/def/pre-publish.ts +24 -3
  53. package/src/def/rename.ts +11 -2
  54. package/src/def/universalize.ts +9 -0
  55. package/src/def/version.ts +9 -0
  56. package/src/utils/__tests__/__fixtures__/napi_type_def +130 -130
  57. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  58. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  59. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +49 -0
  60. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  61. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +316 -332
  62. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  63. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +8 -7
  64. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  65. package/src/utils/__tests__/config.spec.ts +81 -0
  66. package/src/utils/cargo.ts +2 -2
  67. package/src/utils/config.ts +127 -9
  68. package/src/utils/index.ts +1 -1
  69. package/src/utils/log.ts +1 -1
  70. package/src/utils/metadata.ts +52 -14
  71. package/src/utils/misc.ts +21 -12
  72. package/src/utils/read-config.ts +20 -0
  73. package/src/utils/target.ts +37 -5
  74. package/src/utils/typegen.ts +87 -52
  75. package/src/utils/version.ts +10 -8
  76. package/dist/api/artifacts.js +0 -53
  77. package/dist/api/artifacts.js.map +0 -1
  78. package/dist/api/build.js +0 -498
  79. package/dist/api/build.js.map +0 -1
  80. package/dist/api/create-npm-dirs.js +0 -64
  81. package/dist/api/create-npm-dirs.js.map +0 -1
  82. package/dist/api/load-wasi-template.d.ts +0 -1
  83. package/dist/api/load-wasi-template.js +0 -60
  84. package/dist/api/load-wasi-template.js.map +0 -1
  85. package/dist/api/new.js +0 -155
  86. package/dist/api/new.js.map +0 -1
  87. package/dist/api/pre-publish.js +0 -158
  88. package/dist/api/pre-publish.js.map +0 -1
  89. package/dist/api/rename.js +0 -29
  90. package/dist/api/rename.js.map +0 -1
  91. package/dist/api/templates/.gitignore.d.ts +0 -1
  92. package/dist/api/templates/.gitignore.js +0 -200
  93. package/dist/api/templates/.gitignore.js.map +0 -1
  94. package/dist/api/templates/.npmignore.d.ts +0 -1
  95. package/dist/api/templates/.npmignore.js +0 -15
  96. package/dist/api/templates/.npmignore.js.map +0 -1
  97. package/dist/api/templates/build.rs.d.ts +0 -1
  98. package/dist/api/templates/build.rs.js +0 -5
  99. package/dist/api/templates/build.rs.js.map +0 -1
  100. package/dist/api/templates/cargo.toml.d.ts +0 -6
  101. package/dist/api/templates/cargo.toml.js +0 -26
  102. package/dist/api/templates/cargo.toml.js.map +0 -1
  103. package/dist/api/templates/ci-template.d.ts +0 -1
  104. package/dist/api/templates/ci-template.js +0 -578
  105. package/dist/api/templates/ci-template.js.map +0 -1
  106. package/dist/api/templates/ci.yml.d.ts +0 -1
  107. package/dist/api/templates/ci.yml.js +0 -99
  108. package/dist/api/templates/ci.yml.js.map +0 -1
  109. package/dist/api/templates/index.js +0 -9
  110. package/dist/api/templates/index.js.map +0 -1
  111. package/dist/api/templates/js-binding.js +0 -293
  112. package/dist/api/templates/js-binding.js.map +0 -1
  113. package/dist/api/templates/lib.rs.d.ts +0 -1
  114. package/dist/api/templates/lib.rs.js +0 -8
  115. package/dist/api/templates/lib.rs.js.map +0 -1
  116. package/dist/api/templates/package.json.d.ts +0 -8
  117. package/dist/api/templates/package.json.js +0 -32
  118. package/dist/api/templates/package.json.js.map +0 -1
  119. package/dist/api/universalize.js +0 -39
  120. package/dist/api/universalize.js.map +0 -1
  121. package/dist/api/version.js +0 -17
  122. package/dist/api/version.js.map +0 -1
  123. package/dist/api/wasi-worker-template.d.ts +0 -1
  124. package/dist/api/wasi-worker-template.js +0 -53
  125. package/dist/api/wasi-worker-template.js.map +0 -1
  126. package/dist/cli.js.map +0 -1
  127. package/dist/commands/artifacts.js +0 -20
  128. package/dist/commands/artifacts.js.map +0 -1
  129. package/dist/commands/build.js +0 -35
  130. package/dist/commands/build.js.map +0 -1
  131. package/dist/commands/create-npm-dirs.js +0 -8
  132. package/dist/commands/create-npm-dirs.js.map +0 -1
  133. package/dist/commands/help.js +0 -13
  134. package/dist/commands/help.js.map +0 -1
  135. package/dist/commands/new.js +0 -110
  136. package/dist/commands/new.js.map +0 -1
  137. package/dist/commands/pre-publish.js +0 -9
  138. package/dist/commands/pre-publish.js.map +0 -1
  139. package/dist/commands/rename.js +0 -8
  140. package/dist/commands/rename.js.map +0 -1
  141. package/dist/commands/universalize.js +0 -8
  142. package/dist/commands/universalize.js.map +0 -1
  143. package/dist/commands/version.js +0 -8
  144. package/dist/commands/version.js.map +0 -1
  145. package/dist/def/artifacts.js +0 -39
  146. package/dist/def/artifacts.js.map +0 -1
  147. package/dist/def/build.js +0 -118
  148. package/dist/def/build.js.map +0 -1
  149. package/dist/def/create-npm-dirs.js +0 -39
  150. package/dist/def/create-npm-dirs.js.map +0 -1
  151. package/dist/def/new.js +0 -67
  152. package/dist/def/new.js.map +0 -1
  153. package/dist/def/pre-publish.js +0 -57
  154. package/dist/def/pre-publish.js.map +0 -1
  155. package/dist/def/rename.js +0 -59
  156. package/dist/def/rename.js.map +0 -1
  157. package/dist/def/universalize.js +0 -34
  158. package/dist/def/universalize.js.map +0 -1
  159. package/dist/def/version.js +0 -34
  160. package/dist/def/version.js.map +0 -1
  161. package/dist/index.js.map +0 -1
  162. package/dist/utils/__tests__/target.spec.js +0 -11
  163. package/dist/utils/__tests__/target.spec.js.map +0 -1
  164. package/dist/utils/__tests__/typegen.spec.js +0 -42
  165. package/dist/utils/__tests__/typegen.spec.js.map +0 -1
  166. package/dist/utils/__tests__/version.spec.js +0 -6
  167. package/dist/utils/__tests__/version.spec.js.map +0 -1
  168. package/dist/utils/cargo.js +0 -34
  169. package/dist/utils/cargo.js.map +0 -1
  170. package/dist/utils/ci.d.ts +0 -11
  171. package/dist/utils/ci.js +0 -96
  172. package/dist/utils/ci.js.map +0 -1
  173. package/dist/utils/config.js +0 -43
  174. package/dist/utils/config.js.map +0 -1
  175. package/dist/utils/index.js +0 -10
  176. package/dist/utils/index.js.map +0 -1
  177. package/dist/utils/log.js +0 -15
  178. package/dist/utils/log.js.map +0 -1
  179. package/dist/utils/metadata.js +0 -20
  180. package/dist/utils/metadata.js.map +0 -1
  181. package/dist/utils/misc.js +0 -41
  182. package/dist/utils/misc.js.map +0 -1
  183. package/dist/utils/target.js +0 -121
  184. package/dist/utils/target.js.map +0 -1
  185. package/dist/utils/typegen.js +0 -179
  186. package/dist/utils/typegen.js.map +0 -1
  187. package/dist/utils/version.js +0 -60
  188. package/dist/utils/version.js.map +0 -1
  189. package/src/api/load-wasi-template.ts +0 -62
  190. package/src/api/templates/.gitignore.ts +0 -199
  191. package/src/api/templates/.npmignore.ts +0 -14
  192. package/src/api/templates/build.rs.ts +0 -4
  193. package/src/api/templates/cargo.toml.ts +0 -35
  194. package/src/api/templates/ci-template.ts +0 -577
  195. package/src/api/templates/ci.yml.ts +0 -119
  196. package/src/api/templates/lib.rs.ts +0 -7
  197. package/src/api/templates/package.json.ts +0 -48
  198. package/src/api/wasi-worker-template.ts +0 -52
  199. 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,50 @@ 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
+ * Whether to inject `buffer` to emnapi context
84
+ */
85
+ buffer?: boolean
86
+ }
87
+ }
88
+
32
89
  /**
33
90
  * @deprecated binaryName instead
34
91
  */
@@ -62,6 +119,10 @@ export interface CommonPackageJsonFields {
62
119
  author?: string
63
120
  authors?: string[]
64
121
  license?: string
122
+ cpu?: string[]
123
+ os?: string[]
124
+ libc?: string[]
125
+ files?: string[]
65
126
  repository?: any
66
127
  homepage?: any
67
128
  engines?: Record<string, string>
@@ -76,22 +137,34 @@ export interface CommonPackageJsonFields {
76
137
  main?: string
77
138
  module?: string
78
139
  types?: string
140
+ browser?: string
79
141
  exports?: any
80
142
 
81
143
  dependencies?: Record<string, string>
82
144
  devDependencies?: Record<string, string>
145
+
146
+ ava?: {
147
+ timeout?: string
148
+ }
83
149
  }
84
150
 
85
151
  export type NapiConfig = Required<
86
152
  Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>
87
- > & {
88
- targets: Target[]
89
- packageJson: CommonPackageJsonFields
90
- }
153
+ > &
154
+ Pick<UserNapiConfig, 'wasm' | 'dtsHeader' | 'dtsHeaderFile' | 'constEnum'> & {
155
+ targets: Target[]
156
+ packageJson: CommonPackageJsonFields
157
+ }
91
158
 
92
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
159
+ export async function readNapiConfig(
160
+ path: string,
161
+ configPath?: string,
162
+ ): Promise<NapiConfig> {
163
+ if (configPath && !(await fileExists(configPath))) {
164
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
165
+ }
93
166
  if (!(await fileExists(path))) {
94
- throw new Error(`napi-rs config not found at ${path}`)
167
+ throw new Error(`package.json not found at ${path}`)
95
168
  }
96
169
  // May support multiple config sources later on.
97
170
  const content = await readFileAsync(path, 'utf8')
@@ -99,12 +172,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
99
172
  try {
100
173
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
101
174
  } catch (e) {
102
- throw new Error('Failed to parse napi-rs config', {
175
+ throw new Error(`Failed to parse package.json at ${path}`, {
103
176
  cause: e,
104
177
  })
105
178
  }
106
179
 
180
+ let separatedConfig: UserNapiConfig | undefined
181
+ if (configPath) {
182
+ const configContent = await readFileAsync(configPath, 'utf8')
183
+ try {
184
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
185
+ } catch (e) {
186
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
187
+ cause: e,
188
+ })
189
+ }
190
+ }
191
+
107
192
  const userNapiConfig = pkgJson.napi ?? {}
193
+ if (pkgJson.napi && separatedConfig) {
194
+ const pkgJsonPath = underline(path)
195
+ const configPathUnderline = underline(configPath!)
196
+ console.warn(
197
+ yellow(
198
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
199
+ ),
200
+ )
201
+ Object.assign(userNapiConfig, separatedConfig)
202
+ }
108
203
  const napiConfig: NapiConfig = merge(
109
204
  {
110
205
  binaryName: 'index',
@@ -120,19 +215,42 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
120
215
 
121
216
  // compatible with old config
122
217
  if (userNapiConfig?.name) {
123
- napiConfig.packageName = userNapiConfig.name
218
+ console.warn(
219
+ yellow(
220
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
221
+ ),
222
+ )
223
+ napiConfig.binaryName = userNapiConfig.name
124
224
  }
125
225
 
126
226
  if (!targets.length) {
227
+ let deprecatedWarned = false
228
+ const warning = yellow(
229
+ `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`,
230
+ )
127
231
  if (userNapiConfig.triples?.defaults) {
232
+ deprecatedWarned = true
233
+ console.warn(warning)
128
234
  targets = targets.concat(DEFAULT_TARGETS)
129
235
  }
130
236
 
131
237
  if (userNapiConfig.triples?.additional?.length) {
132
238
  targets = targets.concat(userNapiConfig.triples.additional)
239
+ if (!deprecatedWarned) {
240
+ console.warn(warning)
241
+ }
133
242
  }
134
243
  }
135
244
 
245
+ // find duplicate targets
246
+ const uniqueTargets = new Set(targets)
247
+ if (uniqueTargets.size !== targets.length) {
248
+ const duplicateTarget = targets.find(
249
+ (target, index) => targets.indexOf(target) !== index,
250
+ )
251
+ throw new Error(`Duplicate targets are not allowed: ${duplicateTarget}`)
252
+ }
253
+
136
254
  napiConfig.targets = targets.map(parseTriple)
137
255
 
138
256
  return napiConfig
@@ -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,29 +1,41 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
2
 
3
- export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
3
+ export type Platform = NodeJS.Platform | 'wasm' | 'wasi' | 'openharmony'
4
+
5
+ export const UNIVERSAL_TARGETS = {
6
+ 'universal-apple-darwin': ['aarch64-apple-darwin', 'x86_64-apple-darwin'],
7
+ } as const
4
8
 
5
9
  export const AVAILABLE_TARGETS = [
6
10
  'aarch64-apple-darwin',
7
11
  'aarch64-linux-android',
8
12
  'aarch64-unknown-linux-gnu',
9
13
  'aarch64-unknown-linux-musl',
14
+ 'aarch64-unknown-linux-ohos',
10
15
  'aarch64-pc-windows-msvc',
11
16
  'x86_64-apple-darwin',
12
17
  'x86_64-pc-windows-msvc',
13
18
  'x86_64-unknown-linux-gnu',
14
19
  'x86_64-unknown-linux-musl',
20
+ 'x86_64-unknown-linux-ohos',
15
21
  'x86_64-unknown-freebsd',
16
22
  'i686-pc-windows-msvc',
17
23
  'armv7-unknown-linux-gnueabihf',
24
+ 'armv7-unknown-linux-musleabihf',
18
25
  'armv7-linux-androideabi',
19
26
  'universal-apple-darwin',
20
27
  'riscv64gc-unknown-linux-gnu',
28
+ 'powerpc64le-unknown-linux-gnu',
29
+ 's390x-unknown-linux-gnu',
30
+ 'wasm32-wasi-preview1-threads',
31
+ 'wasm32-wasip1-threads',
21
32
  ] as const
22
33
 
23
34
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
24
35
 
25
36
  export const DEFAULT_TARGETS = [
26
37
  'x86_64-apple-darwin',
38
+ 'aarch64-apple-darwin',
27
39
  'x86_64-pc-windows-msvc',
28
40
  'x86_64-unknown-linux-gnu',
29
41
  ] as const
@@ -31,6 +43,8 @@ export const DEFAULT_TARGETS = [
31
43
  export const TARGET_LINKER: Record<string, string> = {
32
44
  'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
33
45
  'riscv64gc-unknown-linux-gnu': 'riscv64-linux-gnu-gcc',
46
+ 'powerpc64le-unknown-linux-gnu': 'powerpc64le-linux-gnu-gcc',
47
+ 's390x-unknown-linux-gnu': 's390x-linux-gnu-gcc',
34
48
  }
35
49
 
36
50
  // https://nodejs.org/api/process.html#process_process_arch
@@ -48,6 +62,7 @@ type NodeJSArch =
48
62
  | 'x32'
49
63
  | 'x64'
50
64
  | 'universal'
65
+ | 'wasm32'
51
66
 
52
67
  const CpuToNodeArch: Record<string, NodeJSArch> = {
53
68
  x86_64: 'x64',
@@ -55,6 +70,7 @@ const CpuToNodeArch: Record<string, NodeJSArch> = {
55
70
  i686: 'ia32',
56
71
  armv7: 'arm',
57
72
  riscv64gc: 'riscv64',
73
+ powerpc64le: 'ppc64',
58
74
  }
59
75
 
60
76
  export const NodeArchToCpu: Record<string, string> = {
@@ -63,6 +79,7 @@ export const NodeArchToCpu: Record<string, string> = {
63
79
  ia32: 'i686',
64
80
  arm: 'armv7',
65
81
  riscv64: 'riscv64gc',
82
+ ppc64: 'powerpc64le',
66
83
  }
67
84
 
68
85
  const SysToNodePlatform: Record<string, Platform> = {
@@ -70,6 +87,7 @@ const SysToNodePlatform: Record<string, Platform> = {
70
87
  freebsd: 'freebsd',
71
88
  darwin: 'darwin',
72
89
  windows: 'win32',
90
+ ohos: 'openharmony',
73
91
  }
74
92
 
75
93
  export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
@@ -80,7 +98,7 @@ export interface Target {
80
98
  triple: string
81
99
  platformArchABI: string
82
100
  platform: Platform
83
- arch: NodeJSArch | 'wasm32'
101
+ arch: NodeJSArch
84
102
  abi: string | null
85
103
  }
86
104
 
@@ -95,10 +113,14 @@ export interface Target {
95
113
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
96
114
  */
97
115
  export function parseTriple(rawTriple: string): Target {
98
- if (rawTriple === 'wasm32-wasi-preview1-threads') {
116
+ if (
117
+ rawTriple === 'wasm32-wasi' ||
118
+ rawTriple === 'wasm32-wasi-preview1-threads' ||
119
+ rawTriple.startsWith('wasm32-wasip')
120
+ ) {
99
121
  return {
100
122
  triple: rawTriple,
101
- platformArchABI: rawTriple,
123
+ platformArchABI: 'wasm32-wasi',
102
124
  platform: 'wasi',
103
125
  arch: 'wasm32',
104
126
  abi: 'wasi',
@@ -125,6 +147,16 @@ export function parseTriple(rawTriple: string): Target {
125
147
 
126
148
  const platform = SysToNodePlatform[sys] ?? (sys as Platform)
127
149
  const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
150
+
151
+ if (rawTriple.includes('ohos')) {
152
+ return {
153
+ triple: rawTriple,
154
+ platformArchABI: `linux-${arch}-ohos`,
155
+ platform: 'openharmony',
156
+ arch,
157
+ abi: null,
158
+ }
159
+ }
128
160
  return {
129
161
  triple: rawTriple,
130
162
  platformArchABI: abi ? `${platform}-${arch}-${abi}` : `${platform}-${arch}`,