| 1 | export async function checkSpelling(ref: incremental.Ref): Promise<number> { |
| 2 | using scope = ref.start("spelling"); |
| 3 | const config = scope.step(async (b) => { |
| 4 | const settings = await b.readFile("cspell.json").json<cspell.Settings>(); |
| 5 | const pending: ts.JsonValue[] = [settings]; |
| 6 | for (const value of pending) { |
| 7 | if (!value || typeof value !== "object") continue; |
| 8 | if ("import" in value || "dictionaryDefinitions" in value) { |
| 9 | throw new Error( |
| 10 | "Keep spelling vocabulary in cspell.json's words or overrides; external dictionaries and imports are unsupported.", |
| 11 | ); |
| 12 | } |
| 13 | pending.push(...Object.values(value)); |
| 14 | } |
| 15 | const packages = await b.readFile("package-lock.json").hash(); |
| 16 | cspellLib.clearCaches(); |
| 17 | return { settings, packages }; |
| 18 | }); |
| 19 | const files = await scope.step(async (b, config) => { |
| 20 | const { settings } = await b.readStep(config); |
| 21 | const found: string[] = []; |
| 22 | const pending = ["src", "lib"]; |
| 23 | for (const dir of pending) { |
| 24 | for (const name of await b.readDir(dir)) { |
| 25 | const file = `${dir}/${name}`; |
| 26 | // This boundary also applies if the configurable exclusions change. |
| 27 | if (settings.ignorePaths.some((glob) => path.matchesGlob(file, glob))) continue; |
| 28 | if (name.endsWith("/")) pending.push(file.slice(0, -1)); |
| 29 | else if (settings.files.some((glob) => path.matchesGlob(file, glob))) found.push(file); |
| 30 | } |
| 31 | } |
| 32 | return found; |
| 33 | }, config).read(); |
| 34 | |
| 35 | await Promise.all(files.map((file) => scope.step(checkFile, file, config).read())); |
| 36 | return files.length; |
| 37 | } |
| 38 | |
| 39 | async function checkFile( |
| 40 | b: incremental.Build, |
| 41 | file: string, |
| 42 | config: incremental.Step<{ settings: cspell.Settings; packages: string }>, |
| 43 | ) { |
| 44 | const { settings } = await b.readStep(config); |
| 45 | const source = b.readFile(file); |
| 46 | const text = await source.text(); |
| 47 | const { issues, settingsUsed } = await cspell.spellCheck( |
| 48 | source.url, |
| 49 | text, |
| 50 | settings, |
| 51 | new URL(b.readFile("cspell.json").url), |
| 52 | ); |
| 53 | if (!issues.length) return; |
| 54 | |
| 55 | const document = cspellLib.createTextDocument({ uri: source.url, content: text }); |
| 56 | const byWord = await cspell.suggestFixes({ issues, settingsUsed }); |
| 57 | for (const issue of issues) { |
| 58 | const position = document.positionAt(issue.offset); |
| 59 | const suggestions = byWord.get(issue.text) ?? []; |
| 60 | const hint = suggestions.length |
| 61 | ? ` Did you mean ${suggestions.map((word) => JSON.stringify(word)).join(", ")}?` |
| 62 | : ""; |
| 63 | b.warn(Object.assign(new Error(`Unknown word ${JSON.stringify(issue.text)}.${hint}`), { |
| 64 | name: "Spelling", |
| 65 | file, |
| 66 | line: position.line + 1, |
| 67 | col: position.character + 1, |
| 68 | })); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | export async function main(graph: incremental.Graph) { |
| 73 | // A standalone check must not retire the site's outputs from its database. |
| 74 | const checks = new incremental.Graph({ root: graph.root, db: ".clover/spelling.db" }); |
| 75 | const { checkSpelling } = await checks.import<typeof import("./spelling.ts")>("src/spelling.ts", { main: true }); |
| 76 | const options = { buildAndWriteOutput: checkSpelling, progress: progress.globalRoot }; |
| 77 | if (process.argv.includes("--watch")) { |
| 78 | for await (const result of checks.incrementalWatch(options)) { |
| 79 | if (!result.ok) log.error(result.error); |
| 80 | log.info(result.statusLine); |
| 81 | } |
| 82 | } else { |
| 83 | try { |
| 84 | const count = await checks.incrementalBuild(options); |
| 85 | log.info(`Spelling checked in ${count} file${count === 1 ? "" : "s"}.`); |
| 86 | } catch (error) { |
| 87 | if (!(error instanceof incremental.BuildError)) throw error; |
| 88 | log.error(error); |
| 89 | process.exitCode = 1; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | import * as incremental from "@clo/lib/incremental"; |
| 95 | import * as log from "@clo/lib/log"; |
| 96 | import * as progress from "@clo/lib/progress"; |
| 97 | import type * as ts from "@clo/lib/ts"; |
| 98 | import * as cspellLib from "cspell-lib"; |
| 99 | import * as path from "node:path"; |
| 100 | import * as cspell from "./cspell.ts"; |