Commit acf636ed by PLN (Algolia)

cli: buvard fix — dry run by default, --write, --json; subcommand tokens no…

cli: buvard fix — dry run by default, --write, --json; subcommand tokens no longer collected as targets
parent d2af3b0b
...@@ -4,6 +4,12 @@ ...@@ -4,6 +4,12 @@
* *
* buvard check [files|dirs] [--json] [--hidden] [--select ids] [--ignore ids] * buvard check [files|dirs] [--json] [--hidden] [--select ids] [--ignore ids]
* buvard score <file> [--json] single-file score, machine-readable * buvard score <file> [--json] single-file score, machine-readable
* buvard fix <file> [--json] [--write] flags → suggestions via a LOCAL
* model (LM Studio at 127.0.0.1:1234
* by default; BUVARD_FIX_URL for
* another local endpoint — text
* never leaves the machine). Dry
* run by default.
* buvard --help | --version * buvard --help | --version
* *
* Scoring starts at 100; each hit costs its weight (lexique 5, structure 8, * Scoring starts at 100; each hit costs its weight (lexique 5, structure 8,
...@@ -27,7 +33,16 @@ ...@@ -27,7 +33,16 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { CATALOG_VERSION, lint, bodyOf, measuresOf, filterMeasures } from '../lib/index.mjs'; import {
CATALOG_VERSION,
lint,
bodyOf,
measuresOf,
filterMeasures,
fixText,
diffLines,
FixError,
} from '../lib/index.mjs';
const TEXT_EXT = /\.(md|txt|mdx)$/i; const TEXT_EXT = /\.(md|txt|mdx)$/i;
const PKG_VERSION = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version; const PKG_VERSION = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
...@@ -37,6 +52,7 @@ const USAGE = `buvard — l'anti-slop qui tache. Blotting paper for AI-flavored ...@@ -37,6 +52,7 @@ const USAGE = `buvard — l'anti-slop qui tache. Blotting paper for AI-flavored
Usage: Usage:
buvard check [files|dirs...] [--json] [--hidden] lint markdown/text files buvard check [files|dirs...] [--json] [--hidden] lint markdown/text files
buvard score <file> [--json] single-file score buvard score <file> [--json] single-file score
buvard fix <file> [--json] [--write] suggestions via a LOCAL model
buvard --help this help buvard --help this help
buvard --version package version buvard --version package version
...@@ -111,6 +127,10 @@ function cadenceLine(selected, full) { ...@@ -111,6 +127,10 @@ function cadenceLine(selected, full) {
} }
function main(argv) { function main(argv) {
// `fix` a sa propre surface de flags (--write), parsée dans fixMain ;
// tout le reste passe par la boucle durcie ci-dessous.
if (argv[0] === 'fix') return fixMain(argv.slice(1));
const flags = new Set(); const flags = new Set();
const select = []; const select = [];
const ignore = []; const ignore = [];
...@@ -199,6 +219,106 @@ function main(argv) { ...@@ -199,6 +219,106 @@ function main(argv) {
return 0; return 0;
} }
const STATUS_TAG = { appliquée: 'appliquée ', KEEP: 'KEEP ', abandonnée: 'abandonnée ' };
function printFix(result, file, original) {
const rel = path.relative(process.cwd(), file);
console.log(`\n== buvard fix — ${rel} — ${result.before}/100 → ${result.after}/100`);
if (result.rounds.length === 0) {
console.log(' rien à signaler — le linter n\'a rien vu, la voix reste la sienne.');
return;
}
for (const r of result.rounds) console.log(` ${r.summary}`);
console.log('');
for (const r of result.rounds) {
for (const s of r.suggestions) {
console.log(` [${STATUS_TAG[s.status]}] [${s.rule}] « ${s.excerpt} » → « ${s.suggestion} »`);
if (s.why) console.log(` ${s.why}`);
if (s.reason) console.log(` (${s.reason})`);
}
}
if (result.changed) {
console.log('\n--- diff (dry-run — rien n\'est écrit ; --write pour appliquer)');
console.log(diffLines(original, result.text));
}
console.log(`\n---- buvard fix — modèle local : ${result.model} — ${result.flagsBefore} flags → ${result.flagsAfter} ----`);
}
const FIX_USAGE = 'usage: buvard fix <fichier> [--json] [--write] — un seul fichier à la fois';
async function fixMain(argv) {
const flags = new Set();
const targets = [];
for (const a of argv) {
if (a === '--json' || a === '--write') flags.add(a);
else if (a === '-h' || a === '--help') {
console.log(`${FIX_USAGE}\n\nAppel d'un modèle local (127.0.0.1:1234 par défaut, BUVARD_FIX_URL pour un autre) ; texte jamais sorti de la machine. Dry run par défaut — --write pour appliquer.`);
return 0;
} else if (a.startsWith('-')) {
console.error(`buvard fix: option inconnue "${a}"\n\n${FIX_USAGE}`);
return 2;
} else targets.push(a);
}
const json = flags.has('--json');
const write = flags.has('--write');
if (targets.length !== 1) {
console.error(FIX_USAGE);
return 2;
}
const file = path.resolve(targets[0]);
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
console.error(`buvard fix: pas de fichier pour "${targets[0]}"`);
return 2;
}
const original = fs.readFileSync(file, 'utf8');
let result;
try {
result = await fixText(original);
} catch (e) {
if (!(e instanceof FixError)) throw e;
if (json) console.log(JSON.stringify({ buvard: CATALOG_VERSION, error: e.message }, null, 1));
else console.error(`buvard fix: ${e.message}`);
return 1;
}
if (json) {
console.log(JSON.stringify({
buvard: CATALOG_VERSION,
fix: {
file,
model: result.model,
url: result.url,
before: result.before,
after: result.after,
flagsBefore: result.flagsBefore,
flagsAfter: result.flagsAfter,
changed: result.changed,
written: write && result.changed,
rounds: result.rounds,
diff: result.changed ? diffLines(original, result.text) : '',
},
}, null, 1));
} else {
printFix(result, file, original);
}
if (write && result.changed) {
fs.writeFileSync(file, result.text);
if (!json) console.log(` écrit : ${path.relative(process.cwd(), file)}`);
}
return 0;
}
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(import.meta.url.replace('file://', ''))) { if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(import.meta.url.replace('file://', ''))) {
process.exit(main(process.argv.slice(2))); const done = main(process.argv.slice(2));
if (done instanceof Promise) {
done
.then((code) => process.exit(code))
.catch((e) => {
console.error(`buvard fix: ${e.message}`);
process.exit(1);
});
} else {
process.exit(done);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment