updates checkCode to new framework
This commit is contained in:
@@ -1,57 +1,74 @@
|
||||
import { forceArgs } from "../cli/forceArgs.ts";
|
||||
import { cliAlert, cliLog } from "../cli/prompts.ts";
|
||||
import { colorize } from "../cli/style.ts";
|
||||
import type { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||
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!");
|
||||
function getCaseSyntaxPatternByFileExtension(
|
||||
extenstion: string,
|
||||
field: string,
|
||||
) {
|
||||
switch (extenstion.trim().toLowerCase().replace(".", "")) {
|
||||
case "cs":
|
||||
case "js":
|
||||
case "ts":
|
||||
default:
|
||||
return `(?<!//\s?)case ?"${field.replace(/\[\d\]/, `\\[\\?|\\d+\\]`)}"`;
|
||||
}
|
||||
}
|
||||
|
||||
class CheckCode implements ITool {
|
||||
name = "checkcode";
|
||||
description = "Checks if form fields are present in CS code";
|
||||
help() {
|
||||
console.log("Usage: checkcode <pdfPath> <csPath>");
|
||||
description = "Checks if form fields are present in a given code file";
|
||||
private block?: TerminalBlock;
|
||||
setBlock(block: TerminalBlock) {
|
||||
this.block = block;
|
||||
this.block.setPreserveHistory(true);
|
||||
}
|
||||
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);
|
||||
async help() {
|
||||
cliLog("Usage: checkcode <pdfPath> <csPath>", this.block);
|
||||
await cliAlert("", this.block);
|
||||
}
|
||||
async run(pdfPath: string, codePaths: string) {
|
||||
[pdfPath, codePaths] = await forceArgs([pdfPath, codePaths], [
|
||||
"Please provide path to PDF file:",
|
||||
"Please provide path(s) to code file(s) (comma separated for multiple):",
|
||||
]);
|
||||
|
||||
const form = await loadPdfForm(pdfPath);
|
||||
|
||||
const fields = form.getFields();
|
||||
const codeFiles: [string, string][] = codePaths.split(",").map((
|
||||
c,
|
||||
) => [c, Deno.readTextFileSync(c.trim())]);
|
||||
|
||||
const fieldNames: string[] = fields.map((f) => f.getName())
|
||||
.filter((f) => !f.toLowerCase().includes("signature"));
|
||||
let unfound = fieldNames.slice();
|
||||
|
||||
for (const [path, content] of codeFiles) {
|
||||
unfound = unfound.filter((f) => {
|
||||
const rx = new RegExp(
|
||||
getCaseSyntaxPatternByFileExtension(path.split(".").at(-1) ?? "", f),
|
||||
);
|
||||
return rx.test(content);
|
||||
});
|
||||
}
|
||||
|
||||
if (unfound.length) {
|
||||
cliLog(
|
||||
colorize(
|
||||
"The following field names are not present in the CS code",
|
||||
"red",
|
||||
),
|
||||
this.block,
|
||||
);
|
||||
cliLog(unfound, this.block);
|
||||
await cliAlert("Your princess is in another castle...", this.block);
|
||||
} else {
|
||||
cliLog(colorize("All form fields present", "green"), this.block);
|
||||
await cliAlert("Ok!", this.block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -87,8 +87,8 @@ class RenameFields implements ITool {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
help(standalone = false) {
|
||||
cliAlert(
|
||||
async help(standalone = false) {
|
||||
await cliAlert(
|
||||
"Usage: rename-fields <pdfPath> <pattern> <change>\n",
|
||||
standalone ? undefined : this.block,
|
||||
);
|
||||
|
Reference in New Issue
Block a user