drizzle-auto 1.1.2 โ 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 +53 -60
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,104 +6,90 @@ 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
|
-
|
|
13
|
+
bold: "\x1b[1m",
|
|
14
14
|
cyan: "\x1b[36m",
|
|
15
15
|
green: "\x1b[32m",
|
|
16
16
|
yellow: "\x1b[33m",
|
|
17
17
|
red: "\x1b[31m",
|
|
18
18
|
magenta: "\x1b[35m",
|
|
19
|
-
|
|
19
|
+
dim: "\x1b[2m",
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
/* =========================
|
|
23
|
-
๐งญ
|
|
23
|
+
๐งญ ROOT
|
|
24
24
|
========================= */
|
|
25
25
|
const ROOT = process.cwd();
|
|
26
26
|
const PKG = path.join(ROOT, "package.json");
|
|
27
27
|
|
|
28
28
|
/* =========================
|
|
29
|
-
|
|
29
|
+
โ ADD tea SCRIPT
|
|
30
30
|
========================= */
|
|
31
|
-
function
|
|
31
|
+
function injectTea() {
|
|
32
32
|
if (!fs.existsSync(PKG)) return;
|
|
33
|
-
|
|
34
33
|
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
35
34
|
pkg.scripts ||= {};
|
|
36
35
|
|
|
37
36
|
if (!pkg.scripts.tea) {
|
|
38
37
|
pkg.scripts.tea = "drizzle-auto";
|
|
39
38
|
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
40
|
-
console.log(
|
|
41
|
-
`${C.green}โ Added script:${C.reset} ${C.bold}npm run tea${C.reset}`
|
|
42
|
-
);
|
|
39
|
+
console.log(`${C.green}โ Added:${C.reset} npm run tea`);
|
|
43
40
|
}
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
/* =========================
|
|
47
|
-
๐ FIND
|
|
44
|
+
๐ FIND drizzle.config.*
|
|
48
45
|
========================= */
|
|
49
|
-
function
|
|
46
|
+
function findConfig() {
|
|
50
47
|
const exts = ["js", "mjs", "cjs", "ts", "mts"];
|
|
51
48
|
let found = null;
|
|
52
49
|
|
|
53
|
-
function walk(dir) {
|
|
50
|
+
(function walk(dir) {
|
|
54
51
|
if (found) return;
|
|
55
|
-
for (const
|
|
56
|
-
if (
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
if (file.isDirectory()) walk(full);
|
|
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);
|
|
60
56
|
else {
|
|
61
57
|
for (const e of exts) {
|
|
62
|
-
if (
|
|
63
|
-
found =
|
|
58
|
+
if (f.name === `drizzle.config.${e}`) {
|
|
59
|
+
found = p;
|
|
64
60
|
return;
|
|
65
61
|
}
|
|
66
62
|
}
|
|
67
63
|
}
|
|
68
64
|
}
|
|
69
|
-
}
|
|
65
|
+
})(ROOT);
|
|
70
66
|
|
|
71
|
-
walk(ROOT);
|
|
72
67
|
return found;
|
|
73
68
|
}
|
|
74
69
|
|
|
75
70
|
/* =========================
|
|
76
|
-
|
|
77
|
-
========================= */
|
|
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
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/* =========================
|
|
92
|
-
โ๏ธ SAFE RUNNER (NO SERVER KILL)
|
|
71
|
+
โ๏ธ SAFE RUNNER (ERROR AWARE)
|
|
93
72
|
========================= */
|
|
94
73
|
function run(cmd, args) {
|
|
95
74
|
return new Promise((resolve) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
75
|
+
let stderr = "";
|
|
76
|
+
|
|
77
|
+
const p = spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
78
|
+
|
|
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);
|
|
99
83
|
});
|
|
100
84
|
|
|
101
|
-
p.on("close", (code) =>
|
|
85
|
+
p.on("close", (code) => {
|
|
86
|
+
resolve({ ok: code === 0, stderr });
|
|
87
|
+
});
|
|
102
88
|
});
|
|
103
89
|
}
|
|
104
90
|
|
|
105
91
|
/* =========================
|
|
106
|
-
๐ PIPELINE (
|
|
92
|
+
๐ PIPELINE (NO LIES)
|
|
107
93
|
========================= */
|
|
108
94
|
let busy = false;
|
|
109
95
|
|
|
@@ -111,35 +97,42 @@ async function pipeline(trigger) {
|
|
|
111
97
|
if (busy) return;
|
|
112
98
|
busy = true;
|
|
113
99
|
|
|
114
|
-
console.log(
|
|
115
|
-
`\n${C.cyan}โก Trigger โ${C.reset} ${C.bold}${trigger}${C.reset}`
|
|
116
|
-
);
|
|
100
|
+
console.log(`\n${C.cyan}โก Trigger โ${C.reset} ${trigger}`);
|
|
117
101
|
|
|
118
|
-
const config =
|
|
102
|
+
const config = findConfig();
|
|
119
103
|
if (!config) {
|
|
120
104
|
console.log(`${C.yellow}โ No drizzle.config found${C.reset}`);
|
|
121
105
|
busy = false;
|
|
122
106
|
return;
|
|
123
107
|
}
|
|
124
108
|
|
|
125
|
-
if (!validateConfig(config)) {
|
|
126
|
-
console.log(`${C.red}โ Schema path invalid${C.reset}`);
|
|
127
|
-
busy = false;
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
109
|
console.log(`${C.magenta}โถ drizzle-kit generate${C.reset}`);
|
|
132
|
-
const
|
|
133
|
-
if (!
|
|
110
|
+
const gen = await run("npx", ["drizzle-kit", "generate"]);
|
|
111
|
+
if (!gen.ok) {
|
|
134
112
|
console.log(`${C.red}๐ Generate failed โ watcher alive${C.reset}`);
|
|
135
113
|
busy = false;
|
|
136
114
|
return;
|
|
137
115
|
}
|
|
138
116
|
|
|
139
117
|
console.log(`${C.magenta}โถ drizzle-kit push${C.reset}`);
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
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}`);
|
|
143
136
|
busy = false;
|
|
144
137
|
return;
|
|
145
138
|
}
|
|
@@ -151,7 +144,7 @@ async function pipeline(trigger) {
|
|
|
151
144
|
/* =========================
|
|
152
145
|
๐ WATCH EVERYTHING
|
|
153
146
|
========================= */
|
|
154
|
-
|
|
147
|
+
injectTea();
|
|
155
148
|
pipeline("Initial");
|
|
156
149
|
|
|
157
150
|
chokidar.watch(ROOT, {
|