Skip to content

Commit 1fcd818

Browse files
committed
Add flaws check / fix to cli
- add flaws check to validate - add flaws command
1 parent c2d9750 commit 1fcd818

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

build/flaws.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ async function fixFixableFlaws(doc, options, document) {
315315

316316
let newRawHTML = document.rawHTML;
317317

318+
const phrasing = options.fixFlawsDryRun ? "Would fix" : "Fixed";
319+
318320
const loud = options.fixFlawsDryRun || options.fixFlawsVerbose;
319321

320322
// Any 'macros' of type "MacroRedirectedLinkError"...
@@ -333,7 +335,7 @@ async function fixFixableFlaws(doc, options, document) {
333335
if (loud) {
334336
console.log(
335337
chalk.grey(
336-
`Fixed macro ${chalk.white.bold(
338+
`${phrasing} (${flaw.id}) macro ${chalk.white.bold(
337339
flaw.macroSource
338340
)} to ${chalk.white.bold(newMacroSource)}`
339341
)
@@ -357,7 +359,7 @@ async function fixFixableFlaws(doc, options, document) {
357359
if (loud) {
358360
console.log(
359361
chalk.grey(
360-
`Fixed broken_link ${chalk.white.bold(
362+
`${phrasing} (${flaw.id}) broken_link ${chalk.white.bold(
361363
flaw.href
362364
)} to ${chalk.white.bold(flaw.suggestion)}`
363365
)
@@ -379,7 +381,7 @@ async function fixFixableFlaws(doc, options, document) {
379381
// flaw, but this time actually make the mutation.
380382
newRawHTML = newRawHTML.replace(flaw.html, flaw.suggestion);
381383
if (loud) {
382-
console.log(chalk.grey(`Fixed pre_with_html`));
384+
console.log(chalk.grey(`${phrasing} (${flaw.id}) pre_with_html`));
383385
}
384386
}
385387

@@ -429,9 +431,9 @@ async function fixFixableFlaws(doc, options, document) {
429431
if (loud) {
430432
console.log(
431433
chalk.grey(
432-
`Fixed image ${chalk.white.bold(flaw.src)} to ${chalk.white.bold(
433-
newSrc
434-
)}`
434+
`${phrasing} (${flaw.id}) image ${chalk.white.bold(
435+
flaw.src
436+
)} to ${chalk.white.bold(newSrc)}`
435437
)
436438
);
437439
}
@@ -440,10 +442,10 @@ async function fixFixableFlaws(doc, options, document) {
440442
// Finally, summarized what happened...
441443
if (newRawHTML !== document.rawHTML) {
442444
// It changed the raw HTML of the source. So deal with this.
443-
if (options.fixFlawsDryRun) {
445+
if (options.fixFlawsDryRun && options.fixFlawsVerbose) {
444446
console.log(
445447
chalk.yellow(
446-
`Would have modified "${document.fileInfo.path}", if this was not a dry run.`
448+
`Would modify "${document.fileInfo.path}" from fixable flaws.`
447449
)
448450
);
449451
} else {

build/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ async function buildDocument(document, documentOptions = {}) {
212212
flaw.redirectInfo.suggested
213213
)
214214
: null;
215-
const id = `macro_flaw${i}`;
215+
const id = `macro${i}`;
216216
return Object.assign({ id, fixable, suggestion }, flaw);
217217
});
218218
}

docs/cli-tool.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ Run basic validation for a document (only verifies the slug for now).
5252

5353
Open a preview of a given slug in your browser. This depends on a running
5454
dev-server (`yarn start`).
55+
56+
### flaws
57+
58+
Show and optionally fix fixable flaws for a given slug.

testing/tests/destructive.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe("fixing flaws", () => {
9393
),
9494
}).toString();
9595

96-
const regexPattern = /Would have modified "(.*)", if this was not a dry run/g;
96+
const regexPattern = /Would modify "(.*)"./g;
9797
const dryRunNotices = stdout
9898
.split("\n")
9999
.filter((line) => regexPattern.test(line));

tool/cli.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const path = require("path");
99

1010
const { DEFAULT_LOCALE, VALID_LOCALES } = require("../libs/constants");
1111
const { Redirect, Document, buildURL } = require("../content");
12+
const { buildDocument } = require("../build");
1213

1314
const PORT = parseInt(process.env.SERVER_PORT || "5000");
1415

@@ -202,8 +203,28 @@ program
202203
.action(
203204
tryOrExit(async ({ args }) => {
204205
const { slug, locale } = args;
205-
Document.validate(slug, locale);
206-
console.log(chalk.green("✓ All seems fine"));
206+
let okay = true;
207+
const document = Document.findByURL(buildURL(locale, slug));
208+
const { doc } = await buildDocument(document);
209+
210+
if (doc.flaws) {
211+
const flaws = Object.values(doc.flaws)
212+
.map((a) => a.length || 0)
213+
.reduce((a, b) => a + b);
214+
console.log(chalk.red(`Found ${flaws} flaws.`));
215+
okay = false;
216+
}
217+
try {
218+
Document.validate(slug, locale);
219+
} catch (e) {
220+
console.log(chalk.red(e));
221+
okay = false;
222+
}
223+
if (okay) {
224+
console.log(chalk.green("✓ All seems fine"));
225+
} else {
226+
console.log(chalk.red("✗ Found some issues"));
227+
}
207228
})
208229
)
209230

@@ -219,6 +240,40 @@ program
219240
const url = `http://localhost:${PORT}${buildURL(locale, slug)}`;
220241
await open(url);
221242
})
243+
)
244+
245+
.command("flaws", "Find (and fix) flaws in a document")
246+
.argument("<slug>", "Slug of the document in question")
247+
.argument("[locale]", "Locale", {
248+
default: DEFAULT_LOCALE,
249+
validator: [...VALID_LOCALES.values()],
250+
})
251+
.option("-y, --yes", "Assume yes", { default: false })
252+
.action(
253+
tryOrExit(async ({ args, options }) => {
254+
const { slug, locale } = args;
255+
const { yes } = options;
256+
const document = Document.findByURL(buildURL(locale, slug));
257+
const { doc } = await buildDocument(document, {
258+
fixFlaws: true,
259+
fixFlawsDryRun: true,
260+
});
261+
262+
const flaws = Object.values(doc.flaws)
263+
.map((a) => a.filter((f) => f.fixable).length || 0)
264+
.reduce((a, b) => a + b);
265+
const { run } = yes
266+
? true
267+
: await prompts({
268+
type: "confirm",
269+
message: `Proceed fixing ${flaws} flaws?`,
270+
name: "run",
271+
initial: true,
272+
});
273+
if (run) {
274+
buildDocument(document, { fixFlaws: true, fixFlawsVerbose: true });
275+
}
276+
})
222277
);
223278

224279
program.run();

0 commit comments

Comments
 (0)