pdf-tools/tools/checkCode.ts
Emma 9535222fb7 improves block functionality
adds cli compatible prompts/logs
adds logfile function for debug
adds multiselect support
new fieldRename
adds listFieldNames
2025-04-30 01:17:45 -06:00

59 lines
1.7 KiB
TypeScript

import { loadPdfForm } from "../util/saveLoadPdf.ts";
import { callWithArgPrompt } from "util/call.ts";
export async function checkFile(pdfPath: string, csPath: string) {
while (!pdfPath || !pdfPath.endsWith(".pdf")) {
pdfPath = prompt("Please provide path to PDF file:") || "";
}
while (!csPath || !csPath.endsWith(".cs")) {
csPath = prompt("Please provide path to CS class file:") || "";
}
const form = await loadPdfForm(pdfPath);
const fields = form.getFields();
const csFiles = await Promise.all(
csPath.split(",").map((c) => Deno.readTextFile(c.trim())),
);
const fieldNames: string[] = fields.map((f) => f.getName())
.filter((f) => {
const rx = new RegExp(
`(?<!//\s?)case ?"${f.replace(/\[\d\]/, "\\[\\?\\]")}"`,
);
return !csFiles.some((c) => rx.test(c));
})
.filter((f) => !f.toLowerCase().includes("signature"));
if (fieldNames.length) {
console.log(
"%cThe following field names are not present in the CS code",
"color: red",
);
console.log(fieldNames);
alert("Your princess is in another castle...");
} else {
console.log("%cAll form fields present", "color: lime");
alert("Ok!");
}
}
class CheckCode implements ITool {
name = "checkcode";
description = "Checks if form fields are present in CS code";
help() {
console.log("Usage: checkcode <pdfPath> <csPath>");
}
async run(...args: string[]) {
await callWithArgPrompt(checkFile, [
["Please provide path to PDF file:", (p) => !!p && p.endsWith(".pdf")],
[
"Please provide path to CS file (comma separated for multiple):",
(p) => !!p && p.endsWith(".cs"),
],
], args);
}
}
export default new CheckCode();