@napi-rs/cli 3.0.0-alpha.1 → 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 (176) 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 +2 -2
  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 +2 -1
  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 -14
  11. package/dist/def/artifacts.d.ts +21 -1
  12. package/dist/def/build.d.ts +167 -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 +10142 -20524
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.js +3711 -29
  22. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  23. package/dist/utils/config.d.ts +70 -6
  24. package/dist/utils/index.d.ts +1 -1
  25. package/dist/utils/metadata.d.ts +13 -1
  26. package/dist/utils/misc.d.ts +3 -3
  27. package/dist/utils/read-config.d.ts +7 -0
  28. package/dist/utils/target.d.ts +9 -6
  29. package/dist/utils/typegen.d.ts +4 -1
  30. package/dist/utils/version.d.ts +2 -1
  31. package/package.json +60 -24
  32. package/src/api/__tests__/create-npm-dirs.spec.ts +210 -0
  33. package/src/api/__tests__/new.spec.ts +429 -0
  34. package/src/api/artifacts.ts +83 -8
  35. package/src/api/build.ts +682 -134
  36. package/src/api/create-npm-dirs.ts +64 -13
  37. package/src/api/new.ts +389 -125
  38. package/src/api/pre-publish.ts +27 -16
  39. package/src/api/rename.ts +103 -17
  40. package/src/api/templates/index.ts +0 -7
  41. package/src/api/templates/js-binding.ts +212 -107
  42. package/src/api/templates/load-wasi-template.ts +216 -0
  43. package/src/api/templates/wasi-worker-template.ts +122 -0
  44. package/src/api/universalize.ts +13 -8
  45. package/src/api/version.ts +5 -2
  46. package/src/commands/artifacts.ts +2 -2
  47. package/src/commands/build.ts +1 -1
  48. package/src/commands/new.ts +46 -49
  49. package/src/def/artifacts.ts +22 -3
  50. package/src/def/build.ts +83 -8
  51. package/src/def/create-npm-dirs.ts +9 -0
  52. package/src/def/new.ts +28 -3
  53. package/src/def/pre-publish.ts +24 -3
  54. package/src/def/rename.ts +11 -2
  55. package/src/def/universalize.ts +9 -0
  56. package/src/def/version.ts +9 -0
  57. package/src/index.ts +2 -0
  58. package/src/utils/__tests__/__fixtures__/napi_type_def +130 -130
  59. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  60. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  61. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +56 -0
  62. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  63. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +703 -164
  64. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  65. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +9 -8
  66. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  67. package/src/utils/__tests__/config.spec.ts +81 -0
  68. package/src/utils/__tests__/typegen.spec.ts +19 -4
  69. package/src/utils/cargo.ts +2 -2
  70. package/src/utils/config.ts +147 -13
  71. package/src/utils/index.ts +1 -1
  72. package/src/utils/log.ts +1 -1
  73. package/src/utils/metadata.ts +52 -14
  74. package/src/utils/misc.ts +21 -7
  75. package/src/utils/read-config.ts +20 -0
  76. package/src/utils/target.ts +53 -9
  77. package/src/utils/typegen.ts +100 -40
  78. package/src/utils/version.ts +11 -9
  79. package/dist/api/artifacts.js +0 -50
  80. package/dist/api/build.js +0 -404
  81. package/dist/api/create-npm-dirs.js +0 -63
  82. package/dist/api/new.js +0 -154
  83. package/dist/api/pre-publish.js +0 -157
  84. package/dist/api/rename.js +0 -28
  85. package/dist/api/templates/.gitignore.d.ts +0 -1
  86. package/dist/api/templates/.gitignore.js +0 -198
  87. package/dist/api/templates/.npmignore.d.ts +0 -1
  88. package/dist/api/templates/.npmignore.js +0 -14
  89. package/dist/api/templates/build.rs.d.ts +0 -1
  90. package/dist/api/templates/build.rs.js +0 -4
  91. package/dist/api/templates/cargo.toml.d.ts +0 -6
  92. package/dist/api/templates/cargo.toml.js +0 -25
  93. package/dist/api/templates/ci-template.d.ts +0 -1
  94. package/dist/api/templates/ci-template.js +0 -566
  95. package/dist/api/templates/ci.yml.d.ts +0 -1
  96. package/dist/api/templates/ci.yml.js +0 -92
  97. package/dist/api/templates/index.js +0 -8
  98. package/dist/api/templates/js-binding.js +0 -124
  99. package/dist/api/templates/lib.rs.d.ts +0 -1
  100. package/dist/api/templates/lib.rs.js +0 -10
  101. package/dist/api/templates/package.json.d.ts +0 -8
  102. package/dist/api/templates/package.json.js +0 -30
  103. package/dist/api/universalize.js +0 -38
  104. package/dist/api/version.js +0 -16
  105. package/dist/commands/artifacts.js +0 -19
  106. package/dist/commands/build.js +0 -34
  107. package/dist/commands/create-npm-dirs.js +0 -7
  108. package/dist/commands/help.js +0 -12
  109. package/dist/commands/new.js +0 -112
  110. package/dist/commands/pre-publish.js +0 -8
  111. package/dist/commands/rename.js +0 -7
  112. package/dist/commands/universalize.js +0 -7
  113. package/dist/commands/version.js +0 -7
  114. package/dist/def/artifacts.js +0 -38
  115. package/dist/def/build.js +0 -105
  116. package/dist/def/commands/artifacts.d.ts +0 -15
  117. package/dist/def/commands/artifacts.js +0 -29
  118. package/dist/def/commands/build.d.ts +0 -53
  119. package/dist/def/commands/build.js +0 -105
  120. package/dist/def/commands/create-npm-dirs.d.ts +0 -15
  121. package/dist/def/commands/create-npm-dirs.js +0 -29
  122. package/dist/def/commands/new.d.ts +0 -27
  123. package/dist/def/commands/new.js +0 -53
  124. package/dist/def/commands/pre-publish.d.ts +0 -23
  125. package/dist/def/commands/pre-publish.js +0 -45
  126. package/dist/def/commands/rename.d.ts +0 -25
  127. package/dist/def/commands/rename.js +0 -49
  128. package/dist/def/commands/universalize.d.ts +0 -13
  129. package/dist/def/commands/universalize.js +0 -25
  130. package/dist/def/commands/version.d.ts +0 -13
  131. package/dist/def/commands/version.js +0 -25
  132. package/dist/def/create-npm-dirs.js +0 -38
  133. package/dist/def/new.js +0 -66
  134. package/dist/def/options/artifacts.d.ts +0 -35
  135. package/dist/def/options/artifacts.js +0 -11
  136. package/dist/def/options/build.d.ts +0 -97
  137. package/dist/def/options/build.js +0 -3
  138. package/dist/def/options/create-npm-dirs.d.ts +0 -35
  139. package/dist/def/options/create-npm-dirs.js +0 -11
  140. package/dist/def/options/new.d.ts +0 -79
  141. package/dist/def/options/new.js +0 -15
  142. package/dist/def/options/pre-publish.d.ts +0 -65
  143. package/dist/def/options/pre-publish.js +0 -13
  144. package/dist/def/options/rename.d.ts +0 -75
  145. package/dist/def/options/rename.js +0 -11
  146. package/dist/def/options/universalize.d.ts +0 -28
  147. package/dist/def/options/universalize.js +0 -10
  148. package/dist/def/options/version.d.ts +0 -28
  149. package/dist/def/options/version.js +0 -10
  150. package/dist/def/pre-publish.js +0 -56
  151. package/dist/def/rename.js +0 -58
  152. package/dist/def/universalize.js +0 -33
  153. package/dist/def/version.js +0 -33
  154. package/dist/utils/__tests__/target.spec.js +0 -10
  155. package/dist/utils/__tests__/typegen.spec.js +0 -37
  156. package/dist/utils/__tests__/version.spec.js +0 -5
  157. package/dist/utils/cargo.js +0 -33
  158. package/dist/utils/ci.d.ts +0 -11
  159. package/dist/utils/ci.js +0 -86
  160. package/dist/utils/config.js +0 -42
  161. package/dist/utils/index.js +0 -9
  162. package/dist/utils/log.js +0 -14
  163. package/dist/utils/metadata.js +0 -19
  164. package/dist/utils/misc.js +0 -35
  165. package/dist/utils/target.js +0 -109
  166. package/dist/utils/typegen.js +0 -158
  167. package/dist/utils/version.js +0 -59
  168. package/src/api/templates/.gitignore.ts +0 -198
  169. package/src/api/templates/.npmignore.ts +0 -14
  170. package/src/api/templates/build.rs.ts +0 -4
  171. package/src/api/templates/cargo.toml.ts +0 -35
  172. package/src/api/templates/ci-template.ts +0 -566
  173. package/src/api/templates/ci.yml.ts +0 -116
  174. package/src/api/templates/lib.rs.ts +0 -10
  175. package/src/api/templates/package.json.ts +0 -44
  176. package/src/utils/ci.ts +0 -102
@@ -1,34 +1,50 @@
1
- import { execSync } from 'child_process'
1
+ import { execSync } from 'node:child_process'
2
+
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
2
8
 
3
9
  export const AVAILABLE_TARGETS = [
4
10
  'aarch64-apple-darwin',
5
11
  'aarch64-linux-android',
6
12
  'aarch64-unknown-linux-gnu',
7
13
  'aarch64-unknown-linux-musl',
14
+ 'aarch64-unknown-linux-ohos',
8
15
  'aarch64-pc-windows-msvc',
9
16
  'x86_64-apple-darwin',
10
17
  'x86_64-pc-windows-msvc',
11
18
  'x86_64-unknown-linux-gnu',
12
19
  'x86_64-unknown-linux-musl',
20
+ 'x86_64-unknown-linux-ohos',
13
21
  'x86_64-unknown-freebsd',
14
22
  'i686-pc-windows-msvc',
15
23
  'armv7-unknown-linux-gnueabihf',
24
+ 'armv7-unknown-linux-musleabihf',
16
25
  'armv7-linux-androideabi',
17
26
  'universal-apple-darwin',
27
+ 'riscv64gc-unknown-linux-gnu',
28
+ 'powerpc64le-unknown-linux-gnu',
29
+ 's390x-unknown-linux-gnu',
30
+ 'wasm32-wasi-preview1-threads',
31
+ 'wasm32-wasip1-threads',
18
32
  ] as const
19
33
 
20
34
  export type TargetTriple = (typeof AVAILABLE_TARGETS)[number]
21
35
 
22
36
  export const DEFAULT_TARGETS = [
23
37
  'x86_64-apple-darwin',
38
+ 'aarch64-apple-darwin',
24
39
  'x86_64-pc-windows-msvc',
25
40
  'x86_64-unknown-linux-gnu',
26
41
  ] as const
27
42
 
28
43
  export const TARGET_LINKER: Record<string, string> = {
29
- 'aarch64-unknown-linux-gnu': 'aarch64-linux-gnu-gcc',
30
- 'armv7-unknown-linux-gnueabihf': 'arm-linux-gnueabihf-gcc',
31
44
  'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
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',
32
48
  }
33
49
 
34
50
  // https://nodejs.org/api/process.html#process_process_arch
@@ -40,17 +56,21 @@ type NodeJSArch =
40
56
  | 'mipsel'
41
57
  | 'ppc'
42
58
  | 'ppc64'
59
+ | 'riscv64'
43
60
  | 's390'
44
61
  | 's390x'
45
62
  | 'x32'
46
63
  | 'x64'
47
64
  | 'universal'
65
+ | 'wasm32'
48
66
 
49
67
  const CpuToNodeArch: Record<string, NodeJSArch> = {
50
68
  x86_64: 'x64',
51
69
  aarch64: 'arm64',
52
70
  i686: 'ia32',
53
71
  armv7: 'arm',
72
+ riscv64gc: 'riscv64',
73
+ powerpc64le: 'ppc64',
54
74
  }
55
75
 
56
76
  export const NodeArchToCpu: Record<string, string> = {
@@ -58,25 +78,26 @@ export const NodeArchToCpu: Record<string, string> = {
58
78
  arm64: 'aarch64',
59
79
  ia32: 'i686',
60
80
  arm: 'armv7',
81
+ riscv64: 'riscv64gc',
82
+ ppc64: 'powerpc64le',
61
83
  }
62
84
 
63
- const SysToNodePlatform: Record<string, NodeJS.Platform> = {
85
+ const SysToNodePlatform: Record<string, Platform> = {
64
86
  linux: 'linux',
65
87
  freebsd: 'freebsd',
66
88
  darwin: 'darwin',
67
89
  windows: 'win32',
90
+ ohos: 'openharmony',
68
91
  }
69
92
 
70
- export const UniArchsByPlatform: Partial<
71
- Record<NodeJS.Platform, NodeJSArch[]>
72
- > = {
93
+ export const UniArchsByPlatform: Partial<Record<Platform, NodeJSArch[]>> = {
73
94
  darwin: ['x64', 'arm64'],
74
95
  }
75
96
 
76
97
  export interface Target {
77
98
  triple: string
78
99
  platformArchABI: string
79
- platform: NodeJS.Platform
100
+ platform: Platform
80
101
  arch: NodeJSArch
81
102
  abi: string | null
82
103
  }
@@ -92,6 +113,19 @@ export interface Target {
92
113
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
93
114
  */
94
115
  export function parseTriple(rawTriple: string): Target {
116
+ if (
117
+ rawTriple === 'wasm32-wasi' ||
118
+ rawTriple === 'wasm32-wasi-preview1-threads' ||
119
+ rawTriple.startsWith('wasm32-wasip')
120
+ ) {
121
+ return {
122
+ triple: rawTriple,
123
+ platformArchABI: 'wasm32-wasi',
124
+ platform: 'wasi',
125
+ arch: 'wasm32',
126
+ abi: 'wasi',
127
+ }
128
+ }
95
129
  const triple = rawTriple.endsWith('eabi')
96
130
  ? `${rawTriple.slice(0, -4)}-eabi`
97
131
  : rawTriple
@@ -111,8 +145,18 @@ export function parseTriple(rawTriple: string): Target {
111
145
  ;[cpu, , sys, abi = null] = triples
112
146
  }
113
147
 
114
- const platform = SysToNodePlatform[sys] ?? (sys as NodeJS.Platform)
148
+ const platform = SysToNodePlatform[sys] ?? (sys as Platform)
115
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
+ }
116
160
  return {
117
161
  triple: rawTriple,
118
162
  platformArchABI: abi ? `${platform}-${arch}-${abi}` : `${platform}-${arch}`,
@@ -10,7 +10,9 @@ export const DEFAULT_TYPE_DEF_HEADER = `/* auto-generated by NAPI-RS */
10
10
  enum TypeDefKind {
11
11
  Const = 'const',
12
12
  Enum = 'enum',
13
+ StringEnum = 'string_enum',
13
14
  Interface = 'interface',
15
+ Type = 'type',
14
16
  Fn = 'fn',
15
17
  Struct = 'struct',
16
18
  Impl = 'impl',
@@ -25,24 +27,46 @@ interface TypeDefLine {
25
27
  js_mod?: string
26
28
  }
27
29
 
28
- function prettyPrint(line: TypeDefLine, ident: number): string {
30
+ function prettyPrint(
31
+ line: TypeDefLine,
32
+ constEnum: boolean,
33
+ ident: number,
34
+ ambient = false,
35
+ ): string {
29
36
  let s = line.js_doc ?? ''
30
37
  switch (line.kind) {
31
38
  case TypeDefKind.Interface:
32
39
  s += `export interface ${line.name} {\n${line.def}\n}`
33
40
  break
34
41
 
42
+ case TypeDefKind.Type:
43
+ s += `export type ${line.name} = \n${line.def}`
44
+ break
45
+
35
46
  case TypeDefKind.Enum:
36
- s += `export const enum ${line.name} {\n${line.def}\n}`
47
+ const enumName = constEnum ? 'const enum' : 'enum'
48
+ s += `${exportDeclare(ambient)} ${enumName} ${line.name} {\n${line.def}\n}`
49
+ break
50
+
51
+ case TypeDefKind.StringEnum:
52
+ if (constEnum) {
53
+ s += `${exportDeclare(ambient)} const enum ${line.name} {\n${line.def}\n}`
54
+ } else {
55
+ s += `export type ${line.name} = ${line.def.replaceAll(/.*=/g, '').replaceAll(',', '|')};`
56
+ }
37
57
  break
38
58
 
39
59
  case TypeDefKind.Struct:
40
- s += `export class ${line.name} {\n${line.def}\n}`
60
+ s += `${exportDeclare(ambient)} class ${line.name} {\n${line.def}\n}`
41
61
  if (line.original_name && line.original_name !== line.name) {
42
62
  s += `\nexport type ${line.original_name} = ${line.name}`
43
63
  }
44
64
  break
45
65
 
66
+ case TypeDefKind.Fn:
67
+ s += `${exportDeclare(ambient)} ${line.def}`
68
+ break
69
+
46
70
  default:
47
71
  s += line.def
48
72
  }
@@ -50,59 +74,84 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
50
74
  return correctStringIdent(s, ident)
51
75
  }
52
76
 
77
+ function exportDeclare(ambient: boolean): string {
78
+ if (ambient) {
79
+ return 'export'
80
+ }
81
+
82
+ return 'export declare'
83
+ }
84
+
53
85
  export async function processTypeDef(
54
86
  intermediateTypeFile: string,
55
- header?: string,
87
+ constEnum: boolean,
56
88
  ) {
89
+ const exports: string[] = []
57
90
  const defs = await readIntermediateTypeFile(intermediateTypeFile)
58
91
  const groupedDefs = preprocessTypeDef(defs)
59
92
 
60
- header = header ? header + '\n' : ''
61
- let dts = ''
62
-
63
- sortBy(Array.from(groupedDefs), ([namespace]) => namespace).forEach(
64
- ([namespace, defs]) => {
65
- if (namespace === TOP_LEVEL_NAMESPACE) {
66
- for (const def of defs) {
67
- dts += prettyPrint(def, 0) + '\n\n'
93
+ const dts =
94
+ sortBy(Array.from(groupedDefs), ([namespace]) => namespace)
95
+ .map(([namespace, defs]) => {
96
+ if (namespace === TOP_LEVEL_NAMESPACE) {
97
+ return defs
98
+ .map((def) => {
99
+ switch (def.kind) {
100
+ case TypeDefKind.Const:
101
+ case TypeDefKind.Enum:
102
+ case TypeDefKind.StringEnum:
103
+ case TypeDefKind.Fn:
104
+ case TypeDefKind.Struct: {
105
+ exports.push(def.name)
106
+ if (def.original_name && def.original_name !== def.name) {
107
+ exports.push(def.original_name)
108
+ }
109
+ break
110
+ }
111
+ default:
112
+ break
113
+ }
114
+ return prettyPrint(def, constEnum, 0)
115
+ })
116
+ .join('\n\n')
117
+ } else {
118
+ exports.push(namespace)
119
+ let declaration = ''
120
+ declaration += `export declare namespace ${namespace} {\n`
121
+ for (const def of defs) {
122
+ declaration += prettyPrint(def, constEnum, 2, true) + '\n'
123
+ }
124
+ declaration += '}'
125
+ return declaration
68
126
  }
69
- } else {
70
- dts += `export namespace ${namespace} {\n`
71
- for (const def of defs) {
72
- dts += prettyPrint(def, 2) + '\n'
73
- }
74
- dts += '}\n\n'
75
- }
76
- },
77
- )
78
-
79
- if (dts.indexOf('ExternalObject<') > -1) {
80
- header += `
81
- export class ExternalObject<T> {
82
- readonly '': {
83
- readonly '': unique symbol
84
- [K: symbol]: T
85
- }
86
- }
87
- `
88
- }
127
+ })
128
+ .join('\n\n') + '\n'
89
129
 
90
- return header + dts
130
+ return {
131
+ dts,
132
+ exports,
133
+ }
91
134
  }
92
135
 
93
136
  async function readIntermediateTypeFile(file: string) {
94
137
  const content = await readFileAsync(file, 'utf8')
138
+
95
139
  const defs = content
96
140
  .split('\n')
97
141
  .filter(Boolean)
98
142
  .map((line) => {
99
143
  line = line.trim()
100
- if (!line.startsWith('{')) {
101
- // crateName:{ "def": "", ... }
102
- const start = line.indexOf(':') + 1
103
- line = line.slice(start)
144
+ const parsed = JSON.parse(line) as TypeDefLine
145
+ // Convert escaped newlines back to actual newlines in js_doc fields
146
+ if (parsed.js_doc) {
147
+ parsed.js_doc = parsed.js_doc.replace(/\\n/g, '\n')
104
148
  }
105
- return JSON.parse(line) as TypeDefLine
149
+ // Convert escaped newlines to actual newlines in def fields for struct/class/interface/type types
150
+ // where \n represents method/field separators that should be actual newlines
151
+ if (parsed.def) {
152
+ parsed.def = parsed.def.replace(/\\n/g, '\n')
153
+ }
154
+ return parsed
106
155
  })
107
156
 
108
157
  // move all `struct` def to the very top
@@ -145,6 +194,10 @@ function preprocessTypeDef(defs: TypeDefLine[]): Map<string, TypeDefLine[]> {
145
194
  }
146
195
 
147
196
  classDef.def += def.def
197
+ // Convert any remaining \n sequences in the merged def to actual newlines
198
+ if (classDef.def) {
199
+ classDef.def = classDef.def.replace(/\\n/g, '\n')
200
+ }
148
201
  }
149
202
  } else {
150
203
  group.push(def)
@@ -167,13 +220,20 @@ export function correctStringIdent(src: string, ident: number): string {
167
220
  const isInMultilineComment = line.startsWith('*')
168
221
  const isClosingBracket = line.endsWith('}')
169
222
  const isOpeningBracket = line.endsWith('{')
223
+ const isTypeDeclaration = line.endsWith('=')
224
+ const isTypeVariant = line.startsWith('|')
170
225
 
171
226
  let rightIndent = ident
172
- if (isOpeningBracket && !isInMultilineComment) {
227
+ if ((isOpeningBracket || isTypeDeclaration) && !isInMultilineComment) {
173
228
  bracketDepth += 1
174
229
  rightIndent += (bracketDepth - 1) * 2
175
230
  } else {
176
- if (isClosingBracket && bracketDepth > 0 && !isInMultilineComment) {
231
+ if (
232
+ isClosingBracket &&
233
+ bracketDepth > 0 &&
234
+ !isInMultilineComment &&
235
+ !isTypeVariant
236
+ ) {
177
237
  bracketDepth -= 1
178
238
  }
179
239
  rightIndent += bracketDepth * 2
@@ -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,50 +0,0 @@
1
- import { join, parse, resolve } from 'path';
2
- import * as colors from 'colorette';
3
- import { applyDefaultArtifactsOptions, } from '../def/artifacts.js';
4
- import { readNapiConfig, debugFactory, readFileAsync, writeFileAsync, UniArchsByPlatform, readdirAsync, } from '../utils/index.js';
5
- const debug = debugFactory('artifacts');
6
- export async function collectArtifacts(userOptions) {
7
- const options = applyDefaultArtifactsOptions(userOptions);
8
- const packageJsonPath = resolve(options.cwd, options.packageJsonPath);
9
- const { targets, binaryName } = await readNapiConfig(packageJsonPath);
10
- const distDirs = targets.map((platform) => resolve(options.cwd, options.npmDir, platform.platformArchABI));
11
- const universalSourceBins = new Set(targets
12
- .filter((platform) => platform.arch === 'universal')
13
- .flatMap((p) => UniArchsByPlatform[p.platform]?.map((a) => `${p.platform}-${a}`))
14
- .filter(Boolean));
15
- await collectNodeBinaries(resolve(options.cwd, options.outputDir)).then((output) => Promise.all(output.map(async (filePath) => {
16
- debug.info(`Read [${colors.yellowBright(filePath)}]`);
17
- const sourceContent = await readFileAsync(filePath);
18
- const parsedName = parse(filePath);
19
- const [_binaryName, platformArchABI] = parsedName.name.split('.');
20
- if (_binaryName !== binaryName) {
21
- debug.warn(`[${_binaryName}] is not matched with [${binaryName}], skip`);
22
- return;
23
- }
24
- const dir = distDirs.find((dir) => dir.includes(platformArchABI));
25
- if (!dir && universalSourceBins.has(platformArchABI)) {
26
- debug.warn(`[${platformArchABI}] has no dist dir but it is source bin for universal arch, skip`);
27
- return;
28
- }
29
- if (!dir) {
30
- throw new Error(`No dist dir found for ${filePath}`);
31
- }
32
- const distFilePath = join(dir, parsedName.base);
33
- debug.info(`Write file content to [${colors.yellowBright(distFilePath)}]`);
34
- await writeFileAsync(distFilePath, sourceContent);
35
- const distFilePathLocal = join(parse(packageJsonPath).dir, parsedName.base);
36
- debug.info(`Write file content to [${colors.yellowBright(distFilePathLocal)}]`);
37
- await writeFileAsync(distFilePathLocal, sourceContent);
38
- })));
39
- }
40
- async function collectNodeBinaries(root) {
41
- const files = await readdirAsync(root, { withFileTypes: true });
42
- const nodeBinaries = files
43
- .filter((file) => file.isFile() && file.name.endsWith('.node'))
44
- .map((file) => join(root, file.name));
45
- const dirs = files.filter((file) => file.isDirectory());
46
- for (const dir of dirs) {
47
- nodeBinaries.push(...(await collectNodeBinaries(join(root, dir.name))));
48
- }
49
- return nodeBinaries;
50
- }