drizzle-auto 1.0.14 → 1.0.15
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.
- package/index.js +111 -71
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,41 +5,44 @@ const path = require("path");
|
|
|
5
5
|
const chokidar = require("chokidar");
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
|
-
/*
|
|
9
|
-
🎨
|
|
10
|
-
|
|
11
|
-
const
|
|
8
|
+
/* =========================
|
|
9
|
+
🎨 TERMINAL COLORS (PRO)
|
|
10
|
+
========================= */
|
|
11
|
+
const C = {
|
|
12
12
|
reset: "\x1b[0m",
|
|
13
|
+
bold: "\x1b[1m",
|
|
14
|
+
dim: "\x1b[2m",
|
|
15
|
+
|
|
13
16
|
cyan: "\x1b[36m",
|
|
14
17
|
green: "\x1b[32m",
|
|
15
18
|
yellow: "\x1b[33m",
|
|
16
19
|
red: "\x1b[31m",
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
magenta: "\x1b[35m",
|
|
21
|
+
blue: "\x1b[34m",
|
|
22
|
+
gray: "\x1b[90m"
|
|
19
23
|
};
|
|
20
24
|
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const ui = {
|
|
26
|
+
title: m => console.log(`\n${C.bold}${C.magenta}◆ ${m}${C.reset}`),
|
|
27
|
+
info: m => console.log(`${C.cyan}●${C.reset} ${m}`),
|
|
28
|
+
watch: m => console.log(`${C.blue}👀${C.reset} ${m}`),
|
|
29
|
+
ok: m => console.log(`${C.green}✔${C.reset} ${m}`),
|
|
30
|
+
warn: m => console.log(`${C.yellow}▲${C.reset} ${m}`),
|
|
31
|
+
err: m => console.log(`${C.red}✖${C.reset} ${m}`),
|
|
32
|
+
dim: m => console.log(`${C.gray}${m}${C.reset}`)
|
|
27
33
|
};
|
|
28
34
|
|
|
29
|
-
/*
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
/* =========================
|
|
36
|
+
🧭 PROJECT ROOT
|
|
37
|
+
========================= */
|
|
32
38
|
const ROOT = process.cwd();
|
|
33
39
|
const PKG = path.join(ROOT, "package.json");
|
|
34
40
|
|
|
35
|
-
/*
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
function
|
|
39
|
-
if (!fs.existsSync(PKG))
|
|
40
|
-
log.err("package.json not found");
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
41
|
+
/* =========================
|
|
42
|
+
🧩 SCRIPT INJECTOR
|
|
43
|
+
========================= */
|
|
44
|
+
function injectScript() {
|
|
45
|
+
if (!fs.existsSync(PKG)) return;
|
|
43
46
|
|
|
44
47
|
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
45
48
|
pkg.scripts ||= {};
|
|
@@ -47,81 +50,118 @@ function addTeaScript() {
|
|
|
47
50
|
if (!pkg.scripts.tea) {
|
|
48
51
|
pkg.scripts.tea = "drizzle-auto";
|
|
49
52
|
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
50
|
-
|
|
53
|
+
ui.ok("Script injected → npm run tea");
|
|
51
54
|
} else {
|
|
52
|
-
|
|
55
|
+
ui.dim("Script already exists → tea");
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
/*
|
|
57
|
-
🔍 DRIZZLE
|
|
58
|
-
|
|
59
|
-
function
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
fs.existsSync(path.join(ROOT,
|
|
63
|
-
|
|
59
|
+
/* =========================
|
|
60
|
+
🔍 DRIZZLE DETECTOR
|
|
61
|
+
========================= */
|
|
62
|
+
function detectDrizzle() {
|
|
63
|
+
const config = ["js", "mjs", "ts", "mts"]
|
|
64
|
+
.map(ext => `drizzle.config.${ext}`)
|
|
65
|
+
.find(f => fs.existsSync(path.join(ROOT, f)));
|
|
66
|
+
|
|
67
|
+
if (!config) {
|
|
68
|
+
ui.warn("No drizzle.config found");
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let schemaPath = null;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const content = fs.readFileSync(path.join(ROOT, config), "utf8");
|
|
76
|
+
const match = content.match(/schema:\s*["'](.+?)["']/);
|
|
77
|
+
if (match) schemaPath = path.join(ROOT, match[1]);
|
|
78
|
+
} catch {}
|
|
79
|
+
|
|
80
|
+
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
81
|
+
ui.err("Schema file missing");
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
ui.ok(`Detected ${config}`);
|
|
86
|
+
return true;
|
|
64
87
|
}
|
|
65
88
|
|
|
66
|
-
/*
|
|
89
|
+
/* =========================
|
|
67
90
|
⚙️ SAFE COMMAND RUNNER
|
|
68
|
-
|
|
91
|
+
========================= */
|
|
69
92
|
function run(cmd) {
|
|
70
93
|
return new Promise(resolve => {
|
|
71
|
-
|
|
72
|
-
|
|
94
|
+
let failed = false;
|
|
95
|
+
|
|
96
|
+
const p = spawn("npx", cmd, {
|
|
97
|
+
shell: true,
|
|
98
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
p.stdout.on("data", d => process.stdout.write(d));
|
|
102
|
+
p.stderr.on("data", d => {
|
|
103
|
+
failed = true;
|
|
104
|
+
process.stderr.write(d);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
p.on("close", code => resolve(code === 0 && !failed));
|
|
73
108
|
});
|
|
74
109
|
}
|
|
75
110
|
|
|
76
|
-
/*
|
|
77
|
-
🔁 PIPELINE
|
|
78
|
-
|
|
79
|
-
let
|
|
111
|
+
/* =========================
|
|
112
|
+
🔁 PIPELINE (SERVER SAFE)
|
|
113
|
+
========================= */
|
|
114
|
+
let busy = false;
|
|
80
115
|
|
|
81
|
-
async function pipeline(
|
|
82
|
-
if (
|
|
83
|
-
|
|
116
|
+
async function pipeline(trigger) {
|
|
117
|
+
if (busy) return;
|
|
118
|
+
busy = true;
|
|
84
119
|
|
|
85
|
-
|
|
120
|
+
ui.watch(`Trigger → ${trigger}`);
|
|
86
121
|
|
|
87
|
-
if (!
|
|
88
|
-
|
|
89
|
-
running = false;
|
|
122
|
+
if (!detectDrizzle()) {
|
|
123
|
+
busy = false;
|
|
90
124
|
return;
|
|
91
125
|
}
|
|
92
126
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
127
|
+
ui.info("Running drizzle-kit generate...");
|
|
128
|
+
const genOK = await run(["drizzle-kit", "generate"]);
|
|
129
|
+
|
|
130
|
+
if (!genOK) {
|
|
131
|
+
ui.err("Generate failed — pipeline stopped");
|
|
132
|
+
busy = false;
|
|
97
133
|
return;
|
|
98
134
|
}
|
|
99
135
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
136
|
+
ui.ok("Generate successful");
|
|
137
|
+
|
|
138
|
+
ui.info("Running drizzle-kit push...");
|
|
139
|
+
const pushOK = await run(["drizzle-kit", "push"]);
|
|
140
|
+
|
|
141
|
+
if (!pushOK) {
|
|
142
|
+
ui.warn("Push failed — server still running");
|
|
143
|
+
busy = false;
|
|
104
144
|
return;
|
|
105
145
|
}
|
|
106
146
|
|
|
107
|
-
|
|
108
|
-
|
|
147
|
+
ui.ok("Drizzle fully synced ✨");
|
|
148
|
+
busy = false;
|
|
109
149
|
}
|
|
110
150
|
|
|
111
|
-
/*
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
${c.gray}Server-side watcher initialized${c.reset}
|
|
117
|
-
`);
|
|
151
|
+
/* =========================
|
|
152
|
+
👀 WATCHER (ALWAYS ALIVE)
|
|
153
|
+
========================= */
|
|
154
|
+
ui.title("DrizzlePulse — Server Start");
|
|
155
|
+
ui.dim(`Root → ${ROOT}`);
|
|
118
156
|
|
|
119
|
-
|
|
120
|
-
pipeline("
|
|
157
|
+
injectScript();
|
|
158
|
+
pipeline("Initial");
|
|
121
159
|
|
|
122
160
|
chokidar.watch(ROOT, {
|
|
123
161
|
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
124
|
-
ignoreInitial: true
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
)
|
|
162
|
+
ignoreInitial: true,
|
|
163
|
+
usePolling: true,
|
|
164
|
+
interval: 300
|
|
165
|
+
}).on("all", (_, file) => {
|
|
166
|
+
pipeline(path.basename(file));
|
|
167
|
+
});
|