backant-cli 0.0.0-stage → 0.1.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 +292 -2
- package/bin/backant.js +3 -0
- package/dist/commands/application-auth.js +315 -0
- package/dist/commands/data-exploration.js +334 -0
- package/dist/commands/format.js +78 -0
- package/dist/commands/init.js +97 -0
- package/dist/commands/login.js +154 -0
- package/dist/commands/logs.js +103 -0
- package/dist/commands/pull.js +186 -0
- package/dist/commands/push.js +678 -0
- package/dist/commands/secrets.js +48 -0
- package/dist/commands/skill.js +57 -0
- package/dist/commands/snapshot.js +312 -0
- package/dist/commands/validate.js +1429 -0
- package/dist/index.js +49 -0
- package/dist/utils/assets-scanner.js +20 -0
- package/dist/utils/connection.js +115 -0
- package/dist/utils/flow-scanner.js +92 -0
- package/dist/utils/format-utils.js +266 -0
- package/dist/utils/hash-utils.js +227 -0
- package/dist/utils/job-scanner.js +35 -0
- package/dist/utils/live-models-generator.js +189 -0
- package/dist/utils/model-scanner.js +43 -0
- package/dist/utils/package-info.js +31 -0
- package/dist/utils/services-scanner.js +46 -0
- package/dist/utils/skill-installer.js +135 -0
- package/dist/utils/snapshot-writer.js +216 -0
- package/package.json +34 -4
- package/skills/bestudio/SKILL.md +36 -0
- package/skills/bestudio/project-entrypoint.md +12 -0
- package/skills/bestudio/references/project-definitions.md +24 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { queryString, redactDocument, request, withServer } from "./data-exploration.js";
|
|
3
|
+
const validSince = new Set(["30m", "1h", "24h", "7d", "30d"]);
|
|
4
|
+
function safeLog(value) {
|
|
5
|
+
const log = redactDocument(value);
|
|
6
|
+
if (log?.request)
|
|
7
|
+
delete log.request.body;
|
|
8
|
+
if (log?.response)
|
|
9
|
+
delete log.response.body;
|
|
10
|
+
return log;
|
|
11
|
+
}
|
|
12
|
+
function fail(error) {
|
|
13
|
+
console.error(`✖ ${error?.message || "Execution log request failed"}`);
|
|
14
|
+
process.exitCode = 1;
|
|
15
|
+
}
|
|
16
|
+
async function listLogs(options) {
|
|
17
|
+
try {
|
|
18
|
+
if (options.since && !validSince.has(options.since))
|
|
19
|
+
throw new Error("--since must be 30m, 1h, 24h, 7d, or 30d.");
|
|
20
|
+
if (!!options.start !== !!options.end)
|
|
21
|
+
throw new Error("--start and --end must be provided together.");
|
|
22
|
+
if (options.since && options.start)
|
|
23
|
+
throw new Error("Use either --since or --start/--end.");
|
|
24
|
+
const page = Number(options.page || 1);
|
|
25
|
+
const pageSize = Number(options.pageSize || 50);
|
|
26
|
+
if (!Number.isInteger(page) || page < 1 || !Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) {
|
|
27
|
+
throw new Error("--page must be positive and --page-size must be between 1 and 100.");
|
|
28
|
+
}
|
|
29
|
+
const status = options.status === undefined ? undefined : Number(options.status);
|
|
30
|
+
if (status !== undefined && (!Number.isInteger(status) || status < 100 || status > 599))
|
|
31
|
+
throw new Error("--status must be an HTTP status from 100 to 599.");
|
|
32
|
+
const ctx = await withServer(options);
|
|
33
|
+
const query = new URLSearchParams({ ...Object.fromEntries(new URLSearchParams(queryString(ctx))), page: String(page), pageSize: String(pageSize) });
|
|
34
|
+
if (options.since)
|
|
35
|
+
query.set("since", options.since);
|
|
36
|
+
if (options.start && options.end) {
|
|
37
|
+
query.set("start", options.start);
|
|
38
|
+
query.set("end", options.end);
|
|
39
|
+
}
|
|
40
|
+
if (status !== undefined)
|
|
41
|
+
query.set("status", String(status));
|
|
42
|
+
if (options.errors)
|
|
43
|
+
query.set("errors", "true");
|
|
44
|
+
const result = await request(ctx, `/execution-logs?${query.toString()}`);
|
|
45
|
+
const logs = (result.data || []).map(safeLog);
|
|
46
|
+
const output = { serverId: ctx.serverId, env: ctx.env, logs, pagination: result.pagination };
|
|
47
|
+
if (options.json)
|
|
48
|
+
console.log(JSON.stringify(output, null, 2));
|
|
49
|
+
else {
|
|
50
|
+
console.log(`Execution logs (${ctx.env}) — ${result.pagination?.total ?? logs.length} record(s), page ${page}/${result.pagination?.totalPages ?? 1}`);
|
|
51
|
+
for (const log of logs)
|
|
52
|
+
console.log(` ${log.startTime || "time unknown"} | ${log.response?.status ?? (log.error ? "ERROR" : "—")} | ${log.actionId} | ${log.executionId} | ${log.request?.method || ""} ${log.request?.path || ""}`);
|
|
53
|
+
if (!logs.length)
|
|
54
|
+
console.log(" No execution logs found.");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
fail(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function getLog(executionId, options) {
|
|
62
|
+
try {
|
|
63
|
+
const ctx = await withServer(options);
|
|
64
|
+
const query = new URLSearchParams(queryString(ctx));
|
|
65
|
+
const result = await request(ctx, `/execution-logs/${encodeURIComponent(executionId)}?${query.toString()}`);
|
|
66
|
+
const log = safeLog(result.data);
|
|
67
|
+
if (options.json)
|
|
68
|
+
console.log(JSON.stringify({ serverId: ctx.serverId, env: ctx.env, log }, null, 2));
|
|
69
|
+
else
|
|
70
|
+
console.log(`Execution ${log.executionId}\n${log.startTime || ""} | ${log.actionId} | ${log.response?.status ?? (log.error ? "ERROR" : "—")}\n${JSON.stringify(log, null, 2)}`);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
fail(error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function sharedOptions(command) {
|
|
77
|
+
return command.option("--server <id-or-name>", "Target server ID or accessible server name")
|
|
78
|
+
.option("--env <environment>", "Environment on the selected target (default: BESTUDIO_ENV or dev)")
|
|
79
|
+
.option("--local", "Target local BE Studio at http://localhost:3000")
|
|
80
|
+
.option("--url <apiUrl>", "API URL for a custom BE Studio instance; cannot be combined with --local")
|
|
81
|
+
.option("--token <token>", "Access token for this command; otherwise use saved target credentials")
|
|
82
|
+
.option("--json", "Print structured JSON output");
|
|
83
|
+
}
|
|
84
|
+
const logsCommand = new Command("logs")
|
|
85
|
+
.description("Inspect stored action execution logs (read-only)")
|
|
86
|
+
.addHelpText("after", `
|
|
87
|
+
Examples:
|
|
88
|
+
backant logs list --server my-server --env dev
|
|
89
|
+
backant logs list --errors --since 24h --server my-server
|
|
90
|
+
backant logs get <executionId> --server my-server
|
|
91
|
+
`);
|
|
92
|
+
const listCommand = sharedOptions(logsCommand.command("list").description("List execution logs"));
|
|
93
|
+
listCommand.option("--since <duration>", "Time window: 30m, 1h, 24h, 7d, 30d")
|
|
94
|
+
.option("--start <date>", "Custom inclusive start date")
|
|
95
|
+
.option("--end <date>", "Custom inclusive end date")
|
|
96
|
+
.option("--errors", "Only executions with errors")
|
|
97
|
+
.option("--status <code>", "Filter by response status")
|
|
98
|
+
.option("--page <number>", "Page number", "1")
|
|
99
|
+
.option("--page-size <number>", "Records per page (max 100)", "50")
|
|
100
|
+
.action(listLogs);
|
|
101
|
+
sharedOptions(logsCommand.command("get <executionId>").description("Show one execution log"))
|
|
102
|
+
.action(getLog);
|
|
103
|
+
export { logsCommand };
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { writeSnapshotToWorkspace } from "../utils/snapshot-writer.js";
|
|
5
|
+
import { resolveConnection } from "../utils/connection.js";
|
|
6
|
+
import { canonicalizeWorkspace, canonicalizeSnapshot, } from "../utils/format-utils.js";
|
|
7
|
+
function loadSnapshotFile(projectRoot) {
|
|
8
|
+
const snapshotPath = path.join(projectRoot, ".bestudio", "snapshot.json");
|
|
9
|
+
if (!fs.existsSync(snapshotPath)) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
const content = fs.readFileSync(snapshotPath, "utf-8");
|
|
14
|
+
return JSON.parse(content);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function loadConfig(projectRoot) {
|
|
21
|
+
const configPath = path.join(projectRoot, "bestudio.config.json");
|
|
22
|
+
if (!fs.existsSync(configPath)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const content = fs.readFileSync(configPath, "utf-8");
|
|
27
|
+
return JSON.parse(content);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const VALID_ENVS = ["dev", "staging", "prod"];
|
|
34
|
+
async function pullDomain(apiUrl, accessToken, domain, serverId, env = "dev") {
|
|
35
|
+
const url = `${apiUrl.replace(/\/+$/, "")}/system/snapshot/pull?domain=${domain}&serverId=${serverId}&env=${env}`;
|
|
36
|
+
let response;
|
|
37
|
+
try {
|
|
38
|
+
response = await fetch(url, {
|
|
39
|
+
method: "GET",
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Bearer ${accessToken}`,
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
throw new Error(`Network error: ${error.message || "Failed to connect to server"}`);
|
|
48
|
+
}
|
|
49
|
+
let apiResponse;
|
|
50
|
+
try {
|
|
51
|
+
apiResponse = (await response.json());
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw new Error("Failed to parse response as JSON");
|
|
55
|
+
}
|
|
56
|
+
if (!response.ok || !apiResponse.success) {
|
|
57
|
+
const errorMessage = apiResponse.error || response.statusText || "Pull failed";
|
|
58
|
+
const err = new Error(errorMessage);
|
|
59
|
+
err.status = response.status;
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
if (!apiResponse.data) {
|
|
63
|
+
throw new Error("Response missing data field");
|
|
64
|
+
}
|
|
65
|
+
return apiResponse.data;
|
|
66
|
+
}
|
|
67
|
+
export async function pullCommand(domain, options = {}) {
|
|
68
|
+
const resolvedEnv = options.env || process.env.BESTUDIO_ENV || "dev";
|
|
69
|
+
if (!VALID_ENVS.includes(resolvedEnv)) {
|
|
70
|
+
console.error(`✖ Invalid environment "${resolvedEnv}". Must be one of: ${VALID_ENVS.join(", ")}`);
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
let apiUrl;
|
|
74
|
+
let accessToken;
|
|
75
|
+
try {
|
|
76
|
+
const connection = resolveConnection(options);
|
|
77
|
+
apiUrl = connection.apiUrl;
|
|
78
|
+
accessToken = connection.accessToken;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
console.error(`✖ ${error.message || "Connection could not be resolved"}`);
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
// 2. Check project
|
|
85
|
+
const projectRoot = options.projectRoot || process.cwd();
|
|
86
|
+
const snapshotFile = loadSnapshotFile(projectRoot);
|
|
87
|
+
if (!snapshotFile || !snapshotFile.serverId) {
|
|
88
|
+
console.error("✖ No snapshot file found or missing serverId");
|
|
89
|
+
console.error(" Run `backant push` first to create snapshot");
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
// 3. Determine domains to pull
|
|
93
|
+
const allDomains = ["routes", "actions", "jobs", "models", "flows"];
|
|
94
|
+
const domainsToPull = domain ? [domain] : allDomains;
|
|
95
|
+
// 4. Validate domain(s)
|
|
96
|
+
for (const dom of domainsToPull) {
|
|
97
|
+
if (!allDomains.includes(dom)) {
|
|
98
|
+
console.error("✖ Domain must be one of: routes, actions, jobs, models, flows");
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// 5. Pull each domain
|
|
103
|
+
const snapshotPath = path.join(projectRoot, ".bestudio", "snapshot.json");
|
|
104
|
+
const snapshotDir = path.dirname(snapshotPath);
|
|
105
|
+
if (!fs.existsSync(snapshotDir)) {
|
|
106
|
+
fs.mkdirSync(snapshotDir, { recursive: true });
|
|
107
|
+
}
|
|
108
|
+
// Load existing snapshot (preserves flows, services, assets, etc.)
|
|
109
|
+
let existingSnapshot = snapshotFile || {
|
|
110
|
+
snapshotFormatVersion: 1,
|
|
111
|
+
project: snapshotFile?.project || { name: "" },
|
|
112
|
+
routes: [],
|
|
113
|
+
actions: [],
|
|
114
|
+
jobs: [],
|
|
115
|
+
models: [],
|
|
116
|
+
};
|
|
117
|
+
const config = loadConfig(projectRoot);
|
|
118
|
+
const autoFormat = config?.autoFormat !== false; // Default to true
|
|
119
|
+
let hasErrors = false;
|
|
120
|
+
for (const dom of domainsToPull) {
|
|
121
|
+
try {
|
|
122
|
+
console.log(`📥 Pulling ${dom} domain...`);
|
|
123
|
+
const result = await pullDomain(apiUrl, accessToken, dom, snapshotFile.serverId, resolvedEnv);
|
|
124
|
+
// Update snapshot for this domain
|
|
125
|
+
existingSnapshot = {
|
|
126
|
+
...existingSnapshot,
|
|
127
|
+
snapshotFormatVersion: existingSnapshot.snapshotFormatVersion || 1,
|
|
128
|
+
[dom]: result.snapshot,
|
|
129
|
+
hashes: {
|
|
130
|
+
...existingSnapshot.hashes,
|
|
131
|
+
...(dom !== "flows" && { [dom]: result.currentHash }),
|
|
132
|
+
algorithm: result.algorithmHash,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
// Write workspace files for this domain
|
|
136
|
+
writeSnapshotToWorkspace(existingSnapshot, projectRoot, dom);
|
|
137
|
+
console.log(`✔ Workspace files updated for ${dom} domain`);
|
|
138
|
+
// Apply autoFormat if enabled (for this domain)
|
|
139
|
+
if (autoFormat) {
|
|
140
|
+
canonicalizeWorkspace(dom, projectRoot);
|
|
141
|
+
}
|
|
142
|
+
console.log(`✔ Pulled ${dom} domain`);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
// Allow graceful skip of "flows" on older backends that return 400 when pulling all domains
|
|
146
|
+
if (!domain && dom === "flows" && error.status === 400) {
|
|
147
|
+
console.warn(`⚠ Skipping flows domain pull (backend does not support pulling flows)`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
console.error(`✖ Failed to pull ${dom} domain`);
|
|
151
|
+
if (error.message) {
|
|
152
|
+
console.error(` ${error.message}`);
|
|
153
|
+
}
|
|
154
|
+
hasErrors = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// Write snapshot file once after all domains are processed
|
|
158
|
+
if (autoFormat) {
|
|
159
|
+
const formattedSnapshot = canonicalizeSnapshot(existingSnapshot);
|
|
160
|
+
fs.writeFileSync(snapshotPath, JSON.stringify(formattedSnapshot, null, 2) + "\n", "utf-8");
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
fs.writeFileSync(snapshotPath, JSON.stringify(existingSnapshot, null, 2) + "\n", "utf-8");
|
|
164
|
+
}
|
|
165
|
+
const relativePath = path
|
|
166
|
+
.relative(projectRoot, snapshotPath)
|
|
167
|
+
.replace(/\\/g, "/");
|
|
168
|
+
console.log(`✔ Snapshot updated: ${relativePath}`);
|
|
169
|
+
if (hasErrors) {
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export const pullCommandDefinition = new Command("pull")
|
|
174
|
+
.description("Pull domain snapshot from BE Studio")
|
|
175
|
+
.argument("[domain]", "Domain to pull: routes, actions, jobs, models, or flows (optional, pulls all if omitted)")
|
|
176
|
+
.option("--local", "Target local BE Studio at http://localhost:3000")
|
|
177
|
+
.option("--env <env>", "Environment on the selected target (default: BESTUDIO_ENV or dev)")
|
|
178
|
+
.option("--url <apiUrl>", "API URL for a custom BE Studio instance; cannot be combined with --local")
|
|
179
|
+
.option("--token <token>", "Access token for this command; otherwise use saved target credentials")
|
|
180
|
+
.addHelpText("after", `
|
|
181
|
+
Examples:
|
|
182
|
+
backant pull
|
|
183
|
+
backant pull flows --env dev
|
|
184
|
+
backant pull flows --local --env dev
|
|
185
|
+
`)
|
|
186
|
+
.action((domain, options) => pullCommand(domain, options));
|