forked from AutoMaker-Org/automaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.mjs
More file actions
422 lines (363 loc) · 10.9 KB
/
init.mjs
File metadata and controls
422 lines (363 loc) · 10.9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env node
/**
* Automaker - Cross-Platform Development Environment Setup and Launch Script
*
* This script works on Windows, macOS, and Linux.
*/
import { execSync } from 'child_process';
import fs from 'fs';
import http from 'http';
import path from 'path';
import readline from 'readline';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const treeKill = require('tree-kill');
const crossSpawn = require('cross-spawn');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Colors for terminal output (works on modern terminals including Windows)
const colors = {
green: '\x1b[0;32m',
blue: '\x1b[0;34m',
yellow: '\x1b[1;33m',
red: '\x1b[0;31m',
reset: '\x1b[0m',
};
const isWindows = process.platform === 'win32';
// Track background processes for cleanup
let serverProcess = null;
let webProcess = null;
let electronProcess = null;
/**
* Print colored output
*/
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
/**
* Print the header banner
*/
function printHeader() {
console.log('╔═══════════════════════════════════════════════════════╗');
console.log('║ Automaker Development Environment ║');
console.log('╚═══════════════════════════════════════════════════════╝');
console.log('');
}
/**
* Execute a command synchronously and return stdout
*/
function execCommand(command, options = {}) {
try {
return execSync(command, {
encoding: 'utf8',
stdio: 'pipe',
...options,
}).trim();
} catch {
return null;
}
}
/**
* Get process IDs using a specific port (cross-platform)
*/
function getProcessesOnPort(port) {
const pids = new Set();
if (isWindows) {
// Windows: Use netstat to find PIDs
try {
const output = execCommand(`netstat -ano | findstr :${port}`);
if (output) {
const lines = output.split('\n');
for (const line of lines) {
// Match lines with LISTENING or ESTABLISHED on our port
const match = line.match(/:\d+\s+.*?(\d+)\s*$/);
if (match) {
const pid = parseInt(match[1], 10);
if (pid > 0) pids.add(pid);
}
}
}
} catch {
// Ignore errors
}
} else {
// Unix: Use lsof
try {
const output = execCommand(`lsof -ti:${port}`);
if (output) {
output.split('\n').forEach((pid) => {
const parsed = parseInt(pid.trim(), 10);
if (parsed > 0) pids.add(parsed);
});
}
} catch {
// Ignore errors
}
}
return Array.from(pids);
}
/**
* Kill a process by PID (cross-platform)
*/
function killProcess(pid) {
try {
if (isWindows) {
execCommand(`taskkill /F /PID ${pid}`);
} else {
process.kill(pid, 'SIGKILL');
}
return true;
} catch {
return false;
}
}
/**
* Kill processes on a port and wait for it to be freed
*/
async function killPort(port) {
const pids = getProcessesOnPort(port);
if (pids.length === 0) {
log(`✓ Port ${port} is available`, 'green');
return true;
}
log(`Killing process(es) on port ${port}: ${pids.join(', ')}`, 'yellow');
for (const pid of pids) {
killProcess(pid);
}
// Wait for port to be freed (max 5 seconds)
for (let i = 0; i < 10; i++) {
await sleep(500);
const remainingPids = getProcessesOnPort(port);
if (remainingPids.length === 0) {
log(`✓ Port ${port} is now free`, 'green');
return true;
}
}
log(`Warning: Port ${port} may still be in use`, 'red');
return false;
}
/**
* Sleep for a given number of milliseconds
*/
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Check if the server health endpoint is responding
*/
function checkHealth() {
return new Promise((resolve) => {
const req = http.get('http://localhost:3008/api/health', (res) => {
resolve(res.statusCode === 200);
});
req.on('error', () => resolve(false));
req.setTimeout(2000, () => {
req.destroy();
resolve(false);
});
});
}
/**
* Prompt the user for input
*/
function prompt(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
/**
* Run npm command using cross-spawn for Windows compatibility
*/
function runNpm(args, options = {}) {
const spawnOptions = {
stdio: 'inherit',
cwd: __dirname,
...options,
};
// cross-spawn handles Windows .cmd files automatically
return crossSpawn('npm', args, spawnOptions);
}
/**
* Run npx command using cross-spawn for Windows compatibility
*/
function runNpx(args, options = {}) {
const spawnOptions = {
stdio: 'inherit',
cwd: __dirname,
...options,
};
// cross-spawn handles Windows .cmd files automatically
return crossSpawn('npx', args, spawnOptions);
}
/**
* Kill a process tree using tree-kill
*/
function killProcessTree(pid) {
return new Promise((resolve) => {
if (!pid) {
resolve();
return;
}
treeKill(pid, 'SIGTERM', (err) => {
if (err) {
// Try force kill if graceful termination fails
treeKill(pid, 'SIGKILL', () => resolve());
} else {
resolve();
}
});
});
}
/**
* Cleanup function to kill all spawned processes
*/
async function cleanup() {
console.log('\nCleaning up...');
const killPromises = [];
if (serverProcess && !serverProcess.killed && serverProcess.pid) {
killPromises.push(killProcessTree(serverProcess.pid));
}
if (webProcess && !webProcess.killed && webProcess.pid) {
killPromises.push(killProcessTree(webProcess.pid));
}
if (electronProcess && !electronProcess.killed && electronProcess.pid) {
killPromises.push(killProcessTree(electronProcess.pid));
}
await Promise.all(killPromises);
}
/**
* Main function
*/
async function main() {
// Change to script directory
process.chdir(__dirname);
printHeader();
// Check if node_modules exists
if (!fs.existsSync(path.join(__dirname, 'node_modules'))) {
log('Installing dependencies...', 'blue');
const install = runNpm(['install'], { stdio: 'inherit' });
await new Promise((resolve, reject) => {
install.on('close', (code) => {
if (code === 0) resolve();
else reject(new Error(`npm install failed with code ${code}`));
});
});
}
// Install Playwright browsers from apps/ui where @playwright/test is installed
log('Checking Playwright browsers...', 'yellow');
try {
const exitCode = await new Promise((resolve) => {
const playwright = crossSpawn('npx', ['playwright', 'install', 'chromium'], {
stdio: 'inherit',
cwd: path.join(__dirname, 'apps', 'ui'),
});
playwright.on('close', (code) => resolve(code));
playwright.on('error', () => resolve(1));
});
if (exitCode === 0) {
log('Playwright browsers ready', 'green');
} else {
log('Playwright installation failed (browser automation may not work)', 'yellow');
}
} catch {
log('Playwright installation skipped', 'yellow');
}
// Kill any existing processes on required ports
log('Checking for processes on ports 3007 and 3008...', 'yellow');
await killPort(3007);
await killPort(3008);
console.log('');
// Show menu
console.log('═══════════════════════════════════════════════════════');
console.log(' Select Application Mode:');
console.log('═══════════════════════════════════════════════════════');
console.log(' 1) Web Application (Browser)');
console.log(' 2) Desktop Application (Electron)');
console.log('═══════════════════════════════════════════════════════');
console.log('');
// Setup cleanup handlers
let cleaningUp = false;
const handleExit = async (signal) => {
if (cleaningUp) return;
cleaningUp = true;
await cleanup();
process.exit(0);
};
process.on('SIGINT', () => handleExit('SIGINT'));
process.on('SIGTERM', () => handleExit('SIGTERM'));
// Prompt for choice
while (true) {
const choice = await prompt('Enter your choice (1 or 2): ');
if (choice === '1') {
console.log('');
log('Launching Web Application...', 'blue');
// Start the backend server
log('Starting backend server on port 3008...', 'blue');
// Create logs directory
if (!fs.existsSync(path.join(__dirname, 'logs'))) {
fs.mkdirSync(path.join(__dirname, 'logs'), { recursive: true });
}
// Start server in background
const logStream = fs.createWriteStream(path.join(__dirname, 'logs', 'server.log'));
serverProcess = runNpm(['run', 'dev:server'], {
stdio: ['ignore', 'pipe', 'pipe'],
});
serverProcess.stdout?.pipe(logStream);
serverProcess.stderr?.pipe(logStream);
log('Waiting for server to be ready...', 'yellow');
// Wait for server health check
const maxRetries = 30;
let serverReady = false;
for (let i = 0; i < maxRetries; i++) {
if (await checkHealth()) {
serverReady = true;
break;
}
process.stdout.write('.');
await sleep(1000);
}
console.log('');
if (!serverReady) {
log('Error: Server failed to start', 'red');
console.log('Check logs/server.log for details');
cleanup();
process.exit(1);
}
log('✓ Server is ready!', 'green');
log(`The application will be available at: http://localhost:3007`, 'green');
console.log('');
// Start web app
webProcess = runNpm(['run', 'dev:web'], { stdio: 'inherit' });
await new Promise((resolve) => {
webProcess.on('close', resolve);
});
break;
} else if (choice === '2') {
console.log('');
log('Launching Desktop Application...', 'blue');
log('(Electron will start its own backend server)', 'yellow');
console.log('');
electronProcess = runNpm(['run', 'dev:electron'], { stdio: 'inherit' });
await new Promise((resolve) => {
electronProcess.on('close', resolve);
});
break;
} else {
log('Invalid choice. Please enter 1 or 2.', 'red');
}
}
}
// Run main function
main().catch((err) => {
console.error(err);
cleanup();
process.exit(1);
});