citedwise 0.1.7 → 0.1.8
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/cli.js +40 -7
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
// audits run on the server, are stored, and come back with a shareable report link.
|
|
4
4
|
// Only type imports from the rest of the codebase (plus the dependency-free product.ts),
|
|
5
5
|
// so the published package stays tiny.
|
|
6
|
+
import { realpathSync } from 'node:fs';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
6
8
|
import { parseArgs } from 'node:util';
|
|
7
9
|
import { PRODUCT, PRODUCT_URL } from './product.js';
|
|
8
10
|
const DEFAULT_API = PRODUCT_URL;
|
|
@@ -34,6 +36,20 @@ const c = (code) => (s) => (tty ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
|
34
36
|
const green = c('32'), yellow = c('33'), red = c('31'), dim = c('2'), bold = c('1'), cyan = c('36');
|
|
35
37
|
const ICON = { pass: green('✔'), warn: yellow('▲'), fail: red('✖'), info: dim('ℹ') };
|
|
36
38
|
const gradeColor = (score) => (score >= 80 ? green : score >= 65 ? yellow : red);
|
|
39
|
+
// Control characters (C0 except tab and newline, DEL, C1) and bidirectional overrides. Report text
|
|
40
|
+
// comes from the audited page; in a terminal, ESC and BEL sequences in it could move the cursor,
|
|
41
|
+
// repaint the score line, plant hyperlinks or write the clipboard.
|
|
42
|
+
const UNSAFE_TERMINAL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/g;
|
|
43
|
+
/** Every string in an API result made inert for a terminal (`?` for each control character). */
|
|
44
|
+
export function terminalSafe(v) {
|
|
45
|
+
if (typeof v === 'string')
|
|
46
|
+
return v.replace(UNSAFE_TERMINAL, '?');
|
|
47
|
+
if (Array.isArray(v))
|
|
48
|
+
return v.map(terminalSafe);
|
|
49
|
+
if (v && typeof v === 'object')
|
|
50
|
+
return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, terminalSafe(x)]));
|
|
51
|
+
return v;
|
|
52
|
+
}
|
|
37
53
|
export class ApiError extends Error {
|
|
38
54
|
status;
|
|
39
55
|
code;
|
|
@@ -113,6 +129,7 @@ export class Client {
|
|
|
113
129
|
}
|
|
114
130
|
}
|
|
115
131
|
const write = (s) => process.stdout.write(s);
|
|
132
|
+
const writeErr = (s) => process.stderr.write(terminalSafe(s));
|
|
116
133
|
async function main(argv, env = process.env) {
|
|
117
134
|
const { values, positionals } = parseArgs({
|
|
118
135
|
args: argv,
|
|
@@ -156,14 +173,15 @@ async function main(argv, env = process.env) {
|
|
|
156
173
|
const [cmd, arg] = positionals;
|
|
157
174
|
if (cmd === 'whoami') {
|
|
158
175
|
const me = await client.call('GET', '/api/v1/me');
|
|
159
|
-
|
|
176
|
+
const m = terminalSafe(me);
|
|
177
|
+
write(values.json ? JSON.stringify(me, null, 2) + '\n' : `${m.email}: ${m.plan} plan, ${m.usage.used}/${m.usage.limit} audits this ${m.usage.period}\n`);
|
|
160
178
|
return 0;
|
|
161
179
|
}
|
|
162
180
|
if (cmd === 'llms-txt') {
|
|
163
181
|
if (!arg)
|
|
164
182
|
return usage(`${BIN} llms-txt <url>`);
|
|
165
183
|
const r = await client.call('POST', '/api/v1/llms-txt', { url: arg });
|
|
166
|
-
write(values.json ? JSON.stringify(r, null, 2) + '\n' : r.llmsTxt);
|
|
184
|
+
write(values.json ? JSON.stringify(r, null, 2) + '\n' : terminalSafe(r.llmsTxt));
|
|
167
185
|
return 0;
|
|
168
186
|
}
|
|
169
187
|
if (cmd === 'site') {
|
|
@@ -185,9 +203,10 @@ async function main(argv, env = process.env) {
|
|
|
185
203
|
if (values.json)
|
|
186
204
|
write(JSON.stringify(s, null, 2) + '\n');
|
|
187
205
|
else if (s.status === 'failed')
|
|
188
|
-
|
|
206
|
+
writeErr(`${BIN}: ${s.error}\n`);
|
|
189
207
|
else {
|
|
190
|
-
|
|
208
|
+
s = terminalSafe(s);
|
|
209
|
+
write(`\n${bold('Whole-site AI readiness')} ${dim(terminalSafe(arg))}\n\n Average score ${gradeColor(s.score ?? 0)(bold(`${s.score}/100`))} across ${s.summary?.pagesAudited} pages\n\n`);
|
|
191
210
|
for (const i of s.summary?.issues.slice(0, 8) ?? [])
|
|
192
211
|
write(` ${i.fail ? ICON.fail : ICON.warn} ${i.title} ${dim(`— ${i.fail} failing, ${i.warn} to improve`)}\n`);
|
|
193
212
|
write(`\n Full report: ${s.reportUrl}\n\n`);
|
|
@@ -197,16 +216,30 @@ async function main(argv, env = process.env) {
|
|
|
197
216
|
return gate(s.score);
|
|
198
217
|
}
|
|
199
218
|
const r = await client.call('POST', '/api/v1/audits', { url: cmd, fresh: values.fresh });
|
|
200
|
-
write(values.json ? JSON.stringify(r, null, 2) + '\n' : renderText(r.report, r.fixes, r.reportUrl));
|
|
219
|
+
write(values.json ? JSON.stringify(r, null, 2) + '\n' : renderText(terminalSafe(r.report), terminalSafe(r.fixes), terminalSafe(r.reportUrl)));
|
|
201
220
|
return gate(r.report.score);
|
|
202
221
|
}
|
|
203
222
|
function usage(text) {
|
|
204
223
|
process.stderr.write(`Usage: ${text}\n`);
|
|
205
224
|
return 2;
|
|
206
225
|
}
|
|
207
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Run when executed, not when imported (tests). `import.meta.main` would be simpler, but it is
|
|
228
|
+
* missing on Node before 22.18 and on 23.x and 24.0–24.1, where the CLI then did nothing and
|
|
229
|
+
* exited 0, so a `--fail-under` gate passed without auditing (Strix vuln-0002). npx runs the
|
|
230
|
+
* CLI through a symlink in node_modules/.bin, hence realpath.
|
|
231
|
+
*/
|
|
232
|
+
function isEntryPoint() {
|
|
233
|
+
try {
|
|
234
|
+
return !!process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (isEntryPoint()) {
|
|
208
241
|
main(process.argv.slice(2)).then((code) => process.exit(code), (err) => {
|
|
209
|
-
|
|
242
|
+
writeErr(`${BIN}: ${err.message}\n`);
|
|
210
243
|
const auth = err instanceof ApiError && [401, 403].includes(err.status);
|
|
211
244
|
const quota = err instanceof ApiError && err.code === 'quota_exceeded';
|
|
212
245
|
process.exit(auth || quota ? 4 : 3);
|