drizzle-auto 1.0.12 → 1.0.14
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 +87 -98
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,133 +6,122 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
8
|
/* ======================
|
|
9
|
-
|
|
9
|
+
🎨 COLOR SYSTEM
|
|
10
10
|
====================== */
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
const c = {
|
|
12
|
+
reset: "\x1b[0m",
|
|
13
|
+
cyan: "\x1b[36m",
|
|
14
|
+
green: "\x1b[32m",
|
|
15
|
+
yellow: "\x1b[33m",
|
|
16
|
+
red: "\x1b[31m",
|
|
17
|
+
gray: "\x1b[90m",
|
|
18
|
+
bold: "\x1b[1m"
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const log = {
|
|
22
|
+
sys: m => console.log(`${c.cyan}●${c.reset} ${m}`),
|
|
23
|
+
ok: m => console.log(`${c.green}✔${c.reset} ${m}`),
|
|
24
|
+
warn: m => console.log(`${c.yellow}▲${c.reset} ${m}`),
|
|
25
|
+
err: m => console.log(`${c.red}✖${c.reset} ${m}`),
|
|
26
|
+
info: m => console.log(`${c.gray}${m}${c.reset}`)
|
|
27
|
+
};
|
|
17
28
|
|
|
18
29
|
/* ======================
|
|
19
|
-
|
|
30
|
+
📍 PROJECT ROOT
|
|
20
31
|
====================== */
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
const start = txt => {
|
|
24
|
-
stop();
|
|
25
|
-
spin = setInterval(()=>{
|
|
26
|
-
process.stdout.write(`\r${color()}${FRAMES[i++%FRAMES.length]} ${txt}${RESET}`);
|
|
27
|
-
},80);
|
|
28
|
-
};
|
|
29
|
-
const stop = ()=>{ if(spin){ clearInterval(spin); spin=null; process.stdout.write("\r\x1b[K"); }};
|
|
32
|
+
const ROOT = process.cwd();
|
|
33
|
+
const PKG = path.join(ROOT, "package.json");
|
|
30
34
|
|
|
31
35
|
/* ======================
|
|
32
|
-
|
|
36
|
+
☕ ADD tea SCRIPT
|
|
33
37
|
====================== */
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
function addTeaScript() {
|
|
39
|
+
if (!fs.existsSync(PKG)) {
|
|
40
|
+
log.err("package.json not found");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
if(!fs.existsSync(PKG)) return;
|
|
39
|
-
const pkg = JSON.parse(fs.readFileSync(PKG,"utf8"));
|
|
44
|
+
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
40
45
|
pkg.scripts ||= {};
|
|
41
|
-
|
|
46
|
+
|
|
47
|
+
if (!pkg.scripts.tea) {
|
|
42
48
|
pkg.scripts.tea = "drizzle-auto";
|
|
43
|
-
fs.writeFileSync(PKG, JSON.stringify(pkg,null,2));
|
|
44
|
-
|
|
49
|
+
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
50
|
+
log.ok("Script added → pnpm run tea");
|
|
51
|
+
} else {
|
|
52
|
+
log.info("Script already exists");
|
|
45
53
|
}
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
/* ======================
|
|
49
|
-
|
|
57
|
+
🔍 DRIZZLE CONFIG SCAN
|
|
50
58
|
====================== */
|
|
51
|
-
function
|
|
52
|
-
const exts = ["js","
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if(!cfg) return fail("Drizzle config not found");
|
|
57
|
-
|
|
58
|
-
const txt = fs.readFileSync(path.join(ROOT,cfg),"utf8");
|
|
59
|
-
const m = txt.match(/schema:\s*["'](.+?)["']/);
|
|
60
|
-
if(!m) return fail("Schema path missing");
|
|
61
|
-
|
|
62
|
-
if(!fs.existsSync(path.join(ROOT,m[1])))
|
|
63
|
-
return fail(`Schema not found → ${m[1]}`);
|
|
64
|
-
|
|
65
|
-
if(!fs.existsSync(path.join(ROOT,".env")))
|
|
66
|
-
return fail(".env missing");
|
|
67
|
-
|
|
68
|
-
if(!fs.existsSync(path.join(ROOT,"node_modules")))
|
|
69
|
-
return fail("node_modules missing");
|
|
70
|
-
|
|
71
|
-
return { ok:true };
|
|
59
|
+
function findDrizzleConfig() {
|
|
60
|
+
const exts = ["js", "mjs", "ts", "mts"];
|
|
61
|
+
return exts.find(ext =>
|
|
62
|
+
fs.existsSync(path.join(ROOT, `drizzle.config.${ext}`))
|
|
63
|
+
);
|
|
72
64
|
}
|
|
73
65
|
|
|
74
|
-
const fail = m => ({ ok:false, msg:m });
|
|
75
|
-
|
|
76
66
|
/* ======================
|
|
77
|
-
⚙️ RUNNER
|
|
67
|
+
⚙️ SAFE COMMAND RUNNER
|
|
78
68
|
====================== */
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
stop();
|
|
84
|
-
res(code===0);
|
|
69
|
+
function run(cmd) {
|
|
70
|
+
return new Promise(resolve => {
|
|
71
|
+
const p = spawn("npx", cmd, { stdio: "inherit", shell: true });
|
|
72
|
+
p.on("close", code => resolve(code === 0));
|
|
85
73
|
});
|
|
86
|
-
}
|
|
74
|
+
}
|
|
87
75
|
|
|
88
76
|
/* ======================
|
|
89
|
-
|
|
77
|
+
🔁 PIPELINE
|
|
90
78
|
====================== */
|
|
91
|
-
let
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
console.log("\x1b[31m❌ "+a.msg+RESET);
|
|
103
|
-
busy=false;
|
|
79
|
+
let running = false;
|
|
80
|
+
|
|
81
|
+
async function pipeline(reason) {
|
|
82
|
+
if (running) return;
|
|
83
|
+
running = true;
|
|
84
|
+
|
|
85
|
+
log.sys(`Triggered by ${reason}`);
|
|
86
|
+
|
|
87
|
+
if (!findDrizzleConfig()) {
|
|
88
|
+
log.warn("No drizzle.config found (js/mjs/ts/mts)");
|
|
89
|
+
running = false;
|
|
104
90
|
return;
|
|
105
91
|
}
|
|
106
92
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
93
|
+
log.sys("Running drizzle generate...");
|
|
94
|
+
if (!(await run(["drizzle-kit", "generate"]))) {
|
|
95
|
+
log.err("Generate failed — waiting for fix");
|
|
96
|
+
running = false;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
log.sys("Running drizzle push...");
|
|
101
|
+
if (!(await run(["drizzle-kit", "push"]))) {
|
|
102
|
+
log.err("Push failed — waiting for fix");
|
|
103
|
+
running = false;
|
|
104
|
+
return;
|
|
113
105
|
}
|
|
114
106
|
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
log.ok("Database synced successfully");
|
|
108
|
+
running = false;
|
|
117
109
|
}
|
|
118
110
|
|
|
119
111
|
/* ======================
|
|
120
|
-
|
|
112
|
+
🚀 START
|
|
121
113
|
====================== */
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
console.log("\x1b[31m🔥 Crash:",e.message,RESET);
|
|
137
|
-
busy=false;
|
|
138
|
-
});
|
|
114
|
+
console.log(`
|
|
115
|
+
${c.bold}${c.cyan}Drizzle Auto${c.reset}
|
|
116
|
+
${c.gray}Server-side watcher initialized${c.reset}
|
|
117
|
+
`);
|
|
118
|
+
|
|
119
|
+
addTeaScript();
|
|
120
|
+
pipeline("startup");
|
|
121
|
+
|
|
122
|
+
chokidar.watch(ROOT, {
|
|
123
|
+
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
124
|
+
ignoreInitial: true
|
|
125
|
+
}).on("change", file =>
|
|
126
|
+
pipeline(path.basename(file))
|
|
127
|
+
);
|