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

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.
@@ -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
@@ -43,6 +43,21 @@ test('should process type def correctly', async (t) => {
43
43
  '__fixtures__',
44
44
  'napi_type_def',
45
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(
54
+ join(
55
+ fileURLToPath(import.meta.url),
56
+ '../',
57
+ '__fixtures__',
58
+ 'napi_type_def',
59
+ ),
60
+ false,
46
61
  )
47
62
 
48
63
  t.snapshot(dts)
@@ -24,6 +24,11 @@ interface UserNapiConfig {
24
24
  */
25
25
  npmClient?: string
26
26
 
27
+ /**
28
+ * Whether generate const enum for typescript bindings
29
+ */
30
+ constEnum?: boolean
31
+
27
32
  /**
28
33
  * @deprecated binaryName instead
29
34
  */
@@ -114,8 +119,8 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
114
119
  let targets: string[] = userNapiConfig.targets ?? []
115
120
 
116
121
  // compatible with old config
117
- if (userNapiConfig.package?.name) {
118
- napiConfig.packageName = userNapiConfig.package.name
122
+ if (userNapiConfig?.name) {
123
+ napiConfig.packageName = userNapiConfig.name
119
124
  }
120
125
 
121
126
  if (!targets.length) {
@@ -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,6 +57,7 @@ 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
  ) {
57
63
  const exports: string[] = []
@@ -65,7 +71,7 @@ export async function processTypeDef(
65
71
  ([namespace, defs]) => {
66
72
  if (namespace === TOP_LEVEL_NAMESPACE) {
67
73
  for (const def of defs) {
68
- dts += prettyPrint(def, 0) + '\n\n'
74
+ dts += prettyPrint(def, constEnum, 0) + '\n\n'
69
75
  switch (def.kind) {
70
76
  case TypeDefKind.Const:
71
77
  case TypeDefKind.Enum:
@@ -85,7 +91,7 @@ export async function processTypeDef(
85
91
  exports.push(namespace)
86
92
  dts += `export namespace ${namespace} {\n`
87
93
  for (const def of defs) {
88
- dts += prettyPrint(def, 2) + '\n'
94
+ dts += prettyPrint(def, constEnum, 2) + '\n'
89
95
  }
90
96
  dts += '}\n\n'
91
97
  }