drizzle-auto 1.0.7 โ†’ 1.0.9

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 +147 -59
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -3,85 +3,173 @@
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
5
  const chokidar = require("chokidar");
6
- const spawn = require("cross-spawn");
7
-
8
- /* =========================
9
- ๐Ÿง  Detect project root
10
- ========================= */
11
- function getProjectRoot() {
12
- const init = process.env.INIT_CWD;
13
- if (init && fs.existsSync(path.join(init, "package.json"))) return init;
14
- let cwd = process.cwd();
15
- if (cwd.includes("node_modules")) cwd = cwd.split(`node_modules${path.sep}`)[0];
16
- return cwd;
6
+ const { spawn } = require("child_process");
7
+
8
+ /* =======================
9
+ ๐ŸŽจ NeonPulse Colors
10
+ ======================= */
11
+ const COLORS = ["\x1b[31m","\x1b[32m","\x1b[33m","\x1b[34m","\x1b[35m","\x1b[36m"];
12
+ const RESET = "\x1b[0m";
13
+ const BOLD = "\x1b[1m";
14
+ const DIM = "\x1b[90m";
15
+ let colorIndex = 0;
16
+ const nextColor = () => COLORS[colorIndex++ % COLORS.length];
17
+ const rainbow = text => text.split("").map(c => `${nextColor()}${c}`).join("") + RESET;
18
+
19
+ /* =======================
20
+ โณ Spinner
21
+ ======================= */
22
+ const FRAMES = ["โ ‹","โ ™","โ น","โ ธ","โ ผ","โ ด","โ ฆ","โ ง","โ ‡","โ "];
23
+ let spinIndex = 0;
24
+ let spinnerInterval = null;
25
+ function startSpinner(msg) {
26
+ stopSpinner();
27
+ spinnerInterval = setInterval(() => {
28
+ process.stdout.write(`\r${nextColor()}${FRAMES[spinIndex++ % FRAMES.length]} ${msg}${RESET}`);
29
+ }, 80);
30
+ }
31
+ function stopSpinner() {
32
+ if (spinnerInterval) {
33
+ clearInterval(spinnerInterval);
34
+ spinnerInterval = null;
35
+ process.stdout.write("\r\x1b[K");
36
+ }
17
37
  }
18
38
 
19
- const projectRoot = getProjectRoot();
20
- const pkgPath = path.join(projectRoot, "package.json");
39
+ /* =======================
40
+ ๐Ÿง  Project Root + Package.json
41
+ ======================= */
42
+ const PROJECT_ROOT = process.env.INIT_CWD || process.cwd();
43
+ const PKG_PATH = path.join(PROJECT_ROOT, "package.json");
21
44
 
22
- /* =========================
23
- โœ๏ธ Inject "tea" script automatically
24
- ========================= */
25
- function injectTeaScript() {
26
- if (!fs.existsSync(pkgPath)) return;
27
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
45
+ function addTeaScript() {
46
+ if (!fs.existsSync(PKG_PATH)) return;
47
+ const pkg = JSON.parse(fs.readFileSync(PKG_PATH, "utf8"));
28
48
  pkg.scripts ||= {};
29
49
  if (!pkg.scripts.tea) {
30
50
  pkg.scripts.tea = "drizzle-auto";
31
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
32
- console.log("โšก Script injected โ†’ npm run tea");
51
+ fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2));
52
+ console.log(`โšก tea script added โ†’ npm run tea`);
53
+ }
54
+ }
55
+
56
+ /* =======================
57
+ ๐Ÿงช Infra Audit
58
+ ======================= */
59
+ function auditInfra() {
60
+ const results = [];
61
+
62
+ // Drizzle config
63
+ const configFile = ["ts","js","mjs","mts"].map(ext => `drizzle.config.${ext}`)
64
+ .find(f => fs.existsSync(path.join(PROJECT_ROOT, f)));
65
+ results.push({ name: "Drizzle Config", ok: !!configFile });
66
+
67
+ // Schema path
68
+ if (configFile) {
69
+ try {
70
+ const cfg = fs.readFileSync(path.join(PROJECT_ROOT, configFile), "utf8");
71
+ const match = cfg.match(/schema:\s*["'](.+?)["']/);
72
+ if (match) {
73
+ results.push({
74
+ name: `Schema (${match[1]})`,
75
+ ok: fs.existsSync(path.join(PROJECT_ROOT, match[1]))
76
+ });
77
+ } else results.push({ name: "Schema Path", ok: false });
78
+ } catch {
79
+ results.push({ name: "Config Readable", ok: false });
80
+ }
33
81
  }
82
+
83
+ // Essentials
84
+ results.push({ name: ".env", ok: fs.existsSync(path.join(PROJECT_ROOT, ".env")) });
85
+ results.push({ name: "node_modules", ok: fs.existsSync(path.join(PROJECT_ROOT, "node_modules")) });
86
+
87
+ const missing = results.filter(r => !r.ok).map(r => r.name);
88
+ return { ok: missing.length === 0, missing };
34
89
  }
35
90
 
36
- /* =========================
37
- ๐Ÿš€ Run Drizzle pipeline
38
- ========================= */
91
+ /* =======================
92
+ โšก Run npx command
93
+ ======================= */
94
+ function runCmd(cmd) {
95
+ return new Promise(resolve => {
96
+ let failed = false;
97
+ startSpinner(`npx ${cmd.join(" ")}`);
98
+ const p = spawn("npx", cmd, { shell: true });
99
+ p.stdout.on("data", d => {
100
+ stopSpinner();
101
+ process.stdout.write(DIM + d.toString() + RESET);
102
+ if (/error|failed/i.test(d)) failed = true;
103
+ });
104
+ p.stderr.on("data", d => {
105
+ stopSpinner();
106
+ process.stderr.write("\x1b[31m" + d + RESET);
107
+ failed = true;
108
+ });
109
+ p.on("close", code => resolve(code === 0 && !failed));
110
+ });
111
+ }
112
+
113
+ /* =======================
114
+ ๐Ÿ Workflow
115
+ ======================= */
39
116
  let running = false;
117
+ let hasError = false;
40
118
 
41
- function runPipeline() {
119
+ async function workflow(trigger) {
42
120
  if (running) return;
43
121
  running = true;
44
- console.clear();
45
- console.log("๐Ÿš€ Drizzle Pipeline starting...");
46
122
 
47
- const p = spawn("npx", ["drizzle-kit", "check", "generate", "push"], {
48
- stdio: "inherit",
49
- shell: true,
50
- cwd: projectRoot,
51
- });
123
+ if (!hasError) console.clear();
124
+ console.log(`\n${nextColor()}โšก Trigger โ†’ ${trigger}${RESET}`);
52
125
 
53
- p.on("close", (code) => {
126
+ const audit = auditInfra();
127
+ if (!audit.ok) {
128
+ console.log("\n\x1b[31mโŒ Infra not ready\x1b[0m");
129
+ audit.missing.forEach(m => console.log(DIM + "โ€ข " + m + RESET));
130
+ hasError = true;
54
131
  running = false;
55
- if (code === 0) console.log("โœ… Drizzle synced!");
56
- else console.log("โŒ Drizzle pipeline failed. Fix errors & save files.");
57
- });
58
- }
132
+ return;
133
+ }
59
134
 
60
- /* =========================
61
- ๐Ÿ‘€ Postinstall hook
62
- ========================= */
63
- if (process.argv.includes("--postinstall") || process.env.npm_config_argv) {
64
- injectTeaScript();
65
- runPipeline();
135
+ const steps = [
136
+ ["drizzle-kit", "check"],
137
+ ["drizzle-kit", "generate"],
138
+ ["drizzle-kit", "push"]
139
+ ];
140
+
141
+ for (const step of steps) {
142
+ if (!(await runCmd(step))) {
143
+ console.log("\n\x1b[31m๐Ÿ’€ Pipeline crashed\x1b[0m");
144
+ hasError = true;
145
+ running = false;
146
+ return;
147
+ }
148
+ }
149
+
150
+ hasError = false;
151
+ console.log("\n\x1b[32mโœจ Drizzle synced\x1b[0m");
152
+ setTimeout(() => running = false, 1000);
66
153
  }
67
154
 
68
- /* =========================
69
- ๐Ÿ‘€ Watcher for any changes
70
- ========================= */
71
- injectTeaScript();
72
- runPipeline(); // run immediately on npx drizzle-auto
73
-
74
- chokidar
75
- .watch(projectRoot, {
76
- ignored: /node_modules|\.git|\.next/,
77
- ignoreInitial: true,
78
- })
79
- .on("all", (event, filePath) => {
80
- console.log(`โšก File changed โ†’ ${filePath}`);
81
- runPipeline();
82
- });
155
+ /* =======================
156
+ ๐Ÿ‘€ Watcher
157
+ ======================= */
158
+ chokidar.watch(PROJECT_ROOT, {
159
+ ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
160
+ ignoreInitial: true,
161
+ usePolling: true,
162
+ interval: 300
163
+ }).on("all", (e, f) => workflow(`${e.toUpperCase()} โ†’ ${path.basename(f)}`));
164
+
165
+ /* =======================
166
+ ๐Ÿš€ Init
167
+ ======================= */
168
+ addTeaScript(); // inject tea script automatically
169
+ workflow("Initial Audit");
83
170
 
84
- process.on("uncaughtException", (err) => {
171
+ process.on("uncaughtException", err => {
172
+ stopSpinner();
85
173
  console.log("\x1b[31m๐Ÿ”ฅ Crash:\x1b[0m", err.message);
86
174
  running = false;
87
175
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-auto",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "bin": {
5
5
  "drizzle-auto": "index.js"
6
6
  },