anki/ts/tools/sql_format.ts
Damien Elmes 9f5b7e79cc Move markpure to TypeScript
Combined with the previous changes, this allows the mobile clients to
build the web components without having to set up a Python environment,
and should speed up AnkiDroid CI.
2023-07-03 17:24:27 +10:00

46 lines
1.3 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import sqlFormatter from "@sqltools/formatter";
import { createPatch } from "diff";
import { readFileSync, writeFileSync } from "fs";
import { argv } from "process";
function formatText(text: string): string {
let newText: string = sqlFormatter.format(text, {
indent: " ",
reservedWordCase: "upper",
});
// downcase some keywords that Anki uses in tables/columns
for (const keyword of ["type", "fields"]) {
newText = newText.replace(
new RegExp(`\\b${keyword.toUpperCase()}\\b`, "g"),
keyword,
);
}
return newText;
}
const [_tsx, _script, mode, ...files] = argv;
const wantFix = mode == "fix";
let errorFound = false;
for (const path of files) {
const orig = readFileSync(path).toString();
const formatted = formatText(orig);
if (orig !== formatted) {
if (wantFix) {
writeFileSync(path, formatted);
console.log(`Fixed ${path}`);
} else {
if (!errorFound) {
errorFound = true;
console.log("SQL formatting issues found:");
}
console.log(createPatch(path, orig, formatted));
}
}
}
if (errorFound) {
process.exit(1);
}