-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (122 loc) · 3.89 KB
/
index.js
File metadata and controls
141 lines (122 loc) · 3.89 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
const { execSync, spawn } = require("child_process");
const net = require("net");
const {join, resolve} = require("node:path");
const {existsSync, writeFileSync} = require("node:fs");
// Configuration
const MYSQL_PORT = 3306;
const MYSQL_CONTAINER_NAME = "mysql";
const BACKEND_PATH = "./backend";
const SQLITE_DB_PATH = resolve(join(__dirname, "backend"), "database", "database.sqlite");
const shouldSeed = process.argv.includes("--seed");
function getComposerCommand() {
try {
const command = process.platform === "win32" ? "where composer" : "which composer";
const composerPath = execSync(command).toString().trim();
if (!composerPath) {
throw new Error();
}
if (process.platform === "win32") {
const list = composerPath.split("\r\n")
const batIndex = list.findIndex(path => path.endsWith(".bat"));
if (batIndex > -1) {
return list[0]
}
return list[batIndex]
}
return composerPath;
} catch (error) {
console.error("❌ ERROR: Composer is not installed or not found in system PATH.");
process.exit(1);
}
}
function isMySQLRunning() {
return new Promise((resolve) => {
const socket = net.createConnection(MYSQL_PORT, "127.0.0.1", () => {
socket.end();
resolve(true);
});
socket.on("error", () => {
resolve(false);
});
});
}
function isDockerAvailable() {
try {
execSync("docker info", { stdio: "ignore" });
return true;
} catch (error) {
return false;
}
}
function startMySQLContainer() {
console.log("Starting MySQL container...");
try {
execSync(
`docker run -d --name ${MYSQL_CONTAINER_NAME} --restart unless-stopped -e MYSQL_ROOT_PASSWORD=rootpassword -e MYSQL_DATABASE=laravel_db -e MYSQL_USER=laravel_user -e MYSQL_PASSWORD=laravel_pass -v mysql_data:/var/lib/mysql -p 3306:3306 mysql:8.0`,
{ stdio: "inherit" }
);
console.log("MySQL container started successfully.");
} catch (error) {
console.error("Failed to start MySQL container:", error.message);
process.exit(1);
}
}
(async () => {
const mysqlRunning = await isMySQLRunning();
const env = {...process.env};
if (mysqlRunning) {
console.log("✅ MySQL is already running on port 3306.");
env.DB_DATABASE='laravel_db'
env.DB_USERNAME='laravel_user'
env.DB_PASSWORD='laravel_pass'
} else {
console.log("⚠️ MySQL is not running on port 3306.");
if (isDockerAvailable()) {
env.DB_DATABASE='laravel_db'
env.DB_USERNAME='laravel_user'
env.DB_PASSWORD='laravel_pass'
startMySQLContainer();
} else {
console.log("❌ Docker is not running. Switching to SQLite.");
env.DB_CONNECTION = "sqlite";
env.DB_DATABASE = SQLITE_DB_PATH;
// Ensure SQLite database file exists
if (!existsSync(SQLITE_DB_PATH)) {
console.log(`🔧 Creating SQLite database at ${SQLITE_DB_PATH}`);
writeFileSync(SQLITE_DB_PATH, "");
}
}
}
const COMPOSER_CMD = getComposerCommand();
const processOptions = {
cwd: BACKEND_PATH,
env: env,
stdio: "inherit",
shell: true
}
console.log("🚀 Starting backend...");
spawn(COMPOSER_CMD, ["install"], processOptions);
console.log("📜 Running database migrations...");
try {
execSync("php artisan migrate ", processOptions);
} catch (error) {
console.error("❌ ERROR: Failed to run migrations:", error.message);
process.exit(1);
}
if (shouldSeed) {
console.log("🌱 Running database seeder...");
try {
execSync("php artisan db:seed", processOptions);
} catch (error) {
console.error("❌ ERROR: Failed to seed database:", error.message);
process.exit(1);
}
}
const backendProcess = spawn("php", ["artisan", "serve", "--host", "0.0.0.0"], {
...processOptions,
shell: undefined
});
backendProcess.on("close", (code) => {
console.log(`Backend process exited with code ${code}`);
});
})();