@napi-rs/cli 3.0.0-alpha.2 → 3.0.0-alpha.20

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 (201) hide show
  1. package/dist/api/artifacts.js +5 -2
  2. package/dist/api/artifacts.js.map +1 -0
  3. package/dist/api/build.d.ts +1 -1
  4. package/dist/api/build.js +182 -32
  5. package/dist/api/build.js.map +1 -0
  6. package/dist/api/create-npm-dirs.js +3 -2
  7. package/dist/api/create-npm-dirs.js.map +1 -0
  8. package/dist/api/load-wasi-template.d.ts +1 -0
  9. package/dist/api/load-wasi-template.js +60 -0
  10. package/dist/api/load-wasi-template.js.map +1 -0
  11. package/dist/api/new.js +31 -12
  12. package/dist/api/new.js.map +1 -0
  13. package/dist/api/pre-publish.js +4 -3
  14. package/dist/api/pre-publish.js.map +1 -0
  15. package/dist/api/rename.js +9 -0
  16. package/dist/api/rename.js.map +1 -0
  17. package/dist/api/templates/.gitignore.d.ts +1 -1
  18. package/dist/api/templates/.gitignore.js +2 -0
  19. package/dist/api/templates/.gitignore.js.map +1 -0
  20. package/dist/api/templates/.npmignore.js +1 -0
  21. package/dist/api/templates/.npmignore.js.map +1 -0
  22. package/dist/api/templates/build.rs.js +1 -0
  23. package/dist/api/templates/build.rs.js.map +1 -0
  24. package/dist/api/templates/cargo.toml.js +1 -0
  25. package/dist/api/templates/cargo.toml.js.map +1 -0
  26. package/dist/api/templates/ci-template.js +64 -57
  27. package/dist/api/templates/ci-template.js.map +1 -0
  28. package/dist/api/templates/ci.yml.d.ts +1 -1
  29. package/dist/api/templates/ci.yml.js +10 -3
  30. package/dist/api/templates/ci.yml.js.map +1 -0
  31. package/dist/api/templates/index.js +1 -0
  32. package/dist/api/templates/index.js.map +1 -0
  33. package/dist/api/templates/js-binding.d.ts +1 -1
  34. package/dist/api/templates/js-binding.js +306 -108
  35. package/dist/api/templates/js-binding.js.map +1 -0
  36. package/dist/api/templates/lib.rs.js +2 -4
  37. package/dist/api/templates/lib.rs.js.map +1 -0
  38. package/dist/api/templates/package.json.js +30 -28
  39. package/dist/api/templates/package.json.js.map +1 -0
  40. package/dist/api/universalize.js +2 -1
  41. package/dist/api/universalize.js.map +1 -0
  42. package/dist/api/version.js +2 -1
  43. package/dist/api/version.js.map +1 -0
  44. package/dist/api/wasi-worker-template.d.ts +1 -0
  45. package/dist/api/wasi-worker-template.js +53 -0
  46. package/dist/api/wasi-worker-template.js.map +1 -0
  47. package/dist/cli.js +1 -0
  48. package/dist/cli.js.map +1 -0
  49. package/dist/commands/artifacts.js +1 -0
  50. package/dist/commands/artifacts.js.map +1 -0
  51. package/dist/commands/build.js +1 -0
  52. package/dist/commands/build.js.map +1 -0
  53. package/dist/commands/create-npm-dirs.js +1 -0
  54. package/dist/commands/create-npm-dirs.js.map +1 -0
  55. package/dist/commands/help.js +1 -0
  56. package/dist/commands/help.js.map +1 -0
  57. package/dist/commands/new.js +2 -4
  58. package/dist/commands/new.js.map +1 -0
  59. package/dist/commands/pre-publish.js +1 -0
  60. package/dist/commands/pre-publish.js.map +1 -0
  61. package/dist/commands/rename.js +1 -0
  62. package/dist/commands/rename.js.map +1 -0
  63. package/dist/commands/universalize.js +1 -0
  64. package/dist/commands/universalize.js.map +1 -0
  65. package/dist/commands/version.js +1 -0
  66. package/dist/commands/version.js.map +1 -0
  67. package/dist/def/artifacts.d.ts +11 -1
  68. package/dist/def/artifacts.js +7 -2
  69. package/dist/def/artifacts.js.map +1 -0
  70. package/dist/def/build.d.ts +32 -2
  71. package/dist/def/build.js +23 -2
  72. package/dist/def/build.js.map +1 -0
  73. package/dist/def/create-npm-dirs.d.ts +10 -0
  74. package/dist/def/create-npm-dirs.js +5 -0
  75. package/dist/def/create-npm-dirs.js.map +1 -0
  76. package/dist/def/new.d.ts +2 -2
  77. package/dist/def/new.js +1 -0
  78. package/dist/def/new.js.map +1 -0
  79. package/dist/def/pre-publish.d.ts +10 -0
  80. package/dist/def/pre-publish.js +7 -2
  81. package/dist/def/pre-publish.js.map +1 -0
  82. package/dist/def/rename.d.ts +11 -1
  83. package/dist/def/rename.js +6 -1
  84. package/dist/def/rename.js.map +1 -0
  85. package/dist/def/universalize.d.ts +10 -0
  86. package/dist/def/universalize.js +5 -0
  87. package/dist/def/universalize.js.map +1 -0
  88. package/dist/def/version.d.ts +10 -0
  89. package/dist/def/version.js +5 -0
  90. package/dist/def/version.js.map +1 -0
  91. package/dist/index.cjs +3424 -8044
  92. package/dist/index.d.ts +1 -0
  93. package/dist/index.js +2 -0
  94. package/dist/index.js.map +1 -0
  95. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  96. package/dist/utils/__tests__/config.spec.js +63 -0
  97. package/dist/utils/__tests__/config.spec.js.map +1 -0
  98. package/dist/utils/__tests__/target.spec.js +1 -0
  99. package/dist/utils/__tests__/target.spec.js.map +1 -0
  100. package/dist/utils/__tests__/typegen.spec.js +9 -4
  101. package/dist/utils/__tests__/typegen.spec.js.map +1 -0
  102. package/dist/utils/__tests__/version.spec.js +1 -0
  103. package/dist/utils/__tests__/version.spec.js.map +1 -0
  104. package/dist/utils/cargo.js +1 -0
  105. package/dist/utils/cargo.js.map +1 -0
  106. package/dist/utils/ci.js +10 -0
  107. package/dist/utils/ci.js.map +1 -0
  108. package/dist/utils/config.d.ts +16 -5
  109. package/dist/utils/config.js +37 -6
  110. package/dist/utils/config.js.map +1 -0
  111. package/dist/utils/index.js +1 -0
  112. package/dist/utils/index.js.map +1 -0
  113. package/dist/utils/log.js +1 -0
  114. package/dist/utils/log.js.map +1 -0
  115. package/dist/utils/metadata.js +1 -0
  116. package/dist/utils/metadata.js.map +1 -0
  117. package/dist/utils/misc.js +1 -0
  118. package/dist/utils/misc.js.map +1 -0
  119. package/dist/utils/target.d.ts +6 -5
  120. package/dist/utils/target.js +15 -1
  121. package/dist/utils/target.js.map +1 -0
  122. package/dist/utils/typegen.d.ts +4 -1
  123. package/dist/utils/typegen.js +27 -6
  124. package/dist/utils/typegen.js.map +1 -0
  125. package/dist/utils/version.d.ts +2 -1
  126. package/dist/utils/version.js +13 -10
  127. package/dist/utils/version.js.map +1 -0
  128. package/package.json +48 -15
  129. package/src/api/artifacts.ts +7 -4
  130. package/src/api/build.ts +283 -39
  131. package/src/api/create-npm-dirs.ts +5 -2
  132. package/src/api/load-wasi-template.ts +62 -0
  133. package/src/api/new.ts +37 -14
  134. package/src/api/pre-publish.ts +14 -8
  135. package/src/api/rename.ts +9 -0
  136. package/src/api/templates/.gitignore.ts +1 -0
  137. package/src/api/templates/ci-template.ts +63 -57
  138. package/src/api/templates/ci.yml.ts +9 -6
  139. package/src/api/templates/js-binding.ts +299 -104
  140. package/src/api/templates/lib.rs.ts +1 -4
  141. package/src/api/templates/package.json.ts +31 -27
  142. package/src/api/universalize.ts +4 -1
  143. package/src/api/version.ts +4 -1
  144. package/src/api/wasi-worker-template.ts +52 -0
  145. package/src/commands/new.ts +1 -5
  146. package/src/def/artifacts.ts +12 -3
  147. package/src/def/build.ts +51 -4
  148. package/src/def/create-npm-dirs.ts +9 -0
  149. package/src/def/new.ts +1 -1
  150. package/src/def/pre-publish.ts +11 -2
  151. package/src/def/rename.ts +11 -2
  152. package/src/def/universalize.ts +9 -0
  153. package/src/def/version.ts +9 -0
  154. package/src/index.ts +2 -0
  155. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  156. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  157. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +7 -0
  158. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  159. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +555 -0
  160. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  161. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +9 -8
  162. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  163. package/src/utils/__tests__/config.spec.ts +81 -0
  164. package/src/utils/__tests__/typegen.spec.ts +19 -4
  165. package/src/utils/ci.ts +9 -0
  166. package/src/utils/config.ts +64 -9
  167. package/src/utils/target.ts +22 -8
  168. package/src/utils/typegen.ts +30 -5
  169. package/src/utils/version.ts +11 -9
  170. package/dist/def/commands/artifacts.d.ts +0 -15
  171. package/dist/def/commands/artifacts.js +0 -29
  172. package/dist/def/commands/build.d.ts +0 -53
  173. package/dist/def/commands/build.js +0 -105
  174. package/dist/def/commands/create-npm-dirs.d.ts +0 -15
  175. package/dist/def/commands/create-npm-dirs.js +0 -29
  176. package/dist/def/commands/new.d.ts +0 -27
  177. package/dist/def/commands/new.js +0 -53
  178. package/dist/def/commands/pre-publish.d.ts +0 -23
  179. package/dist/def/commands/pre-publish.js +0 -45
  180. package/dist/def/commands/rename.d.ts +0 -25
  181. package/dist/def/commands/rename.js +0 -49
  182. package/dist/def/commands/universalize.d.ts +0 -13
  183. package/dist/def/commands/universalize.js +0 -25
  184. package/dist/def/commands/version.d.ts +0 -13
  185. package/dist/def/commands/version.js +0 -25
  186. package/dist/def/options/artifacts.d.ts +0 -35
  187. package/dist/def/options/artifacts.js +0 -11
  188. package/dist/def/options/build.d.ts +0 -97
  189. package/dist/def/options/build.js +0 -3
  190. package/dist/def/options/create-npm-dirs.d.ts +0 -35
  191. package/dist/def/options/create-npm-dirs.js +0 -11
  192. package/dist/def/options/new.d.ts +0 -79
  193. package/dist/def/options/new.js +0 -15
  194. package/dist/def/options/pre-publish.d.ts +0 -65
  195. package/dist/def/options/pre-publish.js +0 -13
  196. package/dist/def/options/rename.d.ts +0 -75
  197. package/dist/def/options/rename.js +0 -11
  198. package/dist/def/options/universalize.d.ts +0 -28
  199. package/dist/def/options/universalize.js +0 -10
  200. package/dist/def/options/version.d.ts +0 -28
  201. 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)
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,10 @@
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 interface UserNapiConfig {
7
8
  /**
8
9
  * Name of the binary to be generated, default to `index`
9
10
  */
@@ -24,6 +25,11 @@ interface UserNapiConfig {
24
25
  */
25
26
  npmClient?: string
26
27
 
28
+ /**
29
+ * Whether generate const enum for typescript bindings
30
+ */
31
+ constEnum?: boolean
32
+
27
33
  /**
28
34
  * @deprecated binaryName instead
29
35
  */
@@ -41,7 +47,7 @@ interface UserNapiConfig {
41
47
  /**
42
48
  * Whether enable default targets
43
49
  */
44
- default: boolean
50
+ defaults: boolean
45
51
  /**
46
52
  * Additional targets to be compiled for
47
53
  */
@@ -49,7 +55,7 @@ interface UserNapiConfig {
49
55
  }
50
56
  }
51
57
 
52
- interface CommonPackageJsonFields {
58
+ export interface CommonPackageJsonFields {
53
59
  name: string
54
60
  version: string
55
61
  description?: string
@@ -64,6 +70,17 @@ interface CommonPackageJsonFields {
64
70
  bugs?: any
65
71
  // eslint-disable-next-line no-use-before-define
66
72
  napi?: UserNapiConfig
73
+ type?: 'module' | 'commonjs'
74
+ scripts?: Record<string, string>
75
+
76
+ // modules
77
+ main?: string
78
+ module?: string
79
+ types?: string
80
+ exports?: any
81
+
82
+ dependencies?: Record<string, string>
83
+ devDependencies?: Record<string, string>
67
84
  }
68
85
 
69
86
  export type NapiConfig = Required<
@@ -73,9 +90,15 @@ export type NapiConfig = Required<
73
90
  packageJson: CommonPackageJsonFields
74
91
  }
75
92
 
76
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
93
+ export async function readNapiConfig(
94
+ path: string,
95
+ configPath?: string,
96
+ ): Promise<NapiConfig> {
97
+ if (configPath && !(await fileExists(configPath))) {
98
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
99
+ }
77
100
  if (!(await fileExists(path))) {
78
- throw new Error(`napi-rs config not found at ${path}`)
101
+ throw new Error(`package.json not found at ${path}`)
79
102
  }
80
103
  // May support multiple config sources later on.
81
104
  const content = await readFileAsync(path, 'utf8')
@@ -83,12 +106,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
83
106
  try {
84
107
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
85
108
  } catch (e) {
86
- throw new Error('Failed to parse napi-rs config', {
109
+ throw new Error(`Failed to parse package.json at ${path}`, {
87
110
  cause: e,
88
111
  })
89
112
  }
90
113
 
114
+ let separatedConfig: UserNapiConfig | undefined
115
+ if (configPath) {
116
+ const configContent = await readFileAsync(configPath, 'utf8')
117
+ try {
118
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
119
+ } catch (e) {
120
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
121
+ cause: e,
122
+ })
123
+ }
124
+ }
125
+
91
126
  const userNapiConfig = pkgJson.napi ?? {}
127
+ if (pkgJson.napi && separatedConfig) {
128
+ const pkgJsonPath = underline(path)
129
+ const configPathUnderline = underline(configPath!)
130
+ console.warn(
131
+ yellow(
132
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
133
+ ),
134
+ )
135
+ Object.assign(userNapiConfig, separatedConfig)
136
+ }
92
137
  const napiConfig: NapiConfig = merge(
93
138
  {
94
139
  binaryName: 'index',
@@ -103,17 +148,27 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
103
148
  let targets: string[] = userNapiConfig.targets ?? []
104
149
 
105
150
  // compatible with old config
106
- if (userNapiConfig.package?.name) {
107
- napiConfig.packageName = userNapiConfig.package.name
151
+ if (userNapiConfig?.name) {
152
+ console.warn(
153
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
154
+ )
155
+ napiConfig.binaryName = userNapiConfig.name
108
156
  }
109
157
 
110
158
  if (!targets.length) {
111
- if (userNapiConfig.triples?.default) {
159
+ let deprecatedWarned = false
160
+ const warning = `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`
161
+ if (userNapiConfig.triples?.defaults) {
162
+ deprecatedWarned = true
163
+ console.warn(warning)
112
164
  targets = targets.concat(DEFAULT_TARGETS)
113
165
  }
114
166
 
115
167
  if (userNapiConfig.triples?.additional?.length) {
116
168
  targets = targets.concat(userNapiConfig.triples.additional)
169
+ if (!deprecatedWarned) {
170
+ console.warn(warning)
171
+ }
117
172
  }
118
173
  }
119
174
 
@@ -1,4 +1,6 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
+
3
+ export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
2
4
 
3
5
  export const AVAILABLE_TARGETS = [
4
6
  'aarch64-apple-darwin',
@@ -15,6 +17,7 @@ export const AVAILABLE_TARGETS = [
15
17
  'armv7-unknown-linux-gnueabihf',
16
18
  'armv7-linux-androideabi',
17
19
  'universal-apple-darwin',
20
+ 'riscv64gc-unknown-linux-gnu',
18
21
  ] as const
19
22
 
20
23
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
@@ -27,6 +30,7 @@ export const DEFAULT_TARGETS = [
27
30
 
28
31
  export const TARGET_LINKER: Record<string, string> = {
29
32
  'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
33
+ 'riscv64gc-unknown-linux-gnu': 'riscv64-linux-gnu-gcc',
30
34
  }
31
35
 
32
36
  // https://nodejs.org/api/process.html#process_process_arch
@@ -38,6 +42,7 @@ type NodeJSArch =
38
42
  | 'mipsel'
39
43
  | 'ppc'
40
44
  | 'ppc64'
45
+ | 'riscv64'
41
46
  | 's390'
42
47
  | 's390x'
43
48
  | 'x32'
@@ -49,6 +54,7 @@ const CpuToNodeArch: Record<string, NodeJSArch> = {
49
54
  aarch64: 'arm64',
50
55
  i686: 'ia32',
51
56
  armv7: 'arm',
57
+ riscv64gc: 'riscv64',
52
58
  }
53
59
 
54
60
  export const NodeArchToCpu: Record<string, string> = {
@@ -56,26 +62,25 @@ export const NodeArchToCpu: Record<string, string> = {
56
62
  arm64: 'aarch64',
57
63
  ia32: 'i686',
58
64
  arm: 'armv7',
65
+ riscv64: 'riscv64gc',
59
66
  }
60
67
 
61
- const SysToNodePlatform: Record<string, NodeJS.Platform> = {
68
+ const SysToNodePlatform: Record<string, Platform> = {
62
69
  linux: 'linux',
63
70
  freebsd: 'freebsd',
64
71
  darwin: 'darwin',
65
72
  windows: 'win32',
66
73
  }
67
74
 
68
- export const UniArchsByPlatform: Partial<
69
- Record<NodeJS.Platform, NodeJSArch[]>
70
- > = {
75
+ export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
71
76
  darwin: ['x64', 'arm64'],
72
77
  }
73
78
 
74
79
  export interface Target {
75
80
  triple: string
76
81
  platformArchABI: string
77
- platform: NodeJS.Platform
78
- arch: NodeJSArch
82
+ platform: Platform
83
+ arch: NodeJSArch | 'wasm32'
79
84
  abi: string | null
80
85
  }
81
86
 
@@ -90,6 +95,15 @@ export interface Target {
90
95
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
91
96
  */
92
97
  export function parseTriple(rawTriple: string): Target {
98
+ if (rawTriple === 'wasm32-wasi-preview1-threads') {
99
+ return {
100
+ triple: rawTriple,
101
+ platformArchABI: rawTriple,
102
+ platform: 'wasi',
103
+ arch: 'wasm32',
104
+ abi: 'wasi',
105
+ }
106
+ }
93
107
  const triple = rawTriple.endsWith('eabi')
94
108
  ? `${rawTriple.slice(0, -4)}-eabi`
95
109
  : rawTriple
@@ -109,7 +123,7 @@ export function parseTriple(rawTriple: string): Target {
109
123
  ;[cpu, , sys, abi = null] = triples
110
124
  }
111
125
 
112
- const platform = SysToNodePlatform[sys] ?? (sys as NodeJS.Platform)
126
+ const platform = SysToNodePlatform[sys] ?? (sys as Platform)
113
127
  const arch = CpuToNodeArch[cpu] ?? (cpu as NodeJSArch)
114
128
  return {
115
129
  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
- }
@@ -1,15 +0,0 @@
1
- import { Command } from 'clipanion';
2
- export declare abstract class BaseCreateNpmDirsCommand extends Command {
3
- static paths: string[][];
4
- static usage: import("clipanion").Usage;
5
- cwd: string;
6
- packageJsonPath: string;
7
- npmDir: string;
8
- dryRun: boolean;
9
- getOptions(): {
10
- cwd: string;
11
- packageJsonPath: string;
12
- npmDir: string;
13
- dryRun: boolean;
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 BaseCreateNpmDirsCommand extends Command {
5
- static paths = [['create-npm-dirs']];
6
- static usage = Command.Usage({
7
- description: 'Create npm package dirs for different platforms',
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
- npmDir = Option.String('--npm-dir', 'npm', {
16
- description: 'Path to the folder where the npm packages put',
17
- });
18
- dryRun = Option.Boolean('--dry-run', false, {
19
- description: 'Dry run without touching file system',
20
- });
21
- getOptions() {
22
- return {
23
- cwd: this.cwd,
24
- packageJsonPath: this.packageJsonPath,
25
- npmDir: this.npmDir,
26
- dryRun: this.dryRun,
27
- };
28
- }
29
- }