api-spec-cli 0.0.1 → 0.0.2
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 +191 -0
- package/bin/spec.js +5 -0
- package/package.json +32 -8
- package/src/args.js +46 -0
- package/src/cli.js +79 -0
- package/src/commands/call.js +198 -0
- package/src/commands/config.js +75 -0
- package/src/commands/list.js +47 -0
- package/src/commands/load.js +230 -0
- package/src/commands/show.js +176 -0
- package/src/commands/validate.js +269 -0
- package/src/output.js +61 -0
- package/src/store.js +36 -0
package/src/output.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import YAML from "yaml";
|
|
2
|
+
|
|
3
|
+
let outputFormat = "json";
|
|
4
|
+
|
|
5
|
+
export function setFormat(format) {
|
|
6
|
+
if (format && ["json", "text", "yaml"].includes(format)) {
|
|
7
|
+
outputFormat = format;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function out(data) {
|
|
12
|
+
switch (outputFormat) {
|
|
13
|
+
case "yaml":
|
|
14
|
+
console.log(YAML.stringify(data).trimEnd());
|
|
15
|
+
break;
|
|
16
|
+
case "text":
|
|
17
|
+
console.log(formatText(data));
|
|
18
|
+
break;
|
|
19
|
+
case "json":
|
|
20
|
+
default:
|
|
21
|
+
console.log(JSON.stringify(data, null, 2));
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function err(message) {
|
|
27
|
+
// Errors are always JSON for reliable agent parsing
|
|
28
|
+
console.error(JSON.stringify({ error: message }));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function formatText(data, indent = 0) {
|
|
32
|
+
if (data === null || data === undefined) return "null";
|
|
33
|
+
if (typeof data === "string") return data;
|
|
34
|
+
if (typeof data === "number" || typeof data === "boolean") return String(data);
|
|
35
|
+
|
|
36
|
+
if (Array.isArray(data)) {
|
|
37
|
+
if (data.length === 0) return "(empty)";
|
|
38
|
+
return data
|
|
39
|
+
.map((item, i) => {
|
|
40
|
+
if (typeof item === "object" && item !== null) {
|
|
41
|
+
return `${" ".repeat(indent)}[${i}]\n${formatText(item, indent + 1)}`;
|
|
42
|
+
}
|
|
43
|
+
return `${" ".repeat(indent)}- ${item}`;
|
|
44
|
+
})
|
|
45
|
+
.join("\n");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (typeof data === "object") {
|
|
49
|
+
return Object.entries(data)
|
|
50
|
+
.map(([key, val]) => {
|
|
51
|
+
if (val === null || val === undefined) return `${" ".repeat(indent)}${key}: null`;
|
|
52
|
+
if (typeof val === "object") {
|
|
53
|
+
return `${" ".repeat(indent)}${key}:\n${formatText(val, indent + 1)}`;
|
|
54
|
+
}
|
|
55
|
+
return `${" ".repeat(indent)}${key}: ${val}`;
|
|
56
|
+
})
|
|
57
|
+
.join("\n");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return String(data);
|
|
61
|
+
}
|
package/src/store.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Persistent storage in .spec-cli/ directory (project-local)
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
|
|
5
|
+
const DIR = join(process.cwd(), ".spec-cli");
|
|
6
|
+
|
|
7
|
+
function ensureDir() {
|
|
8
|
+
if (!existsSync(DIR)) mkdirSync(DIR, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function readStore(name) {
|
|
12
|
+
const file = join(DIR, `${name}.json`);
|
|
13
|
+
if (!existsSync(file)) return null;
|
|
14
|
+
return JSON.parse(readFileSync(file, "utf-8"));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function writeStore(name, data) {
|
|
18
|
+
ensureDir();
|
|
19
|
+
writeFileSync(join(DIR, `${name}.json`), JSON.stringify(data, null, 2));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function getConfig() {
|
|
23
|
+
return readStore("config") || { baseUrl: null, headers: {}, auth: null };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function setConfig(config) {
|
|
27
|
+
writeStore("config", config);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getSpec() {
|
|
31
|
+
return readStore("spec");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function saveSpec(spec) {
|
|
35
|
+
writeStore("spec", spec);
|
|
36
|
+
}
|