drizzle-auto 1.1.1 β†’ 1.1.2

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 +76 -95
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,25 +6,17 @@ const chokidar = require("chokidar");
6
6
  const { spawn } = require("child_process");
7
7
 
8
8
  /* =========================
9
- 🎨 COLORS (clean & pro)
9
+ 🎨 COLORS (CLEAN + PRO)
10
10
  ========================= */
11
11
  const C = {
12
12
  reset: "\x1b[0m",
13
13
  dim: "\x1b[2m",
14
- bold: "\x1b[1m",
15
14
  cyan: "\x1b[36m",
16
15
  green: "\x1b[32m",
17
16
  yellow: "\x1b[33m",
18
17
  red: "\x1b[31m",
19
18
  magenta: "\x1b[35m",
20
- };
21
-
22
- const log = {
23
- info: (m) => console.log(`${C.cyan}β„Ή ${m}${C.reset}`),
24
- ok: (m) => console.log(`${C.green}βœ“ ${m}${C.reset}`),
25
- warn: (m) => console.log(`${C.yellow}⚠ ${m}${C.reset}`),
26
- err: (m) => console.log(`${C.red}βœ— ${m}${C.reset}`),
27
- trig: (m) => console.log(`${C.magenta}⚑ ${m}${C.reset}`),
19
+ bold: "\x1b[1m",
28
20
  };
29
21
 
30
22
  /* =========================
@@ -34,9 +26,9 @@ const ROOT = process.cwd();
34
26
  const PKG = path.join(ROOT, "package.json");
35
27
 
36
28
  /* =========================
37
- 🧩 SCRIPT INJECTOR
29
+ 🧩 SCRIPT INJECTOR (REAL FIX)
38
30
  ========================= */
39
- function injectScript() {
31
+ function injectTeaScript() {
40
32
  if (!fs.existsSync(PKG)) return;
41
33
 
42
34
  const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
@@ -45,137 +37,126 @@ function injectScript() {
45
37
  if (!pkg.scripts.tea) {
46
38
  pkg.scripts.tea = "drizzle-auto";
47
39
  fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
48
- log.ok("Added script β†’ npm run tea");
40
+ console.log(
41
+ `${C.green}β˜• Added script:${C.reset} ${C.bold}npm run tea${C.reset}`
42
+ );
49
43
  }
50
44
  }
51
45
 
52
46
  /* =========================
53
- πŸ” FIND DRIZZLE CONFIG
54
- (js | mjs | ts | mts)
47
+ πŸ” FIND DRIZZLE CONFIG (ANYWHERE)
55
48
  ========================= */
56
49
  function findDrizzleConfig() {
57
- const exts = ["js", "mjs", "ts", "mts"];
58
- for (const e of exts) {
59
- const p = path.join(ROOT, `drizzle.config.${e}`);
60
- if (fs.existsSync(p)) return p;
50
+ const exts = ["js", "mjs", "cjs", "ts", "mts"];
51
+ let found = null;
52
+
53
+ function walk(dir) {
54
+ if (found) return;
55
+ for (const file of fs.readdirSync(dir, { withFileTypes: true })) {
56
+ if (file.name === "node_modules" || file.name.startsWith(".")) continue;
57
+ const full = path.join(dir, file.name);
58
+
59
+ if (file.isDirectory()) walk(full);
60
+ else {
61
+ for (const e of exts) {
62
+ if (file.name === `drizzle.config.${e}`) {
63
+ found = full;
64
+ return;
65
+ }
66
+ }
67
+ }
68
+ }
61
69
  }
62
- return null;
70
+
71
+ walk(ROOT);
72
+ return found;
63
73
  }
64
74
 
65
75
  /* =========================
66
- πŸ”Ž FILE FILTER (ALL TYPES)
76
+ πŸ§ͺ VALIDATE SCHEMA EXISTS
67
77
  ========================= */
68
- const WATCH_EXTS = [
69
- ".js", ".mjs", ".cjs",
70
- ".ts", ".mts",
71
- ".jsx", ".tsx",
72
- ".sql"
73
- ];
74
-
75
- function isValidFile(file) {
76
- return WATCH_EXTS.includes(path.extname(file));
78
+ function validateConfig(configPath) {
79
+ try {
80
+ const txt = fs.readFileSync(configPath, "utf8");
81
+ const m = txt.match(/schema:\s*["'](.+?)["']/);
82
+ if (!m) return true;
83
+
84
+ const schemaPath = path.resolve(path.dirname(configPath), m[1]);
85
+ return fs.existsSync(schemaPath);
86
+ } catch {
87
+ return false;
88
+ }
77
89
  }
78
90
 
79
91
  /* =========================
80
- βš™οΈ SAFE SPAWN (NO SHELL)
92
+ βš™οΈ SAFE RUNNER (NO SERVER KILL)
81
93
  ========================= */
82
- function exec(cmd, args = []) {
94
+ function run(cmd, args) {
83
95
  return new Promise((resolve) => {
84
- const p = spawn(cmd, args, { stdio: "inherit" });
85
-
86
- p.on("error", (e) => {
87
- log.err(`Process error: ${e.message}`);
88
- resolve(false);
96
+ const p = spawn(cmd, args, {
97
+ stdio: "inherit",
98
+ shell: false,
89
99
  });
90
100
 
91
- p.on("close", (code) => {
92
- resolve(code === 0);
93
- });
101
+ p.on("close", (code) => resolve(code === 0));
94
102
  });
95
103
  }
96
104
 
97
105
  /* =========================
98
- πŸ›‘οΈ CRASH PROTECTION
106
+ πŸ” PIPELINE (SERVER SAFE)
99
107
  ========================= */
100
- process.on("uncaughtException", (e) => {
101
- log.err(`Crash prevented: ${e.message}`);
102
- });
103
- process.on("unhandledRejection", (e) => {
104
- log.err(`Promise rejected: ${e}`);
105
- });
106
-
107
- /* =========================
108
- πŸ” PIPELINE (DEV SAFE)
109
- ========================= */
110
- let running = false;
111
- let lastPush = 0;
112
-
113
- const DEV_MODE = process.argv.includes("--dev");
114
- const PUSH_COOLDOWN = DEV_MODE ? 120_000 : 30_000; // safer in dev
108
+ let busy = false;
115
109
 
116
110
  async function pipeline(trigger) {
117
- if (running) return;
118
- running = true;
111
+ if (busy) return;
112
+ busy = true;
119
113
 
120
- log.trig(`Trigger β†’ ${trigger}`);
114
+ console.log(
115
+ `\n${C.cyan}⚑ Trigger β†’${C.reset} ${C.bold}${trigger}${C.reset}`
116
+ );
121
117
 
122
118
  const config = findDrizzleConfig();
123
119
  if (!config) {
124
- log.warn("No drizzle.config found");
125
- running = false;
120
+ console.log(`${C.yellow}⚠ No drizzle.config found${C.reset}`);
121
+ busy = false;
126
122
  return;
127
123
  }
128
124
 
129
- log.info("Running drizzle generate…");
130
- const gen = await exec("npx", ["drizzle-kit", "generate"]);
131
- if (!gen) {
132
- log.err("Generate failed β€” watching continues");
133
- running = false;
125
+ if (!validateConfig(config)) {
126
+ console.log(`${C.red}❌ Schema path invalid${C.reset}`);
127
+ busy = false;
134
128
  return;
135
129
  }
136
130
 
137
- const now = Date.now();
138
- if (now - lastPush < PUSH_COOLDOWN) {
139
- log.warn("Push skipped (cooldown)");
140
- running = false;
131
+ console.log(`${C.magenta}β–Ά drizzle-kit generate${C.reset}`);
132
+ const genOK = await run("npx", ["drizzle-kit", "generate"]);
133
+ if (!genOK) {
134
+ console.log(`${C.red}πŸ›‘ Generate failed β€” watcher alive${C.reset}`);
135
+ busy = false;
141
136
  return;
142
137
  }
143
138
 
144
- log.info("Running drizzle push…");
145
- lastPush = now;
146
-
147
- const push = await exec("npx", ["drizzle-kit", "push"]);
148
- if (!push) {
149
- log.err("Push failed β€” DB untouched, server alive");
150
- running = false;
139
+ console.log(`${C.magenta}β–Ά drizzle-kit push${C.reset}`);
140
+ const pushOK = await run("npx", ["drizzle-kit", "push"]);
141
+ if (!pushOK) {
142
+ console.log(`${C.red}πŸ›‘ Push failed β€” watcher alive${C.reset}`);
143
+ busy = false;
151
144
  return;
152
145
  }
153
146
 
154
- log.ok("Drizzle fully synced");
155
- running = false;
147
+ console.log(`${C.green}✨ Drizzle fully synced${C.reset}`);
148
+ busy = false;
156
149
  }
157
150
 
158
151
  /* =========================
159
- πŸ‘€ WATCHER (ALL FILES)
152
+ πŸ‘€ WATCH EVERYTHING
160
153
  ========================= */
161
- injectScript();
154
+ injectTeaScript();
162
155
  pipeline("Initial");
163
156
 
164
- log.info(`Watching files (${DEV_MODE ? "DEV MODE" : "SAFE MODE"})…`);
165
-
166
157
  chokidar.watch(ROOT, {
167
- ignored: [
168
- /node_modules/,
169
- /\.git/,
170
- /\.next/,
171
- /dist/,
172
- /build/
173
- ],
158
+ ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
174
159
  ignoreInitial: true,
175
- usePolling: true,
176
- interval: 300,
177
160
  }).on("all", (_, file) => {
178
- if (isValidFile(file)) {
179
- pipeline(path.relative(ROOT, file));
180
- }
161
+ pipeline(path.relative(ROOT, file));
181
162
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-auto",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "pavel ahmmed hridoy",