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