-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-script.js
More file actions
200 lines (176 loc) · 4.73 KB
/
dev-script.js
File metadata and controls
200 lines (176 loc) · 4.73 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
#!/usr/bin/env node
const { spawn } = require("child_process");
const fs = require("fs");
const path = require("path");
const COMMANDS = {
setup: "Configura o projeto",
build: "Build de produção",
test: "Executa todos os testes",
dev: "Inicia ambiente de desenvolvimento",
docker: "Inicia containers",
clean: "Remove artefatos",
quality: "Executa análise com Sonar",
};
function showHelp() {
console.log("Uso: npm run dev-script <comando>");
Object.entries(COMMANDS).forEach(([cmd, desc]) => {
console.log(` ${cmd.padEnd(10)} - ${desc}`);
});
}
function execCmd(command, cwd = process.cwd()) {
return new Promise((resolve, reject) => {
const child = spawn(command, {
shell: true,
stdio: "inherit",
cwd,
});
child.on("close", (code) => {
if (code === 0) resolve();
else reject(new Error(`Falha ${code}`));
});
});
}
function checkDependencies() {
const deps = ["docker", "dotnet", "node"];
for (const dep of deps) {
try {
require("child_process").execSync(`${dep} --version`, {
stdio: "ignore",
});
} catch {
console.error(`${dep} não encontrado`);
process.exit(1);
}
}
}
function ensureEnvFile() {
const envPath = path.join(process.cwd(), ".env");
const envExamplePath = path.join(process.cwd(), ".env.example");
if (!fs.existsSync(envPath)) {
if (fs.existsSync(envExamplePath)) {
console.log("Criando arquivo .env baseado no .env.example");
fs.copyFileSync(envExamplePath, envPath);
console.log("Arquivo .env criado");
console.log("Configure as variáveis de ambiente conforme necessário.");
} else {
console.error("Arquivo .env.example não encontrado");
process.exit(1);
}
}
}
async function setup() {
checkDependencies();
ensureEnvFile();
console.log("Tenha o docker em execução na sua máquina.");
await execCmd("dotnet restore", "api");
await execCmd("dotnet build", "api");
if (fs.existsSync("web/node_modules"))
fs.rmSync("web/node_modules", { recursive: true, force: true });
if (fs.existsSync("web/package-lock.json"))
fs.unlinkSync("web/package-lock.json");
await execCmd("npm install", "web");
await execCmd("docker compose up db rabbitmq -d");
}
async function build() {
await execCmd("dotnet build --configuration Release", "api");
await execCmd("npm run build", "web");
}
async function test() {
await execCmd("dotnet test --configuration Release", "api");
await execCmd("npm test", "web");
await execCmd("npm run lint", "web");
}
async function dev() {
ensureEnvFile();
await execCmd("docker compose up db rabbitmq -d");
console.log("Execute:");
console.log(" cd api && dotnet run --project Ecommerce.Api");
console.log(" cd web && npm run dev");
}
async function docker() {
ensureEnvFile();
console.log("Tenha o docker em execução na sua máquina.");
await execCmd("docker compose up --build");
}
async function clean() {
const paths = [
"api/bin",
"api/obj",
"web/dist",
"web/node_modules",
"web/coverage",
];
for (const p of paths) {
if (fs.existsSync(p)) {
fs.rmSync(p, { recursive: true, force: true });
}
}
}
async function quality() {
try {
require("child_process").execSync(
"curl -s http://localhost:9000/api/system/status",
{ stdio: "ignore" }
);
} catch {
await execCmd("docker compose up sonarqube -d");
let retries = 30;
while (retries > 0) {
try {
require("child_process").execSync(
'curl -s http://localhost:9000/api/system/status | grep -q "UP"',
{ stdio: "ignore" }
);
break;
} catch {
await new Promise((r) => setTimeout(r, 10000));
retries--;
}
}
}
await execCmd(
'dotnet sonarscanner begin /k:"ecommerce-api" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="admin" /d:sonar.password="admin"',
"api"
);
await execCmd("dotnet build", "api");
await execCmd(
'dotnet sonarscanner end /d:sonar.login="admin" /d:sonar.password="admin"',
"api"
);
await execCmd("npm run sonar", "web");
}
const command = process.argv[2];
if (!command || !COMMANDS[command]) {
showHelp();
process.exit(1);
}
(async () => {
try {
switch (command) {
case "setup":
await setup();
break;
case "build":
await build();
break;
case "test":
await test();
break;
case "dev":
await dev();
break;
case "docker":
await docker();
break;
case "clean":
await clean();
break;
case "quality":
await quality();
break;
}
} catch (error) {
console.error("Erro:", error.message);
process.exit(1);
}
})();