Skip to content

Commit 4e5f258

Browse files
committed
初始化
0 parents  commit 4e5f258

58 files changed

Lines changed: 7769 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.electron-vite/afterPack.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// pack后对不同位数的系统迁移不同的文件
2+
'use strict';
3+
const { copySync, ensureDirSync } = require('fs-extra');
4+
const { join } = require('path');
5+
const { Arch } = require('electron-builder');
6+
// type ElectronPlatformName = "darwin" | "linux" | "win32" | "mas"
7+
exports.default = async context => {
8+
const LIB_OUTPUT_DIR = context.appOutDir;
9+
const LIB_INPUT_DIR = join("rootLib", context.electronPlatformName, Arch[context.arch]);
10+
const LIB_COMMON_INPUT_DIR = join("rootLib", "common");
11+
// 确保文件夹存在
12+
ensureDirSync(LIB_COMMON_INPUT_DIR);
13+
ensureDirSync(LIB_INPUT_DIR);
14+
// 移动文件文件
15+
copySync(LIB_INPUT_DIR, LIB_OUTPUT_DIR);
16+
copySync(LIB_COMMON_INPUT_DIR, LIB_OUTPUT_DIR);
17+
};

.electron-vite/build.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
process.env.NODE_ENV = 'production'
2+
3+
import { join } from 'path'
4+
import { say } from 'cfonts'
5+
import { sync } from 'del'
6+
import { build } from 'vite'
7+
import chalk from 'chalk'
8+
import { rollup, OutputOptions } from 'rollup'
9+
import Multispinner from 'Multispinner'
10+
import rollupOptions from './rollup.config'
11+
import { okayLog, errorLog, doneLog } from './log'
12+
13+
14+
const mainOpt = rollupOptions(process.env.NODE_ENV, "main");
15+
const isCI = process.env.CI || false
16+
17+
18+
if (process.env.BUILD_TARGET === 'web') web()
19+
else unionBuild()
20+
21+
function clean() {
22+
sync(['dist/electron/main/*', 'dist/electron/renderer/*', 'dist/web/*', 'build/*', '!build/icons', '!build/lib', '!build/lib/electron-build.*', '!build/icons/icon.*'])
23+
console.log(`\n${doneLog}clear done`)
24+
if (process.env.BUILD_TARGET === 'onlyClean') process.exit()
25+
}
26+
27+
function unionBuild() {
28+
greeting()
29+
if (process.env.BUILD_TARGET === 'clean' || process.env.BUILD_TARGET === 'onlyClean') clean()
30+
31+
const tasks = ['main', 'renderer']
32+
const m = new Multispinner(tasks, {
33+
preText: 'building',
34+
postText: 'process'
35+
})
36+
let results = ''
37+
38+
m.on('success', () => {
39+
process.stdout.write('\x1B[2J\x1B[0f')
40+
console.log(`\n\n${results}`)
41+
console.log(`${okayLog}take it away ${chalk.yellow('`electron-builder`')}\n`)
42+
process.exit()
43+
})
44+
45+
rollup(mainOpt)
46+
.then(build => {
47+
results += `${doneLog}MainProcess build success` + '\n\n'
48+
build.write(mainOpt.output as OutputOptions).then(() => {
49+
m.success('main')
50+
})
51+
})
52+
.catch(error => {
53+
m.error('main')
54+
console.log(`\n ${errorLog}failed to build main process`)
55+
console.error(`\n${error}\n`)
56+
process.exit(1)
57+
});
58+
59+
build({ configFile: join(__dirname, 'vite.config') }).then(res => {
60+
results += `${doneLog}RendererProcess build success` + '\n\n'
61+
m.success('renderer')
62+
}).catch(err => {
63+
m.error('renderer')
64+
console.log(`\n ${errorLog}failed to build renderer process`)
65+
console.error(`\n${err}\n`)
66+
process.exit(1)
67+
})
68+
}
69+
70+
function web() {
71+
sync(['dist/web/*', '!.gitkeep'])
72+
build({ configFile: join(__dirname, 'vite.config') }).then(res => {
73+
console.log(`${doneLog}RendererProcess build success`)
74+
process.exit()
75+
})
76+
}
77+
78+
function greeting() {
79+
const cols = process.stdout.columns
80+
let text: boolean | string = ''
81+
82+
if (cols > 85) text = `let's-build`
83+
else if (cols > 60) text = `let's-|build`
84+
else text = false
85+
86+
if (text && !isCI) {
87+
say(text, {
88+
colors: ['yellow'],
89+
font: 'simple3d',
90+
space: false
91+
})
92+
} else console.log(chalk.yellow.bold(`\n let's-build`))
93+
console.log()
94+
}

.electron-vite/dev-runner.ts

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+ Electron(?: Helper)?\[\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+:ERROR:CONSOLE\(\d+\)\]/.test(chunk)) {
51+
return false;
52+
}
53+
54+
// Example: ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
55+
if (/ALSA lib [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

Comments
 (0)