@citemap/cli 0.3.3 → 0.4.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 +7 -0
- package/bin/citemap.js +2 -0
- package/dist/index.mjs +286 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -102,3 +102,10 @@ If a citemap claims a level via `citemapLevel` that doesn't match actual content
|
|
|
102
102
|
## License
|
|
103
103
|
|
|
104
104
|
MIT
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
## 0.4.0
|
|
108
|
+
|
|
109
|
+
- `citemap validate <url>` reads a citemap over https.
|
|
110
|
+
- Content files: when a citemap lists `contentFiles`, the CLI reads each one (https only, up to 50, 4 MB each), checks it, and checks the joined record (duplicate `@id`s, relationship endpoints). `--no-follow` skips them.
|
|
111
|
+
- v3.3 / v3.4 files are checked against the spec's structural rules instead of the strict v3.0 schema.
|
package/bin/citemap.js
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { readFileSync } from "fs";
|
|
5
|
+
import { resolve } from "path";
|
|
6
|
+
import { validate, validateAndDiagnose, validateContentFile, checkAcrossFiles } from "@citemap/validator";
|
|
7
|
+
var VERSION = "0.4.0";
|
|
8
|
+
var colors = {
|
|
9
|
+
reset: "\x1B[0m",
|
|
10
|
+
bright: "\x1B[1m",
|
|
11
|
+
dim: "\x1B[2m",
|
|
12
|
+
red: "\x1B[31m",
|
|
13
|
+
green: "\x1B[32m",
|
|
14
|
+
yellow: "\x1B[33m",
|
|
15
|
+
blue: "\x1B[34m",
|
|
16
|
+
magenta: "\x1B[35m",
|
|
17
|
+
cyan: "\x1B[36m"
|
|
18
|
+
};
|
|
19
|
+
function printUsage() {
|
|
20
|
+
console.log(`
|
|
21
|
+
${colors.bright}citemap${colors.reset} \u2014 Validate citemap.json files for AI accuracy
|
|
22
|
+
|
|
23
|
+
${colors.bright}Usage:${colors.reset}
|
|
24
|
+
citemap validate <path|url> Validate a citemap.json file or URL
|
|
25
|
+
citemap diagnose <path|url> Run detailed analysis with scores and level assessment
|
|
26
|
+
|
|
27
|
+
${colors.bright}Options:${colors.reset}
|
|
28
|
+
--no-follow Don't read the content files a citemap lists in contentFiles
|
|
29
|
+
(followed by default: https only, up to 50 files)
|
|
30
|
+
citemap --help Show this help message
|
|
31
|
+
citemap --version Show version number
|
|
32
|
+
|
|
33
|
+
${colors.bright}Examples:${colors.reset}
|
|
34
|
+
citemap validate ./citemap.json
|
|
35
|
+
citemap validate https://example.com/citemap.json
|
|
36
|
+
citemap diagnose .well-known/citemap.json
|
|
37
|
+
|
|
38
|
+
${colors.bright}Learn more:${colors.reset}
|
|
39
|
+
https://github.com/citemaps/citemap
|
|
40
|
+
`);
|
|
41
|
+
}
|
|
42
|
+
function printVersion() {
|
|
43
|
+
console.log(`citemap v${VERSION}`);
|
|
44
|
+
}
|
|
45
|
+
function loadJSON(filePath) {
|
|
46
|
+
try {
|
|
47
|
+
const absolutePath = resolve(filePath);
|
|
48
|
+
const content = readFileSync(absolutePath, "utf-8");
|
|
49
|
+
return JSON.parse(content);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof SyntaxError) {
|
|
52
|
+
console.error(`${colors.red}\u2717 Invalid JSON${colors.reset}: ${error.message}`);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
56
|
+
console.error(`${colors.red}\u2717 File not found${colors.reset}: ${filePath}`);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
console.error(`${colors.red}\u2717 Error reading file${colors.reset}: ${error instanceof Error ? error.message : String(error)}`);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function getProgressBar(percentage, width = 20) {
|
|
64
|
+
const filled = Math.round(percentage / 100 * width);
|
|
65
|
+
const empty = width - filled;
|
|
66
|
+
return `[${colors.green}${"\u2588".repeat(filled)}${colors.reset}${colors.dim}${"\u2591".repeat(empty)}${colors.reset}]`;
|
|
67
|
+
}
|
|
68
|
+
function scoreColor(score) {
|
|
69
|
+
if (score >= 80) return colors.green;
|
|
70
|
+
if (score >= 60) return colors.yellow;
|
|
71
|
+
return colors.red;
|
|
72
|
+
}
|
|
73
|
+
function formatValidationOutput(result) {
|
|
74
|
+
const statusIcon = result.valid ? `${colors.green}\u2713${colors.reset}` : `${colors.red}\u2717${colors.reset}`;
|
|
75
|
+
const statusText = result.valid ? `${colors.green}Valid${colors.reset}` : `${colors.red}Invalid${colors.reset}`;
|
|
76
|
+
console.log(`
|
|
77
|
+
${statusIcon} ${statusText} citemap.json ${colors.dim}v${result.version}${colors.reset}`);
|
|
78
|
+
const lvl = result.level;
|
|
79
|
+
const levelColor = lvl.current === 3 ? colors.green : lvl.current === 2 ? colors.cyan : colors.yellow;
|
|
80
|
+
console.log(` ${levelColor}${colors.bright}${lvl.badge}${colors.reset}`);
|
|
81
|
+
if (!lvl.claimAccurate && lvl.claimed != null) {
|
|
82
|
+
console.log(` ${colors.yellow}\u26A0 Claimed Level ${lvl.claimed}, actual Level ${lvl.current}${colors.reset}`);
|
|
83
|
+
}
|
|
84
|
+
console.log();
|
|
85
|
+
if (result.errors.length > 0) {
|
|
86
|
+
console.log(`${colors.red}${colors.bright}Errors (${result.errors.length}):${colors.reset}`);
|
|
87
|
+
result.errors.forEach((error) => {
|
|
88
|
+
console.log(` ${colors.red}\u2022${colors.reset} ${error.path}: ${error.message}`);
|
|
89
|
+
});
|
|
90
|
+
console.log();
|
|
91
|
+
}
|
|
92
|
+
if (result.warnings.length > 0) {
|
|
93
|
+
console.log(`${colors.yellow}${colors.bright}Warnings (${result.warnings.length}):${colors.reset}`);
|
|
94
|
+
result.warnings.forEach((warning) => {
|
|
95
|
+
console.log(` ${colors.yellow}\u2022${colors.reset} ${warning.path}: ${warning.message}`);
|
|
96
|
+
console.log(` ${colors.dim}\u2192 ${warning.suggestion}${colors.reset}`);
|
|
97
|
+
});
|
|
98
|
+
console.log();
|
|
99
|
+
}
|
|
100
|
+
const s = result.score;
|
|
101
|
+
console.log(`${colors.bright}Quality Score: ${scoreColor(s.overall)}${s.overall}%${colors.reset}`);
|
|
102
|
+
console.log(` Completeness ${scoreColor(s.completeness)}${s.completeness.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.completeness)}`);
|
|
103
|
+
console.log(` Modules ${scoreColor(s.modules)}${s.modules.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.modules)}`);
|
|
104
|
+
console.log(` Trust ${scoreColor(s.trust)}${s.trust.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.trust)}`);
|
|
105
|
+
console.log();
|
|
106
|
+
if (result.valid) {
|
|
107
|
+
console.log(`${colors.green}All validations passed.${colors.reset}
|
|
108
|
+
`);
|
|
109
|
+
}
|
|
110
|
+
return result.valid;
|
|
111
|
+
}
|
|
112
|
+
function formatDiagnosticOutput(data) {
|
|
113
|
+
const result = validateAndDiagnose(data);
|
|
114
|
+
const diag = result.diagnosis;
|
|
115
|
+
const statusIcon = result.valid ? `${colors.green}\u2713${colors.reset}` : `${colors.red}\u2717${colors.reset}`;
|
|
116
|
+
const statusText = result.valid ? `${colors.green}Valid${colors.reset}` : `${colors.red}Invalid${colors.reset}`;
|
|
117
|
+
console.log(`
|
|
118
|
+
${statusIcon} ${statusText} citemap.json ${colors.dim}v${result.version}${colors.reset}`);
|
|
119
|
+
const lvl = result.level;
|
|
120
|
+
const levelColor = lvl.current === 3 ? colors.green : lvl.current === 2 ? colors.cyan : colors.yellow;
|
|
121
|
+
console.log(` ${levelColor}${colors.bright}${lvl.badge}${colors.reset}`);
|
|
122
|
+
if (!lvl.claimAccurate && lvl.claimed != null) {
|
|
123
|
+
console.log(` ${colors.yellow}\u26A0 Claimed Level ${lvl.claimed}, actual Level ${lvl.current}${colors.reset}`);
|
|
124
|
+
}
|
|
125
|
+
console.log();
|
|
126
|
+
if (result.errors.length > 0) {
|
|
127
|
+
console.log(`${colors.red}${colors.bright}Errors (${result.errors.length}):${colors.reset}`);
|
|
128
|
+
result.errors.forEach((error) => {
|
|
129
|
+
console.log(` ${colors.red}\u2022${colors.reset} ${error.path}: ${error.message}`);
|
|
130
|
+
});
|
|
131
|
+
console.log();
|
|
132
|
+
}
|
|
133
|
+
if (result.warnings.length > 0) {
|
|
134
|
+
console.log(`${colors.yellow}${colors.bright}Warnings (${result.warnings.length}):${colors.reset}`);
|
|
135
|
+
result.warnings.forEach((warning) => {
|
|
136
|
+
console.log(` ${colors.yellow}\u2022${colors.reset} ${warning.path}: ${warning.message}`);
|
|
137
|
+
console.log(` ${colors.dim}\u2192 ${warning.suggestion}${colors.reset}`);
|
|
138
|
+
});
|
|
139
|
+
console.log();
|
|
140
|
+
}
|
|
141
|
+
const s = result.score;
|
|
142
|
+
console.log(`${colors.bright}Quality Scores:${colors.reset}`);
|
|
143
|
+
console.log(` Overall ${scoreColor(s.overall)}${s.overall.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.overall)}`);
|
|
144
|
+
console.log(` Completeness ${scoreColor(s.completeness)}${s.completeness.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.completeness)}`);
|
|
145
|
+
console.log(` Modules ${scoreColor(s.modules)}${s.modules.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.modules)}`);
|
|
146
|
+
console.log(` Trust ${scoreColor(s.trust)}${s.trust.toString().padStart(3)}%${colors.reset} ${getProgressBar(s.trust)}`);
|
|
147
|
+
console.log();
|
|
148
|
+
console.log(`${colors.bright}Diagnosis:${colors.reset}`);
|
|
149
|
+
console.log(` Site type: ${colors.cyan}${diag.siteType}${colors.reset}`);
|
|
150
|
+
console.log(` Field coverage: ${scoreColor(diag.fieldCoverage.percentage)}${diag.fieldCoverage.filled}/${diag.fieldCoverage.total} (${diag.fieldCoverage.percentage}%)${colors.reset}`);
|
|
151
|
+
if (diag.presentModules.length > 0) {
|
|
152
|
+
console.log(` Modules present: ${colors.green}${diag.presentModules.join(", ")}${colors.reset}`);
|
|
153
|
+
}
|
|
154
|
+
if (diag.missingModules.length > 0) {
|
|
155
|
+
console.log(` Modules missing: ${colors.yellow}${diag.missingModules.join(", ")}${colors.reset}`);
|
|
156
|
+
}
|
|
157
|
+
console.log();
|
|
158
|
+
if (lvl.nextLevelHints.length > 0) {
|
|
159
|
+
const nextLevel = lvl.current + 1;
|
|
160
|
+
console.log(`${colors.bright}To reach Level ${nextLevel}:${colors.reset}`);
|
|
161
|
+
lvl.nextLevelHints.forEach((hint) => {
|
|
162
|
+
console.log(` ${colors.magenta}\u2192${colors.reset} ${hint}`);
|
|
163
|
+
});
|
|
164
|
+
console.log();
|
|
165
|
+
}
|
|
166
|
+
if (diag.recommendations.length > 0) {
|
|
167
|
+
console.log(`${colors.bright}Recommendations:${colors.reset}`);
|
|
168
|
+
diag.recommendations.forEach((rec) => {
|
|
169
|
+
console.log(` ${colors.blue}\u2022${colors.reset} ${rec}`);
|
|
170
|
+
});
|
|
171
|
+
console.log();
|
|
172
|
+
}
|
|
173
|
+
if (result.valid) {
|
|
174
|
+
console.log(`${colors.green}\u2713 All validations passed.${colors.reset}
|
|
175
|
+
`);
|
|
176
|
+
} else {
|
|
177
|
+
console.log(`${colors.red}\u2717 Validation failed. See errors above.${colors.reset}
|
|
178
|
+
`);
|
|
179
|
+
}
|
|
180
|
+
return result.valid;
|
|
181
|
+
}
|
|
182
|
+
var MAX_FILES = 50;
|
|
183
|
+
var MAX_BYTES = 4 * 1024 * 1024;
|
|
184
|
+
async function fetchJSON(url) {
|
|
185
|
+
const ctrl = new AbortController();
|
|
186
|
+
const t = setTimeout(() => ctrl.abort(), 15e3);
|
|
187
|
+
try {
|
|
188
|
+
const res = await fetch(url, { signal: ctrl.signal, headers: { Accept: "application/json", "User-Agent": `citemap-cli/${VERSION}` } });
|
|
189
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
190
|
+
const text = await res.text();
|
|
191
|
+
if (text.length > MAX_BYTES) throw new Error("larger than 4 MB");
|
|
192
|
+
return JSON.parse(text);
|
|
193
|
+
} finally {
|
|
194
|
+
clearTimeout(t);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async function checkContentFiles(main2) {
|
|
198
|
+
const refs = Array.isArray(main2?.contentFiles) ? main2.contentFiles : [];
|
|
199
|
+
if (!refs.length) return true;
|
|
200
|
+
console.log(`${colors.bright}Content files${colors.reset} ${colors.dim}(${refs.length} listed)${colors.reset}`);
|
|
201
|
+
const read = [];
|
|
202
|
+
let ok = true;
|
|
203
|
+
for (const ref of refs.slice(0, MAX_FILES)) {
|
|
204
|
+
const label = `${ref?.type ?? "?"}${ref?.year ? " " + ref.year : ""}${ref?.category ? " \xB7 " + ref.category : ""}`;
|
|
205
|
+
if (typeof ref?.url !== "string" || !/^https:\/\//i.test(ref.url)) {
|
|
206
|
+
console.log(` ${colors.red}\u2717${colors.reset} ${label}: url isn't https`);
|
|
207
|
+
ok = false;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const data = await fetchJSON(ref.url);
|
|
212
|
+
const r = validateContentFile(data, ref);
|
|
213
|
+
read.push({ url: ref.url, data });
|
|
214
|
+
if (r.valid) console.log(` ${colors.green}\u2713${colors.reset} ${label} ${colors.dim}(${r.count} item${r.count === 1 ? "" : "s"})${colors.reset}`);
|
|
215
|
+
else {
|
|
216
|
+
ok = false;
|
|
217
|
+
console.log(` ${colors.red}\u2717${colors.reset} ${label}: ${r.errors.map((e) => e.message).join("; ")}`);
|
|
218
|
+
}
|
|
219
|
+
for (const w of r.warnings) console.log(` ${colors.yellow}\u26A0${colors.reset} ${w.message}`);
|
|
220
|
+
} catch (e) {
|
|
221
|
+
ok = false;
|
|
222
|
+
console.log(` ${colors.red}\u2717${colors.reset} ${label}: couldn't read ${ref.url} (${e instanceof Error ? e.message : String(e)})`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (refs.length > MAX_FILES) console.log(` ${colors.yellow}\u26A0${colors.reset} ${refs.length - MAX_FILES} more listed; only ${MAX_FILES} read`);
|
|
226
|
+
const across = checkAcrossFiles(main2, read);
|
|
227
|
+
for (const e of across.errors) {
|
|
228
|
+
ok = false;
|
|
229
|
+
console.log(` ${colors.red}\u2717${colors.reset} ${e.message}`);
|
|
230
|
+
}
|
|
231
|
+
for (const w of across.warnings) console.log(` ${colors.yellow}\u26A0${colors.reset} ${w.message}`);
|
|
232
|
+
console.log("");
|
|
233
|
+
return ok;
|
|
234
|
+
}
|
|
235
|
+
async function main() {
|
|
236
|
+
const args = process.argv.slice(2);
|
|
237
|
+
if (args.length === 0 || args[0] === "--help") {
|
|
238
|
+
printUsage();
|
|
239
|
+
process.exit(0);
|
|
240
|
+
}
|
|
241
|
+
if (args[0] === "--version") {
|
|
242
|
+
printVersion();
|
|
243
|
+
process.exit(0);
|
|
244
|
+
}
|
|
245
|
+
const command = args[0];
|
|
246
|
+
const follow = !args.includes("--no-follow");
|
|
247
|
+
const filePath = args.slice(1).find((a) => !a.startsWith("--"));
|
|
248
|
+
if (!filePath) {
|
|
249
|
+
console.error(`${colors.red}Error: File path required${colors.reset}
|
|
250
|
+
`);
|
|
251
|
+
printUsage();
|
|
252
|
+
process.exit(1);
|
|
253
|
+
}
|
|
254
|
+
if (command !== "validate" && command !== "diagnose") {
|
|
255
|
+
console.error(`${colors.red}Error: Unknown command '${command}'${colors.reset}
|
|
256
|
+
`);
|
|
257
|
+
console.error(`Supported commands: ${colors.cyan}validate${colors.reset}, ${colors.cyan}diagnose${colors.reset}
|
|
258
|
+
`);
|
|
259
|
+
process.exit(1);
|
|
260
|
+
}
|
|
261
|
+
let data;
|
|
262
|
+
if (/^https?:\/\//i.test(filePath)) {
|
|
263
|
+
try {
|
|
264
|
+
data = await fetchJSON(filePath);
|
|
265
|
+
} catch (e) {
|
|
266
|
+
console.error(`${colors.red}\u2717 Couldn't read ${filePath}${colors.reset}: ${e instanceof Error ? e.message : String(e)}`);
|
|
267
|
+
process.exit(1);
|
|
268
|
+
}
|
|
269
|
+
} else data = loadJSON(filePath);
|
|
270
|
+
if (data?.["@type"] === "CitemapContentFile") {
|
|
271
|
+
const r = validateContentFile(data);
|
|
272
|
+
console.log(`
|
|
273
|
+
${r.valid ? colors.green + "\u2713 Valid" : colors.red + "\u2717 Invalid"}${colors.reset} content file ${colors.dim}(${r.contentType ?? "?"}, ${r.count} items)${colors.reset}`);
|
|
274
|
+
for (const e of r.errors) console.log(` ${colors.red}\u2717${colors.reset} ${e.path}: ${e.message}`);
|
|
275
|
+
for (const w of r.warnings) console.log(` ${colors.yellow}\u26A0${colors.reset} ${w.path}: ${w.message}`);
|
|
276
|
+
console.log("");
|
|
277
|
+
process.exit(r.valid ? 0 : 1);
|
|
278
|
+
}
|
|
279
|
+
const valid = command === "validate" ? formatValidationOutput(validate(data)) : formatDiagnosticOutput(data);
|
|
280
|
+
const filesOk = follow ? await checkContentFiles(data) : true;
|
|
281
|
+
process.exit(valid && filesOk ? 0 : 1);
|
|
282
|
+
}
|
|
283
|
+
main().catch((error) => {
|
|
284
|
+
console.error(`${colors.red}Fatal error:${colors.reset}`, error instanceof Error ? error.message : String(error));
|
|
285
|
+
process.exit(1);
|
|
286
|
+
});
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@citemap/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Command-line tool for validating citemap.json files",
|
|
5
|
-
"type": "module",
|
|
6
5
|
"bin": {
|
|
7
|
-
"citemap": "
|
|
6
|
+
"citemap": "bin/citemap.js"
|
|
8
7
|
},
|
|
9
8
|
"files": [
|
|
10
|
-
"dist"
|
|
9
|
+
"dist",
|
|
10
|
+
"bin"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
13
|
"build": "tsup src/index.ts --format esm --no-dts --out-dir dist",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"node": ">=18.0.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@citemap/validator": "^0.
|
|
33
|
+
"@citemap/validator": "^0.4.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^25.5.0",
|