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.

crbn: windows fixes

Sidwebworksf29346f5c25d
diff --git a/apps/erp/package.json b/apps/erp/package.json
index 7b3f0faecd..f06078415f 100644
--- a/apps/erp/package.json
+++ b/apps/erp/package.json
@@ -166,7 +166,7 @@
"build": "react-router build",
"clean": "rimraf .cache .turbo node_modules dist build",
"dev": "portless --script dev:app run --force",
- "dev:app": "react-router dev --port $PORT --host $HOST",
+ "dev:app": "react-router dev",
"dev:novu": "pnpm dlx novu@latest dev --port 3000",
"sync:novu": "pnpm dlx novu@latest sync ",
"lint": "biome lint --write ",
diff --git a/apps/mes/package.json b/apps/mes/package.json
index b5ba4759ae..676bc4b22b 100644
--- a/apps/mes/package.json
+++ b/apps/mes/package.json
@@ -101,7 +101,7 @@
"build": "react-router build",
"clean": "rimraf .cache .turbo node_modules dist build",
"dev": "portless --script dev:app run --force",
- "dev:app": "react-router dev --port $PORT --host $HOST",
+ "dev:app": "react-router dev",
"lint": "biome lint --write ",
"start": "NODE_ENV=production react-router-serve ./build/server/index.js",
"typecheck": "tsgo --noEmit",
diff --git a/packages/dev/bin/crbn b/packages/dev/bin/crbn
index 79a529ac8c..32081aa256 100755
--- a/packages/dev/bin/crbn
+++ b/packages/dev/bin/crbn
@@ -19,6 +19,20 @@ case "$(uname -s)" in
;;
esac
+# Node 22+ required (import.meta, native fetch, fs.glob, etc).
+if command -v node >/dev/null 2>&1; then
+ node_ver="$(node -v)"
+ node_major="${node_ver#v}"
+ node_major="${node_major%%.*}"
+ if [[ "$node_major" -lt 22 ]]; then
+ printf '\033[1;31m✗\033[0m crbn requires Node.js 22+ (found %s)\n' "$node_ver" >&2
+ exit 1
+ fi
+else
+ printf '\033[1;31m✗\033[0m crbn requires Node.js 22+ but node is not in PATH\n' >&2
+ exit 1
+fi
+
# ---------------------------------------------------------------------------
# Output helpers (colors when stderr is a TTY).
# All listings/errors go to stderr so they never get eval'd by the `crbn()`
diff --git a/packages/dev/src/services/apps.ts b/packages/dev/src/services/apps.ts
index ea2db5a797..629480e243 100644
--- a/packages/dev/src/services/apps.ts
+++ b/packages/dev/src/services/apps.ts
@@ -61,17 +61,28 @@ export function spawnApps(opts: {
// (`<app>.<prefix>.dev`) without portless auto-prefix mangling.
const portKey = APP_PORT_KEYS[id];
const port = portKey ? ports[portKey] : undefined;
- const child = execa("pnpm", ["run", "dev:app"], {
- cwd: join(root, "apps", id),
- env: {
- ...spawnAppEnv(root, id),
- HOST: "127.0.0.1",
- ...(port !== undefined ? { PORT: String(port) } : {})
- },
- reject: false,
- stdin: "ignore",
- detached: true
- });
+ const child = execa(
+ "pnpm",
+ [
+ "exec",
+ "react-router",
+ "dev",
+ ...(port !== undefined ? ["--port", String(port)] : []),
+ "--host",
+ "127.0.0.1"
+ ],
+ {
+ cwd: join(root, "apps", id),
+ env: {
+ ...spawnAppEnv(root, id),
+ HOST: "127.0.0.1",
+ ...(port !== undefined ? { PORT: String(port) } : {})
+ },
+ reject: false,
+ stdin: "ignore",
+ detached: true
+ }
+ );
const prefix = color(pc.bold(`${id.padEnd(3)} | `));
const pipe = (
diff --git a/packages/dev/src/services/migrations.ts b/packages/dev/src/services/migrations.ts
index 9d4002ba2a..425ceee7f1 100644
--- a/packages/dev/src/services/migrations.ts
+++ b/packages/dev/src/services/migrations.ts
@@ -1,5 +1,6 @@
-import { readFileSync } from "node:fs";
+import { readdirSync, readFileSync } from "node:fs";
import { setTimeout as sleep } from "node:timers/promises";
+import { log } from "@clack/prompts";
import { execa } from "execa";
import { join } from "pathe";
import pg from "pg";
@@ -131,13 +132,8 @@ export async function applyMigrations(
root: string,
dbPort: number
): Promise<{ applied: boolean }> {
- const args = [
- "migration",
- "up",
- "--include-all",
- "--db-url",
- `postgresql://supabase_admin:postgres@localhost:${dbPort}/postgres`
- ];
+ const dbUrl = `postgresql://supabase_admin:postgres@localhost:${dbPort}/postgres`;
+ const args = ["migration", "up", "--include-all", "--db-url", dbUrl];
const cwd = join(root, "packages/database");
const r = await execa("supabase", args, {
cwd,
@@ -145,6 +141,29 @@ export async function applyMigrations(
preferLocal: true
});
if (r.exitCode !== 0) {
+ const output = `${r.stderr ?? ""}\n${r.stdout ?? ""}`;
+ // Auto-repair: DB has migration versions not present locally (stale from
+ // another branch or incomplete volume wipe). Remove them and retry.
+ if (/remote migration versions not found in local/i.test(output)) {
+ const repaired = await repairStaleMigrations(root, dbPort);
+ if (repaired > 0) {
+ log.warn(`repaired ${repaired} stale migration(s) — retrying`);
+ const retry = await execa("supabase", args, {
+ cwd,
+ reject: false,
+ preferLocal: true
+ });
+ if (retry.exitCode === 0) {
+ const applied = /Applying migration/i.test(retry.stdout ?? "");
+ return { applied };
+ }
+ process.stderr.write(retry.stderr?.toString() ?? "");
+ process.stdout.write(retry.stdout?.toString() ?? "");
+ throw new Error(
+ `supabase ${args.join(" ")} failed after repair (exit ${retry.exitCode})`
+ );
+ }
+ }
process.stderr.write(r.stderr?.toString() ?? "");
process.stdout.write(r.stdout?.toString() ?? "");
throw new Error(`supabase ${args.join(" ")} failed (exit ${r.exitCode})`);
@@ -155,6 +174,49 @@ export async function applyMigrations(
return { applied };
}
+// Find migration versions in DB that have no corresponding local file and
+// remove them from supabase_migrations.schema_migrations.
+async function repairStaleMigrations(
+ root: string,
+ dbPort: number
+): Promise<number> {
+ const migrationsDir = join(root, "packages/database/supabase/migrations");
+ const localVersions = new Set(
+ readdirSync(migrationsDir)
+ .filter((f) => f.endsWith(".sql"))
+ .map((f) => f.split("_")[0]!)
+ );
+
+ const remoteVersions = await withClient(
+ dbPort,
+ async (c) => {
+ const res = await c.query<{ version: string }>(
+ "SELECT version FROM supabase_migrations.schema_migrations ORDER BY version"
+ );
+ return res.rows.map((r) => r.version);
+ },
+ { user: "supabase_admin", password: "postgres" }
+ );
+
+ const stale = remoteVersions.filter((v) => !localVersions.has(v));
+ if (stale.length === 0) return 0;
+
+ await withClient(
+ dbPort,
+ async (c) => {
+ for (const version of stale) {
+ await c.query(
+ "DELETE FROM supabase_migrations.schema_migrations WHERE version = $1",
+ [version]
+ );
+ }
+ },
+ { user: "supabase_admin", password: "postgres" }
+ );
+
+ return stale.length;
+}
+
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
diff --git a/packages/dev/src/services/portless.ts b/packages/dev/src/services/portless.ts
index 08be3bbe44..ccb82f4afe 100644
--- a/packages/dev/src/services/portless.ts
+++ b/packages/dev/src/services/portless.ts
@@ -163,6 +163,8 @@ export async function ensureProxyPrivileges() {
const issues = detectPrivilegeIssues();
if (issues.length === 0) return;
+ const isWindows = process.platform === "win32";
+
log.warn(
[
"portless needs a privileged proxy to serve `*.dev` cleanly:",
@@ -174,40 +176,42 @@ export async function ensureProxyPrivileges() {
].join("\n")
);
+ const elevateHint = isWindows
+ ? "Will bind :443, install the local CA, and write hosts entries (requires Administrator terminal)."
+ : "Set it up now? Will run sudo to bind :443, install the local CA, and write /etc/hosts entries.";
+
const proceed = await confirm({
- message:
- "Set it up now? Will run sudo to bind :443, install the local CA, and write /etc/hosts entries.",
+ message: elevateHint,
initialValue: true
});
if (isCancel(proceed) || !proceed) {
- throw new Error(
- "Aborted. Run manually: `sudo portless proxy stop && sudo portless proxy start --tld dev && sudo portless trust`. Then re-run `crbn up`."
+ const manual = isWindows
+ ? "Aborted. Run in an Administrator terminal: `portless proxy stop && portless proxy start --tld dev && portless trust`. Then re-run `crbn up`."
+ : "Aborted. Run manually: `sudo portless proxy stop && sudo portless proxy start --tld dev && sudo portless trust`. Then re-run `crbn up`.";
+ throw new Error(manual);
+ }
+
+ if (isWindows) {
+ log.info(
+ "running privileged commands — ensure this terminal is running as Administrator"
);
+ } else {
+ log.info("running sudo commands — you'll be prompted for your password");
}
- log.info("running sudo commands — you'll be prompted for your password");
+ const elevate = (args: string[]) =>
+ isWindows
+ ? execa("portless", args, { stdio: "inherit", reject: false })
+ : execa("sudo", sudoPortless(args), { stdio: "inherit", reject: false });
- await execa("sudo", sudoPortless(["proxy", "stop"]), {
- stdio: "inherit",
- reject: false
- });
+ await elevate(["proxy", "stop"]);
- const start = await execa(
- "sudo",
- sudoPortless(["proxy", "start", "--tld", PORTLESS_TLD]),
- {
- stdio: "inherit",
- reject: false
- }
- );
+ const start = await elevate(["proxy", "start", "--tld", PORTLESS_TLD]);
if (start.exitCode !== 0) {
throw new Error(`portless proxy start failed (exit ${start.exitCode})`);
}
- const trust = await execa("sudo", sudoPortless(["trust"]), {
- stdio: "inherit",
- reject: false
- });
+ const trust = await elevate(["trust"]);
if (trust.exitCode !== 0) {
log.warn(
`portless trust failed (exit ${trust.exitCode}); browsers may show cert warnings until you run it manually.`
@@ -224,15 +228,22 @@ export async function ensureProxyPrivileges() {
log.success("portless proxy on :443");
}
-// Push registered routes into /etc/hosts. Needs sudo; idempotent.
+// Push registered routes into /etc/hosts. Needs elevated privileges; idempotent.
export async function syncHostsFile() {
- const r = await execa("sudo", sudoPortless(["hosts", "sync"]), {
+ const isWindows = process.platform === "win32";
+ const cmd = isWindows ? "portless" : "sudo";
+ const args = isWindows ? ["hosts", "sync"] : sudoPortless(["hosts", "sync"]);
+
+ const r = await execa(cmd, args, {
stdio: "inherit",
reject: false
});
if (r.exitCode !== 0) {
+ const hint = isWindows
+ ? "Run this command in an Administrator terminal to fix DNS."
+ : "Run it manually to fix DNS.";
throw new Error(
- `sudo portless hosts sync failed (exit ${r.exitCode}). Run it manually to fix DNS.`
+ `${isWindows ? "" : "sudo "}portless hosts sync failed (exit ${r.exitCode}). ${hint}`
);
}
}