feat: field rename multiple files
This commit is contained in:
parent
9573291582
commit
19eaf2d664
@ -20,7 +20,8 @@ Deno >=2.2 (not required if downloading .exe)
|
|||||||
|
|
||||||
### Deno install
|
### Deno install
|
||||||
|
|
||||||
`deno task install` -> installs as global command `checkfields`
|
`deno install -g --allow-read --allow-write --allow-net --allow-env jsr:@bearmetal/pdf-tools`
|
||||||
|
-> installs as global command `pdf-tools`
|
||||||
|
|
||||||
### Compile
|
### Compile
|
||||||
|
|
||||||
|
@ -103,50 +103,57 @@ class RenameFields implements ITool {
|
|||||||
[pdfPath, pattern, change] = await forceArgs(
|
[pdfPath, pattern, change] = await forceArgs(
|
||||||
[pdfPath, pattern, change],
|
[pdfPath, pattern, change],
|
||||||
[
|
[
|
||||||
["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
[
|
||||||
|
"Please provide path to PDF (comma separated for multiple):",
|
||||||
|
(p) => !!p && p.endsWith(".pdf"),
|
||||||
|
],
|
||||||
"Please provide search string:",
|
"Please provide search string:",
|
||||||
"Please provide requested change:",
|
"Please provide requested change:",
|
||||||
],
|
],
|
||||||
this.block,
|
this.block,
|
||||||
);
|
);
|
||||||
|
|
||||||
const patternRegex = new RegExp(pattern);
|
const paths = pdfPath.split(",");
|
||||||
|
|
||||||
const pdf = await loadPdf(pdfPath);
|
for (const pdfPath of paths) {
|
||||||
const form = pdf.getForm();
|
const patternRegex = new RegExp(pattern);
|
||||||
const fields = form.getFields();
|
|
||||||
|
|
||||||
const foundUpdates: [string, callback][] = [];
|
const pdf = await loadPdf(pdfPath);
|
||||||
|
const form = pdf.getForm();
|
||||||
|
const fields = form.getFields();
|
||||||
|
|
||||||
for (const field of fields) {
|
const foundUpdates: [string, callback][] = [];
|
||||||
const name = field.getName();
|
|
||||||
const match = patternRegex.exec(name);
|
for (const field of fields) {
|
||||||
if (match) {
|
const name = field.getName();
|
||||||
const toChange = evaluateChange(change, match);
|
const match = patternRegex.exec(name);
|
||||||
const preview = name.replace(new RegExp(patternRegex), toChange);
|
if (match) {
|
||||||
foundUpdates.push([
|
const toChange = evaluateChange(change, match);
|
||||||
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
const preview = name.replace(new RegExp(patternRegex), toChange);
|
||||||
() => {
|
foundUpdates.push([
|
||||||
applyRename(field, name, patternRegex, toChange);
|
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
||||||
},
|
() => {
|
||||||
]);
|
applyRename(field, name, patternRegex, toChange);
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (foundUpdates.length) {
|
if (foundUpdates.length) {
|
||||||
cliLog("Found updates:", this.block);
|
cliLog("Found updates:", this.block);
|
||||||
await multiSelectMenuInteractive(
|
await multiSelectMenuInteractive(
|
||||||
"Please select an option to apply",
|
"Please select an option to apply",
|
||||||
foundUpdates,
|
foundUpdates,
|
||||||
{ terminalBlock: this.block },
|
{ terminalBlock: this.block },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = await cliPrompt(
|
||||||
|
"Save to path (or hit enter to keep current):",
|
||||||
|
this.block,
|
||||||
);
|
);
|
||||||
|
await savePdf(pdf, path || pdfPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = await cliPrompt(
|
|
||||||
"Save to path (or hit enter to keep current):",
|
|
||||||
this.block,
|
|
||||||
);
|
|
||||||
await savePdf(pdf, path || pdfPath);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default new RenameFields();
|
export default new RenameFields();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user