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