Support global CLI service workflow
This commit is contained in:
1 parent
aa6f6321e5
commit
0dce586266
8 files changed
+410
-102
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadCorpConfigs, resolveEnterprisesDir } from "./loader.js";
|
||||
|
||||
const ORIGINAL_ENV = process.env.SEAL_HOME_ENTERPRISES_DIR;
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
if (ORIGINAL_ENV === undefined) {
|
||||
delete process.env.SEAL_HOME_ENTERPRISES_DIR;
|
||||
} else {
|
||||
process.env.SEAL_HOME_ENTERPRISES_DIR = ORIGINAL_ENV;
|
||||
}
|
||||
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("loadCorpConfigs", () => {
|
||||
test("loads configs from SEAL_HOME_ENTERPRISES_DIR", () => {
|
||||
const dir = makeTempDir();
|
||||
writeFileSync(join(dir, "example.hose.json"), "{}");
|
||||
writeFileSync(join(dir, "local.json"), JSON.stringify({
|
||||
id: "direct",
|
||||
name: "Direct",
|
||||
source: {
|
||||
type: "direct",
|
||||
token: "seal-token",
|
||||
sealUrl: "https://direct.sealai.cc"
|
||||
}
|
||||
}));
|
||||
process.env.SEAL_HOME_ENTERPRISES_DIR = dir;
|
||||
|
||||
expect(resolveEnterprisesDir()).toBe(dir);
|
||||
expect(loadCorpConfigs().map((corp) => corp.id)).toEqual(["direct"]);
|
||||
});
|
||||
|
||||
test("returns no configs when explicit env directory does not exist", () => {
|
||||
process.env.SEAL_HOME_ENTERPRISES_DIR = join(makeTempDir(), "missing");
|
||||
|
||||
expect(resolveEnterprisesDir()).toBeUndefined();
|
||||
expect(loadCorpConfigs()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
function makeTempDir(): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "seal-home-loader-"));
|
||||
mkdirSync(dir, { recursive: true });
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
@@ -1,19 +1,40 @@
|
||||
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types.js";
|
||||
|
||||
const ENTERPRISES_DIR = "enterprises";
|
||||
const CONFIG_ENV = "SEAL_HOME_ENTERPRISES_DIR";
|
||||
|
||||
export function loadCorpConfigs(): CorpConfig[] {
|
||||
if (!existsSync(ENTERPRISES_DIR)) {
|
||||
const dir = resolveEnterprisesDir();
|
||||
if (!dir) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return readdirSync(ENTERPRISES_DIR)
|
||||
return readdirSync(dir)
|
||||
.filter((file) => file.endsWith(".json"))
|
||||
.filter((file) => !file.startsWith("example."))
|
||||
.map((file) => {
|
||||
const content = readFileSync(join(ENTERPRISES_DIR, file), "utf-8");
|
||||
const content = readFileSync(join(dir, file), "utf-8");
|
||||
return CorpConfigSchema.parse(JSON.parse(content));
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveEnterprisesDir(): string | undefined {
|
||||
const configured = process.env[CONFIG_ENV]?.trim();
|
||||
if (configured) return existsSync(configured) ? configured : undefined;
|
||||
|
||||
if (existsSync(ENTERPRISES_DIR)) return ENTERPRISES_DIR;
|
||||
|
||||
const userConfigDir = join(homedir(), ".config", "seal-home", "enterprises");
|
||||
return existsSync(userConfigDir) ? userConfigDir : undefined;
|
||||
}
|
||||
|
||||
export function getEnterprisesDirCandidates(): string[] {
|
||||
return [
|
||||
`${CONFIG_ENV}=<path>`,
|
||||
ENTERPRISES_DIR,
|
||||
join(homedir(), ".config", "seal-home", "enterprises")
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user