buncargo 3.2.3 → 3.2.4

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.
@@ -0,0 +1,250 @@
1
+ import {
2
+ resolveExposeTargets,
3
+ startPublicTunnels,
4
+ stopPublicTunnels
5
+ } from "./index-gfjdt37q.js";
6
+ import {
7
+ spawnWatchdog,
8
+ startHeartbeat,
9
+ stopHeartbeat
10
+ } from "./index-mam0bcyz.js";
11
+ import {
12
+ killProcessesOnAppPorts
13
+ } from "./index-mm412dkp.js";
14
+
15
+ // src/cli/run-cli.ts
16
+ import { spawn } from "node:child_process";
17
+ var ACCEPTED_FLAGS = [
18
+ "--help",
19
+ "--down",
20
+ "--reset",
21
+ "--migrate",
22
+ "--seed",
23
+ "--up-only",
24
+ "--expose"
25
+ ];
26
+ function printHelp() {
27
+ console.log(`
28
+ Usage: buncargo dev [options]
29
+
30
+ Options:
31
+ --help Show this help message
32
+ --down Stop all containers
33
+ --reset Stop containers and remove volumes (fresh start)
34
+ --migrate Run migrations and exit
35
+ --seed Run migrations and seeders, then exit
36
+ --up-only Start containers and run migrations, then exit (no dev servers)
37
+ --expose Expose configured targets via public quick tunnels
38
+
39
+ Examples:
40
+ bun dev Start dev environment with all services
41
+ bun dev --seed Run migrations and seed the database
42
+ bun dev --down Stop all containers
43
+ bun dev --reset Stop containers and remove all data
44
+ bun dev --expose Expose all targets with expose: true
45
+ bun dev --expose=api,web Expose specific targets
46
+ `);
47
+ }
48
+ function getUnknownFlags(args) {
49
+ return args.filter((arg) => arg.startsWith("--") && !ACCEPTED_FLAGS.includes(arg.includes("=") ? arg.split("=")[0] : arg));
50
+ }
51
+ async function runCli(env, options = {}) {
52
+ const {
53
+ args = process.argv.slice(2),
54
+ watchdog = true,
55
+ watchdogTimeout = 10,
56
+ devServersCommand,
57
+ cliTestTunnel
58
+ } = options;
59
+ const tunnelApi = cliTestTunnel ?? {
60
+ resolveExposeTargets,
61
+ startPublicTunnels,
62
+ stopPublicTunnels
63
+ };
64
+ const exposeRequested = hasFlag(args, "--expose");
65
+ const exposeValue = getFlagValue(args, "--expose");
66
+ let tunnels = [];
67
+ async function cleanupTunnels() {
68
+ env.clearPublicUrls();
69
+ if (tunnels.length === 0)
70
+ return;
71
+ await tunnelApi.stopPublicTunnels(tunnels);
72
+ tunnels = [];
73
+ }
74
+ if (args.includes("--help")) {
75
+ printHelp();
76
+ process.exit(0);
77
+ }
78
+ const unknownFlags = getUnknownFlags(args);
79
+ if (unknownFlags.length > 0) {
80
+ console.error(`❌ Unknown flag${unknownFlags.length > 1 ? "s" : ""}: ${unknownFlags.join(", ")}`);
81
+ console.error("");
82
+ printHelp();
83
+ process.exit(1);
84
+ }
85
+ if (args.includes("--down")) {
86
+ env.logInfo();
87
+ await cleanupTunnels();
88
+ await env.stop();
89
+ process.exit(0);
90
+ }
91
+ if (args.includes("--reset")) {
92
+ env.logInfo();
93
+ await cleanupTunnels();
94
+ await env.stop({ removeVolumes: true });
95
+ process.exit(0);
96
+ }
97
+ const skipSeed = args.includes("--seed");
98
+ await env.start({
99
+ startServers: false,
100
+ wait: true,
101
+ skipSeed,
102
+ skipEnvironmentLog: exposeRequested
103
+ });
104
+ if (exposeRequested) {
105
+ const { targets, unknownNames, notEnabledNames } = tunnelApi.resolveExposeTargets(env, exposeValue);
106
+ if (unknownNames.length > 0) {
107
+ console.error(`❌ Unknown expose target${unknownNames.length > 1 ? "s" : ""}: ${unknownNames.join(", ")}`);
108
+ await cleanupTunnels();
109
+ process.exit(1);
110
+ }
111
+ if (notEnabledNames.length > 0) {
112
+ console.error(`❌ Target${notEnabledNames.length > 1 ? "s" : ""} missing expose: true: ${notEnabledNames.join(", ")}`);
113
+ console.error(" Mark these in dev.config.ts with expose: true or remove them from --expose.");
114
+ await cleanupTunnels();
115
+ process.exit(1);
116
+ }
117
+ if (targets.length === 0) {
118
+ console.error("❌ No expose targets selected. Add expose: true to services/apps or pass names with --expose=<name>.");
119
+ await cleanupTunnels();
120
+ process.exit(1);
121
+ }
122
+ tunnels = await tunnelApi.startPublicTunnels(targets);
123
+ env.setPublicUrls(Object.fromEntries(tunnels.map((tunnel) => [tunnel.name, tunnel.publicUrl])));
124
+ env.logInfo("Dev Environment", tunnels);
125
+ }
126
+ if (args.includes("--migrate")) {
127
+ console.log("");
128
+ console.log("✅ Migrations applied successfully");
129
+ await cleanupTunnels();
130
+ process.exit(0);
131
+ }
132
+ if (args.includes("--seed")) {
133
+ console.log("\uD83C\uDF31 Running seeders...");
134
+ const result = await env.exec("bun run run:seeder", {
135
+ throwOnError: false
136
+ });
137
+ if (result.exitCode !== 0) {
138
+ console.error("❌ Seeding failed");
139
+ if (result.stderr) {
140
+ console.error(result.stderr);
141
+ }
142
+ if (result.stdout) {
143
+ console.error(result.stdout);
144
+ }
145
+ await cleanupTunnels();
146
+ process.exit(1);
147
+ }
148
+ console.log("");
149
+ console.log("✅ Seeding complete");
150
+ await cleanupTunnels();
151
+ process.exit(0);
152
+ }
153
+ if (args.includes("--up-only")) {
154
+ console.log("");
155
+ console.log("✅ Containers started. Environment ready.");
156
+ console.log("");
157
+ await cleanupTunnels();
158
+ process.exit(0);
159
+ }
160
+ if (watchdog) {
161
+ await spawnWatchdog(env.projectName, env.root, {
162
+ timeoutMinutes: watchdogTimeout,
163
+ verbose: true,
164
+ composeFile: env.composeFile
165
+ });
166
+ startHeartbeat(env.projectName);
167
+ }
168
+ const command = devServersCommand ?? buildDevServersCommand(env.apps);
169
+ if (!command) {
170
+ console.log("✅ Containers ready. No apps configured.");
171
+ await new Promise(() => {});
172
+ await cleanupTunnels();
173
+ return;
174
+ }
175
+ await killProcessesOnAppPorts(env.apps, env.ports);
176
+ console.log("");
177
+ console.log("\uD83D\uDD27 Starting dev servers...");
178
+ console.log("");
179
+ await runCommand(command, env.root, env.buildEnvVars(), {
180
+ onSignal: async () => {
181
+ await cleanupTunnels();
182
+ stopHeartbeat();
183
+ }
184
+ });
185
+ stopHeartbeat();
186
+ await cleanupTunnels();
187
+ }
188
+ function buildDevServersCommand(apps) {
189
+ const appEntries = Object.entries(apps);
190
+ if (appEntries.length === 0)
191
+ return null;
192
+ const commands = [];
193
+ const names = [];
194
+ const colors = ["blue", "green", "yellow", "magenta", "cyan", "red"];
195
+ for (const [name, config] of appEntries) {
196
+ names.push(name);
197
+ const cwdPart = config.cwd ? `--cwd ${config.cwd}` : "";
198
+ commands.push(`"bun run ${cwdPart} ${config.devCommand}"`.replace(/\s+/g, " ").trim());
199
+ }
200
+ const namesArg = `-n ${names.join(",")}`;
201
+ const colorsArg = `-c ${colors.slice(0, names.length).join(",")}`;
202
+ const commandsArg = commands.join(" ");
203
+ return `bun concurrently ${namesArg} ${colorsArg} ${commandsArg}`;
204
+ }
205
+ function runCommand(command, cwd, envVars, options = {}) {
206
+ const { onSignal } = options;
207
+ return new Promise((resolve, reject) => {
208
+ const proc = spawn(command, [], {
209
+ cwd,
210
+ env: { ...process.env, ...envVars },
211
+ stdio: "inherit",
212
+ shell: true
213
+ });
214
+ proc.on("close", (code) => {
215
+ if (code === 0 || code === null) {
216
+ resolve();
217
+ } else {
218
+ reject(new Error(`Command exited with code ${code}`));
219
+ }
220
+ });
221
+ proc.on("error", reject);
222
+ const cleanup = () => {
223
+ if (onSignal) {
224
+ onSignal();
225
+ }
226
+ proc.kill("SIGTERM");
227
+ };
228
+ process.on("SIGINT", cleanup);
229
+ process.on("SIGTERM", cleanup);
230
+ });
231
+ }
232
+ function hasFlag(args, flag) {
233
+ return args.some((arg) => arg === flag || arg.startsWith(`${flag}=`));
234
+ }
235
+ function getFlagValue(args, flag) {
236
+ const prefixed = args.find((arg) => arg.startsWith(`${flag}=`));
237
+ if (prefixed) {
238
+ return prefixed.split("=")[1];
239
+ }
240
+ const index = args.indexOf(flag);
241
+ if (index !== -1 && index + 1 < args.length) {
242
+ const nextArg = args[index + 1];
243
+ if (nextArg !== undefined && !nextArg.startsWith("-")) {
244
+ return nextArg;
245
+ }
246
+ }
247
+ return;
248
+ }
249
+
250
+ export { runCli, hasFlag, getFlagValue };
@@ -0,0 +1,228 @@
1
+ import {
2
+ sleep
3
+ } from "./index-fkgqg6w2.js";
4
+
5
+ // src/docker/runtime.ts
6
+ import { execSync } from "node:child_process";
7
+ var POLL_INTERVAL = 250;
8
+ var MAX_ATTEMPTS = 120;
9
+ var DOCKER_NOT_RUNNING_MESSAGE = "Docker is not running. Please start Docker and try again.";
10
+ async function isContainerRunning(project, service) {
11
+ try {
12
+ const result = execSync(`docker ps --filter "label=com.docker.compose.project=${project}" --filter "label=com.docker.compose.service=${service}" --format "{{.State}}"`, { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
13
+ return result.trim() === "running";
14
+ } catch {
15
+ return false;
16
+ }
17
+ }
18
+ function isDockerRunning() {
19
+ try {
20
+ execSync('docker info --format "{{.ServerVersion}}"', {
21
+ encoding: "utf-8",
22
+ stdio: ["pipe", "pipe", "pipe"]
23
+ });
24
+ return true;
25
+ } catch {
26
+ return false;
27
+ }
28
+ }
29
+ function assertDockerRunning() {
30
+ if (!isDockerRunning()) {
31
+ throw new Error(DOCKER_NOT_RUNNING_MESSAGE);
32
+ }
33
+ }
34
+ async function areContainersRunning(project, minCount = 1) {
35
+ try {
36
+ const result = execSync(`docker ps --filter "label=com.docker.compose.project=${project}" --format "{{.State}}"`, { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
37
+ const states = result.trim().split(`
38
+ `).filter(Boolean);
39
+ if (states.length < minCount)
40
+ return false;
41
+ return states.every((state) => state === "running");
42
+ } catch {
43
+ return false;
44
+ }
45
+ }
46
+ function getComposeArg(composeFile) {
47
+ return composeFile ? `-f "${composeFile}"` : "";
48
+ }
49
+ function startContainers(root, projectName, envVars, options = {}) {
50
+ const { verbose = true, wait = true, composeFile } = options;
51
+ assertDockerRunning();
52
+ if (verbose)
53
+ console.log("\uD83D\uDC33 Starting Docker containers...");
54
+ const composeArg = getComposeArg(composeFile);
55
+ const waitFlag = wait ? "--wait" : "";
56
+ const cmd = `docker compose ${composeArg} up -d ${waitFlag}`.trim();
57
+ execSync(cmd, {
58
+ cwd: root,
59
+ env: { ...process.env, ...envVars, COMPOSE_PROJECT_NAME: projectName },
60
+ stdio: verbose ? "inherit" : "ignore"
61
+ });
62
+ if (verbose)
63
+ console.log("✓ Containers started");
64
+ }
65
+ function stopContainers(root, projectName, options = {}) {
66
+ const { verbose = true, removeVolumes = false, composeFile } = options;
67
+ assertDockerRunning();
68
+ if (verbose) {
69
+ console.log(removeVolumes ? "\uD83D\uDDD1️ Stopping containers and removing volumes..." : "\uD83D\uDED1 Stopping containers...");
70
+ }
71
+ const composeArg = getComposeArg(composeFile);
72
+ const volumeFlag = removeVolumes ? "-v" : "";
73
+ const cmd = `docker compose ${composeArg} down ${volumeFlag}`.trim();
74
+ execSync(cmd, {
75
+ cwd: root,
76
+ env: { ...process.env, COMPOSE_PROJECT_NAME: projectName },
77
+ stdio: verbose ? "inherit" : "ignore"
78
+ });
79
+ if (verbose)
80
+ console.log("✓ Containers stopped");
81
+ }
82
+ function startService(root, projectName, serviceName, envVars, options = {}) {
83
+ const { verbose = true, composeFile } = options;
84
+ assertDockerRunning();
85
+ if (verbose)
86
+ console.log(`\uD83D\uDC33 Starting ${serviceName}...`);
87
+ const composeArg = getComposeArg(composeFile);
88
+ const cmd = `docker compose ${composeArg} up -d ${serviceName}`.trim();
89
+ execSync(cmd, {
90
+ cwd: root,
91
+ env: { ...process.env, ...envVars, COMPOSE_PROJECT_NAME: projectName },
92
+ stdio: verbose ? "inherit" : "ignore"
93
+ });
94
+ }
95
+ function createBuiltInHealthCheck(type, serviceName, context = {}) {
96
+ const { projectName, root } = context;
97
+ switch (type) {
98
+ case "pg_isready":
99
+ return async () => {
100
+ try {
101
+ const projectArg = projectName ? `-p ${projectName}` : "";
102
+ execSync(`docker compose ${projectArg} exec -T ${serviceName} pg_isready -U postgres`, {
103
+ cwd: root,
104
+ stdio: ["pipe", "pipe", "pipe"]
105
+ });
106
+ return true;
107
+ } catch {
108
+ return false;
109
+ }
110
+ };
111
+ case "redis-cli":
112
+ return async () => {
113
+ try {
114
+ const projectArg = projectName ? `-p ${projectName}` : "";
115
+ execSync(`docker compose ${projectArg} exec -T ${serviceName} redis-cli ping`, {
116
+ cwd: root,
117
+ stdio: ["pipe", "pipe", "pipe"]
118
+ });
119
+ return true;
120
+ } catch {
121
+ return false;
122
+ }
123
+ };
124
+ case "http":
125
+ return async (port) => {
126
+ try {
127
+ const controller = new AbortController;
128
+ const timeoutId = setTimeout(() => controller.abort(), 2000);
129
+ try {
130
+ const response = await fetch(`http://localhost:${port}/`, {
131
+ signal: controller.signal
132
+ });
133
+ clearTimeout(timeoutId);
134
+ return response.ok || response.status === 404;
135
+ } catch {
136
+ clearTimeout(timeoutId);
137
+ return false;
138
+ }
139
+ } catch {
140
+ return false;
141
+ }
142
+ };
143
+ case "tcp":
144
+ return async (port) => {
145
+ try {
146
+ const controller = new AbortController;
147
+ const timeoutId = setTimeout(() => controller.abort(), 1000);
148
+ try {
149
+ await fetch(`http://localhost:${port}/`, {
150
+ signal: controller.signal
151
+ });
152
+ clearTimeout(timeoutId);
153
+ return true;
154
+ } catch (error) {
155
+ clearTimeout(timeoutId);
156
+ if (error instanceof Error && error.message.includes("ECONNREFUSED")) {
157
+ return false;
158
+ }
159
+ return true;
160
+ }
161
+ } catch {
162
+ return false;
163
+ }
164
+ };
165
+ default:
166
+ return async () => true;
167
+ }
168
+ }
169
+ async function waitForService(serviceName, config, port, options = {}) {
170
+ const {
171
+ maxAttempts = MAX_ATTEMPTS,
172
+ pollInterval = POLL_INTERVAL,
173
+ projectName,
174
+ root
175
+ } = options;
176
+ if (config.healthCheck === false || config.healthCheck === undefined) {
177
+ return;
178
+ }
179
+ const healthCheckFn = typeof config.healthCheck === "function" ? config.healthCheck : createBuiltInHealthCheck(config.healthCheck, config.serviceName ?? serviceName, { projectName, root });
180
+ for (let i = 0;i < maxAttempts; i++) {
181
+ const isHealthy = await healthCheckFn(port);
182
+ if (isHealthy)
183
+ return;
184
+ await sleep(pollInterval);
185
+ }
186
+ throw new Error(`Service ${serviceName} did not become ready in time`);
187
+ }
188
+ async function waitForAllServices(services, ports, options = {}) {
189
+ const { verbose = true, ...waitOptions } = options;
190
+ if (verbose)
191
+ console.log("⏳ Waiting for services to be healthy...");
192
+ const promises = Object.entries(services).map(([name, config]) => {
193
+ const port = ports[name];
194
+ if (port === undefined) {
195
+ console.warn(`⚠️ No port found for service ${name}, skipping health check`);
196
+ return Promise.resolve();
197
+ }
198
+ return waitForService(name, config, port, waitOptions);
199
+ });
200
+ await Promise.all(promises);
201
+ if (verbose)
202
+ console.log("✓ All services healthy");
203
+ }
204
+ async function waitForServiceByType(serviceName, healthCheckType, port, options = {}) {
205
+ const {
206
+ maxAttempts = MAX_ATTEMPTS,
207
+ pollInterval = POLL_INTERVAL,
208
+ verbose = false,
209
+ projectName,
210
+ root
211
+ } = options;
212
+ const healthCheckFn = createBuiltInHealthCheck(healthCheckType, serviceName, {
213
+ projectName,
214
+ root
215
+ });
216
+ for (let i = 0;i < maxAttempts; i++) {
217
+ const isHealthy = await healthCheckFn(port);
218
+ if (isHealthy) {
219
+ if (verbose)
220
+ console.log(`✓ ${serviceName} is ready`);
221
+ return;
222
+ }
223
+ await sleep(pollInterval);
224
+ }
225
+ throw new Error(`Service ${serviceName} did not become ready in time`);
226
+ }
227
+
228
+ export { POLL_INTERVAL, MAX_ATTEMPTS, DOCKER_NOT_RUNNING_MESSAGE, isContainerRunning, isDockerRunning, assertDockerRunning, areContainersRunning, getComposeArg, startContainers, stopContainers, startService, createBuiltInHealthCheck, waitForService, waitForAllServices, waitForServiceByType };