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.
- package/index.js +76 -95
- 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 (
|
|
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
|
|
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
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
70
|
+
|
|
71
|
+
walk(ROOT);
|
|
72
|
+
return found;
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
/* =========================
|
|
66
|
-
|
|
76
|
+
π§ͺ VALIDATE SCHEMA EXISTS
|
|
67
77
|
========================= */
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
|
92
|
+
βοΈ SAFE RUNNER (NO SERVER KILL)
|
|
81
93
|
========================= */
|
|
82
|
-
function
|
|
94
|
+
function run(cmd, args) {
|
|
83
95
|
return new Promise((resolve) => {
|
|
84
|
-
const p = spawn(cmd, args, {
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
106
|
+
π PIPELINE (SERVER SAFE)
|
|
99
107
|
========================= */
|
|
100
|
-
|
|
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 (
|
|
118
|
-
|
|
111
|
+
if (busy) return;
|
|
112
|
+
busy = true;
|
|
119
113
|
|
|
120
|
-
log
|
|
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.
|
|
125
|
-
|
|
120
|
+
console.log(`${C.yellow}β No drizzle.config found${C.reset}`);
|
|
121
|
+
busy = false;
|
|
126
122
|
return;
|
|
127
123
|
}
|
|
128
124
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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.
|
|
155
|
-
|
|
147
|
+
console.log(`${C.green}β¨ Drizzle fully synced${C.reset}`);
|
|
148
|
+
busy = false;
|
|
156
149
|
}
|
|
157
150
|
|
|
158
151
|
/* =========================
|
|
159
|
-
π
|
|
152
|
+
π WATCH EVERYTHING
|
|
160
153
|
========================= */
|
|
161
|
-
|
|
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
|
-
|
|
179
|
-
pipeline(path.relative(ROOT, file));
|
|
180
|
-
}
|
|
161
|
+
pipeline(path.relative(ROOT, file));
|
|
181
162
|
});
|