11import fs from 'node:fs'
22import path from 'node:path'
3+ import cp from 'node:child_process'
34import type { AddressInfo } from 'node:net'
45import { builtinModules } from 'node:module'
56import {
@@ -9,6 +10,12 @@ import {
910} from 'vite'
1011import type { ElectronOptions } from '.'
1112
13+ export interface PidTree {
14+ pid : number
15+ ppid : number
16+ children ?: PidTree [ ]
17+ }
18+
1219/** Resolve the default Vite's `InlineConfig` for build Electron-Main */
1320export function resolveViteConfig ( options : ElectronOptions ) : InlineConfig {
1421 const packageJson = resolvePackageJson ( ) ?? { }
@@ -36,6 +43,10 @@ export function resolveViteConfig(options: ElectronOptions): InlineConfig {
3643 // It corrupts bundling packages like `ws` and `isomorphic-ws`, for example.
3744 mainFields : [ 'module' , 'jsnext:main' , 'jsnext' ] ,
3845 } ,
46+ define : {
47+ // @see - https://github.com/vitejs/vite/blob/v5.0.11/packages/vite/src/node/plugins/define.ts#L20
48+ 'process.env' : 'process.env' ,
49+ } ,
3950 }
4051
4152 return mergeConfig ( defaultConfig , options ?. vite || { } )
@@ -110,26 +121,6 @@ export function resolveServerUrl(server: ViteDevServer): string | void {
110121 }
111122}
112123
113- export function calcEntryCount ( optionsArray : ElectronOptions [ ] ) {
114- return optionsArray . reduce ( ( count , options ) => {
115- const input = options . vite ?. build ?. rollupOptions ?. input
116-
117- // `input` option have higher priority.
118- // https://github.com/vitejs/vite/blob/v4.4.9/packages/vite/src/node/build.ts#L494
119- if ( input ) {
120- count += typeof input === 'string'
121- ? 1
122- : Object . keys ( input ) . length
123- } else if ( options . entry ) {
124- count += typeof options . entry === 'string'
125- ? 1
126- : Object . keys ( options . entry ) . length
127- }
128-
129- return count
130- } , 0 )
131- }
132-
133124export function resolvePackageJson ( root = process . cwd ( ) ) : {
134125 type ?: 'module' | 'commonjs'
135126 [ key : string ] : any
@@ -142,3 +133,44 @@ export function resolvePackageJson(root = process.cwd()): {
142133 return null
143134 }
144135}
136+
137+ /**
138+ * Inspired `tree-kill`, implemented based on sync-api. #168
139+ * @see https://github.com/pkrumins/node-tree-kill/blob/v1.2.2/index.js
140+ */
141+ export function treeKillSync ( pid : number ) {
142+ if ( process . platform === 'win32' ) {
143+ cp . execSync ( `taskkill /pid ${ pid } /T /F` )
144+ } else {
145+ killTree ( pidTree ( ) )
146+ }
147+ }
148+
149+ function pidTree ( tree : PidTree = { pid : process . pid , ppid : process . ppid } ) {
150+ const command = process . platform === 'darwin'
151+ ? `pgrep -P ${ tree . pid } ` // Mac
152+ : `ps -o pid --no-headers --ppid ${ tree . ppid } ` // Linux
153+
154+ try {
155+ const childs = cp
156+ . execSync ( command , { encoding : 'utf8' } )
157+ . match ( / \d + / g)
158+ ?. map ( id => + id )
159+
160+ if ( childs ) {
161+ tree . children = childs . map ( cid => pidTree ( { pid : cid , ppid : tree . pid } ) )
162+ }
163+ } catch { }
164+
165+ return tree
166+ }
167+
168+ function killTree ( tree : PidTree ) {
169+ if ( tree . children ) {
170+ for ( const child of tree . children ) {
171+ killTree ( child )
172+ }
173+ }
174+
175+ process . kill ( tree . pid )
176+ }
0 commit comments