drizzle-auto 1.0.13 ā 1.0.14
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 +84 -83
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,122 +5,123 @@ const path = require("path");
|
|
|
5
5
|
const chokidar = require("chokidar");
|
|
6
6
|
const { spawn } = require("child_process");
|
|
7
7
|
|
|
8
|
-
/*
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
/* ======================
|
|
9
|
+
šØ COLOR SYSTEM
|
|
10
|
+
====================== */
|
|
11
|
+
const c = {
|
|
12
|
+
reset: "\x1b[0m",
|
|
13
|
+
cyan: "\x1b[36m",
|
|
14
|
+
green: "\x1b[32m",
|
|
15
|
+
yellow: "\x1b[33m",
|
|
16
|
+
red: "\x1b[31m",
|
|
17
|
+
gray: "\x1b[90m",
|
|
18
|
+
bold: "\x1b[1m"
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const log = {
|
|
22
|
+
sys: m => console.log(`${c.cyan}ā${c.reset} ${m}`),
|
|
23
|
+
ok: m => console.log(`${c.green}ā${c.reset} ${m}`),
|
|
24
|
+
warn: m => console.log(`${c.yellow}ā²${c.reset} ${m}`),
|
|
25
|
+
err: m => console.log(`${c.red}ā${c.reset} ${m}`),
|
|
26
|
+
info: m => console.log(`${c.gray}${m}${c.reset}`)
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/* ======================
|
|
30
|
+
š PROJECT ROOT
|
|
31
|
+
====================== */
|
|
11
32
|
const ROOT = process.cwd();
|
|
12
33
|
const PKG = path.join(ROOT, "package.json");
|
|
13
34
|
|
|
14
|
-
/*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function
|
|
18
|
-
if (!fs.existsSync(PKG))
|
|
35
|
+
/* ======================
|
|
36
|
+
ā ADD tea SCRIPT
|
|
37
|
+
====================== */
|
|
38
|
+
function addTeaScript() {
|
|
39
|
+
if (!fs.existsSync(PKG)) {
|
|
40
|
+
log.err("package.json not found");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
19
44
|
const pkg = JSON.parse(fs.readFileSync(PKG, "utf8"));
|
|
20
45
|
pkg.scripts ||= {};
|
|
21
46
|
|
|
22
47
|
if (!pkg.scripts.tea) {
|
|
23
48
|
pkg.scripts.tea = "drizzle-auto";
|
|
24
49
|
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2));
|
|
25
|
-
|
|
50
|
+
log.ok("Script added ā pnpm run tea");
|
|
51
|
+
} else {
|
|
52
|
+
log.info("Script already exists");
|
|
26
53
|
}
|
|
27
54
|
}
|
|
28
55
|
|
|
29
|
-
/*
|
|
30
|
-
š DRIZZLE
|
|
31
|
-
|
|
32
|
-
function
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
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 };
|
|
56
|
+
/* ======================
|
|
57
|
+
š DRIZZLE CONFIG SCAN
|
|
58
|
+
====================== */
|
|
59
|
+
function findDrizzleConfig() {
|
|
60
|
+
const exts = ["js", "mjs", "ts", "mts"];
|
|
61
|
+
return exts.find(ext =>
|
|
62
|
+
fs.existsSync(path.join(ROOT, `drizzle.config.${ext}`))
|
|
63
|
+
);
|
|
55
64
|
}
|
|
56
65
|
|
|
57
|
-
/*
|
|
66
|
+
/* ======================
|
|
58
67
|
āļø SAFE COMMAND RUNNER
|
|
59
|
-
|
|
68
|
+
====================== */
|
|
60
69
|
function run(cmd) {
|
|
61
|
-
return new Promise(
|
|
62
|
-
|
|
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
|
-
});
|
|
70
|
+
return new Promise(resolve => {
|
|
71
|
+
const p = spawn("npx", cmd, { stdio: "inherit", shell: true });
|
|
72
|
+
p.on("close", code => resolve(code === 0));
|
|
75
73
|
});
|
|
76
74
|
}
|
|
77
75
|
|
|
78
|
-
/*
|
|
79
|
-
š PIPELINE
|
|
80
|
-
|
|
81
|
-
let
|
|
76
|
+
/* ======================
|
|
77
|
+
š PIPELINE
|
|
78
|
+
====================== */
|
|
79
|
+
let running = false;
|
|
82
80
|
|
|
83
|
-
async function pipeline(
|
|
84
|
-
if (
|
|
85
|
-
|
|
81
|
+
async function pipeline(reason) {
|
|
82
|
+
if (running) return;
|
|
83
|
+
running = true;
|
|
86
84
|
|
|
87
|
-
|
|
85
|
+
log.sys(`Triggered by ${reason}`);
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
if (!findDrizzleConfig()) {
|
|
88
|
+
log.warn("No drizzle.config found (js/mjs/ts/mts)");
|
|
89
|
+
running = false;
|
|
92
90
|
return;
|
|
93
91
|
}
|
|
94
92
|
|
|
95
|
-
|
|
96
|
-
if (!
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
log.sys("Running drizzle generate...");
|
|
94
|
+
if (!(await run(["drizzle-kit", "generate"]))) {
|
|
95
|
+
log.err("Generate failed ā waiting for fix");
|
|
96
|
+
running = false;
|
|
99
97
|
return;
|
|
100
98
|
}
|
|
101
99
|
|
|
102
|
-
|
|
103
|
-
if (!
|
|
104
|
-
|
|
105
|
-
|
|
100
|
+
log.sys("Running drizzle push...");
|
|
101
|
+
if (!(await run(["drizzle-kit", "push"]))) {
|
|
102
|
+
log.err("Push failed ā waiting for fix");
|
|
103
|
+
running = false;
|
|
106
104
|
return;
|
|
107
105
|
}
|
|
108
106
|
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
log.ok("Database synced successfully");
|
|
108
|
+
running = false;
|
|
111
109
|
}
|
|
112
110
|
|
|
113
|
-
/*
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
111
|
+
/* ======================
|
|
112
|
+
š START
|
|
113
|
+
====================== */
|
|
114
|
+
console.log(`
|
|
115
|
+
${c.bold}${c.cyan}Drizzle Auto${c.reset}
|
|
116
|
+
${c.gray}Server-side watcher initialized${c.reset}
|
|
117
|
+
`);
|
|
118
|
+
|
|
119
|
+
addTeaScript();
|
|
120
|
+
pipeline("startup");
|
|
118
121
|
|
|
119
122
|
chokidar.watch(ROOT, {
|
|
120
123
|
ignored: [/node_modules/, /\.git/, /\.next/, /dist/],
|
|
121
|
-
ignoreInitial: true
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
pipeline(path.basename(file));
|
|
126
|
-
});
|
|
124
|
+
ignoreInitial: true
|
|
125
|
+
}).on("change", file =>
|
|
126
|
+
pipeline(path.basename(file))
|
|
127
|
+
);
|