This repository was archived by the owner on Dec 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomScriptNodeStartServices.js
More file actions
117 lines (101 loc) · 2.8 KB
/
customScriptNodeStartServices.js
File metadata and controls
117 lines (101 loc) · 2.8 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
import {exec} from 'child_process';
import axios from 'axios';
const checkBackendHealth = async () => {
try {
await axios.get('http://localhost:3003/ping');
return true;
} catch (error) {
return false;
}
};
const openBrowser = (url) => {
switch (process.platform) {
case 'darwin':
exec(`open ${url}`);
console.log(`Identified macOS opening ${url}`)
break;
case 'win32':
exec(`start ${url}`);
console.log(`Identified Windows opening ${url}`)
break;
default:
exec(`xdg-open ${url}`);
console.log(`Identified Linux opening ${url}`)
}
};
const startBackend = () => {
return new Promise((resolve, reject) => {
const backend = exec('cd flightdiary-backend && npm install && npm run dev');
backend.stdout.on('data', (data) => {
console.log(`Backend: ${data}`);
if (data.includes('Server running')) {
resolve();
}
});
backend.stderr.on('data', (data) => {
console.error(`Backend Error: ${data}`);
reject();
});
});
};
const startFrontend = () => {
return new Promise((resolve, reject) => {
let localUrl;
const frontend = exec('npm install && npm run dev');
frontend.stdout.on('data', (data) => {
console.log(`Frontend: ${data}`);
if (data.includes('Local:')) {
localUrl = data.match(/http:\/\/[^\s]+/)[0]; // Extract URL from the console output
resolve(localUrl);
}
});
frontend.stderr.on('data', (data) => {
console.error(`Frontend Error: ${data}`);
reject();
});
});
};
const runTests = () => {
return new Promise((resolve, reject) => {
const testRunner = exec('npm run test');
testRunner.stdout.on('data', (data) => {
console.log(`Tests: ${data}`);
});
testRunner.stderr.on('data', (data) => {
console.error(`Test Error: ${data}`);
reject();
});
testRunner.on('close', (code) => {
if (code === 0) {
console.log('Tests completed successfully.');
resolve();
} else {
console.error(`Tests failed with exit code ${code}`);
reject(`Tests failed with exit code ${code}`);
}
});
});
};
const init = async () => {
try {
await startBackend();
console.log('Backend started sucessfully.\nPing backend start:')
let backendUp = false;
while (!backendUp) {
backendUp = await checkBackendHealth();
if (!backendUp) {
console.log('Waiting 2seconds for backend to start...');
await new Promise(resolve => setTimeout(resolve, 2000));
}
}
const localUrl = await startFrontend();
console.log('Frontend started successfully.');
if (localUrl) {
openBrowser(localUrl);
}
await runTests();
} catch (error) {
console.error('Failed to start services:', error);
}
};
init();