drizzle-auto 1.0.23 ā 1.1.0
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 +68 -53
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,80 +6,98 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
8
|
/* =========================
|
|
9
|
-
|
|
9
|
+
šØ COLORS (PRO)
|
|
10
10
|
========================= */
|
|
11
|
-
const
|
|
12
|
-
const PKG = path.join(ROOT, "package.json");
|
|
13
|
-
|
|
14
|
-
/* =========================
|
|
15
|
-
šØ COLOR HELPERS
|
|
16
|
-
========================= */
|
|
17
|
-
const colors = {
|
|
11
|
+
const C = {
|
|
18
12
|
reset: "\x1b[0m",
|
|
19
|
-
bright: "\x1b[1m",
|
|
20
|
-
red: "\x1b[31m",
|
|
21
|
-
green: "\x1b[32m",
|
|
22
|
-
yellow: "\x1b[33m",
|
|
23
13
|
blue: "\x1b[34m",
|
|
24
|
-
magenta: "\x1b[35m",
|
|
25
14
|
cyan: "\x1b[36m",
|
|
15
|
+
green: "\x1b[32m",
|
|
16
|
+
yellow: "\x1b[33m",
|
|
17
|
+
red: "\x1b[31m",
|
|
18
|
+
dim: "\x1b[90m",
|
|
26
19
|
};
|
|
27
20
|
|
|
28
21
|
/* =========================
|
|
29
|
-
|
|
22
|
+
š§ ROOT + PKG
|
|
23
|
+
========================= */
|
|
24
|
+
const ROOT = process.cwd();
|
|
25
|
+
const PKG = path.join(ROOT, "package.json");
|
|
26
|
+
|
|
27
|
+
/* =========================
|
|
28
|
+
ā SCRIPT INJECTOR
|
|
30
29
|
========================= */
|
|
31
|
-
function
|
|
30
|
+
function injectTea() {
|
|
32
31
|
if (!fs.existsSync(PKG)) return;
|
|
32
|
+
|
|
33
33
|
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
34
34
|
pkg.scripts ||= {};
|
|
35
35
|
|
|
36
36
|
if (!pkg.scripts.tea) {
|
|
37
37
|
pkg.scripts.tea = "drizzle-auto";
|
|
38
38
|
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
39
|
-
console.log(`${
|
|
39
|
+
console.log(`${C.cyan}ā Added script: npm run tea${C.reset}`);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/* =========================
|
|
44
|
-
š
|
|
44
|
+
š RECURSIVE SEARCH
|
|
45
45
|
========================= */
|
|
46
|
-
function
|
|
47
|
-
const
|
|
48
|
-
.
|
|
49
|
-
.
|
|
46
|
+
function walk(dir, results = []) {
|
|
47
|
+
for (const file of fs.readdirSync(dir)) {
|
|
48
|
+
const p = path.join(dir, file);
|
|
49
|
+
if (p.includes("node_modules") || p.includes(".git")) continue;
|
|
50
|
+
|
|
51
|
+
const stat = fs.statSync(p);
|
|
52
|
+
if (stat.isDirectory()) walk(p, results);
|
|
53
|
+
else results.push(p);
|
|
54
|
+
}
|
|
55
|
+
return results;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function findDrizzleConfig() {
|
|
59
|
+
const files = walk(ROOT);
|
|
60
|
+
const config = files.find(f =>
|
|
61
|
+
/drizzle\.config\.(js|mjs|ts|mts)$/.test(f)
|
|
62
|
+
);
|
|
50
63
|
|
|
51
|
-
if (!
|
|
52
|
-
console.log(`${
|
|
64
|
+
if (!config) {
|
|
65
|
+
console.log(`${C.yellow}ā ļø drizzle.config not found (anywhere)${C.reset}`);
|
|
53
66
|
return null;
|
|
54
67
|
}
|
|
55
68
|
|
|
56
69
|
let schemaPath = null;
|
|
57
70
|
try {
|
|
58
|
-
const content = fs.readFileSync(
|
|
71
|
+
const content = fs.readFileSync(config, "utf8");
|
|
59
72
|
const match = content.match(/schema:\s*["'](.+?)["']/);
|
|
60
|
-
if (match)
|
|
73
|
+
if (match) {
|
|
74
|
+
schemaPath = path.resolve(path.dirname(config), match[1]);
|
|
75
|
+
}
|
|
61
76
|
} catch {}
|
|
62
77
|
|
|
63
78
|
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
64
|
-
console.log(`${
|
|
79
|
+
console.log(`${C.red}ā Schema missing: ${schemaPath}${C.reset}`);
|
|
65
80
|
return null;
|
|
66
81
|
}
|
|
67
82
|
|
|
68
|
-
|
|
83
|
+
console.log(`${C.green}ā Drizzle config found${C.reset}`);
|
|
84
|
+
return true;
|
|
69
85
|
}
|
|
70
86
|
|
|
71
87
|
/* =========================
|
|
72
|
-
āļø
|
|
88
|
+
āļø COMMAND RUNNER
|
|
73
89
|
========================= */
|
|
74
90
|
function run(cmd) {
|
|
75
|
-
return new Promise(
|
|
91
|
+
return new Promise(resolve => {
|
|
76
92
|
let failed = false;
|
|
77
93
|
const p = spawn("npx", cmd, { shell: true });
|
|
78
94
|
|
|
79
|
-
p.stdout.on("data", d =>
|
|
95
|
+
p.stdout.on("data", d =>
|
|
96
|
+
process.stdout.write(C.dim + d.toString() + C.reset)
|
|
97
|
+
);
|
|
80
98
|
p.stderr.on("data", d => {
|
|
81
99
|
failed = true;
|
|
82
|
-
process.stderr.write(
|
|
100
|
+
process.stderr.write(C.red + d.toString() + C.reset);
|
|
83
101
|
});
|
|
84
102
|
|
|
85
103
|
p.on("close", code => resolve(code === 0 && !failed));
|
|
@@ -87,49 +105,46 @@ function run(cmd) {
|
|
|
87
105
|
}
|
|
88
106
|
|
|
89
107
|
/* =========================
|
|
90
|
-
š PIPELINE (
|
|
108
|
+
š PIPELINE (SAFE)
|
|
91
109
|
========================= */
|
|
92
|
-
let
|
|
110
|
+
let running = false;
|
|
93
111
|
|
|
94
112
|
async function pipeline(trigger) {
|
|
95
|
-
if (
|
|
96
|
-
|
|
113
|
+
if (running) return;
|
|
114
|
+
running = true;
|
|
97
115
|
|
|
98
|
-
console.log(`${
|
|
116
|
+
console.log(`${C.blue}\nā” Trigger ā ${trigger}${C.reset}`);
|
|
99
117
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
busy = false;
|
|
118
|
+
if (!findDrizzleConfig()) {
|
|
119
|
+
running = false;
|
|
103
120
|
return;
|
|
104
121
|
}
|
|
105
122
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
busy = false;
|
|
123
|
+
if (!(await run(["drizzle-kit", "generate"]))) {
|
|
124
|
+
console.log(`${C.red}š generate failed (watcher alive)${C.reset}`);
|
|
125
|
+
running = false;
|
|
110
126
|
return;
|
|
111
127
|
}
|
|
112
128
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
busy = false;
|
|
129
|
+
if (!(await run(["drizzle-kit", "push"]))) {
|
|
130
|
+
console.log(`${C.red}š push failed (watcher alive)${C.reset}`);
|
|
131
|
+
running = false;
|
|
117
132
|
return;
|
|
118
133
|
}
|
|
119
134
|
|
|
120
|
-
console.log(`${
|
|
121
|
-
|
|
135
|
+
console.log(`${C.green}⨠Drizzle synced${C.reset}`);
|
|
136
|
+
running = false;
|
|
122
137
|
}
|
|
123
138
|
|
|
124
139
|
/* =========================
|
|
125
|
-
š WATCHER
|
|
140
|
+
š WATCHER
|
|
126
141
|
========================= */
|
|
127
|
-
|
|
128
|
-
pipeline("
|
|
142
|
+
injectTea();
|
|
143
|
+
pipeline("initial");
|
|
129
144
|
|
|
130
145
|
chokidar.watch(ROOT, {
|
|
131
146
|
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
132
147
|
ignoreInitial: true,
|
|
133
148
|
usePolling: true,
|
|
134
|
-
interval: 300
|
|
149
|
+
interval: 300,
|
|
135
150
|
}).on("all", (_, file) => pipeline(path.basename(file)));
|