drizzle-auto 1.0.19 ā 1.0.20
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 +90 -87
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,119 +6,122 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
8
|
/* =========================
|
|
9
|
-
š§ PROJECT ROOT
|
|
9
|
+
š§ PROJECT ROOT
|
|
10
10
|
========================= */
|
|
11
|
-
const ROOT = process.
|
|
11
|
+
const ROOT = process.cwd();
|
|
12
12
|
const PKG = path.join(ROOT, "package.json");
|
|
13
13
|
|
|
14
|
-
/* =========================
|
|
15
|
-
šØ COLORS
|
|
16
|
-
========================= */
|
|
17
|
-
const C = {
|
|
18
|
-
reset: "\x1b[0m",
|
|
19
|
-
green: "\x1b[32m",
|
|
20
|
-
yellow: "\x1b[33m",
|
|
21
|
-
cyan: "\x1b[36m",
|
|
22
|
-
red: "\x1b[31m",
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const log = {
|
|
26
|
-
ok: (m) => console.log(`${C.green}ā${C.reset} ${m}`),
|
|
27
|
-
warn: (m) => console.log(`${C.yellow}ā²${C.reset} ${m}`),
|
|
28
|
-
err: (m) => console.log(`${C.red}ā${C.reset} ${m}`),
|
|
29
|
-
info: (m) => console.log(`${C.cyan}ā${C.reset} ${m}`)
|
|
30
|
-
};
|
|
31
|
-
|
|
32
14
|
/* =========================
|
|
33
15
|
š§© SCRIPT INJECTOR
|
|
34
16
|
========================= */
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
log.info("Script 'tea' already exists ā
");
|
|
46
|
-
}
|
|
17
|
+
function injectScript() {
|
|
18
|
+
if (!fs.existsSync(PKG)) return;
|
|
19
|
+
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
20
|
+
pkg.scripts ||= {};
|
|
21
|
+
|
|
22
|
+
if (!pkg.scripts.tea) {
|
|
23
|
+
pkg.scripts.tea = "drizzle-auto";
|
|
24
|
+
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
25
|
+
console.log("ā Script added ā npm run tea");
|
|
26
|
+
}
|
|
47
27
|
}
|
|
48
28
|
|
|
49
29
|
/* =========================
|
|
50
|
-
|
|
30
|
+
š DRIZZLE DETECTOR
|
|
51
31
|
========================= */
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
32
|
+
function detectDrizzle() {
|
|
33
|
+
const configs = ["js","mjs","ts","mts"]
|
|
34
|
+
.map(e => drizzle.config.${e})
|
|
35
|
+
.find(f => fs.existsSync(path.join(ROOT, f)));
|
|
36
|
+
|
|
37
|
+
if (!configs) {
|
|
38
|
+
console.log("ā ļø No drizzle.config found");
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let schemaPath = null;
|
|
43
|
+
try {
|
|
44
|
+
const content = fs.readFileSync(path.join(ROOT, configs), "utf8");
|
|
45
|
+
const match = content.match(/schema:\s*"'["']/);
|
|
46
|
+
if (match) schemaPath = path.join(ROOT, match[1]);
|
|
47
|
+
} catch {}
|
|
48
|
+
|
|
49
|
+
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
50
|
+
console.log("ā Schema file missing");
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { config: configs, schema: schemaPath };
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/* =========================
|
|
58
|
-
SAFE RUNNER
|
|
58
|
+
āļø SAFE COMMAND RUNNER
|
|
59
59
|
========================= */
|
|
60
60
|
function run(cmd) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
let failed = false;
|
|
63
|
+
|
|
64
|
+
const p = spawn("npx", cmd, { shell: true });
|
|
65
|
+
|
|
66
|
+
p.stdout.on("data", d => process.stdout.write(d));
|
|
67
|
+
p.stderr.on("data", d => {
|
|
68
|
+
failed = true;
|
|
69
|
+
process.stderr.write(d);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
p.on("close", code => {
|
|
73
|
+
resolve(code === 0 && !failed);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
});
|
|
71
77
|
}
|
|
72
78
|
|
|
73
79
|
/* =========================
|
|
74
|
-
PIPELINE (NON-KILLING)
|
|
80
|
+
š PIPELINE (NON-KILLING)
|
|
75
81
|
========================= */
|
|
76
82
|
let busy = false;
|
|
83
|
+
|
|
77
84
|
async function pipeline(trigger) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
log.ok("Drizzle fully synced āØ");
|
|
108
|
-
busy = false;
|
|
85
|
+
if (busy) return;
|
|
86
|
+
busy = true;
|
|
87
|
+
|
|
88
|
+
console.log(\nā” Trigger ā ${trigger});
|
|
89
|
+
|
|
90
|
+
const drizzle = detectDrizzle();
|
|
91
|
+
if (!drizzle) {
|
|
92
|
+
busy = false;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const genOK = await run(["drizzle-kit", "generate"]);
|
|
97
|
+
if (!genOK) {
|
|
98
|
+
console.log("š Generate failed ā pipeline paused");
|
|
99
|
+
busy = false;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const pushOK = await run(["drizzle-kit", "push"]);
|
|
104
|
+
if (!pushOK) {
|
|
105
|
+
console.log("š Push failed ā server still running");
|
|
106
|
+
busy = false;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
console.log("⨠Drizzle fully synced");
|
|
111
|
+
busy = false;
|
|
109
112
|
}
|
|
110
113
|
|
|
111
114
|
/* =========================
|
|
112
|
-
WATCHER (ALWAYS ALIVE)
|
|
115
|
+
š WATCHER (ALWAYS ALIVE)
|
|
113
116
|
========================= */
|
|
114
|
-
|
|
117
|
+
injectScript();
|
|
115
118
|
pipeline("Initial");
|
|
116
119
|
|
|
117
120
|
chokidar.watch(ROOT, {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
ignored: [/node_modules/, /.git/, /.next/, /dist/],
|
|
122
|
+
ignoreInitial: true,
|
|
123
|
+
usePolling: true,
|
|
124
|
+
interval: 300
|
|
122
125
|
}).on("all", (_, file) => {
|
|
123
|
-
|
|
124
|
-
});
|
|
126
|
+
pipeline(path.basename(file));
|
|
127
|
+
});
|