buncargo 1.0.13 → 1.0.17
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/bin.ts +17 -1
- package/cli.ts +4 -10
- package/core/process.ts +36 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +22 -8
- package/dist/cli.js +3 -3
- package/dist/core/index.js +4 -2
- package/dist/core/process.d.ts +7 -0
- package/dist/core/process.js +3 -1
- package/dist/core/watchdog.js +2 -2
- package/dist/environment.js +3 -3
- package/dist/index-1yvbwj4k.js +274 -0
- package/dist/index-2f47khe5.js +390 -0
- package/dist/index-8hbbj1mp.js +136 -0
- package/dist/index-g6eb5wdw.js +132 -0
- package/dist/index-ggq3yryx.js +120 -0
- package/dist/index-kf3dhser.js +160 -0
- package/dist/index-qw4093g2.js +58 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +8 -6
- package/dist/loader.js +4 -4
- package/index.ts +1 -0
- package/package.json +140 -140
- package/readme.md +3 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isProcessAlive
|
|
3
|
+
} from "./index-1yvbwj4k.js";
|
|
4
|
+
|
|
5
|
+
// core/watchdog.ts
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
8
|
+
function getHeartbeatFile(projectName) {
|
|
9
|
+
return `/tmp/${projectName}-heartbeat`;
|
|
10
|
+
}
|
|
11
|
+
function getWatchdogPidFile(projectName) {
|
|
12
|
+
return `/tmp/${projectName}-watchdog.pid`;
|
|
13
|
+
}
|
|
14
|
+
var heartbeatInterval = null;
|
|
15
|
+
function startHeartbeat(projectName, intervalMs = 30000) {
|
|
16
|
+
const heartbeatFile = getHeartbeatFile(projectName);
|
|
17
|
+
writeFileSync(heartbeatFile, Date.now().toString());
|
|
18
|
+
heartbeatInterval = setInterval(() => {
|
|
19
|
+
writeFileSync(heartbeatFile, Date.now().toString());
|
|
20
|
+
}, intervalMs);
|
|
21
|
+
}
|
|
22
|
+
function stopHeartbeat() {
|
|
23
|
+
if (heartbeatInterval) {
|
|
24
|
+
clearInterval(heartbeatInterval);
|
|
25
|
+
heartbeatInterval = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function readHeartbeat(projectName) {
|
|
29
|
+
const heartbeatFile = getHeartbeatFile(projectName);
|
|
30
|
+
try {
|
|
31
|
+
if (!existsSync(heartbeatFile))
|
|
32
|
+
return null;
|
|
33
|
+
const content = readFileSync(heartbeatFile, "utf-8");
|
|
34
|
+
const timestamp = parseInt(content, 10);
|
|
35
|
+
return Number.isNaN(timestamp) ? null : timestamp;
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function removeHeartbeatFile(projectName) {
|
|
41
|
+
const heartbeatFile = getHeartbeatFile(projectName);
|
|
42
|
+
try {
|
|
43
|
+
unlinkSync(heartbeatFile);
|
|
44
|
+
} catch {}
|
|
45
|
+
}
|
|
46
|
+
function isWatchdogRunning(projectName) {
|
|
47
|
+
const pidFile = getWatchdogPidFile(projectName);
|
|
48
|
+
try {
|
|
49
|
+
if (!existsSync(pidFile))
|
|
50
|
+
return false;
|
|
51
|
+
const content = readFileSync(pidFile, "utf-8");
|
|
52
|
+
const pid = parseInt(content, 10);
|
|
53
|
+
if (Number.isNaN(pid))
|
|
54
|
+
return false;
|
|
55
|
+
return isProcessAlive(pid);
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function getWatchdogPid(projectName) {
|
|
61
|
+
const pidFile = getWatchdogPidFile(projectName);
|
|
62
|
+
try {
|
|
63
|
+
if (!existsSync(pidFile))
|
|
64
|
+
return null;
|
|
65
|
+
const content = readFileSync(pidFile, "utf-8");
|
|
66
|
+
const pid = parseInt(content, 10);
|
|
67
|
+
if (Number.isNaN(pid))
|
|
68
|
+
return null;
|
|
69
|
+
if (!isProcessAlive(pid))
|
|
70
|
+
return null;
|
|
71
|
+
return pid;
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function spawnWatchdog(projectName, root, options = {}) {
|
|
77
|
+
const { timeoutMinutes = 10, verbose = true, composeFile } = options;
|
|
78
|
+
const existingPid = getWatchdogPid(projectName);
|
|
79
|
+
if (existingPid) {
|
|
80
|
+
if (verbose)
|
|
81
|
+
console.log(`✓ Watchdog already running (PID: ${existingPid})`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const pidFile = getWatchdogPidFile(projectName);
|
|
85
|
+
try {
|
|
86
|
+
unlinkSync(pidFile);
|
|
87
|
+
} catch {}
|
|
88
|
+
const watchdogScript = new URL("./watchdog-runner.ts", import.meta.url).pathname;
|
|
89
|
+
const proc = spawn("bun", ["run", watchdogScript], {
|
|
90
|
+
cwd: root,
|
|
91
|
+
detached: true,
|
|
92
|
+
stdio: "ignore",
|
|
93
|
+
env: {
|
|
94
|
+
...process.env,
|
|
95
|
+
WATCHDOG_PROJECT_NAME: projectName,
|
|
96
|
+
WATCHDOG_HEARTBEAT_FILE: getHeartbeatFile(projectName),
|
|
97
|
+
WATCHDOG_PID_FILE: pidFile,
|
|
98
|
+
WATCHDOG_TIMEOUT_MS: String(timeoutMinutes * 60 * 1000),
|
|
99
|
+
WATCHDOG_COMPOSE_ARG: composeFile ? `-f ${composeFile}` : ""
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
proc.unref();
|
|
103
|
+
if (verbose && proc.pid) {
|
|
104
|
+
console.log(`✓ Watchdog started (PID: ${proc.pid})`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function stopWatchdog(projectName) {
|
|
108
|
+
const pid = getWatchdogPid(projectName);
|
|
109
|
+
if (pid) {
|
|
110
|
+
try {
|
|
111
|
+
process.kill(pid, "SIGTERM");
|
|
112
|
+
} catch {}
|
|
113
|
+
}
|
|
114
|
+
const pidFile = getWatchdogPidFile(projectName);
|
|
115
|
+
try {
|
|
116
|
+
unlinkSync(pidFile);
|
|
117
|
+
} catch {}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { getHeartbeatFile, getWatchdogPidFile, startHeartbeat, stopHeartbeat, readHeartbeat, removeHeartbeatFile, isWatchdogRunning, getWatchdogPid, spawnWatchdog, stopWatchdog };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
spawnWatchdog,
|
|
3
|
+
startHeartbeat,
|
|
4
|
+
stopHeartbeat
|
|
5
|
+
} from "./index-vhs88xhe.js";
|
|
6
|
+
import {
|
|
7
|
+
getProcessOnPort,
|
|
8
|
+
killProcessOnPortAndWait
|
|
9
|
+
} from "./index-cty0bcry.js";
|
|
10
|
+
|
|
11
|
+
// cli.ts
|
|
12
|
+
import { spawn } from "node:child_process";
|
|
13
|
+
async function runCli(env, options = {}) {
|
|
14
|
+
const {
|
|
15
|
+
args = process.argv.slice(2),
|
|
16
|
+
watchdog = true,
|
|
17
|
+
watchdogTimeout = 10,
|
|
18
|
+
devServersCommand
|
|
19
|
+
} = options;
|
|
20
|
+
env.logInfo();
|
|
21
|
+
if (args.includes("--down")) {
|
|
22
|
+
await env.stop();
|
|
23
|
+
process.exit(0);
|
|
24
|
+
}
|
|
25
|
+
if (args.includes("--reset")) {
|
|
26
|
+
await env.stop({ removeVolumes: true });
|
|
27
|
+
process.exit(0);
|
|
28
|
+
}
|
|
29
|
+
const running = await env.isRunning();
|
|
30
|
+
if (running) {
|
|
31
|
+
console.log("✓ Containers already running");
|
|
32
|
+
} else {
|
|
33
|
+
await env.start({ startServers: false, wait: true });
|
|
34
|
+
}
|
|
35
|
+
if (args.includes("--migrate")) {
|
|
36
|
+
console.log("");
|
|
37
|
+
console.log("✅ Migrations applied successfully");
|
|
38
|
+
process.exit(0);
|
|
39
|
+
}
|
|
40
|
+
if (args.includes("--seed")) {
|
|
41
|
+
console.log("\uD83C\uDF31 Running seeders...");
|
|
42
|
+
const result = await env.exec("bun run run:seeder", {
|
|
43
|
+
throwOnError: false
|
|
44
|
+
});
|
|
45
|
+
if (result.exitCode !== 0) {
|
|
46
|
+
console.error("❌ Seeding failed");
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
console.log("");
|
|
50
|
+
console.log("✅ Seeding complete");
|
|
51
|
+
process.exit(0);
|
|
52
|
+
}
|
|
53
|
+
if (args.includes("--up-only")) {
|
|
54
|
+
console.log("");
|
|
55
|
+
console.log("✅ Containers started. Environment ready.");
|
|
56
|
+
console.log("");
|
|
57
|
+
process.exit(0);
|
|
58
|
+
}
|
|
59
|
+
if (watchdog) {
|
|
60
|
+
await spawnWatchdog(env.projectName, env.root, {
|
|
61
|
+
timeoutMinutes: watchdogTimeout,
|
|
62
|
+
verbose: true
|
|
63
|
+
});
|
|
64
|
+
startHeartbeat(env.projectName);
|
|
65
|
+
}
|
|
66
|
+
const command = devServersCommand ?? buildDevServersCommand(env.apps);
|
|
67
|
+
if (!command) {
|
|
68
|
+
console.log("✅ Containers ready. No apps configured.");
|
|
69
|
+
await new Promise(() => {});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
await killExistingProcessesOnPorts(env.apps, env.ports);
|
|
73
|
+
console.log("");
|
|
74
|
+
console.log("\uD83D\uDD27 Starting dev servers...");
|
|
75
|
+
console.log("");
|
|
76
|
+
await runCommand(command, env.root, env.buildEnvVars());
|
|
77
|
+
stopHeartbeat();
|
|
78
|
+
}
|
|
79
|
+
async function killExistingProcessesOnPorts(apps, ports) {
|
|
80
|
+
const appNames = Object.keys(apps);
|
|
81
|
+
if (appNames.length === 0)
|
|
82
|
+
return;
|
|
83
|
+
let killedAny = false;
|
|
84
|
+
for (const name of appNames) {
|
|
85
|
+
const port = ports[name];
|
|
86
|
+
if (port === undefined)
|
|
87
|
+
continue;
|
|
88
|
+
const existingPid = getProcessOnPort(port);
|
|
89
|
+
if (existingPid !== null) {
|
|
90
|
+
if (!killedAny) {
|
|
91
|
+
console.log("");
|
|
92
|
+
killedAny = true;
|
|
93
|
+
}
|
|
94
|
+
console.log(`⚠️ Port ${port} (${name}) is in use by process ${existingPid}`);
|
|
95
|
+
const killed = await killProcessOnPortAndWait(port, { verbose: true });
|
|
96
|
+
if (!killed) {
|
|
97
|
+
console.log(` ⚠️ Could not kill process on port ${port}, server may fail to start`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function buildDevServersCommand(apps) {
|
|
103
|
+
const appEntries = Object.entries(apps);
|
|
104
|
+
if (appEntries.length === 0)
|
|
105
|
+
return null;
|
|
106
|
+
const commands = [];
|
|
107
|
+
const names = [];
|
|
108
|
+
const colors = ["blue", "green", "yellow", "magenta", "cyan", "red"];
|
|
109
|
+
for (const [name, config] of appEntries) {
|
|
110
|
+
names.push(name);
|
|
111
|
+
const cwdPart = config.cwd ? `--cwd ${config.cwd}` : "";
|
|
112
|
+
commands.push(`"bun run ${cwdPart} ${config.devCommand}"`.replace(/\s+/g, " ").trim());
|
|
113
|
+
}
|
|
114
|
+
const namesArg = `-n ${names.join(",")}`;
|
|
115
|
+
const colorsArg = `-c ${colors.slice(0, names.length).join(",")}`;
|
|
116
|
+
const commandsArg = commands.join(" ");
|
|
117
|
+
return `bun concurrently ${namesArg} ${colorsArg} ${commandsArg}`;
|
|
118
|
+
}
|
|
119
|
+
function runCommand(command, cwd, envVars) {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
const proc = spawn(command, [], {
|
|
122
|
+
cwd,
|
|
123
|
+
env: { ...process.env, ...envVars },
|
|
124
|
+
stdio: "inherit",
|
|
125
|
+
shell: true
|
|
126
|
+
});
|
|
127
|
+
proc.on("close", (code) => {
|
|
128
|
+
if (code === 0 || code === null) {
|
|
129
|
+
resolve();
|
|
130
|
+
} else {
|
|
131
|
+
reject(new Error(`Command exited with code ${code}`));
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
proc.on("error", reject);
|
|
135
|
+
const cleanup = () => {
|
|
136
|
+
proc.kill("SIGTERM");
|
|
137
|
+
};
|
|
138
|
+
process.on("SIGINT", cleanup);
|
|
139
|
+
process.on("SIGTERM", cleanup);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function hasFlag(args, flag) {
|
|
143
|
+
return args.includes(flag);
|
|
144
|
+
}
|
|
145
|
+
function getFlagValue(args, flag) {
|
|
146
|
+
const prefixed = args.find((arg) => arg.startsWith(`${flag}=`));
|
|
147
|
+
if (prefixed) {
|
|
148
|
+
return prefixed.split("=")[1];
|
|
149
|
+
}
|
|
150
|
+
const index = args.indexOf(flag);
|
|
151
|
+
if (index !== -1 && index + 1 < args.length) {
|
|
152
|
+
const nextArg = args[index + 1];
|
|
153
|
+
if (nextArg !== undefined && !nextArg.startsWith("-")) {
|
|
154
|
+
return nextArg;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { runCli, hasFlag, getFlagValue };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDevEnvironment
|
|
3
|
+
} from "./index-2f47khe5.js";
|
|
4
|
+
|
|
5
|
+
// loader.ts
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
var CONFIG_FILES = [
|
|
9
|
+
"dev.config.ts",
|
|
10
|
+
"dev.config.js",
|
|
11
|
+
"dev-tools.config.ts",
|
|
12
|
+
"dev-tools.config.js"
|
|
13
|
+
];
|
|
14
|
+
function findConfigFile(startDir) {
|
|
15
|
+
let currentDir = startDir;
|
|
16
|
+
while (true) {
|
|
17
|
+
for (const file of CONFIG_FILES) {
|
|
18
|
+
const configPath = join(currentDir, file);
|
|
19
|
+
if (existsSync(configPath)) {
|
|
20
|
+
return configPath;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const parentDir = dirname(currentDir);
|
|
24
|
+
if (parentDir === currentDir) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
currentDir = parentDir;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
var cachedEnv = null;
|
|
31
|
+
async function loadDevEnv(options) {
|
|
32
|
+
if (cachedEnv && !options?.reload) {
|
|
33
|
+
return cachedEnv;
|
|
34
|
+
}
|
|
35
|
+
const cwd = options?.cwd ?? process.cwd();
|
|
36
|
+
const configPath = findConfigFile(cwd);
|
|
37
|
+
if (configPath) {
|
|
38
|
+
const mod = await import(configPath);
|
|
39
|
+
const config = mod.default;
|
|
40
|
+
if (!config?.projectPrefix || !config?.services) {
|
|
41
|
+
throw new Error(`Invalid config in "${configPath}". Use defineDevConfig() and export as default.`);
|
|
42
|
+
}
|
|
43
|
+
cachedEnv = createDevEnvironment(config);
|
|
44
|
+
return cachedEnv;
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`No config file found. Create dev.config.ts with: export default defineDevConfig({ ... })`);
|
|
47
|
+
}
|
|
48
|
+
function getDevEnv() {
|
|
49
|
+
if (!cachedEnv) {
|
|
50
|
+
throw new Error("Dev environment not loaded. Call loadDevEnv() first.");
|
|
51
|
+
}
|
|
52
|
+
return cachedEnv;
|
|
53
|
+
}
|
|
54
|
+
function clearDevEnvCache() {
|
|
55
|
+
cachedEnv = null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { CONFIG_FILES, findConfigFile, loadDevEnv, getDevEnv, clearDevEnvCache };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,6 @@ export type { AppConfig, BuiltInHealthCheck, CliOptions, ComputedPorts, Computed
|
|
|
7
7
|
export { areContainersRunning, isContainerRunning, MAX_ATTEMPTS, POLL_INTERVAL, } from "./core/docker";
|
|
8
8
|
export { getLocalIp, isPortAvailable, waitForServer } from "./core/network";
|
|
9
9
|
export { calculatePortOffset, findMonorepoRoot, getProjectName, getWorktreeName, isWorktree, } from "./core/ports";
|
|
10
|
-
export { getProcessOnPort, isPortInUse, isProcessAlive, killProcessOnPort, killProcessOnPortAndWait, } from "./core/process";
|
|
10
|
+
export { getProcessOnPort, isPortInUse, isProcessAlive, killProcessesOnAppPorts, killProcessOnPort, killProcessOnPortAndWait, } from "./core/process";
|
|
11
11
|
export { getEnvVar, isCI, logApiUrl, logExpoApiUrl, logFrontendPort, sleep, } from "./core/utils";
|
|
12
12
|
export { getHeartbeatFile, getWatchdogPidFile, isWatchdogRunning, spawnWatchdog, startHeartbeat, stopHeartbeat, stopWatchdog, } from "./core/watchdog";
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
getFlagValue,
|
|
3
3
|
hasFlag,
|
|
4
4
|
runCli
|
|
5
|
-
} from "./index-
|
|
5
|
+
} from "./index-8hbbj1mp.js";
|
|
6
6
|
import {
|
|
7
7
|
runWorkspaceTypecheck
|
|
8
8
|
} from "./index-d9efy0n4.js";
|
|
@@ -10,10 +10,10 @@ import {
|
|
|
10
10
|
clearDevEnvCache,
|
|
11
11
|
getDevEnv,
|
|
12
12
|
loadDevEnv
|
|
13
|
-
} from "./index-
|
|
13
|
+
} from "./index-qw4093g2.js";
|
|
14
14
|
import {
|
|
15
15
|
createDevEnvironment
|
|
16
|
-
} from "./index-
|
|
16
|
+
} from "./index-2f47khe5.js";
|
|
17
17
|
import {
|
|
18
18
|
getHeartbeatFile,
|
|
19
19
|
getWatchdogPidFile,
|
|
@@ -22,14 +22,15 @@ import {
|
|
|
22
22
|
startHeartbeat,
|
|
23
23
|
stopHeartbeat,
|
|
24
24
|
stopWatchdog
|
|
25
|
-
} from "./index-
|
|
25
|
+
} from "./index-ggq3yryx.js";
|
|
26
26
|
import {
|
|
27
27
|
getProcessOnPort,
|
|
28
28
|
isPortInUse,
|
|
29
29
|
isProcessAlive,
|
|
30
30
|
killProcessOnPort,
|
|
31
|
-
killProcessOnPortAndWait
|
|
32
|
-
|
|
31
|
+
killProcessOnPortAndWait,
|
|
32
|
+
killProcessesOnAppPorts
|
|
33
|
+
} from "./index-1yvbwj4k.js";
|
|
33
34
|
import {
|
|
34
35
|
assertValidConfig,
|
|
35
36
|
defineDevConfig,
|
|
@@ -77,6 +78,7 @@ export {
|
|
|
77
78
|
logExpoApiUrl,
|
|
78
79
|
logApiUrl,
|
|
79
80
|
loadDevEnv,
|
|
81
|
+
killProcessesOnAppPorts,
|
|
80
82
|
killProcessOnPortAndWait,
|
|
81
83
|
killProcessOnPort,
|
|
82
84
|
isWorktree,
|
package/dist/loader.js
CHANGED
|
@@ -4,10 +4,10 @@ import {
|
|
|
4
4
|
findConfigFile,
|
|
5
5
|
getDevEnv,
|
|
6
6
|
loadDevEnv
|
|
7
|
-
} from "./index-
|
|
8
|
-
import"./index-
|
|
9
|
-
import"./index-
|
|
10
|
-
import"./index-
|
|
7
|
+
} from "./index-qw4093g2.js";
|
|
8
|
+
import"./index-2f47khe5.js";
|
|
9
|
+
import"./index-ggq3yryx.js";
|
|
10
|
+
import"./index-1yvbwj4k.js";
|
|
11
11
|
import"./index-tjqw9vtj.js";
|
|
12
12
|
import"./index-5hka0tff.js";
|
|
13
13
|
import"./index-2fr3g85b.js";
|