drizzle-auto 1.0.11 → 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 +87 -115
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -5,150 +5,122 @@ const path = require("path");
5
5
  const chokidar = require("chokidar");
6
6
  const { spawn } = require("child_process");
7
7
 
8
- /* ======================= 🎨 NeonPulse Colors ======================= */
9
- const COLORS = ["\x1b[31m","\x1b[32m","\x1b[33m","\x1b[34m","\x1b[35m","\x1b[36m"];
10
- const RESET = "\x1b[0m";
11
- const DIM = "\x1b[90m";
12
- let colorIndex = 0;
13
- const nextColor = () => COLORS[colorIndex++ % COLORS.length];
14
-
15
- /* ======================= ⏳ Spinner ======================= */
16
- const FRAMES = ["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"];
17
- let spinIndex = 0;
18
- let spinnerInterval = null;
19
-
20
- function startSpinner(msg) {
21
- stopSpinner();
22
- spinnerInterval = setInterval(() => {
23
- process.stdout.write(`\r${nextColor()}${FRAMES[spinIndex++ % FRAMES.length]} ${msg}${RESET}`);
24
- }, 80);
25
- }
26
-
27
- function stopSpinner() {
28
- if (spinnerInterval) {
29
- clearInterval(spinnerInterval);
30
- spinnerInterval = null;
31
- process.stdout.write("\r\x1b[K");
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"));
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");
32
26
  }
33
27
  }
34
28
 
35
- /* ======================= 🧠 Project Root + Package.json ======================= */
36
- const PROJECT_ROOT = process.cwd();
37
- const PKG_PATH = path.join(PROJECT_ROOT, "package.json");
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
+ }
38
41
 
39
- function addTeaScript() {
40
- if (!fs.existsSync(PKG_PATH)) return;
42
+ let schemaPath = null;
41
43
  try {
42
- const pkg = JSON.parse(fs.readFileSync(PKG_PATH, "utf8"));
43
- pkg.scripts = pkg.scripts || {};
44
-
45
- // ফিক্স: সরাসরি node index.js চালানো নিশ্চিত করা
46
- if (!pkg.scripts.tea) {
47
- pkg.scripts.tea = "node index.js";
48
- fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2));
49
- console.log(`\x1b[32m⚡ tea script added → npm run tea\x1b[0m`);
50
- }
51
- } catch (e) {
52
- console.error("❌ Could not update package.json");
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;
53
52
  }
54
- }
55
53
 
56
- /* ======================= 🧪 Infra Audit ======================= */
57
- function auditInfra() {
58
- const results = [];
59
- const configFile = ["ts","js","mjs","mts"].map(ext => `drizzle.config.${ext}`)
60
- .find(f => fs.existsSync(path.join(PROJECT_ROOT, f)));
61
-
62
- results.push({ name: "Drizzle Config", ok: !!configFile });
63
- results.push({ name: ".env", ok: fs.existsSync(path.join(PROJECT_ROOT, ".env")) });
64
- results.push({ name: "node_modules", ok: fs.existsSync(path.join(PROJECT_ROOT, "node_modules")) });
65
-
66
- const missing = results.filter(r => !r.ok).map(r => r.name);
67
- return { ok: missing.length === 0, missing };
54
+ return { config: configs, schema: schemaPath };
68
55
  }
69
56
 
70
- /* ======================= ⚡ Run npx command ======================= */
71
- function runCmd(cmd) {
72
- return new Promise(resolve => {
57
+ /* =========================
58
+ ⚙️ SAFE COMMAND RUNNER
59
+ ========================= */
60
+ function run(cmd) {
61
+ return new Promise((resolve) => {
73
62
  let failed = false;
74
- startSpinner(`npx ${cmd.join(" ")}`);
75
- const p = spawn("npx", cmd, { shell: true, cwd: PROJECT_ROOT });
76
63
 
77
- p.stdout.on("data", d => {
78
- stopSpinner();
79
- process.stdout.write(DIM + d.toString() + RESET);
80
- if (/error|failed/i.test(d)) failed = true;
81
- });
64
+ const p = spawn("npx", cmd, { shell: true });
82
65
 
66
+ p.stdout.on("data", d => process.stdout.write(d));
83
67
  p.stderr.on("data", d => {
84
- stopSpinner();
85
- process.stderr.write("\x1b[31m" + d + RESET);
86
68
  failed = true;
69
+ process.stderr.write(d);
87
70
  });
88
71
 
89
- p.on("close", code => resolve(code === 0 && !failed));
72
+ p.on("close", code => {
73
+ resolve(code === 0 && !failed);
74
+ });
90
75
  });
91
76
  }
92
77
 
93
- /* ======================= 🏁 Workflow ======================= */
94
- let running = false;
95
- let hasError = false;
78
+ /* =========================
79
+ 🔁 PIPELINE (NON-KILLING)
80
+ ========================= */
81
+ let busy = false;
96
82
 
97
- async function workflow(trigger) {
98
- if (running) return;
99
- running = true;
83
+ async function pipeline(trigger) {
84
+ if (busy) return;
85
+ busy = true;
100
86
 
101
- if (!hasError) console.clear();
102
- console.log(`\n${nextColor()}⚡ Trigger → ${trigger}${RESET}`);
87
+ console.log(`\n⚡ Trigger → ${trigger}`);
103
88
 
104
- const audit = auditInfra();
105
- if (!audit.ok) {
106
- console.log("\n\x1b[31m❌ Infra not ready\x1b[0m");
107
- audit.missing.forEach(m => console.log(DIM + "• " + m + RESET));
108
- hasError = true;
109
- running = false;
89
+ const drizzle = detectDrizzle();
90
+ if (!drizzle) {
91
+ busy = false;
110
92
  return;
111
93
  }
112
94
 
113
- const steps = [
114
- ["drizzle-kit", "generate"],
115
- ["drizzle-kit", "push"]
116
- ];
117
-
118
- for (const step of steps) {
119
- if (!(await runCmd(step))) {
120
- console.log("\n\x1b[31m💀 Pipeline crashed\x1b[0m");
121
- hasError = true;
122
- running = false;
123
- return;
124
- }
95
+ const genOK = await run(["drizzle-kit", "generate"]);
96
+ if (!genOK) {
97
+ console.log("🛑 Generate failed — pipeline paused");
98
+ busy = false;
99
+ return;
125
100
  }
126
101
 
127
- hasError = false;
128
- console.log("\n\x1b[32m✨ Drizzle synced\x1b[0m");
129
- setTimeout(() => { running = false; }, 1000);
102
+ const pushOK = await run(["drizzle-kit", "push"]);
103
+ if (!pushOK) {
104
+ console.log("🛑 Push failed server still running");
105
+ busy = false;
106
+ return;
107
+ }
108
+
109
+ console.log("✨ Drizzle fully synced");
110
+ busy = false;
130
111
  }
131
112
 
132
- /* ======================= 👀 Watcher ======================= */
133
- const watcher = chokidar.watch(PROJECT_ROOT, {
134
- ignored: [/node_modules/, /\.git/, /\.next/, /dist/, /migrations/],
113
+ /* =========================
114
+ 👀 WATCHER (ALWAYS ALIVE)
115
+ ========================= */
116
+ injectScript();
117
+ pipeline("Initial");
118
+
119
+ chokidar.watch(ROOT, {
120
+ ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
135
121
  ignoreInitial: true,
136
122
  usePolling: true,
137
123
  interval: 300
138
- });
139
-
140
- watcher.on("all", (event, file) => {
141
- if (file.includes("schema") || file.endsWith(".ts") || file.endsWith(".js")) {
142
- workflow(`${event.toUpperCase()} → ${path.basename(file)}`);
143
- }
144
- });
145
-
146
- /* ======================= 🚀 Init ======================= */
147
- addTeaScript();
148
- workflow("Initial Audit");
149
-
150
- process.on("uncaughtException", err => {
151
- stopSpinner();
152
- console.log("\x1b[31m🔥 Crash:\x1b[0m", err.message);
153
- running = false;
154
- });
124
+ }).on("all", (_, file) => {
125
+ pipeline(path.basename(file));
126
+ });
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "drizzle-auto",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "bin": {
5
5
  "drizzle-auto": "index.js"
6
6
  },
7
7
  "scripts": {
8
- "postinstall": "node index.js --postinstall"
8
+ "postinstall": "node index.js --postinstall",
9
+ "tea": "node index.js"
9
10
  },
10
11
  "dependencies": {
11
12
  "chokidar": "^5.0.0",