-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
164 lines (134 loc) · 4.38 KB
/
dev.js
File metadata and controls
164 lines (134 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { spawn } = require("child_process");
const { createInterface } = require("readline");
const colors = require("colors/safe");
// Log formatting
const log = {
info: (text) => console.log(colors.blue(`[INFO] ${text}`)),
success: (text) => console.log(colors.green(`[SUCCESS] ${text}`)),
warn: (text) => console.log(colors.yellow(`[WARN] ${text}`)),
error: (text) => console.log(colors.red(`[ERROR] ${text}`)),
};
// Store processes to kill them later
const processes = [];
// Process output formatter
function formatOutput(name, data, isError = false) {
const color = isError ? colors.red : colors.white;
const lines = data.toString().trim().split("\n");
const prefix = colors.cyan(`[${name}]`);
lines.forEach((line) => {
if (line.trim()) {
console.log(`${prefix} ${color(line)}`);
}
});
}
// Start a process and capture its output
function startProcess(command, args, name) {
log.info(`Starting ${name}...`);
const proc = spawn(command, args, {
shell: true,
env: { ...process.env, FORCE_COLOR: true },
});
processes.push(proc);
proc.stdout.on("data", (data) => formatOutput(name, data));
proc.stderr.on("data", (data) => formatOutput(name, data, true));
proc.on("close", (code) => {
if (code !== 0) {
log.error(`${name} process exited with code ${code}`);
} else {
log.success(`${name} process completed successfully`);
}
});
return proc;
}
// Clean exit handler
function cleanExit() {
log.warn("Shutting down development environment...");
processes.forEach((proc) => {
if (!proc.killed) {
proc.kill();
}
});
log.success("All processes terminated. Goodbye!");
process.exit(0);
}
// Set up clean exit handlers
process.on("SIGINT", cleanExit);
process.on("SIGTERM", cleanExit);
// Main function
async function main() {
log.info("Starting WinDropper development environment...");
// Start TypeScript compilation for main process
const tscProcess = startProcess(
"npx",
["tsc", "-p", "tsconfig.main.json", "-w"],
"TSC"
);
// Start Vite dev server
const viteProcess = startProcess("npx", ["vite"], "VITE");
// Wait for Vite to be ready (it logs "ready in" when it's up)
let viteReady = false;
const viteReadyPromise = new Promise((resolve) => {
viteProcess.stdout.on("data", (data) => {
const output = data.toString();
if (output.includes("ready in") && !viteReady) {
viteReady = true;
log.success("Vite server is ready!");
resolve();
}
});
});
// Wait for both TypeScript compilation and Vite to be ready
// We need to give TSC time to compile initially
log.info("Waiting for initial TypeScript compilation...");
await new Promise((resolve) => setTimeout(resolve, 3000));
log.info("Waiting for Vite server...");
await viteReadyPromise;
// Start Electron
log.info("Starting Electron...");
const electronProcess = startProcess(
"npx",
["cross-env", "NODE_ENV=development", "electron", "."],
"ELECTRON"
);
// Set up readline interface for user commands
const rl = createInterface({
input: process.stdin,
output: process.stdout,
prompt: colors.cyan("dev> "),
});
rl.prompt();
rl.on("line", (line) => {
const command = line.trim();
if (command === "restart" || command === "r") {
log.info("Restarting Electron...");
if (!electronProcess.killed) {
electronProcess.kill();
}
// Start a new Electron process after a small delay
setTimeout(() => {
startProcess(
"npx",
["cross-env", "NODE_ENV=development", "electron", "."],
"ELECTRON"
);
}, 1000);
} else if (command === "quit" || command === "q" || command === "exit") {
cleanExit();
} else if (command === "help" || command === "h") {
console.log(colors.cyan("\nAvailable commands:"));
console.log(" restart, r - Restart the Electron process");
console.log(" quit, q - Exit the development environment");
console.log(" help, h - Show this help message\n");
} else if (command) {
log.warn(`Unknown command: ${command}`);
log.info('Type "help" to see available commands');
}
rl.prompt();
});
log.success("Development environment is running!");
log.info('Type "help" to see available commands');
}
main().catch((err) => {
log.error(`Error in development script: ${err}`);
cleanExit();
});