@napi-rs/cli 3.0.0-alpha.80 → 3.0.0-alpha.82

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.
@@ -1,4 +1,4 @@
1
- import { spawnSync } from 'node:child_process'
1
+ import { spawn } from 'node:child_process'
2
2
  import fs from 'node:fs'
3
3
 
4
4
  export type CrateTargetKind =
@@ -48,24 +48,37 @@ export interface CargoWorkspaceMetadata {
48
48
  workspace_root: string
49
49
  }
50
50
 
51
- export function parseMetadata(manifestPath: string) {
51
+ export async function parseMetadata(manifestPath: string) {
52
52
  if (!fs.existsSync(manifestPath)) {
53
53
  throw new Error(`No crate found in manifest: ${manifestPath}`)
54
54
  }
55
55
 
56
- const { stdout, stderr, status, error } = spawnSync(
56
+ const childProcess = spawn(
57
57
  'cargo',
58
- [
59
- 'metadata',
60
- '--manifest-path',
61
- manifestPath,
62
- '--format-version',
63
- '1',
64
- '--no-deps',
65
- ],
66
- { encoding: 'utf-8' },
58
+ ['metadata', '--manifest-path', manifestPath, '--format-version', '1'],
59
+ { stdio: 'pipe' },
67
60
  )
68
61
 
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
+
69
82
  if (error) {
70
83
  throw new Error('cargo metadata failed to run', { cause: error })
71
84
  }
package/src/utils/misc.ts CHANGED
@@ -7,19 +7,11 @@ import {
7
7
  stat,
8
8
  readdir,
9
9
  } from 'node:fs'
10
- import { createRequire } from 'node:module'
11
10
  import { promisify } from 'node:util'
12
11
 
12
+ import pkgJson from '../../package.json' with { type: 'json' }
13
13
  import { debug } from './log.js'
14
14
 
15
- const require = createRequire(import.meta.url)
16
- // NOTE:
17
- // import pkgJson from '@napi-rs/cli/package.json' assert { type: 'json' }
18
- // is experimental feature now, avoid using it.
19
- // see: https://nodejs.org/api/esm.html#import-assertions
20
- // eslint-disable-next-line import/no-extraneous-dependencies
21
- const pkgJson = require('@napi-rs/cli/package.json')
22
-
23
15
  export const readFileAsync = promisify(readFile)
24
16
  export const writeFileAsync = promisify(writeFile)
25
17
  export const unlinkAsync = promisify(unlink)
@@ -35,6 +27,15 @@ export async function fileExists(path: string) {
35
27
  return exists
36
28
  }
37
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
+
38
39
  export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> {
39
40
  return keys.reduce((acc, key) => {
40
41
  acc[key] = o[key]
@@ -1,4 +1,4 @@
1
- import { sortBy, unionWith, isEqual } from 'lodash-es'
1
+ import { sortBy } from 'lodash-es'
2
2
 
3
3
  import { readFileAsync } from './misc.js'
4
4
 
@@ -85,14 +85,11 @@ function exportDeclare(ambient: boolean): string {
85
85
  export async function processTypeDef(
86
86
  intermediateTypeFile: string,
87
87
  constEnum: boolean,
88
- header?: string,
89
88
  ) {
90
89
  const exports: string[] = []
91
90
  const defs = await readIntermediateTypeFile(intermediateTypeFile)
92
91
  const groupedDefs = preprocessTypeDef(defs)
93
92
 
94
- header = header ?? ''
95
-
96
93
  const dts =
97
94
  sortBy(Array.from(groupedDefs), ([namespace]) => namespace)
98
95
  .map(([namespace, defs]) => {
@@ -130,41 +127,22 @@ export async function processTypeDef(
130
127
  })
131
128
  .join('\n\n') + '\n'
132
129
 
133
- if (dts.indexOf('ExternalObject<') > -1) {
134
- header += `
135
- export declare class ExternalObject<T> {
136
- readonly '': {
137
- readonly '': unique symbol
138
- [K: symbol]: T
139
- }
140
- }
141
- `
142
- }
143
-
144
130
  return {
145
- dts: header + dts,
131
+ dts,
146
132
  exports,
147
133
  }
148
134
  }
149
135
 
150
136
  async function readIntermediateTypeFile(file: string) {
151
137
  const content = await readFileAsync(file, 'utf8')
152
- const defs = unionWith(
153
- content
154
- .split('\n')
155
- .filter(Boolean)
156
- .map((line) => {
157
- line = line.trim()
158
- if (!line.startsWith('{')) {
159
- // crateName:{ "def": "", ... }
160
- const start = line.indexOf(':') + 1
161
- line = line.slice(start)
162
- }
163
- return JSON.parse(line) as TypeDefLine
164
- })
165
- .filter((def) => !!def.kind),
166
- (a, b) => isEqual(a, b),
167
- )
138
+
139
+ const defs = content
140
+ .split('\n')
141
+ .filter(Boolean)
142
+ .map((line) => {
143
+ line = line.trim()
144
+ return JSON.parse(line) as TypeDefLine
145
+ })
168
146
 
169
147
  // move all `struct` def to the very top
170
148
  // and order the rest alphabetically.