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