feat(cli): add WORKFLOW_STORAGE_ROOT env var support

Add user-facing WORKFLOW_STORAGE_ROOT environment variable to override
the default storage directory (~/.uncaged/workflow). The existing
UNCAGED_WORKFLOW_STORAGE_ROOT (internal/test) takes priority.

- Update storage-env.ts with priority chain: internal > user > default
- Add env var documentation to CLI help text
- Add 5 tests covering all priority/fallback scenarios

Fixes #63
This commit is contained in:
2026-05-07 14:07:30 +00:00
committed by 星月
parent c663ba9e9c
commit 7b0e256c13
3 changed files with 73 additions and 4 deletions
@@ -0,0 +1,54 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { getDefaultWorkflowStorageRoot } from "@uncaged/workflow";
import { resolveWorkflowStorageRoot } from "../src/storage-env.js";
describe("resolveWorkflowStorageRoot", () => {
let savedInternal: string | undefined;
let savedUser: string | undefined;
beforeEach(() => {
savedInternal = process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
savedUser = process.env.WORKFLOW_STORAGE_ROOT;
delete process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
delete process.env.WORKFLOW_STORAGE_ROOT;
});
afterEach(() => {
if (savedInternal === undefined) {
delete process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
} else {
process.env.UNCAGED_WORKFLOW_STORAGE_ROOT = savedInternal;
}
if (savedUser === undefined) {
delete process.env.WORKFLOW_STORAGE_ROOT;
} else {
process.env.WORKFLOW_STORAGE_ROOT = savedUser;
}
});
test("returns default when no env vars are set", () => {
expect(resolveWorkflowStorageRoot()).toBe(getDefaultWorkflowStorageRoot());
});
test("WORKFLOW_STORAGE_ROOT overrides default", () => {
process.env.WORKFLOW_STORAGE_ROOT = "/tmp/custom-storage";
expect(resolveWorkflowStorageRoot()).toBe("/tmp/custom-storage");
});
test("UNCAGED_WORKFLOW_STORAGE_ROOT takes priority over WORKFLOW_STORAGE_ROOT", () => {
process.env.WORKFLOW_STORAGE_ROOT = "/tmp/user-path";
process.env.UNCAGED_WORKFLOW_STORAGE_ROOT = "/tmp/internal-path";
expect(resolveWorkflowStorageRoot()).toBe("/tmp/internal-path");
});
test("ignores empty WORKFLOW_STORAGE_ROOT", () => {
process.env.WORKFLOW_STORAGE_ROOT = "";
expect(resolveWorkflowStorageRoot()).toBe(getDefaultWorkflowStorageRoot());
});
test("ignores empty UNCAGED_WORKFLOW_STORAGE_ROOT and falls through to WORKFLOW_STORAGE_ROOT", () => {
process.env.UNCAGED_WORKFLOW_STORAGE_ROOT = "";
process.env.WORKFLOW_STORAGE_ROOT = "/tmp/user-fallback";
expect(resolveWorkflowStorageRoot()).toBe("/tmp/user-fallback");
});
});
@@ -54,6 +54,10 @@ export function formatCliUsage(): string {
"",
" uncaged-workflow run <name> [...] (shortcut for thread run)",
" uncaged-workflow live <thread-id> [...] (shortcut for thread live)",
"",
"Environment variables:",
" WORKFLOW_STORAGE_ROOT Override storage directory (default: ~/.uncaged/workflow)",
" UNCAGED_WORKFLOW_STORAGE_ROOT Internal override (takes priority over WORKFLOW_STORAGE_ROOT)",
].join("\n");
}
+15 -4
View File
@@ -1,10 +1,21 @@
import { getDefaultWorkflowStorageRoot } from "@uncaged/workflow";
/** Resolve storage root, honoring `UNCAGED_WORKFLOW_STORAGE_ROOT` for tests/tools. */
/**
* Resolve storage root with env var override support.
*
* Priority (highest first):
* 1. `UNCAGED_WORKFLOW_STORAGE_ROOT` — internal/test override
* 2. `WORKFLOW_STORAGE_ROOT` — user-facing override
* 3. Default (`~/.uncaged/workflow`)
*/
export function resolveWorkflowStorageRoot(): string {
const override = process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
if (override !== undefined && override !== "") {
return override;
const internal = process.env.UNCAGED_WORKFLOW_STORAGE_ROOT;
if (internal !== undefined && internal !== "") {
return internal;
}
const userOverride = process.env.WORKFLOW_STORAGE_ROOT;
if (userOverride !== undefined && userOverride !== "") {
return userOverride;
}
return getDefaultWorkflowStorageRoot();
}