drizzle-auto 1.0.12 → 1.0.13
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 +98 -110
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,134 +5,122 @@ const path = require("path");
|
|
|
5
5
|
const chokidar = require("chokidar");
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
|
-
/*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
====================== */
|
|
21
|
-
const FRAMES = ["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"];
|
|
22
|
-
let i=0, spin;
|
|
23
|
-
const start = txt => {
|
|
24
|
-
stop();
|
|
25
|
-
spin = setInterval(()=>{
|
|
26
|
-
process.stdout.write(`\r${color()}${FRAMES[i++%FRAMES.length]} ${txt}${RESET}`);
|
|
27
|
-
},80);
|
|
28
|
-
};
|
|
29
|
-
const stop = ()=>{ if(spin){ clearInterval(spin); spin=null; process.stdout.write("\r\x1b[K"); }};
|
|
30
|
-
|
|
31
|
-
/* ======================
|
|
32
|
-
📦 ROOT + SCRIPT INJECT
|
|
33
|
-
====================== */
|
|
34
|
-
const ROOT = process.env.INIT_CWD || process.cwd();
|
|
35
|
-
const PKG = path.join(ROOT,"package.json");
|
|
36
|
-
|
|
37
|
-
function injectTea(){
|
|
38
|
-
if(!fs.existsSync(PKG)) return;
|
|
39
|
-
const pkg = JSON.parse(fs.readFileSync(PKG,"utf8"));
|
|
8
|
+
/* =========================
|
|
9
|
+
🧭 PROJECT ROOT
|
|
10
|
+
========================= */
|
|
11
|
+
const ROOT = process.cwd();
|
|
12
|
+
const PKG = path.join(ROOT, "package.json");
|
|
13
|
+
|
|
14
|
+
/* =========================
|
|
15
|
+
🧩 SCRIPT INJECTOR
|
|
16
|
+
========================= */
|
|
17
|
+
function injectScript() {
|
|
18
|
+
if (!fs.existsSync(PKG)) return;
|
|
19
|
+
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
40
20
|
pkg.scripts ||= {};
|
|
41
|
-
|
|
21
|
+
|
|
22
|
+
if (!pkg.scripts.tea) {
|
|
42
23
|
pkg.scripts.tea = "drizzle-auto";
|
|
43
|
-
fs.writeFileSync(PKG, JSON.stringify(pkg,null,2));
|
|
44
|
-
console.log(
|
|
24
|
+
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
25
|
+
console.log("☕ Script added → npm run tea");
|
|
45
26
|
}
|
|
46
27
|
}
|
|
47
28
|
|
|
48
|
-
/*
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
.find(f=>fs.existsSync(path.join(ROOT,f)));
|
|
29
|
+
/* =========================
|
|
30
|
+
🔍 DRIZZLE DETECTOR
|
|
31
|
+
========================= */
|
|
32
|
+
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
|
+
}
|
|
55
41
|
|
|
56
|
-
|
|
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 {}
|
|
57
48
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
50
|
+
console.log("❌ Schema file missing");
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
61
53
|
|
|
62
|
-
|
|
63
|
-
|
|
54
|
+
return { config: configs, schema: schemaPath };
|
|
55
|
+
}
|
|
64
56
|
|
|
65
|
-
|
|
66
|
-
|
|
57
|
+
/* =========================
|
|
58
|
+
⚙️ SAFE COMMAND RUNNER
|
|
59
|
+
========================= */
|
|
60
|
+
function run(cmd) {
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
let failed = false;
|
|
67
63
|
|
|
68
|
-
|
|
69
|
-
return fail("node_modules missing");
|
|
64
|
+
const p = spawn("npx", cmd, { shell: true });
|
|
70
65
|
|
|
71
|
-
|
|
72
|
-
|
|
66
|
+
p.stdout.on("data", d => process.stdout.write(d));
|
|
67
|
+
p.stderr.on("data", d => {
|
|
68
|
+
failed = true;
|
|
69
|
+
process.stderr.write(d);
|
|
70
|
+
});
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
⚙️ RUNNER
|
|
78
|
-
====================== */
|
|
79
|
-
const run = cmd => new Promise(res=>{
|
|
80
|
-
start("npx "+cmd.join(" "));
|
|
81
|
-
const p = spawn("npx", cmd, { stdio:"inherit", shell:true });
|
|
82
|
-
p.on("close", code=>{
|
|
83
|
-
stop();
|
|
84
|
-
res(code===0);
|
|
72
|
+
p.on("close", code => {
|
|
73
|
+
resolve(code === 0 && !failed);
|
|
74
|
+
});
|
|
85
75
|
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
let busy=false;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
busy
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
console.log(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
console.log("\x1b[31m❌ "+a.msg+RESET);
|
|
103
|
-
busy=false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/* =========================
|
|
79
|
+
🔁 PIPELINE (NON-KILLING)
|
|
80
|
+
========================= */
|
|
81
|
+
let busy = false;
|
|
82
|
+
|
|
83
|
+
async function pipeline(trigger) {
|
|
84
|
+
if (busy) return;
|
|
85
|
+
busy = true;
|
|
86
|
+
|
|
87
|
+
console.log(`\n⚡ Trigger → ${trigger}`);
|
|
88
|
+
|
|
89
|
+
const drizzle = detectDrizzle();
|
|
90
|
+
if (!drizzle) {
|
|
91
|
+
busy = false;
|
|
104
92
|
return;
|
|
105
93
|
}
|
|
106
94
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
95
|
+
const genOK = await run(["drizzle-kit", "generate"]);
|
|
96
|
+
if (!genOK) {
|
|
97
|
+
console.log("🛑 Generate failed — pipeline paused");
|
|
98
|
+
busy = false;
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const pushOK = await run(["drizzle-kit", "push"]);
|
|
103
|
+
if (!pushOK) {
|
|
104
|
+
console.log("🛑 Push failed — server still running");
|
|
105
|
+
busy = false;
|
|
106
|
+
return;
|
|
113
107
|
}
|
|
114
108
|
|
|
115
|
-
console.log("
|
|
116
|
-
busy=false;
|
|
109
|
+
console.log("✨ Drizzle fully synced");
|
|
110
|
+
busy = false;
|
|
117
111
|
}
|
|
118
112
|
|
|
119
|
-
/*
|
|
120
|
-
👀
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
pipeline("
|
|
124
|
-
|
|
125
|
-
chokidar.watch(ROOT,{
|
|
126
|
-
ignored:[/node_modules
|
|
127
|
-
ignoreInitial:true,
|
|
128
|
-
usePolling:true,
|
|
129
|
-
interval:300
|
|
130
|
-
}).on("all",(
|
|
131
|
-
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
process.on("uncaughtException",e=>{
|
|
135
|
-
stop();
|
|
136
|
-
console.log("\x1b[31m🔥 Crash:",e.message,RESET);
|
|
137
|
-
busy=false;
|
|
113
|
+
/* =========================
|
|
114
|
+
👀 WATCHER (ALWAYS ALIVE)
|
|
115
|
+
========================= */
|
|
116
|
+
injectScript();
|
|
117
|
+
pipeline("Initial");
|
|
118
|
+
|
|
119
|
+
chokidar.watch(ROOT, {
|
|
120
|
+
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
121
|
+
ignoreInitial: true,
|
|
122
|
+
usePolling: true,
|
|
123
|
+
interval: 300
|
|
124
|
+
}).on("all", (_, file) => {
|
|
125
|
+
pipeline(path.basename(file));
|
|
138
126
|
});
|