drizzle-auto 1.0.7 โ 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +147 -59
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,85 +3,173 @@
|
|
|
3
3
|
const fs = require("fs");
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const chokidar = require("chokidar");
|
|
6
|
-
const spawn = require("
|
|
7
|
-
|
|
8
|
-
/*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
6
|
+
const { spawn } = require("child_process");
|
|
7
|
+
|
|
8
|
+
/* =======================
|
|
9
|
+
๐จ NeonPulse Colors
|
|
10
|
+
======================= */
|
|
11
|
+
const COLORS = ["\x1b[31m","\x1b[32m","\x1b[33m","\x1b[34m","\x1b[35m","\x1b[36m"];
|
|
12
|
+
const RESET = "\x1b[0m";
|
|
13
|
+
const BOLD = "\x1b[1m";
|
|
14
|
+
const DIM = "\x1b[90m";
|
|
15
|
+
let colorIndex = 0;
|
|
16
|
+
const nextColor = () => COLORS[colorIndex++ % COLORS.length];
|
|
17
|
+
const rainbow = text => text.split("").map(c => `${nextColor()}${c}`).join("") + RESET;
|
|
18
|
+
|
|
19
|
+
/* =======================
|
|
20
|
+
โณ Spinner
|
|
21
|
+
======================= */
|
|
22
|
+
const FRAMES = ["โ ","โ ","โ น","โ ธ","โ ผ","โ ด","โ ฆ","โ ง","โ ","โ "];
|
|
23
|
+
let spinIndex = 0;
|
|
24
|
+
let spinnerInterval = null;
|
|
25
|
+
function startSpinner(msg) {
|
|
26
|
+
stopSpinner();
|
|
27
|
+
spinnerInterval = setInterval(() => {
|
|
28
|
+
process.stdout.write(`\r${nextColor()}${FRAMES[spinIndex++ % FRAMES.length]} ${msg}${RESET}`);
|
|
29
|
+
}, 80);
|
|
30
|
+
}
|
|
31
|
+
function stopSpinner() {
|
|
32
|
+
if (spinnerInterval) {
|
|
33
|
+
clearInterval(spinnerInterval);
|
|
34
|
+
spinnerInterval = null;
|
|
35
|
+
process.stdout.write("\r\x1b[K");
|
|
36
|
+
}
|
|
17
37
|
}
|
|
18
38
|
|
|
19
|
-
|
|
20
|
-
|
|
39
|
+
/* =======================
|
|
40
|
+
๐ง Project Root + Package.json
|
|
41
|
+
======================= */
|
|
42
|
+
const PROJECT_ROOT = process.env.INIT_CWD || process.cwd();
|
|
43
|
+
const PKG_PATH = path.join(PROJECT_ROOT, "package.json");
|
|
21
44
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
function injectTeaScript() {
|
|
26
|
-
if (!fs.existsSync(pkgPath)) return;
|
|
27
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
45
|
+
function addTeaScript() {
|
|
46
|
+
if (!fs.existsSync(PKG_PATH)) return;
|
|
47
|
+
const pkg = JSON.parse(fs.readFileSync(PKG_PATH, "utf8"));
|
|
28
48
|
pkg.scripts ||= {};
|
|
29
49
|
if (!pkg.scripts.tea) {
|
|
30
50
|
pkg.scripts.tea = "drizzle-auto";
|
|
31
|
-
fs.writeFileSync(
|
|
32
|
-
console.log(
|
|
51
|
+
fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2));
|
|
52
|
+
console.log(`โก tea script added โ npm run tea`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* =======================
|
|
57
|
+
๐งช Infra Audit
|
|
58
|
+
======================= */
|
|
59
|
+
function auditInfra() {
|
|
60
|
+
const results = [];
|
|
61
|
+
|
|
62
|
+
// Drizzle config
|
|
63
|
+
const configFile = ["ts","js","mjs","mts"].map(ext => `drizzle.config.${ext}`)
|
|
64
|
+
.find(f => fs.existsSync(path.join(PROJECT_ROOT, f)));
|
|
65
|
+
results.push({ name: "Drizzle Config", ok: !!configFile });
|
|
66
|
+
|
|
67
|
+
// Schema path
|
|
68
|
+
if (configFile) {
|
|
69
|
+
try {
|
|
70
|
+
const cfg = fs.readFileSync(path.join(PROJECT_ROOT, configFile), "utf8");
|
|
71
|
+
const match = cfg.match(/schema:\s*["'](.+?)["']/);
|
|
72
|
+
if (match) {
|
|
73
|
+
results.push({
|
|
74
|
+
name: `Schema (${match[1]})`,
|
|
75
|
+
ok: fs.existsSync(path.join(PROJECT_ROOT, match[1]))
|
|
76
|
+
});
|
|
77
|
+
} else results.push({ name: "Schema Path", ok: false });
|
|
78
|
+
} catch {
|
|
79
|
+
results.push({ name: "Config Readable", ok: false });
|
|
80
|
+
}
|
|
33
81
|
}
|
|
82
|
+
|
|
83
|
+
// Essentials
|
|
84
|
+
results.push({ name: ".env", ok: fs.existsSync(path.join(PROJECT_ROOT, ".env")) });
|
|
85
|
+
results.push({ name: "node_modules", ok: fs.existsSync(path.join(PROJECT_ROOT, "node_modules")) });
|
|
86
|
+
|
|
87
|
+
const missing = results.filter(r => !r.ok).map(r => r.name);
|
|
88
|
+
return { ok: missing.length === 0, missing };
|
|
34
89
|
}
|
|
35
90
|
|
|
36
|
-
/*
|
|
37
|
-
|
|
38
|
-
|
|
91
|
+
/* =======================
|
|
92
|
+
โก Run npx command
|
|
93
|
+
======================= */
|
|
94
|
+
function runCmd(cmd) {
|
|
95
|
+
return new Promise(resolve => {
|
|
96
|
+
let failed = false;
|
|
97
|
+
startSpinner(`npx ${cmd.join(" ")}`);
|
|
98
|
+
const p = spawn("npx", cmd, { shell: true });
|
|
99
|
+
p.stdout.on("data", d => {
|
|
100
|
+
stopSpinner();
|
|
101
|
+
process.stdout.write(DIM + d.toString() + RESET);
|
|
102
|
+
if (/error|failed/i.test(d)) failed = true;
|
|
103
|
+
});
|
|
104
|
+
p.stderr.on("data", d => {
|
|
105
|
+
stopSpinner();
|
|
106
|
+
process.stderr.write("\x1b[31m" + d + RESET);
|
|
107
|
+
failed = true;
|
|
108
|
+
});
|
|
109
|
+
p.on("close", code => resolve(code === 0 && !failed));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/* =======================
|
|
114
|
+
๐ Workflow
|
|
115
|
+
======================= */
|
|
39
116
|
let running = false;
|
|
117
|
+
let hasError = false;
|
|
40
118
|
|
|
41
|
-
function
|
|
119
|
+
async function workflow(trigger) {
|
|
42
120
|
if (running) return;
|
|
43
121
|
running = true;
|
|
44
|
-
console.clear();
|
|
45
|
-
console.log("๐ Drizzle Pipeline starting...");
|
|
46
122
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
shell: true,
|
|
50
|
-
cwd: projectRoot,
|
|
51
|
-
});
|
|
123
|
+
if (!hasError) console.clear();
|
|
124
|
+
console.log(`\n${nextColor()}โก Trigger โ ${trigger}${RESET}`);
|
|
52
125
|
|
|
53
|
-
|
|
126
|
+
const audit = auditInfra();
|
|
127
|
+
if (!audit.ok) {
|
|
128
|
+
console.log("\n\x1b[31mโ Infra not ready\x1b[0m");
|
|
129
|
+
audit.missing.forEach(m => console.log(DIM + "โข " + m + RESET));
|
|
130
|
+
hasError = true;
|
|
54
131
|
running = false;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
58
|
-
}
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
59
134
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
135
|
+
const steps = [
|
|
136
|
+
["drizzle-kit", "check"],
|
|
137
|
+
["drizzle-kit", "generate"],
|
|
138
|
+
["drizzle-kit", "push"]
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
for (const step of steps) {
|
|
142
|
+
if (!(await runCmd(step))) {
|
|
143
|
+
console.log("\n\x1b[31m๐ Pipeline crashed\x1b[0m");
|
|
144
|
+
hasError = true;
|
|
145
|
+
running = false;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
hasError = false;
|
|
151
|
+
console.log("\n\x1b[32mโจ Drizzle synced\x1b[0m");
|
|
152
|
+
setTimeout(() => running = false, 1000);
|
|
66
153
|
}
|
|
67
154
|
|
|
68
|
-
/*
|
|
69
|
-
๐ Watcher
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
155
|
+
/* =======================
|
|
156
|
+
๐ Watcher
|
|
157
|
+
======================= */
|
|
158
|
+
chokidar.watch(PROJECT_ROOT, {
|
|
159
|
+
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
160
|
+
ignoreInitial: true,
|
|
161
|
+
usePolling: true,
|
|
162
|
+
interval: 300
|
|
163
|
+
}).on("all", (e, f) => workflow(`${e.toUpperCase()} โ ${path.basename(f)}`));
|
|
164
|
+
|
|
165
|
+
/* =======================
|
|
166
|
+
๐ Init
|
|
167
|
+
======================= */
|
|
168
|
+
addTeaScript(); // inject tea script automatically
|
|
169
|
+
workflow("Initial Audit");
|
|
83
170
|
|
|
84
|
-
process.on("uncaughtException",
|
|
171
|
+
process.on("uncaughtException", err => {
|
|
172
|
+
stopSpinner();
|
|
85
173
|
console.log("\x1b[31m๐ฅ Crash:\x1b[0m", err.message);
|
|
86
174
|
running = false;
|
|
87
175
|
});
|