drizzle-auto 1.0.13 → 1.0.14

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 +84 -83
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -5,122 +5,123 @@ const path = require("path");
5
5
  const chokidar = require("chokidar");
6
6
  const { spawn } = require("child_process");
7
7
 
8
- /* =========================
9
- 🧭 PROJECT ROOT
10
- ========================= */
8
+ /* ======================
9
+ šŸŽØ COLOR SYSTEM
10
+ ====================== */
11
+ const c = {
12
+ reset: "\x1b[0m",
13
+ cyan: "\x1b[36m",
14
+ green: "\x1b[32m",
15
+ yellow: "\x1b[33m",
16
+ red: "\x1b[31m",
17
+ gray: "\x1b[90m",
18
+ bold: "\x1b[1m"
19
+ };
20
+
21
+ const log = {
22
+ sys: m => console.log(`${c.cyan}ā—${c.reset} ${m}`),
23
+ ok: m => console.log(`${c.green}āœ”${c.reset} ${m}`),
24
+ warn: m => console.log(`${c.yellow}ā–²${c.reset} ${m}`),
25
+ err: m => console.log(`${c.red}āœ–${c.reset} ${m}`),
26
+ info: m => console.log(`${c.gray}${m}${c.reset}`)
27
+ };
28
+
29
+ /* ======================
30
+ šŸ“ PROJECT ROOT
31
+ ====================== */
11
32
  const ROOT = process.cwd();
12
33
  const PKG = path.join(ROOT, "package.json");
13
34
 
14
- /* =========================
15
- 🧩 SCRIPT INJECTOR
16
- ========================= */
17
- function injectScript() {
18
- if (!fs.existsSync(PKG)) return;
35
+ /* ======================
36
+ ā˜• ADD tea SCRIPT
37
+ ====================== */
38
+ function addTeaScript() {
39
+ if (!fs.existsSync(PKG)) {
40
+ log.err("package.json not found");
41
+ return;
42
+ }
43
+
19
44
  const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
20
45
  pkg.scripts ||= {};
21
46
 
22
47
  if (!pkg.scripts.tea) {
23
48
  pkg.scripts.tea = "drizzle-auto";
24
49
  fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
25
- console.log("ā˜• Script added → npm run tea");
50
+ log.ok("Script added → pnpm run tea");
51
+ } else {
52
+ log.info("Script already exists");
26
53
  }
27
54
  }
28
55
 
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
- }
41
-
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 {}
48
-
49
- if (schemaPath && !fs.existsSync(schemaPath)) {
50
- console.log("āŒ Schema file missing");
51
- return null;
52
- }
53
-
54
- return { config: configs, schema: schemaPath };
56
+ /* ======================
57
+ šŸ” DRIZZLE CONFIG SCAN
58
+ ====================== */
59
+ function findDrizzleConfig() {
60
+ const exts = ["js", "mjs", "ts", "mts"];
61
+ return exts.find(ext =>
62
+ fs.existsSync(path.join(ROOT, `drizzle.config.${ext}`))
63
+ );
55
64
  }
56
65
 
57
- /* =========================
66
+ /* ======================
58
67
  āš™ļø SAFE COMMAND RUNNER
59
- ========================= */
68
+ ====================== */
60
69
  function run(cmd) {
61
- return new Promise((resolve) => {
62
- let failed = false;
63
-
64
- const p = spawn("npx", cmd, { shell: true });
65
-
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
- });
71
-
72
- p.on("close", code => {
73
- resolve(code === 0 && !failed);
74
- });
70
+ return new Promise(resolve => {
71
+ const p = spawn("npx", cmd, { stdio: "inherit", shell: true });
72
+ p.on("close", code => resolve(code === 0));
75
73
  });
76
74
  }
77
75
 
78
- /* =========================
79
- šŸ” PIPELINE (NON-KILLING)
80
- ========================= */
81
- let busy = false;
76
+ /* ======================
77
+ šŸ” PIPELINE
78
+ ====================== */
79
+ let running = false;
82
80
 
83
- async function pipeline(trigger) {
84
- if (busy) return;
85
- busy = true;
81
+ async function pipeline(reason) {
82
+ if (running) return;
83
+ running = true;
86
84
 
87
- console.log(`\n⚔ Trigger → ${trigger}`);
85
+ log.sys(`Triggered by ${reason}`);
88
86
 
89
- const drizzle = detectDrizzle();
90
- if (!drizzle) {
91
- busy = false;
87
+ if (!findDrizzleConfig()) {
88
+ log.warn("No drizzle.config found (js/mjs/ts/mts)");
89
+ running = false;
92
90
  return;
93
91
  }
94
92
 
95
- const genOK = await run(["drizzle-kit", "generate"]);
96
- if (!genOK) {
97
- console.log("šŸ›‘ Generate failed — pipeline paused");
98
- busy = false;
93
+ log.sys("Running drizzle generate...");
94
+ if (!(await run(["drizzle-kit", "generate"]))) {
95
+ log.err("Generate failed — waiting for fix");
96
+ running = false;
99
97
  return;
100
98
  }
101
99
 
102
- const pushOK = await run(["drizzle-kit", "push"]);
103
- if (!pushOK) {
104
- console.log("šŸ›‘ Push failed — server still running");
105
- busy = false;
100
+ log.sys("Running drizzle push...");
101
+ if (!(await run(["drizzle-kit", "push"]))) {
102
+ log.err("Push failed — waiting for fix");
103
+ running = false;
106
104
  return;
107
105
  }
108
106
 
109
- console.log("✨ Drizzle fully synced");
110
- busy = false;
107
+ log.ok("Database synced successfully");
108
+ running = false;
111
109
  }
112
110
 
113
- /* =========================
114
- šŸ‘€ WATCHER (ALWAYS ALIVE)
115
- ========================= */
116
- injectScript();
117
- pipeline("Initial");
111
+ /* ======================
112
+ šŸš€ START
113
+ ====================== */
114
+ console.log(`
115
+ ${c.bold}${c.cyan}Drizzle Auto${c.reset}
116
+ ${c.gray}Server-side watcher initialized${c.reset}
117
+ `);
118
+
119
+ addTeaScript();
120
+ pipeline("startup");
118
121
 
119
122
  chokidar.watch(ROOT, {
120
123
  ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
121
- ignoreInitial: true,
122
- usePolling: true,
123
- interval: 300
124
- }).on("all", (_, file) => {
125
- pipeline(path.basename(file));
126
- });
124
+ ignoreInitial: true
125
+ }).on("change", file =>
126
+ pipeline(path.basename(file))
127
+ );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-auto",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "bin": {
5
5
  "drizzle-auto": "index.js"
6
6
  },