drizzle-auto 1.1.1 β 1.1.3
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 +78 -104
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,176 +6,150 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
8
|
/* =========================
|
|
9
|
-
π¨ COLORS (
|
|
9
|
+
π¨ COLORS (PRO ONLY)
|
|
10
10
|
========================= */
|
|
11
11
|
const C = {
|
|
12
12
|
reset: "\x1b[0m",
|
|
13
|
-
dim: "\x1b[2m",
|
|
14
13
|
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
|
+
dim: "\x1b[2m",
|
|
28
20
|
};
|
|
29
21
|
|
|
30
22
|
/* =========================
|
|
31
|
-
π§
|
|
23
|
+
π§ ROOT
|
|
32
24
|
========================= */
|
|
33
25
|
const ROOT = process.cwd();
|
|
34
26
|
const PKG = path.join(ROOT, "package.json");
|
|
35
27
|
|
|
36
28
|
/* =========================
|
|
37
|
-
|
|
29
|
+
β ADD tea SCRIPT
|
|
38
30
|
========================= */
|
|
39
|
-
function
|
|
31
|
+
function injectTea() {
|
|
40
32
|
if (!fs.existsSync(PKG)) return;
|
|
41
|
-
|
|
42
33
|
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
43
34
|
pkg.scripts ||= {};
|
|
44
35
|
|
|
45
36
|
if (!pkg.scripts.tea) {
|
|
46
37
|
pkg.scripts.tea = "drizzle-auto";
|
|
47
38
|
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
48
|
-
log.
|
|
39
|
+
console.log(`${C.green}β Added:${C.reset} npm run tea`);
|
|
49
40
|
}
|
|
50
41
|
}
|
|
51
42
|
|
|
52
43
|
/* =========================
|
|
53
|
-
π FIND
|
|
54
|
-
(js | mjs | ts | mts)
|
|
44
|
+
π FIND drizzle.config.*
|
|
55
45
|
========================= */
|
|
56
|
-
function
|
|
57
|
-
const exts = ["js", "mjs", "ts", "mts"];
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
46
|
+
function findConfig() {
|
|
47
|
+
const exts = ["js", "mjs", "cjs", "ts", "mts"];
|
|
48
|
+
let found = null;
|
|
49
|
+
|
|
50
|
+
(function walk(dir) {
|
|
51
|
+
if (found) return;
|
|
52
|
+
for (const f of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
53
|
+
if (f.name === "node_modules" || f.name.startsWith(".")) continue;
|
|
54
|
+
const p = path.join(dir, f.name);
|
|
55
|
+
if (f.isDirectory()) walk(p);
|
|
56
|
+
else {
|
|
57
|
+
for (const e of exts) {
|
|
58
|
+
if (f.name === `drizzle.config.${e}`) {
|
|
59
|
+
found = p;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
})(ROOT);
|
|
66
|
+
|
|
67
|
+
return found;
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
/* =========================
|
|
66
|
-
|
|
71
|
+
βοΈ SAFE RUNNER (ERROR AWARE)
|
|
67
72
|
========================= */
|
|
68
|
-
|
|
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));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/* =========================
|
|
80
|
-
βοΈ SAFE SPAWN (NO SHELL)
|
|
81
|
-
========================= */
|
|
82
|
-
function exec(cmd, args = []) {
|
|
73
|
+
function run(cmd, args) {
|
|
83
74
|
return new Promise((resolve) => {
|
|
84
|
-
|
|
75
|
+
let stderr = "";
|
|
76
|
+
|
|
77
|
+
const p = spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
85
78
|
|
|
86
|
-
p.on("
|
|
87
|
-
|
|
88
|
-
|
|
79
|
+
p.stdout.on("data", d => process.stdout.write(d));
|
|
80
|
+
p.stderr.on("data", d => {
|
|
81
|
+
stderr += d.toString();
|
|
82
|
+
process.stderr.write(d);
|
|
89
83
|
});
|
|
90
84
|
|
|
91
85
|
p.on("close", (code) => {
|
|
92
|
-
resolve(code === 0);
|
|
86
|
+
resolve({ ok: code === 0, stderr });
|
|
93
87
|
});
|
|
94
88
|
});
|
|
95
89
|
}
|
|
96
90
|
|
|
97
91
|
/* =========================
|
|
98
|
-
|
|
92
|
+
π PIPELINE (NO LIES)
|
|
99
93
|
========================= */
|
|
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
|
|
94
|
+
let busy = false;
|
|
115
95
|
|
|
116
96
|
async function pipeline(trigger) {
|
|
117
|
-
if (
|
|
118
|
-
|
|
97
|
+
if (busy) return;
|
|
98
|
+
busy = true;
|
|
119
99
|
|
|
120
|
-
log.
|
|
100
|
+
console.log(`\n${C.cyan}β‘ Trigger β${C.reset} ${trigger}`);
|
|
121
101
|
|
|
122
|
-
const config =
|
|
102
|
+
const config = findConfig();
|
|
123
103
|
if (!config) {
|
|
124
|
-
log.
|
|
125
|
-
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
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;
|
|
104
|
+
console.log(`${C.yellow}β No drizzle.config found${C.reset}`);
|
|
105
|
+
busy = false;
|
|
134
106
|
return;
|
|
135
107
|
}
|
|
136
108
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
109
|
+
console.log(`${C.magenta}βΆ drizzle-kit generate${C.reset}`);
|
|
110
|
+
const gen = await run("npx", ["drizzle-kit", "generate"]);
|
|
111
|
+
if (!gen.ok) {
|
|
112
|
+
console.log(`${C.red}π Generate failed β watcher alive${C.reset}`);
|
|
113
|
+
busy = false;
|
|
141
114
|
return;
|
|
142
115
|
}
|
|
143
116
|
|
|
144
|
-
log.
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
117
|
+
console.log(`${C.magenta}βΆ drizzle-kit push${C.reset}`);
|
|
118
|
+
const push = await run("npx", ["drizzle-kit", "push"]);
|
|
119
|
+
|
|
120
|
+
if (!push.ok) {
|
|
121
|
+
console.log(`\n${C.red}${C.bold}π¨ PUSH FAILED${C.reset}`);
|
|
122
|
+
|
|
123
|
+
if (
|
|
124
|
+
push.stderr.includes("ENOTFOUND") ||
|
|
125
|
+
push.stderr.includes("websocket")
|
|
126
|
+
) {
|
|
127
|
+
console.log(
|
|
128
|
+
`${C.yellow}β Neon / network issue detected${C.reset}\n` +
|
|
129
|
+
`${C.dim}β’ Check internet\n` +
|
|
130
|
+
`β’ Check DATABASE_URL\n` +
|
|
131
|
+
`β’ Neon endpoint may be sleeping${C.reset}`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
console.log(`${C.dim}Watcher still running. Server NOT stopped.${C.reset}`);
|
|
136
|
+
busy = false;
|
|
151
137
|
return;
|
|
152
138
|
}
|
|
153
139
|
|
|
154
|
-
log.
|
|
155
|
-
|
|
140
|
+
console.log(`${C.green}β¨ Drizzle fully synced${C.reset}`);
|
|
141
|
+
busy = false;
|
|
156
142
|
}
|
|
157
143
|
|
|
158
144
|
/* =========================
|
|
159
|
-
π
|
|
145
|
+
π WATCH EVERYTHING
|
|
160
146
|
========================= */
|
|
161
|
-
|
|
147
|
+
injectTea();
|
|
162
148
|
pipeline("Initial");
|
|
163
149
|
|
|
164
|
-
log.info(`Watching files (${DEV_MODE ? "DEV MODE" : "SAFE MODE"})β¦`);
|
|
165
|
-
|
|
166
150
|
chokidar.watch(ROOT, {
|
|
167
|
-
ignored: [
|
|
168
|
-
/node_modules/,
|
|
169
|
-
/\.git/,
|
|
170
|
-
/\.next/,
|
|
171
|
-
/dist/,
|
|
172
|
-
/build/
|
|
173
|
-
],
|
|
151
|
+
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
174
152
|
ignoreInitial: true,
|
|
175
|
-
usePolling: true,
|
|
176
|
-
interval: 300,
|
|
177
153
|
}).on("all", (_, file) => {
|
|
178
|
-
|
|
179
|
-
pipeline(path.relative(ROOT, file));
|
|
180
|
-
}
|
|
154
|
+
pipeline(path.relative(ROOT, file));
|
|
181
155
|
});
|