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.
Files changed (2) hide show
  1. package/index.js +98 -110
  2. 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
- 🌈 NEON CORE
10
- ====================== */
11
- const COLORS = ["\x1b[31m","\x1b[32m","\x1b[33m","\x1b[34m","\x1b[35m","\x1b[36m"];
12
- const RESET = "\x1b[0m";
13
- const DIM = "\x1b[90m";
14
- let c = 0;
15
- const color = () => COLORS[c++ % COLORS.length];
16
- const rainbow = t => t.split("").map(x => color()+x).join("")+RESET;
17
-
18
- /* ======================
19
- SPINNER
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
- if(!pkg.scripts.tea){
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(color()+" script added → npm run tea"+RESET);
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
- 🧠 DRIZZLE SCAN
50
- ====================== */
51
- function audit(){
52
- const exts = ["js","ts","mjs","mts"];
53
- const cfg = exts.map(e=>`drizzle.config.${e}`)
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
- if(!cfg) return fail("Drizzle config not found");
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
- const txt = fs.readFileSync(path.join(ROOT,cfg),"utf8");
59
- const m = txt.match(/schema:\s*["'](.+?)["']/);
60
- if(!m) return fail("Schema path missing");
49
+ if (schemaPath && !fs.existsSync(schemaPath)) {
50
+ console.log("❌ Schema file missing");
51
+ return null;
52
+ }
61
53
 
62
- if(!fs.existsSync(path.join(ROOT,m[1])))
63
- return fail(`Schema not found → ${m[1]}`);
54
+ return { config: configs, schema: schemaPath };
55
+ }
64
56
 
65
- if(!fs.existsSync(path.join(ROOT,".env")))
66
- return fail(".env missing");
57
+ /* =========================
58
+ ⚙️ SAFE COMMAND RUNNER
59
+ ========================= */
60
+ function run(cmd) {
61
+ return new Promise((resolve) => {
62
+ let failed = false;
67
63
 
68
- if(!fs.existsSync(path.join(ROOT,"node_modules")))
69
- return fail("node_modules missing");
64
+ const p = spawn("npx", cmd, { shell: true });
70
65
 
71
- return { ok:true };
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
- const fail = m => ({ ok:false, msg:m });
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
- 🚀 PIPELINE
90
- ====================== */
91
- let busy=false;
92
- async function pipeline(trigger){
93
- if(busy) return;
94
- busy=true;
95
-
96
- console.clear();
97
- console.log(rainbow("🔥 DrizzlePulse Server Start"));
98
- console.log(DIM+"Trigger → "+trigger+RESET+"\n");
99
-
100
- const a = audit();
101
- if(!a.ok){
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
- for(const step of [["drizzle-kit","generate"],["drizzle-kit","push"]]){
108
- if(!(await run(step))){
109
- console.log("\n\x1b[31m💀 Pipeline crashed"+RESET);
110
- busy=false;
111
- return;
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("\n\x1b[32m✨ Drizzle synced successfully"+RESET);
116
- busy=false;
109
+ console.log("✨ Drizzle fully synced");
110
+ busy = false;
117
111
  }
118
112
 
119
- /* ======================
120
- 👀 WATCH MODE (SERVER)
121
- ====================== */
122
- injectTea();
123
- pipeline("initial");
124
-
125
- chokidar.watch(ROOT,{
126
- ignored:[/node_modules/,/\.git/,/\.next/,/dist/],
127
- ignoreInitial:true,
128
- usePolling:true,
129
- interval:300
130
- }).on("all",(e,f)=>{
131
- if(f) pipeline(`${e.toUpperCase()} → ${path.basename(f)}`);
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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-auto",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "bin": {
5
5
  "drizzle-auto": "index.js"
6
6
  },