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