bakit 2.0.0-alpha.12 → 2.0.0-alpha.13
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/dist/bin/bakit.js +111 -0
- package/package.json +1 -1
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
import { program } from 'commander';
|
|
3
|
+
import { fork } from 'child_process';
|
|
4
|
+
import chokidar from 'chokidar';
|
|
5
|
+
import path, { relative, sep } from 'path';
|
|
6
|
+
import { createJiti } from 'jiti';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
|
|
9
|
+
// src/bin/bakit.ts
|
|
10
|
+
var Module = class {
|
|
11
|
+
static jiti = createJiti(process.cwd());
|
|
12
|
+
static async import(module, defaultImport = false) {
|
|
13
|
+
let path2 = this.resolve(module);
|
|
14
|
+
if (!path2)
|
|
15
|
+
return null;
|
|
16
|
+
try {
|
|
17
|
+
return await this.jiti.import(path2, { default: defaultImport });
|
|
18
|
+
} catch (error) {
|
|
19
|
+
return console.error(`[Module] Import failed for ${path2}:`, error), null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
static isLoaded(module) {
|
|
23
|
+
let path2 = this.resolve(module);
|
|
24
|
+
return !!path2 && !!this.jiti.cache[path2];
|
|
25
|
+
}
|
|
26
|
+
static unload(module) {
|
|
27
|
+
let path2 = this.resolve(module);
|
|
28
|
+
return !path2 || !this.jiti.cache[path2] ? false : (delete this.jiti.cache[path2], true);
|
|
29
|
+
}
|
|
30
|
+
static resolve(module) {
|
|
31
|
+
try {
|
|
32
|
+
let url = this.jiti.esmResolve(module);
|
|
33
|
+
return fileURLToPath(url);
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
static getTopLevel(path2, entryDir) {
|
|
39
|
+
return relative(entryDir, path2).split(sep)[0] ?? null;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/base/process/DevProcessManager.ts
|
|
44
|
+
var DevProcessManager = class {
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this.options = options;
|
|
47
|
+
}
|
|
48
|
+
child = null;
|
|
49
|
+
restartTimer = null;
|
|
50
|
+
start() {
|
|
51
|
+
console.log("Starting bakit in dev mode..."), this.startChild(), this.startWatcher();
|
|
52
|
+
}
|
|
53
|
+
startChild() {
|
|
54
|
+
if (this.child) return;
|
|
55
|
+
let entry = path.resolve(this.options.entry);
|
|
56
|
+
this.child = fork(entry, {
|
|
57
|
+
execArgv: ["--import", "tsx"],
|
|
58
|
+
stdio: "inherit",
|
|
59
|
+
env: {
|
|
60
|
+
...process.env,
|
|
61
|
+
NODE_ENV: "development"
|
|
62
|
+
}
|
|
63
|
+
}), this.child.on("exit", () => {
|
|
64
|
+
this.child = null;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
restartChild() {
|
|
68
|
+
if (!this.child)
|
|
69
|
+
return this.startChild();
|
|
70
|
+
let old = this.child;
|
|
71
|
+
old.once("exit", () => {
|
|
72
|
+
this.child = null, this.startChild();
|
|
73
|
+
}), old.kill("SIGTERM");
|
|
74
|
+
}
|
|
75
|
+
startWatcher() {
|
|
76
|
+
let { rootDir } = this.options;
|
|
77
|
+
chokidar.watch(rootDir, {
|
|
78
|
+
ignoreInitial: true,
|
|
79
|
+
awaitWriteFinish: {
|
|
80
|
+
stabilityThreshold: 200,
|
|
81
|
+
pollInterval: 50
|
|
82
|
+
}
|
|
83
|
+
}).on("change", (file) => this.onFileChanged(file));
|
|
84
|
+
}
|
|
85
|
+
onFileChanged(file) {
|
|
86
|
+
if (!this.child)
|
|
87
|
+
return;
|
|
88
|
+
let top = Module.getTopLevel(file, this.options.rootDir);
|
|
89
|
+
if (top && this.options.hotDirs.includes(top)) {
|
|
90
|
+
this.child.connected && this.child.send({ type: `hmr:${top}`, file });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.scheduleRestart();
|
|
94
|
+
}
|
|
95
|
+
scheduleRestart() {
|
|
96
|
+
this.restartTimer && clearTimeout(this.restartTimer), this.restartTimer = setTimeout(() => {
|
|
97
|
+
console.log("Detected changes, restarting..."), this.restartChild(), this.restartTimer = null;
|
|
98
|
+
}, 150);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// src/bin/bakit.ts
|
|
103
|
+
program.name("bakit");
|
|
104
|
+
program.command("dev").action(() => {
|
|
105
|
+
config({ path: [".env.local", ".env"], quiet: true }), new DevProcessManager({
|
|
106
|
+
rootDir: "src",
|
|
107
|
+
entry: "src/index.ts",
|
|
108
|
+
hotDirs: ["commands", "listeners"]
|
|
109
|
+
}).start();
|
|
110
|
+
});
|
|
111
|
+
program.parse();
|