@finebi/finebi-cli 1.4.0 → 1.5.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 +25 -1
- package/package.json +2 -2
- package/skills/finebi-cli-agent/references/command-workflows.md +54 -4
- package/skills/finebi-cli-agent/references/dashboard-data-result.md +219 -0
- package/skills/finebi-cli-agent/references/dashboard-dsl-api.md +24 -7
- package/skills/finebi-cli-agent/references/file-to-dashboard.md +1 -1
- package/skills/finebi-cli-agent/references/handoff-contract.md +2 -0
- package/skills/finebi-cli-agent/references/mapping-rules.md +1 -1
- package/skills/finebi-cli-agent/references/widget-dsl-api-examples.md +18 -0
- package/skills/finebi-cli-agent/references/widget-dsl-api.md +43 -5
- package/skills/finebi-cli-agent/scripts/finebi-cli/package.json +1 -1
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/config.js +27 -27
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/help.js +65 -57
- 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/schema-options.js +44 -8
- package/skills/finebi-cli-agent/scripts/finebi-cli/src/skill-files.js +94 -94
- package/skills/finebi-cli-agent/scripts/finebi-cli-runner.mjs +4 -4
- package/src/config.js +27 -27
- package/src/help.js +65 -57
- package/src/install-skill.js +89 -89
- package/src/profiles.js +158 -158
- package/src/schema-options.js +44 -8
- package/src/skill-files.js +94 -94
|
@@ -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
|
+
}
|
|
@@ -4,11 +4,12 @@ import {cliError} from "./errors.js";
|
|
|
4
4
|
import {validateSchemaValue} from "./schema-validator.js";
|
|
5
5
|
|
|
6
6
|
export function buildRequestBody(schema, args) {
|
|
7
|
-
const
|
|
7
|
+
const properties = schema.properties || {};
|
|
8
|
+
const repeatable = new Set(Object.values(properties).map(property => property.keyValueOption).filter(Boolean));
|
|
9
|
+
const parsed = parseArgs(args, repeatable);
|
|
8
10
|
if (!parsed.ok) {
|
|
9
11
|
return parsed;
|
|
10
12
|
}
|
|
11
|
-
const properties = schema.properties || {};
|
|
12
13
|
const argumentFields = Object.keys(properties).filter((fieldName) => properties[fieldName].input === "argument");
|
|
13
14
|
if (parsed.positionals.length > argumentFields.length) {
|
|
14
15
|
return invalidArgument(parsed.positionals[argumentFields.length], `Unexpected argument: ${parsed.positionals[argumentFields.length]}.`);
|
|
@@ -33,10 +34,20 @@ export function buildRequestBody(schema, args) {
|
|
|
33
34
|
for (const fieldName of Object.keys(properties)) {
|
|
34
35
|
const property = properties[fieldName];
|
|
35
36
|
const optionName = optionNameFor(fieldName, property);
|
|
37
|
+
const inlineValues = property.keyValueOption && parsed.options[property.keyValueOption];
|
|
38
|
+
if (inlineValues && Object.prototype.hasOwnProperty.call(parsed.options, optionName)) {
|
|
39
|
+
return invalidArgument(fieldName, `--${property.keyValueOption} and --${optionName} are mutually exclusive.`);
|
|
40
|
+
}
|
|
36
41
|
if (property.input === "argument" && body[fieldName] !== undefined) {
|
|
37
42
|
continue;
|
|
38
43
|
}
|
|
39
|
-
if (
|
|
44
|
+
if (inlineValues) {
|
|
45
|
+
const coerced = coerceKeyValues(fieldName, inlineValues, property);
|
|
46
|
+
if (!coerced.ok) {
|
|
47
|
+
return coerced;
|
|
48
|
+
}
|
|
49
|
+
body[fieldName] = coerced.value;
|
|
50
|
+
} else if (Object.prototype.hasOwnProperty.call(parsed.options, optionName)) {
|
|
40
51
|
const coerced = coerceValue(fieldName, parsed.options[optionName], property);
|
|
41
52
|
if (!coerced.ok) {
|
|
42
53
|
return coerced;
|
|
@@ -60,7 +71,8 @@ export function buildRequestBody(schema, args) {
|
|
|
60
71
|
}
|
|
61
72
|
}
|
|
62
73
|
for (const optionName of Object.keys(parsed.options)) {
|
|
63
|
-
if (!Object.keys(properties).some((fieldName) => optionNameFor(fieldName, properties[fieldName]) === optionName
|
|
74
|
+
if (!Object.keys(properties).some((fieldName) => optionNameFor(fieldName, properties[fieldName]) === optionName
|
|
75
|
+
|| properties[fieldName].keyValueOption === optionName)) {
|
|
64
76
|
return invalidArgument(`--${optionName}`, `Unknown option: --${optionName}.`);
|
|
65
77
|
}
|
|
66
78
|
}
|
|
@@ -75,7 +87,7 @@ export function buildRequestBody(schema, args) {
|
|
|
75
87
|
};
|
|
76
88
|
}
|
|
77
89
|
|
|
78
|
-
function parseArgs(args) {
|
|
90
|
+
function parseArgs(args, repeatable) {
|
|
79
91
|
const options = {};
|
|
80
92
|
const positionals = [];
|
|
81
93
|
for (let index = 0; index < args.length; index += 1) {
|
|
@@ -86,10 +98,13 @@ function parseArgs(args) {
|
|
|
86
98
|
}
|
|
87
99
|
const optionName = current.slice(2);
|
|
88
100
|
const next = args[index + 1];
|
|
89
|
-
|
|
90
|
-
|
|
101
|
+
const value = next === undefined || next.startsWith("--") ? true : next;
|
|
102
|
+
if (repeatable.has(optionName)) {
|
|
103
|
+
(options[optionName] ||= []).push(value);
|
|
91
104
|
} else {
|
|
92
|
-
options[optionName] =
|
|
105
|
+
options[optionName] = value;
|
|
106
|
+
}
|
|
107
|
+
if (value !== true) {
|
|
93
108
|
index += 1;
|
|
94
109
|
}
|
|
95
110
|
}
|
|
@@ -100,6 +115,27 @@ function parseArgs(args) {
|
|
|
100
115
|
};
|
|
101
116
|
}
|
|
102
117
|
|
|
118
|
+
function coerceKeyValues(fieldName, values, property) {
|
|
119
|
+
const entries = new Map();
|
|
120
|
+
for (const value of values) {
|
|
121
|
+
const separator = typeof value === "string" ? value.indexOf("=") : -1;
|
|
122
|
+
if (separator < 1 || !value.slice(0, separator).trim() || !value.slice(separator + 1).trim()) {
|
|
123
|
+
return invalidArgument(fieldName, `--${property.keyValueOption} requires name=value with a non-empty name and value.`);
|
|
124
|
+
}
|
|
125
|
+
const name = value.slice(0, separator);
|
|
126
|
+
if (entries.has(name)) {
|
|
127
|
+
return invalidArgument(fieldName, `Duplicate name in --${property.keyValueOption}: ${name}.`);
|
|
128
|
+
}
|
|
129
|
+
entries.set(name, [value.slice(separator + 1)]);
|
|
130
|
+
}
|
|
131
|
+
const value = Object.fromEntries(entries);
|
|
132
|
+
if (property.schema) {
|
|
133
|
+
const validation = validateSchemaValue(value, property.schema, fieldName);
|
|
134
|
+
if (!validation.ok) return validation;
|
|
135
|
+
}
|
|
136
|
+
return {ok: true, value};
|
|
137
|
+
}
|
|
138
|
+
|
|
103
139
|
function coerceValue(fieldName, value, property) {
|
|
104
140
|
if (property.input === "file" || property.file) {
|
|
105
141
|
if (typeof value !== "string" || value.trim() === "") {
|
|
@@ -1,94 +1,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
|
-
["cursor", "Cursor", join(home, ".cursor")],
|
|
13
|
-
["gemini-cli", "Gemini CLI", join(home, ".gemini")],
|
|
14
|
-
["github-copilot", "GitHub Copilot", join(home, ".copilot")],
|
|
15
|
-
["opencode", "OpenCode", join(config, "opencode")],
|
|
16
|
-
["cline", "Cline", join(home, ".agents")],
|
|
17
|
-
["windsurf", "Windsurf", join(home, ".codeium", "windsurf")],
|
|
18
|
-
["trae", "Trae", join(home, ".trae")],
|
|
19
|
-
["trae-cn", "Trae CN", join(home, ".trae-cn")],
|
|
20
|
-
["openclaw", "OpenClaw", [".openclaw", ".clawdbot", ".moltbot"].map(p => join(home, p)).find(existsSync) || join(home, ".openclaw")],
|
|
21
|
-
["roo", "Roo Code", join(home, ".roo")],
|
|
22
|
-
["qwen-code", "Qwen Code", join(home, ".qwen")],
|
|
23
|
-
].map(([id, name, root]) => ({id, name, directory: join(root, "skills"), detected: existsSync(root)}));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function hashTree(root, prefix = "") {
|
|
27
|
-
const files = {};
|
|
28
|
-
for (const entry of (await readdir(join(root, prefix), {withFileTypes: true})).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
29
|
-
const name = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
30
|
-
if (entry.isSymbolicLink()) throw new Error(`Skill contains a symbolic link: ${name}`);
|
|
31
|
-
if (entry.isDirectory()) Object.assign(files, await hashTree(root, name));
|
|
32
|
-
else files[name] = createHash("sha256").update(await readFile(join(root, name))).digest("hex");
|
|
33
|
-
}
|
|
34
|
-
return files;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function sameHashes(left, right) {
|
|
38
|
-
return left && right && Object.keys(left).length === Object.keys(right).length
|
|
39
|
-
&& Object.keys(left).every(key => left[key] === right[key]);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function installSkills({source, home, env, agents, replace = false, previous = []}) {
|
|
43
|
-
const catalog = agentCatalog({home, env});
|
|
44
|
-
const selected = agents.map(id => {
|
|
45
|
-
const agent = catalog.find(item => item.id === id);
|
|
46
|
-
if (!agent) throw new Error(`Unknown Agent: ${id}`);
|
|
47
|
-
return agent;
|
|
48
|
-
});
|
|
49
|
-
const sourceHashes = await hashTree(source);
|
|
50
|
-
const results = [];
|
|
51
|
-
for (const agent of selected) {
|
|
52
|
-
const target = join(agent.directory, "finebi-cli-agent");
|
|
53
|
-
let backup;
|
|
54
|
-
let candidate;
|
|
55
|
-
try {
|
|
56
|
-
const stat = await lstat(target).catch(error => { if (error.code !== "ENOENT") throw error; });
|
|
57
|
-
if (stat) {
|
|
58
|
-
if (stat.isSymbolicLink()) throw new Error("Skill target is a symbolic link; refusing to replace it.");
|
|
59
|
-
const actual = await hashTree(target);
|
|
60
|
-
if (sameHashes(sourceHashes, actual)) {
|
|
61
|
-
results.push({agent: agent.id, path: target, status: "unchanged", hashes: sourceHashes});
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
const recorded = previous.find(item => item.agent === agent.id && item.path === target)?.hashes;
|
|
65
|
-
if (!replace && !sameHashes(recorded, actual)) {
|
|
66
|
-
results.push({agent: agent.id, path: target, status: "kept"});
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
backup = join(home, ".finebi-cli", "skill-backups", `${agent.id}-${randomUUID()}`);
|
|
70
|
-
await mkdir(dirname(backup), {recursive: true});
|
|
71
|
-
await cp(target, backup, {recursive: true, errorOnExist: true, force: false});
|
|
72
|
-
if (!sameHashes(actual, await hashTree(backup))) throw new Error("Skill backup integrity check failed.");
|
|
73
|
-
}
|
|
74
|
-
await mkdir(agent.directory, {recursive: true});
|
|
75
|
-
candidate = await mkdtemp(join(agent.directory, ".finebi-cli-stage-"));
|
|
76
|
-
await cp(source, candidate, {recursive: true});
|
|
77
|
-
if (!sameHashes(sourceHashes, await hashTree(candidate))) throw new Error("Skill copy integrity check failed.");
|
|
78
|
-
const old = `${candidate}-old`;
|
|
79
|
-
if (stat) await rename(target, old);
|
|
80
|
-
try { await rename(candidate, target); }
|
|
81
|
-
catch (error) { if (stat) await rename(old, target); throw error; }
|
|
82
|
-
candidate = undefined;
|
|
83
|
-
// Delete only the relocated, backed-up directory inside this Agent's skills directory.
|
|
84
|
-
if (stat && backup && dirname(resolve(old)) === resolve(agent.directory)) await rm(old, {recursive: true});
|
|
85
|
-
if (!sameHashes(sourceHashes, await hashTree(target))) throw new Error("Installed Skill integrity check failed.");
|
|
86
|
-
results.push({agent: agent.id, path: target, status: "installed", hashes: sourceHashes, ...(backup ? {backup} : {})});
|
|
87
|
-
} catch (error) {
|
|
88
|
-
results.push({agent: agent.id, path: target, status: "failed", error: error.message, ...(backup ? {backup} : {})});
|
|
89
|
-
} finally {
|
|
90
|
-
if (candidate && dirname(resolve(candidate)) === resolve(agent.directory)) await rm(candidate, {recursive: true, force: true});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return results;
|
|
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
|
+
["cursor", "Cursor", join(home, ".cursor")],
|
|
13
|
+
["gemini-cli", "Gemini CLI", join(home, ".gemini")],
|
|
14
|
+
["github-copilot", "GitHub Copilot", join(home, ".copilot")],
|
|
15
|
+
["opencode", "OpenCode", join(config, "opencode")],
|
|
16
|
+
["cline", "Cline", join(home, ".agents")],
|
|
17
|
+
["windsurf", "Windsurf", join(home, ".codeium", "windsurf")],
|
|
18
|
+
["trae", "Trae", join(home, ".trae")],
|
|
19
|
+
["trae-cn", "Trae CN", join(home, ".trae-cn")],
|
|
20
|
+
["openclaw", "OpenClaw", [".openclaw", ".clawdbot", ".moltbot"].map(p => join(home, p)).find(existsSync) || join(home, ".openclaw")],
|
|
21
|
+
["roo", "Roo Code", join(home, ".roo")],
|
|
22
|
+
["qwen-code", "Qwen Code", join(home, ".qwen")],
|
|
23
|
+
].map(([id, name, root]) => ({id, name, directory: join(root, "skills"), detected: existsSync(root)}));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function hashTree(root, prefix = "") {
|
|
27
|
+
const files = {};
|
|
28
|
+
for (const entry of (await readdir(join(root, prefix), {withFileTypes: true})).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
29
|
+
const name = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
30
|
+
if (entry.isSymbolicLink()) throw new Error(`Skill contains a symbolic link: ${name}`);
|
|
31
|
+
if (entry.isDirectory()) Object.assign(files, await hashTree(root, name));
|
|
32
|
+
else files[name] = createHash("sha256").update(await readFile(join(root, name))).digest("hex");
|
|
33
|
+
}
|
|
34
|
+
return files;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function sameHashes(left, right) {
|
|
38
|
+
return left && right && Object.keys(left).length === Object.keys(right).length
|
|
39
|
+
&& Object.keys(left).every(key => left[key] === right[key]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function installSkills({source, home, env, agents, replace = false, previous = []}) {
|
|
43
|
+
const catalog = agentCatalog({home, env});
|
|
44
|
+
const selected = agents.map(id => {
|
|
45
|
+
const agent = catalog.find(item => item.id === id);
|
|
46
|
+
if (!agent) throw new Error(`Unknown Agent: ${id}`);
|
|
47
|
+
return agent;
|
|
48
|
+
});
|
|
49
|
+
const sourceHashes = await hashTree(source);
|
|
50
|
+
const results = [];
|
|
51
|
+
for (const agent of selected) {
|
|
52
|
+
const target = join(agent.directory, "finebi-cli-agent");
|
|
53
|
+
let backup;
|
|
54
|
+
let candidate;
|
|
55
|
+
try {
|
|
56
|
+
const stat = await lstat(target).catch(error => { if (error.code !== "ENOENT") throw error; });
|
|
57
|
+
if (stat) {
|
|
58
|
+
if (stat.isSymbolicLink()) throw new Error("Skill target is a symbolic link; refusing to replace it.");
|
|
59
|
+
const actual = await hashTree(target);
|
|
60
|
+
if (sameHashes(sourceHashes, actual)) {
|
|
61
|
+
results.push({agent: agent.id, path: target, status: "unchanged", hashes: sourceHashes});
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const recorded = previous.find(item => item.agent === agent.id && item.path === target)?.hashes;
|
|
65
|
+
if (!replace && !sameHashes(recorded, actual)) {
|
|
66
|
+
results.push({agent: agent.id, path: target, status: "kept"});
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
backup = join(home, ".finebi-cli", "skill-backups", `${agent.id}-${randomUUID()}`);
|
|
70
|
+
await mkdir(dirname(backup), {recursive: true});
|
|
71
|
+
await cp(target, backup, {recursive: true, errorOnExist: true, force: false});
|
|
72
|
+
if (!sameHashes(actual, await hashTree(backup))) throw new Error("Skill backup integrity check failed.");
|
|
73
|
+
}
|
|
74
|
+
await mkdir(agent.directory, {recursive: true});
|
|
75
|
+
candidate = await mkdtemp(join(agent.directory, ".finebi-cli-stage-"));
|
|
76
|
+
await cp(source, candidate, {recursive: true});
|
|
77
|
+
if (!sameHashes(sourceHashes, await hashTree(candidate))) throw new Error("Skill copy integrity check failed.");
|
|
78
|
+
const old = `${candidate}-old`;
|
|
79
|
+
if (stat) await rename(target, old);
|
|
80
|
+
try { await rename(candidate, target); }
|
|
81
|
+
catch (error) { if (stat) await rename(old, target); throw error; }
|
|
82
|
+
candidate = undefined;
|
|
83
|
+
// Delete only the relocated, backed-up directory inside this Agent's skills directory.
|
|
84
|
+
if (stat && backup && dirname(resolve(old)) === resolve(agent.directory)) await rm(old, {recursive: true});
|
|
85
|
+
if (!sameHashes(sourceHashes, await hashTree(target))) throw new Error("Installed Skill integrity check failed.");
|
|
86
|
+
results.push({agent: agent.id, path: target, status: "installed", hashes: sourceHashes, ...(backup ? {backup} : {})});
|
|
87
|
+
} catch (error) {
|
|
88
|
+
results.push({agent: agent.id, path: target, status: "failed", error: error.message, ...(backup ? {backup} : {})});
|
|
89
|
+
} finally {
|
|
90
|
+
if (candidate && dirname(resolve(candidate)) === resolve(agent.directory)) await rm(candidate, {recursive: true, force: true});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return results;
|
|
94
|
+
}
|
|
@@ -10,10 +10,10 @@ const scriptDirectory = dirname(fileURLToPath(import.meta.url));
|
|
|
10
10
|
const bundledCliEntry = resolve(scriptDirectory, "finebi-cli", "src", "index.js");
|
|
11
11
|
|
|
12
12
|
export async function run(argv = process.argv.slice(2), options = {}) {
|
|
13
|
-
const env = {...(options.env ?? process.env)};
|
|
14
|
-
if (!env.FINEBI_CLI_PATH?.trim() && existsSync(bundledCliEntry)) {
|
|
15
|
-
env.FINEBI_CLI_PATH = bundledCliEntry;
|
|
16
|
-
}
|
|
13
|
+
const env = {...(options.env ?? process.env)};
|
|
14
|
+
if (!env.FINEBI_CLI_PATH?.trim() && existsSync(bundledCliEntry)) {
|
|
15
|
+
env.FINEBI_CLI_PATH = bundledCliEntry;
|
|
16
|
+
}
|
|
17
17
|
return runCore(argv, {...options, env});
|
|
18
18
|
}
|
|
19
19
|
|