@finebi/finebi-cli 1.6.2 → 2.0.0
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.
- package/README.md +3 -1
- package/agent-installation-guide.md +70 -0
- package/package.json +2 -1
- package/skills/finebi-cli-agent/references/dashboard-data-result.md +191 -191
- package/skills/finebi-cli-agent/references/dashboard-dsl-api.md +44 -44
- package/skills/finebi-cli-agent/scripts/finebi-cli/package.json +1 -1
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/app.js +160 -157
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/config.js +27 -27
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/help.js +55 -55
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/install-skill.js +89 -89
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/profiles.js +158 -158
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/skill-files.js +95 -94
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/docs/acceptance.md +80 -80
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/examples/cockpit.config.json +22 -22
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/examples/retail.config.json +185 -185
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/gitignore.template +6 -0
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/index.html +2 -2
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/public/app-config.json +11 -11
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/App.tsx +63 -63
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/cockpit.css +3 -3
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/Chart.tsx +40 -40
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/Insights.tsx +17 -17
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/MonthPicker.tsx +32 -32
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/OptionFilter.tsx +7 -7
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/RangeFilter.tsx +9 -9
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/Widget.tsx +40 -40
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/calendar-locales.ts +3 -3
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/calendar.ts +14 -14
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/charts/Bar.tsx +6 -6
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/charts/Line.tsx +6 -6
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/charts/Pie.tsx +6 -6
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/components/month-picker.css +1 -1
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/config.d.ts +3 -3
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/config.js +58 -58
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/date.d.ts +1 -1
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/date.js +5 -5
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/filters.d.ts +4 -4
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/filters.js +106 -106
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/normalize.ts +235 -235
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/finebi/types.ts +20 -20
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/main.tsx +5 -5
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/retail.css +12 -12
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/services/presentation.ts +36 -36
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/services/table-data.ts +47 -47
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/services/view-data.ts +18 -18
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/src/services/widget-queries.ts +18 -18
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/browser/fixtures.mjs +28 -28
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/browser/index.html +16 -16
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/browser/main.tsx +149 -149
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/browser/test-page.css +4 -4
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/browser/vite.config.mjs +13 -13
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/bundle.test.mjs +19 -19
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/cockpit.test.mjs +73 -73
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/field-selection.test.mjs +70 -70
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/retail.test.mjs +31 -31
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tests/table-normalize.test.mjs +189 -189
- package/skills/finebi-cli-agent/scripts/finebi-cli/templates/web-app/tsconfig.json +4 -4
- package/skills/finebi-cli-agent/scripts/finebi-cli-runner.mjs +4 -4
- package/src/app.js +160 -157
- package/src/config.js +27 -27
- package/src/help.js +55 -55
- package/src/install-skill.js +89 -89
- package/src/profiles.js +158 -158
- package/src/skill-files.js +95 -94
- package/templates/web-app/docs/acceptance.md +80 -80
- package/templates/web-app/examples/cockpit.config.json +22 -22
- package/templates/web-app/examples/retail.config.json +185 -185
- package/templates/web-app/gitignore.template +6 -0
- package/templates/web-app/index.html +2 -2
- package/templates/web-app/public/app-config.json +11 -11
- package/templates/web-app/src/App.tsx +63 -63
- package/templates/web-app/src/cockpit.css +3 -3
- package/templates/web-app/src/components/Chart.tsx +40 -40
- package/templates/web-app/src/components/Insights.tsx +17 -17
- package/templates/web-app/src/components/MonthPicker.tsx +32 -32
- package/templates/web-app/src/components/OptionFilter.tsx +7 -7
- package/templates/web-app/src/components/RangeFilter.tsx +9 -9
- package/templates/web-app/src/components/Widget.tsx +40 -40
- package/templates/web-app/src/components/calendar-locales.ts +3 -3
- package/templates/web-app/src/components/calendar.ts +14 -14
- package/templates/web-app/src/components/charts/Bar.tsx +6 -6
- package/templates/web-app/src/components/charts/Line.tsx +6 -6
- package/templates/web-app/src/components/charts/Pie.tsx +6 -6
- package/templates/web-app/src/components/month-picker.css +1 -1
- package/templates/web-app/src/finebi/config.d.ts +3 -3
- package/templates/web-app/src/finebi/config.js +58 -58
- package/templates/web-app/src/finebi/date.d.ts +1 -1
- package/templates/web-app/src/finebi/date.js +5 -5
- package/templates/web-app/src/finebi/filters.d.ts +4 -4
- package/templates/web-app/src/finebi/filters.js +106 -106
- package/templates/web-app/src/finebi/normalize.ts +235 -235
- package/templates/web-app/src/finebi/types.ts +20 -20
- package/templates/web-app/src/main.tsx +5 -5
- package/templates/web-app/src/retail.css +12 -12
- package/templates/web-app/src/services/presentation.ts +36 -36
- package/templates/web-app/src/services/table-data.ts +47 -47
- package/templates/web-app/src/services/view-data.ts +18 -18
- package/templates/web-app/src/services/widget-queries.ts +18 -18
- package/templates/web-app/tests/browser/fixtures.mjs +28 -28
- package/templates/web-app/tests/browser/index.html +16 -16
- package/templates/web-app/tests/browser/main.tsx +149 -149
- package/templates/web-app/tests/browser/test-page.css +4 -4
- package/templates/web-app/tests/browser/vite.config.mjs +13 -13
- package/templates/web-app/tests/bundle.test.mjs +19 -19
- package/templates/web-app/tests/cockpit.test.mjs +73 -73
- package/templates/web-app/tests/field-selection.test.mjs +70 -70
- package/templates/web-app/tests/retail.test.mjs +31 -31
- package/templates/web-app/tests/table-normalize.test.mjs +189 -189
- package/templates/web-app/tsconfig.json +4 -4
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import {mkdir, readFile, writeFile, rename, open, unlink} from "node:fs/promises";
|
|
2
|
-
import {existsSync} from "node:fs";
|
|
3
|
-
import {homedir} from "node:os";
|
|
4
|
-
import {join, resolve} from "node:path";
|
|
5
|
-
import {fileURLToPath} from "node:url";
|
|
6
|
-
import {createInterface} from "node:readline/promises";
|
|
7
|
-
import {agentCatalog, installSkills} from "./skill-files.js";
|
|
8
|
-
import {cliError} from "./errors.js";
|
|
9
|
-
import {failure, success} from "./output.js";
|
|
10
|
-
|
|
11
|
-
function parseOptions(args) {
|
|
12
|
-
const options = {};
|
|
13
|
-
for (let i = 0; i < args.length; i++) {
|
|
14
|
-
const arg = args[i];
|
|
15
|
-
if (arg === "--json") continue;
|
|
16
|
-
if (arg === "--list") options.list = true;
|
|
17
|
-
else if (arg === "--replace") options.replace = true;
|
|
18
|
-
else if (arg === "--agents") {
|
|
19
|
-
if (!args[i + 1] || args[i + 1].startsWith("--")) throw new Error("--agents requires comma-separated Agent IDs.");
|
|
20
|
-
options.agents = [...new Set(args[++i].split(",").map(id => id.trim()).filter(Boolean))];
|
|
21
|
-
if (!options.agents.length) throw new Error("--agents requires at least one Agent ID.");
|
|
22
|
-
} else throw new Error(`Unknown install-skill option: ${arg}`);
|
|
23
|
-
}
|
|
24
|
-
if (options.list && (options.agents || options.replace)) throw new Error("--list cannot be combined with installation options.");
|
|
25
|
-
return options;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function handleInstallSkill(args, context = {}) {
|
|
29
|
-
const env = context.env || process.env;
|
|
30
|
-
const home = resolve(env.USERPROFILE || env.HOME || homedir());
|
|
31
|
-
const catalog = agentCatalog({home, env});
|
|
32
|
-
let options;
|
|
33
|
-
try {
|
|
34
|
-
options = parseOptions(args);
|
|
35
|
-
if (options.list) return success("install-skill", {agents: catalog});
|
|
36
|
-
if (!options.agents) {
|
|
37
|
-
const interactive = context.isTTY ?? (process.stdin.isTTY && process.stderr.isTTY);
|
|
38
|
-
if (!interactive) throw new Error("Specify --agents <ids>; use --list to see supported Agents.");
|
|
39
|
-
const prompt = createInterface({input: process.stdin, output: process.stderr});
|
|
40
|
-
try {
|
|
41
|
-
process.stderr.write(catalog.map(a => `${a.id}: ${a.name}${a.detected ? " (detected)" : ""}`).join("\n") + "\n");
|
|
42
|
-
const answer = await prompt.question("Select Agent IDs (comma-separated): ");
|
|
43
|
-
options.agents = parseOptions(["--agents", answer]).agents;
|
|
44
|
-
} finally { prompt.close(); }
|
|
45
|
-
}
|
|
46
|
-
for (const id of options.agents) if (!catalog.some(agent => agent.id === id)) throw new Error(`Unknown Agent: ${id}`);
|
|
47
|
-
} catch (error) {
|
|
48
|
-
return failure("install-skill", cliError("INVALID_ARGUMENT", {
|
|
49
|
-
message: error.message, nextActions: ["finebi-cli install-skill --help"],
|
|
50
|
-
}));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const directory = join(home, ".finebi-cli");
|
|
54
|
-
const statePath = join(directory, "installation.json");
|
|
55
|
-
const lockPath = join(directory, "skill-install.lock");
|
|
56
|
-
let lock;
|
|
57
|
-
try {
|
|
58
|
-
const source = context.skillSource || [
|
|
59
|
-
"../skills/finebi-cli-agent/", "../../skills/finebi-cli-agent/", "../../../",
|
|
60
|
-
].map(path => fileURLToPath(new URL(path, import.meta.url)))
|
|
61
|
-
.find(path => existsSync(join(path, "SKILL.md")) && existsSync(join(path, "scripts/finebi-cli/src/index.js")));
|
|
62
|
-
if (!source) throw new Error("FineBI Skill resources were not found. Reinstall the CLI package or build the Skill in the source checkout.");
|
|
63
|
-
await readFile(join(source, "SKILL.md"));
|
|
64
|
-
await readFile(join(source, "scripts/finebi-cli/src/index.js"));
|
|
65
|
-
await mkdir(directory, {recursive: true});
|
|
66
|
-
try { lock = await open(lockPath, "wx"); }
|
|
67
|
-
catch (error) { if (error.code === "EEXIST") throw new Error(`Skill installation lock exists: ${lockPath}`); throw error; }
|
|
68
|
-
let state = {};
|
|
69
|
-
try { state = JSON.parse(await readFile(statePath, "utf8")); }
|
|
70
|
-
catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
71
|
-
const results = await installSkills({source, home, env, agents: options.agents, replace: options.replace, previous: state.skills || []});
|
|
72
|
-
const skills = new Map((state.skills || []).map(skill => [skill.agent, skill]));
|
|
73
|
-
for (const skill of results) skills.set(skill.agent, {...skills.get(skill.agent), ...skill});
|
|
74
|
-
const stagedState = join(directory, "installation.json.stage");
|
|
75
|
-
await writeFile(stagedState, `${JSON.stringify({...state, skills: [...skills.values()], updatedAt: new Date().toISOString()}, null, 2)}\n`);
|
|
76
|
-
await rename(stagedState, statePath);
|
|
77
|
-
const data = {skills: results.map(({hashes, ...skill}) => skill)};
|
|
78
|
-
if (results.some(skill => !["installed", "unchanged"].includes(skill.status))) {
|
|
79
|
-
return failure("install-skill", cliError("SKILL_INSTALL_FAILED", {
|
|
80
|
-
message: "Some Skills were kept or failed. Review the results; --replace backs up and replaces local changes.", partialResult: data,
|
|
81
|
-
}));
|
|
82
|
-
}
|
|
83
|
-
return success("install-skill", data);
|
|
84
|
-
} catch (error) {
|
|
85
|
-
return failure("install-skill", cliError("SKILL_INSTALL_FAILED", {message: error.message}));
|
|
86
|
-
} finally {
|
|
87
|
-
if (lock) { await lock.close(); await unlink(lockPath); }
|
|
88
|
-
}
|
|
89
|
-
}
|
|
1
|
+
import {mkdir, readFile, writeFile, rename, open, unlink} from "node:fs/promises";
|
|
2
|
+
import {existsSync} from "node:fs";
|
|
3
|
+
import {homedir} from "node:os";
|
|
4
|
+
import {join, resolve} from "node:path";
|
|
5
|
+
import {fileURLToPath} from "node:url";
|
|
6
|
+
import {createInterface} from "node:readline/promises";
|
|
7
|
+
import {agentCatalog, installSkills} from "./skill-files.js";
|
|
8
|
+
import {cliError} from "./errors.js";
|
|
9
|
+
import {failure, success} from "./output.js";
|
|
10
|
+
|
|
11
|
+
function parseOptions(args) {
|
|
12
|
+
const options = {};
|
|
13
|
+
for (let i = 0; i < args.length; i++) {
|
|
14
|
+
const arg = args[i];
|
|
15
|
+
if (arg === "--json") continue;
|
|
16
|
+
if (arg === "--list") options.list = true;
|
|
17
|
+
else if (arg === "--replace") options.replace = true;
|
|
18
|
+
else if (arg === "--agents") {
|
|
19
|
+
if (!args[i + 1] || args[i + 1].startsWith("--")) throw new Error("--agents requires comma-separated Agent IDs.");
|
|
20
|
+
options.agents = [...new Set(args[++i].split(",").map(id => id.trim()).filter(Boolean))];
|
|
21
|
+
if (!options.agents.length) throw new Error("--agents requires at least one Agent ID.");
|
|
22
|
+
} else throw new Error(`Unknown install-skill option: ${arg}`);
|
|
23
|
+
}
|
|
24
|
+
if (options.list && (options.agents || options.replace)) throw new Error("--list cannot be combined with installation options.");
|
|
25
|
+
return options;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function handleInstallSkill(args, context = {}) {
|
|
29
|
+
const env = context.env || process.env;
|
|
30
|
+
const home = resolve(env.USERPROFILE || env.HOME || homedir());
|
|
31
|
+
const catalog = agentCatalog({home, env});
|
|
32
|
+
let options;
|
|
33
|
+
try {
|
|
34
|
+
options = parseOptions(args);
|
|
35
|
+
if (options.list) return success("install-skill", {agents: catalog});
|
|
36
|
+
if (!options.agents) {
|
|
37
|
+
const interactive = context.isTTY ?? (process.stdin.isTTY && process.stderr.isTTY);
|
|
38
|
+
if (!interactive) throw new Error("Specify --agents <ids>; use --list to see supported Agents.");
|
|
39
|
+
const prompt = createInterface({input: process.stdin, output: process.stderr});
|
|
40
|
+
try {
|
|
41
|
+
process.stderr.write(catalog.map(a => `${a.id}: ${a.name}${a.detected ? " (detected)" : ""}`).join("\n") + "\n");
|
|
42
|
+
const answer = await prompt.question("Select Agent IDs (comma-separated): ");
|
|
43
|
+
options.agents = parseOptions(["--agents", answer]).agents;
|
|
44
|
+
} finally { prompt.close(); }
|
|
45
|
+
}
|
|
46
|
+
for (const id of options.agents) if (!catalog.some(agent => agent.id === id)) throw new Error(`Unknown Agent: ${id}`);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return failure("install-skill", cliError("INVALID_ARGUMENT", {
|
|
49
|
+
message: error.message, nextActions: ["finebi-cli install-skill --help"],
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const directory = join(home, ".finebi-cli");
|
|
54
|
+
const statePath = join(directory, "installation.json");
|
|
55
|
+
const lockPath = join(directory, "skill-install.lock");
|
|
56
|
+
let lock;
|
|
57
|
+
try {
|
|
58
|
+
const source = context.skillSource || [
|
|
59
|
+
"../skills/finebi-cli-agent/", "../../skills/finebi-cli-agent/", "../../../",
|
|
60
|
+
].map(path => fileURLToPath(new URL(path, import.meta.url)))
|
|
61
|
+
.find(path => existsSync(join(path, "SKILL.md")) && existsSync(join(path, "scripts/finebi-cli/src/index.js")));
|
|
62
|
+
if (!source) throw new Error("FineBI Skill resources were not found. Reinstall the CLI package or build the Skill in the source checkout.");
|
|
63
|
+
await readFile(join(source, "SKILL.md"));
|
|
64
|
+
await readFile(join(source, "scripts/finebi-cli/src/index.js"));
|
|
65
|
+
await mkdir(directory, {recursive: true});
|
|
66
|
+
try { lock = await open(lockPath, "wx"); }
|
|
67
|
+
catch (error) { if (error.code === "EEXIST") throw new Error(`Skill installation lock exists: ${lockPath}`); throw error; }
|
|
68
|
+
let state = {};
|
|
69
|
+
try { state = JSON.parse(await readFile(statePath, "utf8")); }
|
|
70
|
+
catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
71
|
+
const results = await installSkills({source, home, env, agents: options.agents, replace: options.replace, previous: state.skills || []});
|
|
72
|
+
const skills = new Map((state.skills || []).map(skill => [skill.agent, skill]));
|
|
73
|
+
for (const skill of results) skills.set(skill.agent, {...skills.get(skill.agent), ...skill});
|
|
74
|
+
const stagedState = join(directory, "installation.json.stage");
|
|
75
|
+
await writeFile(stagedState, `${JSON.stringify({...state, skills: [...skills.values()], updatedAt: new Date().toISOString()}, null, 2)}\n`);
|
|
76
|
+
await rename(stagedState, statePath);
|
|
77
|
+
const data = {skills: results.map(({hashes, ...skill}) => skill)};
|
|
78
|
+
if (results.some(skill => !["installed", "unchanged"].includes(skill.status))) {
|
|
79
|
+
return failure("install-skill", cliError("SKILL_INSTALL_FAILED", {
|
|
80
|
+
message: "Some Skills were kept or failed. Review the results; --replace backs up and replaces local changes.", partialResult: data,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
return success("install-skill", data);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return failure("install-skill", cliError("SKILL_INSTALL_FAILED", {message: error.message}));
|
|
86
|
+
} finally {
|
|
87
|
+
if (lock) { await lock.close(); await unlink(lockPath); }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
import {closeSync, existsSync, mkdirSync, openSync, rmSync} from "node:fs";
|
|
2
|
-
import {dirname, join} from "node:path";
|
|
3
|
-
|
|
4
|
-
import {normalizeConfig, readConfig, writeConfigAtomically} from "./config.js";
|
|
5
|
-
|
|
6
|
-
export class ProfileError extends Error {}
|
|
7
|
-
|
|
8
|
-
export function validateProfileName(name) {
|
|
9
|
-
if (typeof name !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(name)) {
|
|
10
|
-
throw new ProfileError("Profile name must contain 1-64 letters, digits, underscores or hyphens and start with a letter or digit.");
|
|
11
|
-
}
|
|
12
|
-
return name;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function readProfiles(path) {
|
|
16
|
-
const config = readConfig(path);
|
|
17
|
-
if (config.schemaVersion === 2) {
|
|
18
|
-
if (!config.profiles || typeof config.profiles !== "object" || Array.isArray(config.profiles)) {
|
|
19
|
-
throw new ProfileError("Invalid profiles configuration.");
|
|
20
|
-
}
|
|
21
|
-
return {multi: true, currentProfile: config.currentProfile ?? null, profiles: config.profiles};
|
|
22
|
-
}
|
|
23
|
-
// Preserve legacy session tokens when a single-environment file is migrated.
|
|
24
|
-
if (config.token && !config.auth?.token) {
|
|
25
|
-
config.auth = {...config.auth, token: config.token};
|
|
26
|
-
}
|
|
27
|
-
const present = existsSync(path);
|
|
28
|
-
return {multi: false, currentProfile: present ? "default" : null, profiles: present ? {default: config} : {}};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function selectProfile(path, requested, {allowCreate = false, optional = false} = {}) {
|
|
32
|
-
const store = readProfiles(path);
|
|
33
|
-
const name = requested ?? store.currentProfile ?? (!store.multi ? "default" : null);
|
|
34
|
-
if (!name) {
|
|
35
|
-
if (optional) return null;
|
|
36
|
-
throw new ProfileError("No default environment is selected. Run finebi-cli auth use <name> or pass --profile <name>.");
|
|
37
|
-
}
|
|
38
|
-
validateProfileName(name);
|
|
39
|
-
const implicitInitial = !requested && !store.multi && !existsSync(path);
|
|
40
|
-
if (!Object.hasOwn(store.profiles, name) && !allowCreate && !implicitInitial && !optional) {
|
|
41
|
-
throw new ProfileError(`Environment '${name}' does not exist. Create it with finebi-cli auth login --profile ${name} --base-url <url>.`);
|
|
42
|
-
}
|
|
43
|
-
if (requested && !Object.hasOwn(store.profiles, name) && !allowCreate) {
|
|
44
|
-
throw new ProfileError(`Environment '${name}' does not exist.`);
|
|
45
|
-
}
|
|
46
|
-
return name;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function readProfile(path, name) {
|
|
50
|
-
const store = readProfiles(path);
|
|
51
|
-
return normalizeConfig(name && Object.hasOwn(store.profiles, name) ? store.profiles[name] : {});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function profileCapabilityPath(path, name) {
|
|
55
|
-
if (!name || name === "default") return path;
|
|
56
|
-
// Hex encoding preserves case-sensitive names on case-insensitive filesystems.
|
|
57
|
-
return join(dirname(path), `capabilities-profile-${Buffer.from(validateProfileName(name)).toString("hex")}.json`);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function saveProfile(path, name, config, {makeCurrent = false, forceMulti = false, writer = writeConfigAtomically} = {}) {
|
|
61
|
-
validateProfileName(name);
|
|
62
|
-
return withProfileLock(path, () => {
|
|
63
|
-
const store = readProfiles(path);
|
|
64
|
-
if (!store.multi && name === "default" && !forceMulti) {
|
|
65
|
-
writer(path, config);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const profiles = {...store.profiles, [name]: config};
|
|
69
|
-
writer(path, {schemaVersion: 2, currentProfile: makeCurrent ? name : store.currentProfile, profiles});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function useProfile(path, name) {
|
|
74
|
-
validateProfileName(name);
|
|
75
|
-
return withProfileLock(path, () => {
|
|
76
|
-
const store = readProfiles(path);
|
|
77
|
-
if (!Object.hasOwn(store.profiles, name)) throw new ProfileError(`Environment '${name}' does not exist.`);
|
|
78
|
-
writeConfigAtomically(path, {schemaVersion: 2, currentProfile: name, profiles: store.profiles});
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function removeProfile(path, name) {
|
|
83
|
-
validateProfileName(name);
|
|
84
|
-
return withProfileLock(path, () => {
|
|
85
|
-
const store = readProfiles(path);
|
|
86
|
-
if (!Object.hasOwn(store.profiles, name)) {
|
|
87
|
-
if (!store.multi && name === "default") return;
|
|
88
|
-
throw new ProfileError(`Environment '${name}' does not exist.`);
|
|
89
|
-
}
|
|
90
|
-
if (!store.multi && name === "default") {
|
|
91
|
-
rmSync(path);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
const profiles = {...store.profiles};
|
|
95
|
-
delete profiles[name];
|
|
96
|
-
writeConfigAtomically(path, {schemaVersion: 2, currentProfile: store.currentProfile === name ? null : store.currentProfile, profiles});
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export async function withProfileLoginLock(path, name, operation) {
|
|
101
|
-
validateProfileName(name);
|
|
102
|
-
mkdirSync(dirname(path), {recursive: true, mode: 0o700});
|
|
103
|
-
const lock = `${path}.login-${Buffer.from(name).toString("hex")}.lock`;
|
|
104
|
-
let fd;
|
|
105
|
-
try {
|
|
106
|
-
fd = openSync(lock, "wx", 0o600);
|
|
107
|
-
} catch (error) {
|
|
108
|
-
if (error.code === "EEXIST") throw new ProfileError(`An operation for environment '${name}' is already in progress. Retry after it finishes.`);
|
|
109
|
-
throw error;
|
|
110
|
-
}
|
|
111
|
-
try {
|
|
112
|
-
return await operation();
|
|
113
|
-
} finally {
|
|
114
|
-
closeSync(fd);
|
|
115
|
-
rmSync(lock, {force: true});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function withProfileLock(path, operation) {
|
|
120
|
-
mkdirSync(dirname(path), {recursive: true, mode: 0o700});
|
|
121
|
-
const lock = `${path}.lock`;
|
|
122
|
-
const deadline = Date.now() + 3000;
|
|
123
|
-
let fd;
|
|
124
|
-
while (fd === undefined) {
|
|
125
|
-
try {
|
|
126
|
-
fd = openSync(lock, "wx", 0o600);
|
|
127
|
-
} catch (error) {
|
|
128
|
-
if (error.code !== "EEXIST") throw error;
|
|
129
|
-
if (Date.now() >= deadline) throw new ProfileError("Configuration is locked by another process. Retry after it finishes.");
|
|
130
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
return operation();
|
|
135
|
-
} finally {
|
|
136
|
-
closeSync(fd);
|
|
137
|
-
rmSync(lock, {force: true});
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function parseConnectionArgs(argv) {
|
|
142
|
-
const args = [];
|
|
143
|
-
const flags = {};
|
|
144
|
-
const names = {"--profile": "profile", "--base-url": "baseUrl", "--token": "token"};
|
|
145
|
-
for (let index = 0; index < argv.length; index++) {
|
|
146
|
-
const [option, ...inline] = argv[index].split("=");
|
|
147
|
-
if (!Object.hasOwn(names, option)) {
|
|
148
|
-
args.push(argv[index]);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
const value = inline.length ? inline.join("=") : argv[++index];
|
|
152
|
-
if (!value?.trim() || value.startsWith("--")) throw new ProfileError(`${option} requires a value.`);
|
|
153
|
-
const key = names[option];
|
|
154
|
-
if (Object.hasOwn(flags, key)) throw new ProfileError(`${option} must only be supplied once.`);
|
|
155
|
-
flags[key] = value.trim();
|
|
156
|
-
}
|
|
157
|
-
return {args, flags};
|
|
158
|
-
}
|
|
1
|
+
import {closeSync, existsSync, mkdirSync, openSync, rmSync} from "node:fs";
|
|
2
|
+
import {dirname, join} from "node:path";
|
|
3
|
+
|
|
4
|
+
import {normalizeConfig, readConfig, writeConfigAtomically} from "./config.js";
|
|
5
|
+
|
|
6
|
+
export class ProfileError extends Error {}
|
|
7
|
+
|
|
8
|
+
export function validateProfileName(name) {
|
|
9
|
+
if (typeof name !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(name)) {
|
|
10
|
+
throw new ProfileError("Profile name must contain 1-64 letters, digits, underscores or hyphens and start with a letter or digit.");
|
|
11
|
+
}
|
|
12
|
+
return name;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function readProfiles(path) {
|
|
16
|
+
const config = readConfig(path);
|
|
17
|
+
if (config.schemaVersion === 2) {
|
|
18
|
+
if (!config.profiles || typeof config.profiles !== "object" || Array.isArray(config.profiles)) {
|
|
19
|
+
throw new ProfileError("Invalid profiles configuration.");
|
|
20
|
+
}
|
|
21
|
+
return {multi: true, currentProfile: config.currentProfile ?? null, profiles: config.profiles};
|
|
22
|
+
}
|
|
23
|
+
// Preserve legacy session tokens when a single-environment file is migrated.
|
|
24
|
+
if (config.token && !config.auth?.token) {
|
|
25
|
+
config.auth = {...config.auth, token: config.token};
|
|
26
|
+
}
|
|
27
|
+
const present = existsSync(path);
|
|
28
|
+
return {multi: false, currentProfile: present ? "default" : null, profiles: present ? {default: config} : {}};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function selectProfile(path, requested, {allowCreate = false, optional = false} = {}) {
|
|
32
|
+
const store = readProfiles(path);
|
|
33
|
+
const name = requested ?? store.currentProfile ?? (!store.multi ? "default" : null);
|
|
34
|
+
if (!name) {
|
|
35
|
+
if (optional) return null;
|
|
36
|
+
throw new ProfileError("No default environment is selected. Run finebi-cli auth use <name> or pass --profile <name>.");
|
|
37
|
+
}
|
|
38
|
+
validateProfileName(name);
|
|
39
|
+
const implicitInitial = !requested && !store.multi && !existsSync(path);
|
|
40
|
+
if (!Object.hasOwn(store.profiles, name) && !allowCreate && !implicitInitial && !optional) {
|
|
41
|
+
throw new ProfileError(`Environment '${name}' does not exist. Create it with finebi-cli auth login --profile ${name} --base-url <url>.`);
|
|
42
|
+
}
|
|
43
|
+
if (requested && !Object.hasOwn(store.profiles, name) && !allowCreate) {
|
|
44
|
+
throw new ProfileError(`Environment '${name}' does not exist.`);
|
|
45
|
+
}
|
|
46
|
+
return name;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function readProfile(path, name) {
|
|
50
|
+
const store = readProfiles(path);
|
|
51
|
+
return normalizeConfig(name && Object.hasOwn(store.profiles, name) ? store.profiles[name] : {});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function profileCapabilityPath(path, name) {
|
|
55
|
+
if (!name || name === "default") return path;
|
|
56
|
+
// Hex encoding preserves case-sensitive names on case-insensitive filesystems.
|
|
57
|
+
return join(dirname(path), `capabilities-profile-${Buffer.from(validateProfileName(name)).toString("hex")}.json`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function saveProfile(path, name, config, {makeCurrent = false, forceMulti = false, writer = writeConfigAtomically} = {}) {
|
|
61
|
+
validateProfileName(name);
|
|
62
|
+
return withProfileLock(path, () => {
|
|
63
|
+
const store = readProfiles(path);
|
|
64
|
+
if (!store.multi && name === "default" && !forceMulti) {
|
|
65
|
+
writer(path, config);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const profiles = {...store.profiles, [name]: config};
|
|
69
|
+
writer(path, {schemaVersion: 2, currentProfile: makeCurrent ? name : store.currentProfile, profiles});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function useProfile(path, name) {
|
|
74
|
+
validateProfileName(name);
|
|
75
|
+
return withProfileLock(path, () => {
|
|
76
|
+
const store = readProfiles(path);
|
|
77
|
+
if (!Object.hasOwn(store.profiles, name)) throw new ProfileError(`Environment '${name}' does not exist.`);
|
|
78
|
+
writeConfigAtomically(path, {schemaVersion: 2, currentProfile: name, profiles: store.profiles});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function removeProfile(path, name) {
|
|
83
|
+
validateProfileName(name);
|
|
84
|
+
return withProfileLock(path, () => {
|
|
85
|
+
const store = readProfiles(path);
|
|
86
|
+
if (!Object.hasOwn(store.profiles, name)) {
|
|
87
|
+
if (!store.multi && name === "default") return;
|
|
88
|
+
throw new ProfileError(`Environment '${name}' does not exist.`);
|
|
89
|
+
}
|
|
90
|
+
if (!store.multi && name === "default") {
|
|
91
|
+
rmSync(path);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const profiles = {...store.profiles};
|
|
95
|
+
delete profiles[name];
|
|
96
|
+
writeConfigAtomically(path, {schemaVersion: 2, currentProfile: store.currentProfile === name ? null : store.currentProfile, profiles});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function withProfileLoginLock(path, name, operation) {
|
|
101
|
+
validateProfileName(name);
|
|
102
|
+
mkdirSync(dirname(path), {recursive: true, mode: 0o700});
|
|
103
|
+
const lock = `${path}.login-${Buffer.from(name).toString("hex")}.lock`;
|
|
104
|
+
let fd;
|
|
105
|
+
try {
|
|
106
|
+
fd = openSync(lock, "wx", 0o600);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (error.code === "EEXIST") throw new ProfileError(`An operation for environment '${name}' is already in progress. Retry after it finishes.`);
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
return await operation();
|
|
113
|
+
} finally {
|
|
114
|
+
closeSync(fd);
|
|
115
|
+
rmSync(lock, {force: true});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function withProfileLock(path, operation) {
|
|
120
|
+
mkdirSync(dirname(path), {recursive: true, mode: 0o700});
|
|
121
|
+
const lock = `${path}.lock`;
|
|
122
|
+
const deadline = Date.now() + 3000;
|
|
123
|
+
let fd;
|
|
124
|
+
while (fd === undefined) {
|
|
125
|
+
try {
|
|
126
|
+
fd = openSync(lock, "wx", 0o600);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error.code !== "EEXIST") throw error;
|
|
129
|
+
if (Date.now() >= deadline) throw new ProfileError("Configuration is locked by another process. Retry after it finishes.");
|
|
130
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
return operation();
|
|
135
|
+
} finally {
|
|
136
|
+
closeSync(fd);
|
|
137
|
+
rmSync(lock, {force: true});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function parseConnectionArgs(argv) {
|
|
142
|
+
const args = [];
|
|
143
|
+
const flags = {};
|
|
144
|
+
const names = {"--profile": "profile", "--base-url": "baseUrl", "--token": "token"};
|
|
145
|
+
for (let index = 0; index < argv.length; index++) {
|
|
146
|
+
const [option, ...inline] = argv[index].split("=");
|
|
147
|
+
if (!Object.hasOwn(names, option)) {
|
|
148
|
+
args.push(argv[index]);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const value = inline.length ? inline.join("=") : argv[++index];
|
|
152
|
+
if (!value?.trim() || value.startsWith("--")) throw new ProfileError(`${option} requires a value.`);
|
|
153
|
+
const key = names[option];
|
|
154
|
+
if (Object.hasOwn(flags, key)) throw new ProfileError(`${option} must only be supplied once.`);
|
|
155
|
+
flags[key] = value.trim();
|
|
156
|
+
}
|
|
157
|
+
return {args, flags};
|
|
158
|
+
}
|