Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(crbn): reset command and .env.local stale template

Sidwebworks0b82a0e38e30
diff --git a/packages/dev/src/commands/reset.ts b/packages/dev/src/commands/reset.ts
index ae0ed383c6..4ea39ce0d6 100644
--- a/packages/dev/src/commands/reset.ts
+++ b/packages/dev/src/commands/reset.ts
@@ -1,9 +1,15 @@
import { cancel, intro, log } from "@clack/prompts";
import { confirmReset } from "../prompts.js";
-import { flushDb, stopStack } from "../services/compose.js";
+import {
+ destroyProject,
+ flushDb,
+ listCarbonProjects,
+ stopStack
+} from "../services/compose.js";
import {
getSlot,
getWorktreeRoot,
+ listSlugs,
projectName,
resolveSlug
} from "../worktree.js";
@@ -20,10 +26,32 @@ export async function reset() {
}
log.warn(`resetting ${projectName(slug)}`);
+
+ // Tear down current stack (compose-aware, removes containers + volumes).
await stopStack(root, slug, true);
+
const slot = getSlot(slug);
if (slot && typeof slot.redisDb === "number") {
await flushDb(slot.redisDb);
}
+
+ // Clean up orphan docker projects from deleted worktrees.
+ const knownProjects = new Set(
+ Object.keys(listSlugs()).map((s) => projectName(s))
+ );
+ // The current project is known — it was already cleaned above.
+ knownProjects.add(projectName(slug));
+
+ const allProjects = await listCarbonProjects();
+ const orphans = allProjects.filter(
+ (p) => !knownProjects.has(p) && p !== "carbon-shared"
+ );
+ if (orphans.length > 0) {
+ log.warn(
+ `cleaning ${orphans.length} orphan project(s): ${orphans.join(", ")}`
+ );
+ await Promise.all(orphans.map((p) => destroyProject(p)));
+ }
+
await up();
}
diff --git a/packages/dev/src/services/compose.ts b/packages/dev/src/services/compose.ts
index bbed3b77ce..14ed7e727d 100644
--- a/packages/dev/src/services/compose.ts
+++ b/packages/dev/src/services/compose.ts
@@ -114,8 +114,8 @@ export async function stopStack(
slug: string,
withVolumes: boolean
) {
- const args = devArgs(slug, "down");
- if (withVolumes) args.push("-v");
+ const args = devArgs(slug, "--env-file", ".env.local", "down");
+ if (withVolumes) args.push("-v", "--remove-orphans");
await execa("docker", args, { cwd: root, stdio: "ignore", reject: false });
}
@@ -139,7 +139,18 @@ export async function bootSharedRedis(root: string) {
export async function destroyProjectVolumes(cwd: string, project: string) {
await execa(
"docker",
- ["compose", "-f", COMPOSE_DEV_FILE, "-p", project, "down", "-v"],
+ [
+ "compose",
+ "-f",
+ COMPOSE_DEV_FILE,
+ "--env-file",
+ ".env.local",
+ "-p",
+ project,
+ "down",
+ "-v",
+ "--remove-orphans"
+ ],
{ cwd, stdio: "ignore", reject: false }
);
}
@@ -263,6 +274,94 @@ export async function dockerProjectStates(): Promise<Map<string, string>> {
return out;
}
+// Destroy a compose project by force-removing its containers, volumes, and
+// networks using raw docker commands. Works even when the compose file or
+// worktree directory no longer exists (orphaned projects).
+export async function destroyProject(project: string) {
+ // Find containers belonging to the project.
+ const ctr = await execa(
+ "docker",
+ [
+ "ps",
+ "-a",
+ "-q",
+ "--filter",
+ `label=com.docker.compose.project=${project}`
+ ],
+ { reject: false }
+ );
+ const ids = (ctr.stdout ?? "")
+ .split("\n")
+ .map((s) => s.trim())
+ .filter(Boolean);
+ if (ids.length > 0) {
+ await execa("docker", ["rm", "-f", ...ids], {
+ reject: false,
+ stdio: "ignore"
+ });
+ }
+
+ // Remove volumes prefixed with the project name.
+ const vol = await execa(
+ "docker",
+ ["volume", "ls", "-q", "--filter", `name=${project}_`],
+ {
+ reject: false
+ }
+ );
+ const vols = (vol.stdout ?? "")
+ .split("\n")
+ .map((s) => s.trim())
+ .filter(Boolean);
+ if (vols.length > 0) {
+ await execa("docker", ["volume", "rm", "-f", ...vols], {
+ reject: false,
+ stdio: "ignore"
+ });
+ }
+
+ // Remove networks prefixed with the project name.
+ const net = await execa(
+ "docker",
+ ["network", "ls", "-q", "--filter", `name=${project}_`],
+ {
+ reject: false
+ }
+ );
+ const nets = (net.stdout ?? "")
+ .split("\n")
+ .map((s) => s.trim())
+ .filter(Boolean);
+ if (nets.length > 0) {
+ await execa("docker", ["network", "rm", ...nets], {
+ reject: false,
+ stdio: "ignore"
+ });
+ }
+}
+
+// List all docker compose project names that start with "carbon-".
+export async function listCarbonProjects(): Promise<string[]> {
+ const r = await execa(
+ "docker",
+ [
+ "ps",
+ "-a",
+ "--format",
+ '{{.Label "com.docker.compose.project"}}',
+ "--filter",
+ "label=com.docker.compose.project"
+ ],
+ { reject: false }
+ );
+ const all = new Set<string>();
+ for (const line of (r.stdout ?? "").split("\n")) {
+ const name = line.trim();
+ if (name && name.startsWith("carbon-")) all.add(name);
+ }
+ return [...all];
+}
+
// ---------------------------------------------------------------------------
// Utility
// ---------------------------------------------------------------------------