@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
package/src/profiles.js
CHANGED
|
@@ -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
|
+
}
|
package/src/skill-files.js
CHANGED
|
@@ -1,94 +1,95 @@
|
|
|
1
|
-
import {createHash, randomUUID} from "node:crypto";
|
|
2
|
-
import {existsSync} from "node:fs";
|
|
3
|
-
import {cp, mkdir, lstat, mkdtemp, readFile, readdir, rename, rm} from "node:fs/promises";
|
|
4
|
-
import {homedir} from "node:os";
|
|
5
|
-
import {join, dirname, resolve} from "node:path";
|
|
6
|
-
|
|
7
|
-
export function agentCatalog({home = homedir(), env = process.env} = {}) {
|
|
8
|
-
const config = env.XDG_CONFIG_HOME || join(home, ".config");
|
|
9
|
-
return [
|
|
10
|
-
["claude-code", "Claude Code", env.CLAUDE_CONFIG_DIR || join(home, ".claude")],
|
|
11
|
-
["codex", "Codex", env.CODEX_HOME || join(home, ".codex")],
|
|
12
|
-
["
|
|
13
|
-
["
|
|
14
|
-
["
|
|
15
|
-
["
|
|
16
|
-
["
|
|
17
|
-
["
|
|
18
|
-
["
|
|
19
|
-
["trae
|
|
20
|
-
["
|
|
21
|
-
["
|
|
22
|
-
["
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (entry.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
let
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
await
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
await
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
import {createHash, randomUUID} from "node:crypto";
|
|
2
|
+
import {existsSync} from "node:fs";
|
|
3
|
+
import {cp, mkdir, lstat, mkdtemp, readFile, readdir, rename, rm} from "node:fs/promises";
|
|
4
|
+
import {homedir} from "node:os";
|
|
5
|
+
import {join, dirname, resolve} from "node:path";
|
|
6
|
+
|
|
7
|
+
export function agentCatalog({home = homedir(), env = process.env} = {}) {
|
|
8
|
+
const config = env.XDG_CONFIG_HOME || join(home, ".config");
|
|
9
|
+
return [
|
|
10
|
+
["claude-code", "Claude Code", env.CLAUDE_CONFIG_DIR || join(home, ".claude")],
|
|
11
|
+
["codex", "Codex", env.CODEX_HOME || join(home, ".codex")],
|
|
12
|
+
["dsh", "DeepSeek Harness", env.DSH_HOME || join(home, ".dsh")],
|
|
13
|
+
["cursor", "Cursor", join(home, ".cursor")],
|
|
14
|
+
["gemini-cli", "Gemini CLI", join(home, ".gemini")],
|
|
15
|
+
["github-copilot", "GitHub Copilot", join(home, ".copilot")],
|
|
16
|
+
["opencode", "OpenCode", join(config, "opencode")],
|
|
17
|
+
["cline", "Cline", join(home, ".agents")],
|
|
18
|
+
["windsurf", "Windsurf", join(home, ".codeium", "windsurf")],
|
|
19
|
+
["trae", "Trae", join(home, ".trae")],
|
|
20
|
+
["trae-cn", "Trae CN", join(home, ".trae-cn")],
|
|
21
|
+
["openclaw", "OpenClaw", [".openclaw", ".clawdbot", ".moltbot"].map(p => join(home, p)).find(existsSync) || join(home, ".openclaw")],
|
|
22
|
+
["roo", "Roo Code", join(home, ".roo")],
|
|
23
|
+
["qwen-code", "Qwen Code", join(home, ".qwen")],
|
|
24
|
+
].map(([id, name, root]) => ({id, name, directory: join(root, "skills"), detected: existsSync(root)}));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function hashTree(root, prefix = "") {
|
|
28
|
+
const files = {};
|
|
29
|
+
for (const entry of (await readdir(join(root, prefix), {withFileTypes: true})).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
30
|
+
const name = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
31
|
+
if (entry.isSymbolicLink()) throw new Error(`Skill contains a symbolic link: ${name}`);
|
|
32
|
+
if (entry.isDirectory()) Object.assign(files, await hashTree(root, name));
|
|
33
|
+
else files[name] = createHash("sha256").update(await readFile(join(root, name))).digest("hex");
|
|
34
|
+
}
|
|
35
|
+
return files;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function sameHashes(left, right) {
|
|
39
|
+
return left && right && Object.keys(left).length === Object.keys(right).length
|
|
40
|
+
&& Object.keys(left).every(key => left[key] === right[key]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function installSkills({source, home, env, agents, replace = false, previous = []}) {
|
|
44
|
+
const catalog = agentCatalog({home, env});
|
|
45
|
+
const selected = agents.map(id => {
|
|
46
|
+
const agent = catalog.find(item => item.id === id);
|
|
47
|
+
if (!agent) throw new Error(`Unknown Agent: ${id}`);
|
|
48
|
+
return agent;
|
|
49
|
+
});
|
|
50
|
+
const sourceHashes = await hashTree(source);
|
|
51
|
+
const results = [];
|
|
52
|
+
for (const agent of selected) {
|
|
53
|
+
const target = join(agent.directory, "finebi-cli-agent");
|
|
54
|
+
let backup;
|
|
55
|
+
let candidate;
|
|
56
|
+
try {
|
|
57
|
+
const stat = await lstat(target).catch(error => { if (error.code !== "ENOENT") throw error; });
|
|
58
|
+
if (stat) {
|
|
59
|
+
if (stat.isSymbolicLink()) throw new Error("Skill target is a symbolic link; refusing to replace it.");
|
|
60
|
+
const actual = await hashTree(target);
|
|
61
|
+
if (sameHashes(sourceHashes, actual)) {
|
|
62
|
+
results.push({agent: agent.id, path: target, status: "unchanged", hashes: sourceHashes});
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const recorded = previous.find(item => item.agent === agent.id && item.path === target)?.hashes;
|
|
66
|
+
if (!replace && !sameHashes(recorded, actual)) {
|
|
67
|
+
results.push({agent: agent.id, path: target, status: "kept"});
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
backup = join(home, ".finebi-cli", "skill-backups", `${agent.id}-${randomUUID()}`);
|
|
71
|
+
await mkdir(dirname(backup), {recursive: true});
|
|
72
|
+
await cp(target, backup, {recursive: true, errorOnExist: true, force: false});
|
|
73
|
+
if (!sameHashes(actual, await hashTree(backup))) throw new Error("Skill backup integrity check failed.");
|
|
74
|
+
}
|
|
75
|
+
await mkdir(agent.directory, {recursive: true});
|
|
76
|
+
candidate = await mkdtemp(join(agent.directory, ".finebi-cli-stage-"));
|
|
77
|
+
await cp(source, candidate, {recursive: true});
|
|
78
|
+
if (!sameHashes(sourceHashes, await hashTree(candidate))) throw new Error("Skill copy integrity check failed.");
|
|
79
|
+
const old = `${candidate}-old`;
|
|
80
|
+
if (stat) await rename(target, old);
|
|
81
|
+
try { await rename(candidate, target); }
|
|
82
|
+
catch (error) { if (stat) await rename(old, target); throw error; }
|
|
83
|
+
candidate = undefined;
|
|
84
|
+
// Delete only the relocated, backed-up directory inside this Agent's skills directory.
|
|
85
|
+
if (stat && backup && dirname(resolve(old)) === resolve(agent.directory)) await rm(old, {recursive: true});
|
|
86
|
+
if (!sameHashes(sourceHashes, await hashTree(target))) throw new Error("Installed Skill integrity check failed.");
|
|
87
|
+
results.push({agent: agent.id, path: target, status: "installed", hashes: sourceHashes, ...(backup ? {backup} : {})});
|
|
88
|
+
} catch (error) {
|
|
89
|
+
results.push({agent: agent.id, path: target, status: "failed", error: error.message, ...(backup ? {backup} : {})});
|
|
90
|
+
} finally {
|
|
91
|
+
if (candidate && dirname(resolve(candidate)) === resolve(agent.directory)) await rm(candidate, {recursive: true, force: true});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return results;
|
|
95
|
+
}
|