1+ process . env . NODE_ENV = 'development'
2+
3+ import electron from 'electron' ;
4+ import chalk from 'chalk' ;
5+ import { join } from 'path' ;
6+ import { watch } from 'rollup' ;
7+ import Portfinder from 'Portfinder' ;
8+ import config from '../config'
9+ import { say } from 'cfonts' ;
10+ import { spawn } from 'child_process' ;
11+ import type { ChildProcess } from 'child_process'
12+ import { createServer } from 'vite'
13+ import rollupOptions from './rollup.config'
14+
15+
16+ const mainOpt = rollupOptions ( process . env . NODE_ENV , "main" ) ;
17+
18+ let electronProcess : ChildProcess | null = null
19+ let manualRestart = false
20+
21+ function logStats ( proc : string , data : any ) {
22+ let log = ''
23+
24+ log += chalk . yellow . bold ( `┏ ${ proc } ${ config . dev . chineseLog ? '编译过程' : 'Process' } ${ new Array ( ( 19 - proc . length ) + 1 ) . join ( '-' ) } ` )
25+ log += '\n\n'
26+
27+ if ( typeof data === 'object' ) {
28+ data . toString ( {
29+ colors : true ,
30+ chunks : false
31+ } ) . split ( / \r ? \n / ) . forEach ( line => {
32+ log += ' ' + line + '\n'
33+ } )
34+ } else {
35+ log += ` ${ data } \n`
36+ }
37+
38+ log += '\n' + chalk . yellow . bold ( `┗ ${ new Array ( 28 + 1 ) . join ( '-' ) } ` ) + '\n'
39+ console . log ( log )
40+ }
41+
42+ function removeJunk ( chunk : string ) {
43+ if ( config . dev . removeElectronJunk ) {
44+ // Example: 2018-08-10 22:48:42.866 Electron[90311:4883863] *** WARNING: Textured window <AtomNSWindow: 0x7fb75f68a770>
45+ if ( / \d + - \d + - \d + \d + : \d + : \d + \. \d + E l e c t r o n (?: H e l p e r ) ? \[ \d + : \d + ] / . test ( chunk ) ) {
46+ return false ;
47+ }
48+
49+ // Example: [90789:0810/225804.894349:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
50+ if ( / \[ \d + : \d + \/ | \d + \. \d + : E R R O R : C O N S O L E \( \d + \) \] / . test ( chunk ) ) {
51+ return false ;
52+ }
53+
54+ // Example: ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
55+ if ( / A L S A l i b [ a - z ] + \. c : \d + : \( [ a - z _ ] + \) / . test ( chunk ) ) {
56+ return false ;
57+ }
58+ }
59+
60+
61+ return chunk ;
62+ }
63+
64+ function startRenderer ( ) : Promise < void > {
65+ return new Promise ( ( resolve , reject ) => {
66+ Portfinder . basePort = config . dev . port || 9080
67+ Portfinder . getPort ( async ( err , port ) => {
68+ if ( err ) {
69+ reject ( "PortError:" + err )
70+ } else {
71+ const server = await createServer ( { configFile : join ( __dirname , 'vite.config' ) } )
72+ process . env . PORT = String ( port )
73+ await server . listen ( port )
74+ console . log ( '\n\n' + chalk . blue ( `${ config . dev . chineseLog ? ' 正在准备主进程,请等待...' : ' Preparing main process, please wait...' } ` ) + '\n\n' )
75+ resolve ( )
76+ }
77+ } )
78+ } )
79+ }
80+
81+ function startMain ( ) : Promise < void > {
82+ return new Promise ( ( resolve , reject ) => {
83+ const MainWatcher = watch ( mainOpt ) ;
84+ MainWatcher . on ( 'change' , filename => {
85+ // 主进程日志部分
86+ logStats ( `${ config . dev . chineseLog ? '主进程文件变更' : 'Main-FileChange' } ` , filename )
87+ } ) ;
88+ MainWatcher . on ( 'event' , event => {
89+ if ( event . code === 'END' ) {
90+ if ( electronProcess && electronProcess . kill ) {
91+ manualRestart = true
92+ process . kill ( electronProcess . pid )
93+ electronProcess = null
94+ startElectron ( )
95+
96+ setTimeout ( ( ) => {
97+ manualRestart = false
98+ } , 5000 )
99+ }
100+
101+ resolve ( )
102+
103+ } else if ( event . code === 'ERROR' ) {
104+ reject ( event . error )
105+ }
106+ } )
107+ } )
108+ }
109+
110+
111+ function startElectron ( ) {
112+
113+ var args = [
114+ '--inspect=5858' ,
115+ join ( __dirname , '../dist/electron/main/main.js' )
116+ ]
117+
118+ // detect yarn or npm and process commandline args accordingly
119+ if ( process . env . npm_execpath . endsWith ( 'yarn.js' ) ) {
120+ args = args . concat ( process . argv . slice ( 3 ) )
121+ } else if ( process . env . npm_execpath . endsWith ( 'npm-cli.js' ) ) {
122+ args = args . concat ( process . argv . slice ( 2 ) )
123+ }
124+
125+ electronProcess = spawn ( electron as any , args )
126+
127+ electronProcess . stdout . on ( 'data' , ( data : string ) => {
128+ electronLog ( removeJunk ( data ) , 'blue' )
129+ } )
130+ electronProcess . stderr . on ( 'data' , ( data : string ) => {
131+ electronLog ( removeJunk ( data ) , 'red' )
132+ } )
133+
134+ electronProcess . on ( 'close' , ( ) => {
135+ if ( ! manualRestart ) process . exit ( )
136+ } )
137+ }
138+
139+ function electronLog ( data : any , color : string ) {
140+ if ( data ) {
141+ let log = ''
142+ data = data . toString ( ) . split ( / \r ? \n / )
143+ data . forEach ( line => {
144+ log += ` ${ line } \n`
145+ } )
146+ console . log (
147+ chalk [ color ] . bold ( `┏ ${ config . dev . chineseLog ? '主程序日志' : 'Electron' } -------------------` ) +
148+ '\n\n' +
149+ log +
150+ chalk [ color ] . bold ( '┗ ----------------------------' ) +
151+ '\n'
152+ )
153+ }
154+
155+ }
156+
157+ function greeting ( ) {
158+ const cols = process . stdout . columns
159+ let text : string | boolean = ''
160+
161+ if ( cols > 104 ) text = 'electron-vite'
162+ else if ( cols > 76 ) text = 'electron-|vite'
163+ else text = false
164+
165+ if ( text ) {
166+ say ( text , {
167+ colors : [ 'yellow' ] ,
168+ font : 'simple3d' ,
169+ space : false
170+ } )
171+ } else console . log ( chalk . yellow . bold ( '\n electron-vite' ) )
172+ console . log ( chalk . blue ( `${ config . dev . chineseLog ? ' 准备启动...' : ' getting ready...' } ` ) + '\n' )
173+ }
174+
175+ async function init ( ) {
176+ greeting ( )
177+
178+ try {
179+ await startRenderer ( )
180+ await startMain ( )
181+ startElectron ( )
182+ } catch ( error ) {
183+ console . error ( error )
184+ process . exit ( 1 )
185+ }
186+
187+ }
188+
189+ init ( )
0 commit comments