drizzle-auto 1.0.20 ā 1.0.22
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 +100 -85
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,122 +6,137 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
8
|
/* =========================
|
|
9
|
-
|
|
9
|
+
šØ COLOR CONFIG
|
|
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
|
+
|
|
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
|
+
š§ PROJECT ROOT
|
|
10
21
|
========================= */
|
|
11
22
|
const ROOT = process.cwd();
|
|
12
23
|
const PKG = path.join(ROOT, "package.json");
|
|
13
24
|
|
|
14
25
|
/* =========================
|
|
15
|
-
š§© SCRIPT INJECTOR
|
|
26
|
+
š§© SCRIPT INJECTOR
|
|
16
27
|
========================= */
|
|
17
28
|
function injectScript() {
|
|
18
|
-
if (!fs.existsSync(PKG)) return;
|
|
19
|
-
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
20
|
-
pkg.scripts ||= {};
|
|
21
|
-
|
|
22
|
-
if (!pkg.scripts.tea) {
|
|
23
|
-
pkg.scripts.tea = "drizzle-auto";
|
|
24
|
-
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
25
|
-
console.log("ā Script added ā npm run tea");
|
|
26
|
-
}
|
|
29
|
+
if (!fs.existsSync(PKG)) return;
|
|
30
|
+
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
31
|
+
pkg.scripts ||= {};
|
|
32
|
+
|
|
33
|
+
if (!pkg.scripts.tea) {
|
|
34
|
+
pkg.scripts.tea = "drizzle-auto";
|
|
35
|
+
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
36
|
+
console.log(rainbow("ā Script added ā npm run tea"));
|
|
37
|
+
}
|
|
27
38
|
}
|
|
28
39
|
|
|
29
40
|
/* =========================
|
|
30
|
-
š DRIZZLE DETECTOR
|
|
41
|
+
š DRIZZLE DETECTOR
|
|
31
42
|
========================= */
|
|
32
43
|
function detectDrizzle() {
|
|
33
|
-
const configs = ["js","mjs","ts","mts"]
|
|
34
|
-
.map(e => drizzle.config.${e})
|
|
35
|
-
.find(f => fs.existsSync(path.join(ROOT, f)));
|
|
36
|
-
|
|
37
|
-
if (!configs) {
|
|
38
|
-
console.log("ā ļø No drizzle.config found");
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let schemaPath = null;
|
|
43
|
-
try {
|
|
44
|
-
const content = fs.readFileSync(path.join(ROOT, configs), "utf8");
|
|
45
|
-
const match = content.match(/schema:\s*"'["']/);
|
|
46
|
-
if (match) schemaPath = path.join(ROOT, match[1]);
|
|
47
|
-
} catch {}
|
|
48
|
-
|
|
49
|
-
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
50
|
-
console.log("ā Schema file missing");
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return { config: configs, schema: schemaPath };
|
|
44
|
+
const configs = ["js","mjs","ts","mts"]
|
|
45
|
+
.map(e => `drizzle.config.${e}`)
|
|
46
|
+
.find(f => fs.existsSync(path.join(ROOT, f)));
|
|
47
|
+
|
|
48
|
+
if (!configs) {
|
|
49
|
+
console.log(rainbow("ā ļø No drizzle.config found"));
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let schemaPath = null;
|
|
54
|
+
try {
|
|
55
|
+
const content = fs.readFileSync(path.join(ROOT, configs), "utf8");
|
|
56
|
+
const match = content.match(/schema:\s*["'](.+?)["']/);
|
|
57
|
+
if (match) schemaPath = path.join(ROOT, match[1]);
|
|
58
|
+
} catch {}
|
|
59
|
+
|
|
60
|
+
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
61
|
+
console.log(rainbow("ā Schema file missing"));
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { config: configs, schema: schemaPath };
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
/* =========================
|
|
58
|
-
āļø SAFE COMMAND RUNNER
|
|
69
|
+
āļø SAFE COMMAND RUNNER
|
|
59
70
|
========================= */
|
|
60
71
|
function run(cmd) {
|
|
61
|
-
return new Promise((resolve) => {
|
|
62
|
-
let failed = false;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
p.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
resolve(code === 0 && !failed);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
});
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
let failed = false;
|
|
74
|
+
const p = spawn("npx", cmd, { shell: true });
|
|
75
|
+
|
|
76
|
+
p.stdout.on("data", d => process.stdout.write(d));
|
|
77
|
+
p.stderr.on("data", d => {
|
|
78
|
+
failed = true;
|
|
79
|
+
process.stderr.write("\x1b[31m" + d + RESET);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
p.on("close", code => resolve(code === 0 && !failed));
|
|
83
|
+
});
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
/* =========================
|
|
80
|
-
š PIPELINE (NON-KILLING)
|
|
87
|
+
š PIPELINE (NON-KILLING)
|
|
81
88
|
========================= */
|
|
82
89
|
let busy = false;
|
|
83
90
|
|
|
84
91
|
async function pipeline(trigger) {
|
|
85
|
-
if (busy) return;
|
|
86
|
-
busy = true;
|
|
87
|
-
|
|
88
|
-
console.log(
|
|
89
|
-
|
|
90
|
-
const drizzle = detectDrizzle();
|
|
91
|
-
if (!drizzle) {
|
|
92
|
-
busy = false;
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const genOK = await run(["drizzle-kit", "generate"]);
|
|
97
|
-
if (!genOK) {
|
|
98
|
-
console.log("š Generate failed ā pipeline paused");
|
|
99
|
-
busy = false;
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const pushOK = await run(["drizzle-kit", "push"]);
|
|
104
|
-
if (!pushOK) {
|
|
105
|
-
console.log("š Push failed ā server still running");
|
|
106
|
-
busy = false;
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
console.log("⨠Drizzle fully synced");
|
|
111
|
-
busy = false;
|
|
92
|
+
if (busy) return;
|
|
93
|
+
busy = true;
|
|
94
|
+
|
|
95
|
+
console.log(`\n${BOLD}${nextColor()}ā” Trigger ā ${trigger}${RESET}`);
|
|
96
|
+
|
|
97
|
+
const drizzle = detectDrizzle();
|
|
98
|
+
if (!drizzle) {
|
|
99
|
+
busy = false;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const genOK = await run(["drizzle-kit", "generate"]);
|
|
104
|
+
if (!genOK) {
|
|
105
|
+
console.log(rainbow("š Generate failed ā pipeline paused"));
|
|
106
|
+
busy = false;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const pushOK = await run(["drizzle-kit", "push"]);
|
|
111
|
+
if (!pushOK) {
|
|
112
|
+
console.log(rainbow("š Push failed ā server still running"));
|
|
113
|
+
busy = false;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
console.log(rainbow("⨠Drizzle fully synced"));
|
|
118
|
+
busy = false;
|
|
112
119
|
}
|
|
113
120
|
|
|
114
121
|
/* =========================
|
|
115
|
-
š WATCHER (ALWAYS ALIVE)
|
|
122
|
+
š WATCHER (ALWAYS ALIVE)
|
|
116
123
|
========================= */
|
|
117
124
|
injectScript();
|
|
118
125
|
pipeline("Initial");
|
|
119
126
|
|
|
120
127
|
chokidar.watch(ROOT, {
|
|
121
|
-
ignored: [/node_modules/,
|
|
122
|
-
ignoreInitial: true,
|
|
123
|
-
usePolling: true,
|
|
124
|
-
interval: 300
|
|
128
|
+
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
129
|
+
ignoreInitial: true,
|
|
130
|
+
usePolling: true,
|
|
131
|
+
interval: 300
|
|
125
132
|
}).on("all", (_, file) => {
|
|
126
|
-
pipeline(path.basename(file));
|
|
133
|
+
pipeline(path.basename(file));
|
|
127
134
|
});
|
|
135
|
+
|
|
136
|
+
/* =========================
|
|
137
|
+
šØ HANDLE CRASHES
|
|
138
|
+
========================= */
|
|
139
|
+
process.on("uncaughtException", (err) => {
|
|
140
|
+
console.log("\x1b[31mš„ Crash:\x1b[0m", err.message);
|
|
141
|
+
busy = false;
|
|
142
|
+
});
|