drizzle-auto 1.0.18 → 1.0.19
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 +24 -44
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -6,9 +6,9 @@ const chokidar = require("chokidar");
|
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
8
|
/* =========================
|
|
9
|
-
🧭 PROJECT ROOT
|
|
9
|
+
🧭 PROJECT ROOT (user project)
|
|
10
10
|
========================= */
|
|
11
|
-
const ROOT = process.cwd();
|
|
11
|
+
const ROOT = process.env.INIT_CWD || process.cwd(); // <-- important
|
|
12
12
|
const PKG = path.join(ROOT, "package.json");
|
|
13
13
|
|
|
14
14
|
/* =========================
|
|
@@ -20,22 +20,19 @@ const C = {
|
|
|
20
20
|
yellow: "\x1b[33m",
|
|
21
21
|
cyan: "\x1b[36m",
|
|
22
22
|
red: "\x1b[31m",
|
|
23
|
-
bold: "\x1b[1m",
|
|
24
|
-
dim: "\x1b[90m"
|
|
25
23
|
};
|
|
26
24
|
|
|
27
25
|
const log = {
|
|
28
26
|
ok: (m) => console.log(`${C.green}✔${C.reset} ${m}`),
|
|
29
|
-
info: (m) => console.log(`${C.cyan}●${C.reset} ${m}`),
|
|
30
27
|
warn: (m) => console.log(`${C.yellow}▲${C.reset} ${m}`),
|
|
31
28
|
err: (m) => console.log(`${C.red}✖${C.reset} ${m}`),
|
|
32
|
-
|
|
29
|
+
info: (m) => console.log(`${C.cyan}●${C.reset} ${m}`)
|
|
33
30
|
};
|
|
34
31
|
|
|
35
32
|
/* =========================
|
|
36
33
|
🧩 SCRIPT INJECTOR
|
|
37
34
|
========================= */
|
|
38
|
-
function
|
|
35
|
+
function injectTeaScript() {
|
|
39
36
|
if (!fs.existsSync(PKG)) return;
|
|
40
37
|
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
41
38
|
pkg.scripts ||= {};
|
|
@@ -44,68 +41,51 @@ function injectScript() {
|
|
|
44
41
|
pkg.scripts.tea = "drizzle-auto";
|
|
45
42
|
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
46
43
|
log.ok("Script added → npm run tea");
|
|
44
|
+
} else {
|
|
45
|
+
log.info("Script 'tea' already exists ✅");
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
/* =========================
|
|
51
|
-
|
|
50
|
+
POSTINSTALL / INJECT HANDLER
|
|
52
51
|
========================= */
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.find(f => fs.existsSync(path.join(ROOT, f)));
|
|
57
|
-
|
|
58
|
-
if (!configs) {
|
|
59
|
-
log.warn("No drizzle.config found");
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
let schemaPath = null;
|
|
64
|
-
try {
|
|
65
|
-
const content = fs.readFileSync(path.join(ROOT, configs), "utf8");
|
|
66
|
-
const match = content.match(/schema:\s*['"](.+?)['"]/);
|
|
67
|
-
if (match) schemaPath = path.join(ROOT, match[1]);
|
|
68
|
-
} catch {}
|
|
69
|
-
|
|
70
|
-
if (schemaPath && !fs.existsSync(schemaPath)) {
|
|
71
|
-
log.err("Schema file missing");
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return { config: configs, schema: schemaPath };
|
|
52
|
+
if (process.argv.includes("--postinstall")) {
|
|
53
|
+
injectTeaScript();
|
|
54
|
+
process.exit(0);
|
|
76
55
|
}
|
|
77
56
|
|
|
78
57
|
/* =========================
|
|
79
|
-
|
|
58
|
+
SAFE RUNNER
|
|
80
59
|
========================= */
|
|
81
60
|
function run(cmd) {
|
|
82
|
-
return new Promise(resolve => {
|
|
61
|
+
return new Promise((resolve) => {
|
|
83
62
|
let failed = false;
|
|
84
63
|
const p = spawn("npx", cmd, { shell: true });
|
|
85
|
-
|
|
86
64
|
p.stdout.on("data", d => process.stdout.write(d));
|
|
87
65
|
p.stderr.on("data", d => {
|
|
88
66
|
failed = true;
|
|
89
67
|
process.stderr.write(d);
|
|
90
68
|
});
|
|
91
|
-
|
|
92
69
|
p.on("close", code => resolve(code === 0 && !failed));
|
|
93
70
|
});
|
|
94
71
|
}
|
|
95
72
|
|
|
96
73
|
/* =========================
|
|
97
|
-
|
|
74
|
+
PIPELINE (NON-KILLING)
|
|
98
75
|
========================= */
|
|
99
76
|
let busy = false;
|
|
100
|
-
|
|
101
77
|
async function pipeline(trigger) {
|
|
102
78
|
if (busy) return;
|
|
103
79
|
busy = true;
|
|
104
80
|
|
|
105
|
-
log.
|
|
81
|
+
console.log(`\n${C.cyan}⚡ Trigger → ${trigger}${C.reset}`);
|
|
106
82
|
|
|
107
|
-
const
|
|
108
|
-
|
|
83
|
+
const configs = ["js","mjs","ts","mts"]
|
|
84
|
+
.map(e => `drizzle.config.${e}`)
|
|
85
|
+
.find(f => fs.existsSync(path.join(ROOT, f)));
|
|
86
|
+
|
|
87
|
+
if (!configs) {
|
|
88
|
+
log.warn("No drizzle.config found ❌");
|
|
109
89
|
busy = false;
|
|
110
90
|
return;
|
|
111
91
|
}
|
|
@@ -119,19 +99,19 @@ async function pipeline(trigger) {
|
|
|
119
99
|
|
|
120
100
|
const pushOK = await run(["drizzle-kit", "push"]);
|
|
121
101
|
if (!pushOK) {
|
|
122
|
-
log.
|
|
102
|
+
log.err("Push failed — server still running");
|
|
123
103
|
busy = false;
|
|
124
104
|
return;
|
|
125
105
|
}
|
|
126
106
|
|
|
127
|
-
log.ok("
|
|
107
|
+
log.ok("Drizzle fully synced ✨");
|
|
128
108
|
busy = false;
|
|
129
109
|
}
|
|
130
110
|
|
|
131
111
|
/* =========================
|
|
132
|
-
|
|
112
|
+
WATCHER (ALWAYS ALIVE)
|
|
133
113
|
========================= */
|
|
134
|
-
|
|
114
|
+
injectTeaScript(); // manual npx start
|
|
135
115
|
pipeline("Initial");
|
|
136
116
|
|
|
137
117
|
chokidar.watch(ROOT, {
|