@barqjs/cli 0.0.0-stage → 0.2.0-next.1
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/LICENSE +21 -0
- package/README.md +128 -2
- package/dist/index.d.ts +104 -0
- package/dist/index.js +486 -0
- package/package.json +66 -4
- package/src/check.ts +518 -0
- package/src/index.ts +212 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { stdout } from "node:process";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
7
|
+
// src/check.ts
|
|
8
|
+
/**
|
|
9
|
+
* `barq check`: every file of a project through the compiler's diagnostics,
|
|
10
|
+
* with no build and no dev server.
|
|
11
|
+
*
|
|
12
|
+
* The findings are the ones the Vite plugin reports, because they come from the
|
|
13
|
+
* same `transform`. What this adds is one answer for a whole project, a JSON
|
|
14
|
+
* form a program can read, and the fixes applied for the findings that carry
|
|
15
|
+
* one.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* What a project sets for every run, under `barq.check` in its `package.json`.
|
|
19
|
+
* A flag on the command line replaces the field of the same name.
|
|
20
|
+
*/
|
|
21
|
+
function projectOptions(root) {
|
|
22
|
+
let manifest;
|
|
23
|
+
try {
|
|
24
|
+
manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
25
|
+
} catch {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
const set = manifest.barq?.check;
|
|
29
|
+
if (typeof set !== "object" || set === null) return {};
|
|
30
|
+
const { paths, routes, extraRoutes, levels } = set;
|
|
31
|
+
const strings = (value) => Array.isArray(value) && value.every((item) => typeof item === "string") ? value : void 0;
|
|
32
|
+
const out = {};
|
|
33
|
+
const listed = strings(paths);
|
|
34
|
+
if (listed !== void 0) out.paths = listed;
|
|
35
|
+
if (typeof routes === "string") out.routes = routes;
|
|
36
|
+
const extra = strings(extraRoutes);
|
|
37
|
+
if (extra !== void 0) out.extraRoutes = extra;
|
|
38
|
+
if (typeof levels === "object" && levels !== null) out.levels = Object.entries(levels).filter((entry) => typeof entry[1] === "string");
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
let native;
|
|
42
|
+
function compiler() {
|
|
43
|
+
native ??= createRequire(import.meta.url)("@barqjs/compiler");
|
|
44
|
+
return native;
|
|
45
|
+
}
|
|
46
|
+
const SKILL = "node_modules/@barqjs/compiler/skills/compiler-diagnostics/SKILL.md";
|
|
47
|
+
const JSX = /\.[jt]sx$/;
|
|
48
|
+
const SCRIPT = /\.[cm]?[jt]s$/;
|
|
49
|
+
const SKIPPED = /* @__PURE__ */ new Set([
|
|
50
|
+
"node_modules",
|
|
51
|
+
"dist",
|
|
52
|
+
"build",
|
|
53
|
+
"coverage",
|
|
54
|
+
".git",
|
|
55
|
+
".vite",
|
|
56
|
+
".output"
|
|
57
|
+
]);
|
|
58
|
+
/** A fix can uncover the next one, such as a call inside a call. */
|
|
59
|
+
const MAX_PASSES = 5;
|
|
60
|
+
const NAMED_IMPORT = /import\s*\{([^}]*)\}\s*from\s*["']([^"']+)["']/g;
|
|
61
|
+
/**
|
|
62
|
+
* Whether `code` calls a binding it imports the way a `layer` binding is
|
|
63
|
+
* called, which makes it a module the build compiles though it never names
|
|
64
|
+
* barq. The same guess as `@barqjs/vite`'s, so both compile the same files.
|
|
65
|
+
*/
|
|
66
|
+
function callsThroughImport(code) {
|
|
67
|
+
for (const [, names, from] of code.matchAll(NAMED_IMPORT)) {
|
|
68
|
+
if (from === "@barqjs/css" || names === void 0) continue;
|
|
69
|
+
for (const entry of names.split(",")) {
|
|
70
|
+
const local = entry.trim().replace(/^type\s+/, "").split(/\s+as\s+/).pop()?.trim();
|
|
71
|
+
if (local === void 0 || !/^[A-Za-z_$][\w$]*$/.test(local)) continue;
|
|
72
|
+
if (new RegExp(`(?<![\\w$.])${local.replace(/\$/g, "\\$")}(?:\\.dynamic\\(|(?:\\.(?:create|props))?\\([^()]*\\{)`).test(code)) return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
/** A script with no JSX is compiled only when it names barq, as the build does. */
|
|
78
|
+
function wanted(path, text) {
|
|
79
|
+
if (path.endsWith(".d.ts")) return false;
|
|
80
|
+
if (JSX.test(path)) return true;
|
|
81
|
+
return SCRIPT.test(path) && (text().includes("@barqjs/") || callsThroughImport(text()));
|
|
82
|
+
}
|
|
83
|
+
const EXTENSIONS = [
|
|
84
|
+
"",
|
|
85
|
+
".ts",
|
|
86
|
+
".tsx",
|
|
87
|
+
".js",
|
|
88
|
+
".jsx",
|
|
89
|
+
".mts",
|
|
90
|
+
"/index.ts",
|
|
91
|
+
"/index.tsx",
|
|
92
|
+
"/index.js"
|
|
93
|
+
];
|
|
94
|
+
/** A relative import as the file it names, or `undefined` for a package. */
|
|
95
|
+
function resolveImport(from, specifier) {
|
|
96
|
+
if (!specifier.startsWith(".")) return void 0;
|
|
97
|
+
const base = resolve(dirname(from), specifier);
|
|
98
|
+
for (const extension of EXTENSIONS) {
|
|
99
|
+
const candidate = base + extension;
|
|
100
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function collect(path, out) {
|
|
104
|
+
if (statSync(path).isDirectory()) {
|
|
105
|
+
for (const entry of readdirSync(path).toSorted()) {
|
|
106
|
+
if (SKIPPED.has(entry) || entry.startsWith(".")) continue;
|
|
107
|
+
collect(join(path, entry), out);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (JSX.test(path) || SCRIPT.test(path)) out.push(path);
|
|
112
|
+
}
|
|
113
|
+
function severityOf(level) {
|
|
114
|
+
return level === "error" ? "error" : level === "note" ? "note" : "warning";
|
|
115
|
+
}
|
|
116
|
+
/** How many modules deep a folded value is followed, as `@barqjs/vite` does. */
|
|
117
|
+
const IMPORT_DEPTH = 6;
|
|
118
|
+
/**
|
|
119
|
+
* The transform the build runs for `file`: compiled again with the values it
|
|
120
|
+
* asked for from the files it imports, until a round learns nothing, so a call
|
|
121
|
+
* through an imported `layer` binding or token folds, and declines, as it does
|
|
122
|
+
* in the build.
|
|
123
|
+
*/
|
|
124
|
+
function transformWithImports(file, source, options, project, depth, seen) {
|
|
125
|
+
let out = compiler().transform(source, options);
|
|
126
|
+
if (depth <= 0) return out;
|
|
127
|
+
const cssImports = [];
|
|
128
|
+
const asked = /* @__PURE__ */ new Set();
|
|
129
|
+
for (let round = 0; round < 4; round++) {
|
|
130
|
+
const fresh = (out.cssWanted ?? []).filter(([specifier, name]) => !asked.has(`${specifier}\u0000${name}`));
|
|
131
|
+
if (fresh.length === 0) break;
|
|
132
|
+
let learned = false;
|
|
133
|
+
for (const [specifier = "", name = ""] of fresh) {
|
|
134
|
+
asked.add(`${specifier}\u0000${name}`);
|
|
135
|
+
const dependency = resolveImport(file, specifier);
|
|
136
|
+
if (dependency === void 0) continue;
|
|
137
|
+
for (const row of exportsOf(dependency, project, depth - 1, new Set(seen))) {
|
|
138
|
+
if (row[0] !== name) continue;
|
|
139
|
+
cssImports.push([specifier, ...row]);
|
|
140
|
+
learned = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!learned) break;
|
|
144
|
+
out = compiler().transform(source, {
|
|
145
|
+
...options,
|
|
146
|
+
cssImports
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
function exportsOf(file, project, depth, seen) {
|
|
152
|
+
if (depth <= 0 || seen.has(file)) return [];
|
|
153
|
+
seen.add(file);
|
|
154
|
+
const known = project.exports.get(file);
|
|
155
|
+
if (known !== void 0) return known;
|
|
156
|
+
let rows = [];
|
|
157
|
+
try {
|
|
158
|
+
rows = transformWithImports(file, readFileSync(file, "utf8"), {
|
|
159
|
+
filename: file,
|
|
160
|
+
root: dirname(file),
|
|
161
|
+
dev: true,
|
|
162
|
+
diagnostics: false,
|
|
163
|
+
sourceNames: false,
|
|
164
|
+
cssExports: true
|
|
165
|
+
}, project, depth, seen).cssExports ?? [];
|
|
166
|
+
} catch {
|
|
167
|
+
rows = [];
|
|
168
|
+
}
|
|
169
|
+
project.exports.set(file, rows);
|
|
170
|
+
return rows;
|
|
171
|
+
}
|
|
172
|
+
/** A finding about the project and no one position in a file. */
|
|
173
|
+
function about(file, severity, message) {
|
|
174
|
+
return {
|
|
175
|
+
severity,
|
|
176
|
+
message,
|
|
177
|
+
file,
|
|
178
|
+
line: 1,
|
|
179
|
+
column: 1,
|
|
180
|
+
endLine: 1,
|
|
181
|
+
endColumn: 1
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The file routes, read the way the build reads them and written nowhere: the
|
|
186
|
+
* patterns a link may name, and what the scan itself found wrong.
|
|
187
|
+
*/
|
|
188
|
+
function routesOf(root, dir) {
|
|
189
|
+
if (!existsSync(join(root, dir))) return { findings: [] };
|
|
190
|
+
const tree = compiler().routeTree(root, dir);
|
|
191
|
+
return {
|
|
192
|
+
patterns: tree.patterns,
|
|
193
|
+
findings: [...tree.warnings.map((warning) => about(dir, "warning", warning)), ...tree.mismatches.map((mismatch) => about(mismatch.file, "error", `\`createFileRoute("${mismatch.declared}")\` names an id that differs from the one this file's name gives, \`${mismatch.expected}\`, and a build refuses it. Write \`createFileRoute("${mismatch.expected}")\`.`))]
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function findingsOf(file, root, source, project) {
|
|
197
|
+
const { options } = project;
|
|
198
|
+
const name = relative(root, file).split(sep).join("/");
|
|
199
|
+
let diagnostics;
|
|
200
|
+
try {
|
|
201
|
+
diagnostics = transformWithImports(file, source, {
|
|
202
|
+
filename: file,
|
|
203
|
+
root,
|
|
204
|
+
dev: true,
|
|
205
|
+
diagnostics: true,
|
|
206
|
+
sourceNames: false,
|
|
207
|
+
checks: options.levels?.map(([code, level]) => [code, level]),
|
|
208
|
+
routes: project.patterns
|
|
209
|
+
}, project, IMPORT_DEPTH, /* @__PURE__ */ new Set([file])).diagnostics ?? [];
|
|
210
|
+
} catch (error) {
|
|
211
|
+
return [about(name, "error", error instanceof Error ? error.message : String(error))];
|
|
212
|
+
}
|
|
213
|
+
return diagnostics.map((found) => {
|
|
214
|
+
const finding = {
|
|
215
|
+
severity: severityOf(found.severity),
|
|
216
|
+
message: found.message,
|
|
217
|
+
file: name,
|
|
218
|
+
line: found.line,
|
|
219
|
+
column: found.column,
|
|
220
|
+
endLine: found.endLine,
|
|
221
|
+
endColumn: found.endColumn
|
|
222
|
+
};
|
|
223
|
+
if (found.code !== void 0) {
|
|
224
|
+
finding.code = found.code;
|
|
225
|
+
finding.skill = `${SKILL}#${found.code.toLowerCase()}`;
|
|
226
|
+
}
|
|
227
|
+
if (found.docs !== void 0) finding.docs = found.docs;
|
|
228
|
+
if (found.fix !== void 0 && found.fix.length > 0) finding.fix = found.fix;
|
|
229
|
+
return finding;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* `source` with every fix applied that does not touch another's span, and how
|
|
234
|
+
* many findings that repaired. Last edit first, so an offset stays valid.
|
|
235
|
+
*/
|
|
236
|
+
function applyFixes(source, findings) {
|
|
237
|
+
const chosen = [];
|
|
238
|
+
let repaired = 0;
|
|
239
|
+
for (const finding of findings) {
|
|
240
|
+
const edits = finding.fix;
|
|
241
|
+
if (edits === void 0) continue;
|
|
242
|
+
const clash = edits.some((edit) => chosen.some((taken) => edit.pos < taken.end && taken.pos < edit.end));
|
|
243
|
+
const repeated = edits.every((edit) => chosen.some((taken) => taken.pos === edit.pos && taken.end === edit.end && taken.text === edit.text));
|
|
244
|
+
if (clash || repeated) continue;
|
|
245
|
+
chosen.push(...edits);
|
|
246
|
+
repaired++;
|
|
247
|
+
}
|
|
248
|
+
let text = source;
|
|
249
|
+
for (const edit of chosen.toSorted((a, b) => b.pos - a.pos)) text = text.slice(0, edit.pos) + edit.text + text.slice(edit.end);
|
|
250
|
+
return [text, repaired];
|
|
251
|
+
}
|
|
252
|
+
function check(options) {
|
|
253
|
+
const root = resolve(options.root);
|
|
254
|
+
const files = [];
|
|
255
|
+
for (const path of options.paths === void 0 || options.paths.length === 0 ? ["."] : options.paths) collect(resolve(root, path), files);
|
|
256
|
+
const routes = routesOf(root, options.routes ?? "src/routes");
|
|
257
|
+
const project = {
|
|
258
|
+
options,
|
|
259
|
+
exports: /* @__PURE__ */ new Map(),
|
|
260
|
+
patterns: routes.patterns === void 0 ? void 0 : [...routes.patterns, ...options.extraRoutes ?? []]
|
|
261
|
+
};
|
|
262
|
+
const findings = [...routes.findings];
|
|
263
|
+
let checked = 0;
|
|
264
|
+
let fixed = 0;
|
|
265
|
+
for (const file of files) {
|
|
266
|
+
let source;
|
|
267
|
+
const text = () => source ??= readFileSync(file, "utf8");
|
|
268
|
+
if (!wanted(file, text)) continue;
|
|
269
|
+
checked++;
|
|
270
|
+
let found = findingsOf(file, root, text(), project);
|
|
271
|
+
if (options.fix === true) {
|
|
272
|
+
let current = text();
|
|
273
|
+
for (let pass = 0; pass < MAX_PASSES; pass++) {
|
|
274
|
+
const [next, repaired] = applyFixes(current, found);
|
|
275
|
+
if (repaired === 0) break;
|
|
276
|
+
current = next;
|
|
277
|
+
fixed += repaired;
|
|
278
|
+
found = findingsOf(file, root, current, project);
|
|
279
|
+
}
|
|
280
|
+
if (current !== text()) writeFileSync(file, current);
|
|
281
|
+
}
|
|
282
|
+
findings.push(...found);
|
|
283
|
+
}
|
|
284
|
+
const count = (severity) => findings.filter((finding) => finding.severity === severity).length;
|
|
285
|
+
return {
|
|
286
|
+
version: 1,
|
|
287
|
+
root,
|
|
288
|
+
files: checked,
|
|
289
|
+
summary: {
|
|
290
|
+
errors: count("error"),
|
|
291
|
+
warnings: count("warning"),
|
|
292
|
+
notes: count("note"),
|
|
293
|
+
fixable: findings.filter((finding) => finding.fix !== void 0).length,
|
|
294
|
+
fixed
|
|
295
|
+
},
|
|
296
|
+
findings
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/** Every code the installed compiler can raise, with its default level. */
|
|
300
|
+
function codes() {
|
|
301
|
+
return compiler().diagnosticCodes().map((entry) => ({
|
|
302
|
+
...entry,
|
|
303
|
+
skill: `${SKILL}#${entry.code.toLowerCase()}`
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
307
|
+
/** The result for a person: one finding per block, then the count. */
|
|
308
|
+
function formatText(result, paint) {
|
|
309
|
+
const lines = [];
|
|
310
|
+
for (const finding of result.findings) {
|
|
311
|
+
const at = `${finding.file}:${finding.line}:${finding.column}`;
|
|
312
|
+
const label = paint[finding.severity](finding.severity);
|
|
313
|
+
const code = finding.code === void 0 ? "" : ` ${paint.bold(finding.code)}`;
|
|
314
|
+
lines.push(`${at} ${label}${code} ${finding.message}`);
|
|
315
|
+
if (finding.docs !== void 0) lines.push(paint.dim(` docs: ${finding.docs}`));
|
|
316
|
+
if (finding.fix !== void 0) lines.push(paint.dim(" fix: `barq check --fix` repairs this"));
|
|
317
|
+
lines.push("");
|
|
318
|
+
}
|
|
319
|
+
const { errors, warnings, notes, fixable, fixed } = result.summary;
|
|
320
|
+
let summary = `${[
|
|
321
|
+
plural(errors, "error"),
|
|
322
|
+
plural(warnings, "warning"),
|
|
323
|
+
plural(notes, "note")
|
|
324
|
+
].join(", ")} in ${plural(result.files, "file")}`;
|
|
325
|
+
if (fixed > 0) summary += `, ${fixed} fixed`;
|
|
326
|
+
if (fixable > 0) summary += `, ${fixable} more fixable with \`--fix\``;
|
|
327
|
+
lines.push(summary);
|
|
328
|
+
if (result.findings.some((finding) => finding.code !== void 0)) lines.push(paint.dim(`repair guide: ${SKILL}`));
|
|
329
|
+
return lines.join("\n");
|
|
330
|
+
}
|
|
331
|
+
// src/index.ts
|
|
332
|
+
/**
|
|
333
|
+
* `barq`: the command line of the framework.
|
|
334
|
+
*
|
|
335
|
+
* No dependencies beyond the compiler it drives. The argument parsing is
|
|
336
|
+
* smaller than a library that would do it.
|
|
337
|
+
*/
|
|
338
|
+
const HELP = `\
|
|
339
|
+
barq check [paths...] Report the compiler's findings for a project
|
|
340
|
+
barq codes List every code the compiler can raise
|
|
341
|
+
|
|
342
|
+
check options:
|
|
343
|
+
--json One JSON document on stdout, for a program to read
|
|
344
|
+
--fix Apply the fixes the findings carry, then report what is left
|
|
345
|
+
--root <dir> The project root (default: the current directory)
|
|
346
|
+
--routes <dir> The file routes, for checking links (default: src/routes)
|
|
347
|
+
--level <CODE=level> Set a code to suppress, note, warning or error; repeatable
|
|
348
|
+
--max-warnings <n> Exit 1 when there are more warnings than this
|
|
349
|
+
|
|
350
|
+
codes options:
|
|
351
|
+
--json The list as JSON
|
|
352
|
+
|
|
353
|
+
A project sets its own defaults under "barq": { "check": { ... } } in
|
|
354
|
+
package.json: paths, routes, extraRoutes (patterns beside the file routes) and
|
|
355
|
+
levels ({ "BARQ004": "error" }).
|
|
356
|
+
|
|
357
|
+
Exit status: 0 clean, 1 findings at error level or past --max-warnings, 2 a
|
|
358
|
+
command line this does not understand.
|
|
359
|
+
`;
|
|
360
|
+
const LEVELS = /* @__PURE__ */ new Set([
|
|
361
|
+
"suppress",
|
|
362
|
+
"note",
|
|
363
|
+
"warning",
|
|
364
|
+
"error"
|
|
365
|
+
]);
|
|
366
|
+
var UsageError = class extends Error {};
|
|
367
|
+
function parse(argv) {
|
|
368
|
+
const parsed = {
|
|
369
|
+
command: void 0,
|
|
370
|
+
paths: [],
|
|
371
|
+
json: false,
|
|
372
|
+
fix: false,
|
|
373
|
+
root: process.cwd(),
|
|
374
|
+
routes: void 0,
|
|
375
|
+
levels: [],
|
|
376
|
+
maxWarnings: void 0,
|
|
377
|
+
help: false
|
|
378
|
+
};
|
|
379
|
+
const value = (flag, index) => {
|
|
380
|
+
const next = argv[index];
|
|
381
|
+
if (next === void 0 || next.startsWith("--")) throw new UsageError(`\`${flag}\` takes a value, and none follows it.`);
|
|
382
|
+
return next;
|
|
383
|
+
};
|
|
384
|
+
for (let index = 0; index < argv.length; index++) {
|
|
385
|
+
const arg = argv[index];
|
|
386
|
+
switch (arg) {
|
|
387
|
+
case "--help":
|
|
388
|
+
case "-h":
|
|
389
|
+
parsed.help = true;
|
|
390
|
+
break;
|
|
391
|
+
case "--json":
|
|
392
|
+
parsed.json = true;
|
|
393
|
+
break;
|
|
394
|
+
case "--fix":
|
|
395
|
+
parsed.fix = true;
|
|
396
|
+
break;
|
|
397
|
+
case "--root":
|
|
398
|
+
parsed.root = value(arg, ++index);
|
|
399
|
+
break;
|
|
400
|
+
case "--routes":
|
|
401
|
+
parsed.routes = value(arg, ++index);
|
|
402
|
+
break;
|
|
403
|
+
case "--level": {
|
|
404
|
+
const pair = value(arg, ++index);
|
|
405
|
+
const [code, level] = pair.split("=");
|
|
406
|
+
if (code === void 0 || level === void 0 || !LEVELS.has(level)) throw new UsageError(`\`--level ${pair}\` is not \`CODE=level\`, where level is one of suppress, note, warning, error. Write it as \`--level BARQ004=error\`.`);
|
|
407
|
+
parsed.levels.push([code.toUpperCase(), level]);
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "--max-warnings": {
|
|
411
|
+
const text = value(arg, ++index);
|
|
412
|
+
const count = Number(text);
|
|
413
|
+
if (!Number.isInteger(count) || count < 0) throw new UsageError(`\`--max-warnings ${text}\` is not a whole number. Write it as \`--max-warnings 0\`.`);
|
|
414
|
+
parsed.maxWarnings = count;
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
default:
|
|
418
|
+
if (arg.startsWith("-")) throw new UsageError(`\`${arg}\` is not an option of barq. Run \`barq --help\`.`);
|
|
419
|
+
if (parsed.command === void 0) parsed.command = arg;
|
|
420
|
+
else parsed.paths.push(arg);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return parsed;
|
|
424
|
+
}
|
|
425
|
+
function painter() {
|
|
426
|
+
const styled = stdout.isTTY && process.env["NO_COLOR"] === void 0;
|
|
427
|
+
const code = (open) => (text) => styled ? `\u001B[${open}m${text}\u001B[0m` : text;
|
|
428
|
+
return {
|
|
429
|
+
error: code("31"),
|
|
430
|
+
warning: code("33"),
|
|
431
|
+
note: code("36"),
|
|
432
|
+
dim: code("2"),
|
|
433
|
+
bold: code("1")
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/** Run one command line and answer with the exit status. */
|
|
437
|
+
function main(argv, write) {
|
|
438
|
+
let parsed;
|
|
439
|
+
try {
|
|
440
|
+
parsed = parse(argv);
|
|
441
|
+
} catch (error) {
|
|
442
|
+
if (!(error instanceof UsageError)) throw error;
|
|
443
|
+
write(`${error.message}\n`);
|
|
444
|
+
return 2;
|
|
445
|
+
}
|
|
446
|
+
if (parsed.help || parsed.command === void 0) {
|
|
447
|
+
write(HELP);
|
|
448
|
+
return parsed.help ? 0 : 2;
|
|
449
|
+
}
|
|
450
|
+
switch (parsed.command) {
|
|
451
|
+
case "check": {
|
|
452
|
+
const set = projectOptions(parsed.root);
|
|
453
|
+
const result = check({
|
|
454
|
+
...set,
|
|
455
|
+
root: parsed.root,
|
|
456
|
+
fix: parsed.fix,
|
|
457
|
+
levels: [...set.levels ?? [], ...parsed.levels],
|
|
458
|
+
...parsed.paths.length > 0 ? { paths: parsed.paths } : {},
|
|
459
|
+
...parsed.routes !== void 0 ? { routes: parsed.routes } : {}
|
|
460
|
+
});
|
|
461
|
+
write(parsed.json ? `${JSON.stringify(result, null, 2)}\n` : `${formatText(result, painter())}\n`);
|
|
462
|
+
const { errors, warnings } = result.summary;
|
|
463
|
+
const over = parsed.maxWarnings !== void 0 && warnings > parsed.maxWarnings;
|
|
464
|
+
return errors > 0 || over ? 1 : 0;
|
|
465
|
+
}
|
|
466
|
+
case "codes": {
|
|
467
|
+
const list = codes();
|
|
468
|
+
write(parsed.json ? `${JSON.stringify(list, null, 2)}\n` : `${list.map((entry) => `${entry.code} ${entry.level.padEnd(7)} ${entry.summary}`).join("\n")}\n`);
|
|
469
|
+
return 0;
|
|
470
|
+
}
|
|
471
|
+
default:
|
|
472
|
+
write(`\`${parsed.command}\` is not a command of barq. Run \`barq --help\`.\n`);
|
|
473
|
+
return 2;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* The guard compares the script node was asked to run with this module rather
|
|
478
|
+
* than reading `import.meta.main`, which node defines only from 24.2. `argv[1]`
|
|
479
|
+
* is the `.bin` symlink under a package manager, hence the realpath.
|
|
480
|
+
*/
|
|
481
|
+
function invokedDirectly() {
|
|
482
|
+
const script = process.argv[1];
|
|
483
|
+
return script !== void 0 && realpathSync(script) === fileURLToPath(import.meta.url);
|
|
484
|
+
}
|
|
485
|
+
if (invokedDirectly()) process.exitCode = main(process.argv.slice(2), (text) => stdout.write(text));
|
|
486
|
+
export { applyFixes, check, codes, formatText, main, parse, projectOptions };
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barqjs/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0-next.1",
|
|
4
|
+
"description": "The barq command: check a project against the compiler's diagnostics, as text or JSON, and apply the fixes it offers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agents",
|
|
7
|
+
"barq",
|
|
8
|
+
"check",
|
|
9
|
+
"cli",
|
|
10
|
+
"diagnostics",
|
|
11
|
+
"lint"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/salamaashoush/barq#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/salamaashoush/barq/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/salamaashoush/barq.git",
|
|
21
|
+
"directory": "packages/cli"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"src",
|
|
26
|
+
"!src/**/*.test.ts",
|
|
27
|
+
"!src/**/*.test.tsx",
|
|
28
|
+
"!src/**/test-*.ts",
|
|
29
|
+
"!src/**/*-harness.ts",
|
|
30
|
+
"!src/**/*-probe.ts",
|
|
31
|
+
"!src/**/fixtures",
|
|
32
|
+
"!src/**/fixtures.ts",
|
|
33
|
+
"!src/**/ssr-fixtures.ts"
|
|
34
|
+
],
|
|
35
|
+
"type": "module",
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"bin": {
|
|
38
|
+
"barq": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"bun": "./src/index.ts",
|
|
44
|
+
"import": "./dist/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsdown",
|
|
50
|
+
"dev": "tsdown --watch",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "bun test --timeout 60000"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@barqjs/compiler": "0.2.0-next.1"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@types/bun": "^1.4.4",
|
|
59
|
+
"typescript": "^7.0.2"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=20.19.0"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public",
|
|
66
|
+
"provenance": true
|
|
67
|
+
}
|
|
68
|
+
}
|