Compare commits
39 Commits
65f0b4e0b7
...
v1.0.3
Author | SHA1 | Date | |
---|---|---|---|
da80c4690b | |||
001b90744b | |||
93df271f79 | |||
cdeef54f68 | |||
90f1547e02 | |||
e5b173155a | |||
19eaf2d664 | |||
9573291582 | |||
1a1431c85e | |||
711880a670 | |||
4f043d2bd7 | |||
43d5916e52 | |||
51aaf27fda | |||
d5b9ef8f04 | |||
22487224f2 | |||
6cc772bbf2 | |||
430130cdaf | |||
9b11f14c84 | |||
25378d2d3c | |||
237d4c4349 | |||
b0fe668133 | |||
7ee7d5f291 | |||
37f7a58f96 | |||
4f4aee6a3e | |||
4691ddc745 | |||
fa44985594 | |||
53cb40ebe8 | |||
2113f930a7 | |||
680aae8b4f | |||
969de4aab7 | |||
673424d755 | |||
593f853143 | |||
490b948576 | |||
7972e679ab | |||
91eb569d4b | |||
d1072d8a81 | |||
6346b28581 | |||
03a1e3ed21 | |||
26b7089cc2 |
48
.gitea/workflows/release.yml
Normal file
48
.gitea/workflows/release.yml
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
name: Create Version Tag
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- "prerelease-*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
version-check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
outputs:
|
||||||
|
tag_created: ${{ steps.tag.outputs.tag_created }}
|
||||||
|
tag_name: ${{ steps.tag.outputs.tag_name }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Run version check
|
||||||
|
id: tag
|
||||||
|
uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/version-check@v1
|
||||||
|
|
||||||
|
build-release:
|
||||||
|
if: needs.version-check.outputs.tag_created == 'true'
|
||||||
|
needs: version-check
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Build and release binaries
|
||||||
|
uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/deno-release@main
|
||||||
|
with:
|
||||||
|
entrypoint: main.ts
|
||||||
|
compile-flags: "--allow-read --allow-write --allow-env --allow-net --include asciiart.txt"
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GIT_PAT }}
|
||||||
|
|
||||||
|
publish-release:
|
||||||
|
needs: build-release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up Deno
|
||||||
|
uses: denoland/setup-deno@v1
|
||||||
|
with:
|
||||||
|
deno-version: 2.3.1
|
||||||
|
|
||||||
|
- name: Publish to JSR
|
||||||
|
run: deno publish --token ${{ secrets.JSR_PAT }}
|
39
CHANGELOG.md
Normal file
39
CHANGELOG.md
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## v1.0.2 (2025-05-20)
|
||||||
|
|
||||||
|
<!-- auto-changelog -->
|
||||||
|
|
||||||
|
## v1.0.1 (2025-05-7)
|
||||||
|
|
||||||
|
<!-- auto-changelog -->
|
||||||
|
|
||||||
|
### Known Issues
|
||||||
|
|
||||||
|
- help flags can cause issues
|
||||||
|
|
||||||
|
## v1.0.0 (2025-05-7)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Check Code Tool
|
||||||
|
- reads supplied code files to see if form fields are present and represented
|
||||||
|
in a switch statement
|
||||||
|
- Field Rename Tool
|
||||||
|
- provide a search and replace pattern to bulk rename form fields
|
||||||
|
- List Form Fields
|
||||||
|
- Sometimes you just need to see what fields there are
|
||||||
|
|
||||||
|
### Known Issues
|
||||||
|
|
||||||
|
- Field rename does not represent full change applied to the field, only the
|
||||||
|
replaced text
|
||||||
|
- help flags can cause issues
|
||||||
|
- ascii art is broken (sad face)
|
||||||
|
- banana doesn't work
|
||||||
|
|
||||||
|
## v0.0.0 (never)
|
||||||
|
|
||||||
|
this is just here for a reference to the auto-changelog
|
||||||
|
|
||||||
|
<!-- auto-changelog -->
|
66
README.md
66
README.md
@@ -1,7 +1,16 @@
|
|||||||
# Emma's Simple Form Field Checker
|
# BearMetal PDF Tools
|
||||||
|
|
||||||
Compares a PDF form to a list of CS class files to see if all field names are
|
A collection of tools for working with PDF forms.
|
||||||
present, excluding signature fields.
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Check Code Tool
|
||||||
|
- reads supplied code files to see if form fields are present and represented
|
||||||
|
in a switch statement
|
||||||
|
- Field Rename Tool
|
||||||
|
- provide a search and replace pattern to bulk rename form fields
|
||||||
|
- List Form Fields
|
||||||
|
- Sometimes you just need to see what fields there are
|
||||||
|
|
||||||
## Prereqs
|
## Prereqs
|
||||||
|
|
||||||
@@ -11,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
|
||||||
|
|
||||||
@@ -20,17 +30,51 @@ Deno >=2.2 (not required if downloading .exe)
|
|||||||
> If you want it to be a global command, create an executables directory and add
|
> If you want it to be a global command, create an executables directory and add
|
||||||
> it to the PATH
|
> it to the PATH
|
||||||
|
|
||||||
|
### Precompiled
|
||||||
|
|
||||||
|
Download the latest release from the
|
||||||
|
[releases page](https://git.cyborggrizzly.com/BearMetal/pdf-tools/releases)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
`checkfields <path to PDF> <comma-separated list of paths to CS class files>`
|
`pdf-tools <tool> <args>` -> `<tool>` is one of the following
|
||||||
-OR- `checkfields` and follow prompts.
|
|
||||||
|
|
||||||
### Output
|
- check-code
|
||||||
|
- field-rename
|
||||||
|
- list-form-fields
|
||||||
|
|
||||||
> All form fields present!
|
## Contributing
|
||||||
|
|
||||||
-OR-
|
Contributions are welcome!
|
||||||
|
|
||||||
> The following field names are not present in the CS code
|
## License
|
||||||
|
|
||||||
> \<list of missing form fields\>
|
GPL 3.0
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### About this project
|
||||||
|
|
||||||
|
BearMetal PDF Tools is a collection of tools made to fix the current state of
|
||||||
|
PDF form editing. Adobe Acrobat is a great tool, but it's not always the easiest
|
||||||
|
to use, nor is it free. It also lacks some features that I find useful, such as
|
||||||
|
bulk renaming of form fields. There's also a lack of powerful, free, and open
|
||||||
|
source tools for PDF editing.
|
||||||
|
|
||||||
|
This project aims to fill that gap by providing a set of tools that can be used
|
||||||
|
to edit PDF forms. The tools are written in Deno, a modern and secure runtime
|
||||||
|
for JavaScript and TypeScript. They are designed to be easy to use and to
|
||||||
|
provide a great user experience.
|
||||||
|
|
||||||
|
The tools are designed to be used in a terminal, and are not designed to be
|
||||||
|
integrated into other applications. They are also not designed to be used as a
|
||||||
|
library, but rather as a command line tool.
|
||||||
|
|
||||||
|
### About BearMetal
|
||||||
|
|
||||||
|
BearMetal is a project that aims to decrapify modern web development. It is a
|
||||||
|
collection of tools, libraries, and resources that aim to make web development
|
||||||
|
more accessible and less intimidating. The project is open source and free to
|
||||||
|
use, and is designed to be used by anyone, regardless of skill level or
|
||||||
|
experience. You can find a list of libraries and tools on
|
||||||
|
[JSR](https://jsr.io/@bearmetal).
|
||||||
|
@@ -79,15 +79,15 @@ export class TerminalLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clearAll() {
|
clearAll() {
|
||||||
|
for (const name of this.layoutOrder) {
|
||||||
|
this.blocks[name].clear();
|
||||||
|
}
|
||||||
Deno.stdout.writeSync(
|
Deno.stdout.writeSync(
|
||||||
new TextEncoder().encode(
|
new TextEncoder().encode(
|
||||||
TerminalLayout.ALT_BUFFER_DISABLE,
|
TerminalLayout.ALT_BUFFER_DISABLE,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
Cursor.show();
|
Cursor.show();
|
||||||
for (const name of this.layoutOrder) {
|
|
||||||
this.blocks[name].clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
@@ -111,6 +111,8 @@ export class TerminalBlock {
|
|||||||
private renderHeight: number = 0;
|
private renderHeight: number = 0;
|
||||||
private lastRenderRow = 1;
|
private lastRenderRow = 1;
|
||||||
|
|
||||||
|
private lastRendered: string[] = [];
|
||||||
|
|
||||||
private preserveHistory = false;
|
private preserveHistory = false;
|
||||||
|
|
||||||
constructor(private prepend: string = "") {}
|
constructor(private prepend: string = "") {}
|
||||||
@@ -193,27 +195,37 @@ export class TerminalBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
renderInternal(startRow?: number) {
|
renderInternal(startRow?: number) {
|
||||||
this.lastRenderRow = startRow ?? this.lastRenderRow;
|
const outputLines: string[] = [];
|
||||||
this.clear(); // uses old renderedLineCount
|
|
||||||
|
|
||||||
const outputLines = this.renderLines.map((line) =>
|
for (let i = 0; i < this.renderLines.length; i++) {
|
||||||
`${this.prepend}${line}\x1b[K`
|
const line = `${this.prepend}${this.renderLines[i]}`;
|
||||||
);
|
const previous = this.lastRendered[i];
|
||||||
const output = outputLines.join("\n");
|
if (line !== previous) {
|
||||||
if (startRow !== undefined) {
|
const moveToLine = `\x1b[${(startRow ?? this.lastRenderRow) + i};1H`;
|
||||||
const moveCursor = `\x1b[${startRow};1H`;
|
outputLines.push(moveToLine + line + "\x1b[K");
|
||||||
Deno.stdout.writeSync(new TextEncoder().encode(moveCursor + output));
|
}
|
||||||
} else {
|
|
||||||
Deno.stdout.writeSync(new TextEncoder().encode(output));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update rendered line count *after* rendering
|
if (this.lastRendered.length > this.renderLines.length) {
|
||||||
this.renderedLineCount = outputLines.reduce(
|
const baseRow = startRow ?? this.lastRenderRow;
|
||||||
(count, line) =>
|
for (let i = this.renderLines.length; i < this.lastRendered.length; i++) {
|
||||||
count +
|
const moveToLine = `\x1b[${baseRow + i};1H\x1b[2K`;
|
||||||
Math.ceil((line.length) / (Deno.consoleSize().columns || 80)),
|
Deno.stdout.writeSync(new TextEncoder().encode(moveToLine));
|
||||||
0,
|
}
|
||||||
);
|
}
|
||||||
|
|
||||||
|
const baseRow = startRow ?? this.lastRenderRow;
|
||||||
|
const excessLines = this.renderHeight - this.renderLines.length;
|
||||||
|
for (let i = 0; i < excessLines; i++) {
|
||||||
|
const moveToLine = `[${baseRow + this.renderLines.length + i};1H[2K`;
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode(moveToLine));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastRendered = [...this.renderLines];
|
||||||
|
this.renderedLineCount = this.renderHeight;
|
||||||
|
this.lastRenderRow = baseRow;
|
||||||
|
const output = outputLines.join("\n");
|
||||||
|
Deno.stdout.writeSync(new TextEncoder().encode(output));
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
@@ -223,7 +235,6 @@ export class TerminalBlock {
|
|||||||
for (let i = 0; i < this.renderedLineCount; i++) {
|
for (let i = 0; i < this.renderedLineCount; i++) {
|
||||||
Deno.stdout.writeSync(new TextEncoder().encode(`\x1b[2K\x1b[1E`));
|
Deno.stdout.writeSync(new TextEncoder().encode(`\x1b[2K\x1b[1E`));
|
||||||
}
|
}
|
||||||
this.renderedLineCount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clearAll() {
|
clearAll() {
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
export class ArgParser {
|
export class ArgParser<T extends Record<string, string[]>> {
|
||||||
private args: string[];
|
private args: string[];
|
||||||
|
private flags: Map<keyof T, boolean> = new Map();
|
||||||
|
|
||||||
constructor(args: string[]) {
|
constructor(args: string[]) {
|
||||||
this.args = args;
|
this.args = args;
|
||||||
@@ -11,7 +12,22 @@ export class ArgParser {
|
|||||||
return this.args[index + 1];
|
return this.args[index + 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
get flags() {
|
setFlagDefs(flagDefs: T) {
|
||||||
|
for (const [flag, defs] of Object.entries(flagDefs)) {
|
||||||
|
for (const def of defs) {
|
||||||
|
if (this.argFlags.includes(def)) {
|
||||||
|
this.flags.set(flag, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
getFlag(flag: keyof T) {
|
||||||
|
return this.flags.get(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
get argFlags() {
|
||||||
return this.args.filter((arg) => arg.startsWith("-"));
|
return this.args.filter((arg) => arg.startsWith("-"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +42,9 @@ export class ArgParser {
|
|||||||
get task() {
|
get task() {
|
||||||
return this.nonFlags[0];
|
return this.nonFlags[0];
|
||||||
}
|
}
|
||||||
|
get taskArgs() {
|
||||||
|
return this.nonFlags.slice(1);
|
||||||
|
}
|
||||||
|
|
||||||
static parse(args: string[]) {
|
static parse(args: string[]) {
|
||||||
return new ArgParser(args);
|
return new ArgParser(args);
|
||||||
|
80
cli/index.ts
80
cli/index.ts
@@ -5,35 +5,44 @@ import { colorize } from "./style.ts";
|
|||||||
import { selectMenuInteractive } from "./selectMenu.ts";
|
import { selectMenuInteractive } from "./selectMenu.ts";
|
||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||||
import { cliAlert, cliLog } from "./prompts.ts";
|
import { cliAlert, cliLog } from "./prompts.ts";
|
||||||
|
import type { ITool } from "../types.ts";
|
||||||
|
|
||||||
|
// Register tools here (filename, no extension)
|
||||||
|
const toolRegistry: [string, Promise<{ default: ITool }>][] = [
|
||||||
|
["checkCode", import("../tools/checkCode.ts")],
|
||||||
|
["fieldRename", import("../tools/fieldRename.ts")],
|
||||||
|
["listFormFields", import("../tools/listFormFields.ts")],
|
||||||
|
];
|
||||||
|
|
||||||
export class PdfToolsCli {
|
export class PdfToolsCli {
|
||||||
private tools: Map<string, ITool> = new Map();
|
private tools: Map<string, ITool> = new Map();
|
||||||
private terminalLayout = new TerminalLayout();
|
private terminalLayout = new TerminalLayout();
|
||||||
|
closeMessage?: string;
|
||||||
|
|
||||||
private args = ArgParser.parse(Deno.args);
|
private args = ArgParser.parse(Deno.args).setFlagDefs({
|
||||||
|
help: ["-h", "--help"],
|
||||||
|
banana: ["-b", "--banana"],
|
||||||
|
});
|
||||||
|
|
||||||
async importTools(tools?: string) {
|
async importTools() {
|
||||||
tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools";
|
for (const [name, toolfile] of toolRegistry) {
|
||||||
for (const toolfile of Deno.readDirSync(tools)) {
|
const t = await toolfile;
|
||||||
if (toolfile.isFile) {
|
try {
|
||||||
const tool = await import(
|
if (t.default) {
|
||||||
Deno.cwd() + "/" + tools + "/" + toolfile.name
|
|
||||||
);
|
|
||||||
if (tool.default) {
|
|
||||||
this.tools.set(
|
this.tools.set(
|
||||||
toCase(toolfile.name.replace(".ts", ""), "title"),
|
toCase(name, "title"),
|
||||||
tool.default,
|
t.default,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
cliLog(e + "\n", this.terminalLayout.getBlock("body"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async banana() {
|
private async banana() {
|
||||||
const asciiArt = await getAsciiArt("banana");
|
const asciiArt = await getAsciiArt("banana");
|
||||||
const body = this.terminalLayout.getBlock("body");
|
this.closeMessage = colorize(asciiArt, "yellow");
|
||||||
body.clearAll();
|
|
||||||
cliLog(colorize(asciiArt, "yellow"), body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async help() {
|
private async help() {
|
||||||
@@ -47,24 +56,37 @@ export class PdfToolsCli {
|
|||||||
|
|
||||||
public async run() {
|
public async run() {
|
||||||
try {
|
try {
|
||||||
|
await this.importTools();
|
||||||
const titleBlock = new TerminalBlock();
|
const titleBlock = new TerminalBlock();
|
||||||
this.terminalLayout.register("title", titleBlock);
|
this.terminalLayout.register("title", titleBlock);
|
||||||
const bodyBlock = new TerminalBlock();
|
const bodyBlock = new TerminalBlock();
|
||||||
this.terminalLayout.register("body", bodyBlock);
|
this.terminalLayout.register("body", bodyBlock);
|
||||||
this.embiggenHeader();
|
if (this.args.getFlag("banana")) {
|
||||||
if (Deno.args.length === 0) {
|
titleBlock.setFixedHeight(0);
|
||||||
// console.log(
|
await this.banana();
|
||||||
// colorize("No tool specified. Importing all tools...", "gray"),
|
return;
|
||||||
// );
|
}
|
||||||
await this.importTools();
|
if (this.args.getFlag("help") && !this.args.task) {
|
||||||
|
await this.help();
|
||||||
|
return;
|
||||||
|
} else if (this.args.nonFlags.length === 0 || !this.args.task) {
|
||||||
|
this.embiggenHeader();
|
||||||
|
await this.toolMenu();
|
||||||
|
} else {
|
||||||
|
const task = this.args.task;
|
||||||
|
await this.runTool(toCase(task, "title"));
|
||||||
}
|
}
|
||||||
await this.toolMenu();
|
|
||||||
} finally {
|
} finally {
|
||||||
this.terminalLayout.clearAll();
|
this.cleanup();
|
||||||
Deno.stdin.setRaw(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private cleanup() {
|
||||||
|
this.terminalLayout.clearAll();
|
||||||
|
Deno.stdin.setRaw(false);
|
||||||
|
if (this.closeMessage) console.log(this.closeMessage);
|
||||||
|
}
|
||||||
|
|
||||||
private async toolMenu() {
|
private async toolMenu() {
|
||||||
const tools = this.tools.keys().toArray();
|
const tools = this.tools.keys().toArray();
|
||||||
const bodyBlock = this.terminalLayout.getBlock("body");
|
const bodyBlock = this.terminalLayout.getBlock("body");
|
||||||
@@ -95,9 +117,15 @@ export class PdfToolsCli {
|
|||||||
const bodyBlock = this.terminalLayout.getBlock("body");
|
const bodyBlock = this.terminalLayout.getBlock("body");
|
||||||
bodyBlock.clearAll();
|
bodyBlock.clearAll();
|
||||||
tool.setBlock?.(bodyBlock);
|
tool.setBlock?.(bodyBlock);
|
||||||
await tool.run();
|
if (this.args.getFlag("help")) {
|
||||||
await tool.done?.();
|
await tool.help?.();
|
||||||
this.embiggenHeader();
|
} else {
|
||||||
|
await tool.run(...this.args.taskArgs);
|
||||||
|
await tool.done?.();
|
||||||
|
}
|
||||||
|
await this.embiggenHeader();
|
||||||
|
} else {
|
||||||
|
this.closeMessage = "No tool found for " + toolName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
// deno-lint-disable-must-await-calls
|
||||||
import { Cursor } from "./cursor.ts";
|
import { Cursor } from "./cursor.ts";
|
||||||
import { colorize } from "./style.ts";
|
import { colorize } from "./style.ts";
|
||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||||
@@ -11,11 +12,8 @@ export async function cliPrompt(
|
|||||||
|
|
||||||
await Deno.stdin.setRaw(true);
|
await Deno.stdin.setRaw(true);
|
||||||
|
|
||||||
let cursorVisible = true;
|
const cursorVisible = Cursor["visible"];
|
||||||
if (!block) {
|
Cursor.show();
|
||||||
cursorVisible = Cursor["visible"];
|
|
||||||
Cursor.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
let range: [number, number] = [0, 1];
|
let range: [number, number] = [0, 1];
|
||||||
if (block) {
|
if (block) {
|
||||||
@@ -54,7 +52,7 @@ export async function cliPrompt(
|
|||||||
}
|
}
|
||||||
|
|
||||||
await Deno.stdin.setRaw(false);
|
await Deno.stdin.setRaw(false);
|
||||||
if (!block && !cursorVisible) {
|
if (!cursorVisible) {
|
||||||
Cursor.hide();
|
Cursor.hide();
|
||||||
}
|
}
|
||||||
Deno.stdout.writeSync(encoder.encode("\n"));
|
Deno.stdout.writeSync(encoder.encode("\n"));
|
||||||
@@ -77,19 +75,25 @@ export function cliAlert(message: string, block?: TerminalBlock) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cliLog(message: string, block?: TerminalBlock) {
|
export function cliLog(
|
||||||
|
message: string | object | Array<unknown>,
|
||||||
|
block?: TerminalBlock,
|
||||||
|
) {
|
||||||
if (!block) {
|
if (!block) {
|
||||||
console.log(message);
|
console.log(message);
|
||||||
} else {
|
} else {
|
||||||
|
if (typeof message === "object") message = Deno.inspect(message);
|
||||||
block.setLines(message.split("\n"));
|
block.setLines(message.split("\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
|
Cursor.hide();
|
||||||
const layout = new TerminalLayout();
|
const layout = new TerminalLayout();
|
||||||
const title = new TerminalBlock();
|
const title = new TerminalBlock();
|
||||||
const block = new TerminalBlock();
|
const block = new TerminalBlock();
|
||||||
block.setPreserveHistory(true);
|
block.setPreserveHistory(true);
|
||||||
|
// ScrollManager.enable(block);
|
||||||
title.setLines(["Hello, World!"]);
|
title.setLines(["Hello, World!"]);
|
||||||
title.setFixedHeight(1);
|
title.setFixedHeight(1);
|
||||||
|
|
||||||
@@ -105,6 +109,7 @@ if (import.meta.main) {
|
|||||||
cliLog(`Hello, ${name}!`, block);
|
cliLog(`Hello, ${name}!`, block);
|
||||||
const single = await cliConfirm("Are you single?", block);
|
const single = await cliConfirm("Are you single?", block);
|
||||||
cliLog(single ? "Do you want to go out with me?" : "Okay", block);
|
cliLog(single ? "Do you want to go out with me?" : "Okay", block);
|
||||||
|
// ScrollManager.enable(block);
|
||||||
const loopingConvo = [
|
const loopingConvo = [
|
||||||
"No response?",
|
"No response?",
|
||||||
"I guess that's okay",
|
"I guess that's okay",
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import type { callback } from "../types.ts";
|
||||||
import { colorize } from "./style.ts";
|
import { colorize } from "./style.ts";
|
||||||
import { TerminalBlock } from "./TerminalLayout.ts";
|
import { TerminalBlock } from "./TerminalLayout.ts";
|
||||||
|
|
||||||
|
17
deno.json
17
deno.json
@@ -1,16 +1,19 @@
|
|||||||
{
|
{
|
||||||
"name": "@bearmetal/pdf-tools",
|
"name": "@bearmetal/pdf-tools",
|
||||||
"version": "0.0.1",
|
"version": "1.0.3",
|
||||||
|
"license": "GPL 3.0",
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run -A --env-file=.env --watch main.ts",
|
"dev": "deno run -A --env-file=.env main.ts",
|
||||||
"compile": "deno compile -o compare-form-fields.exe --target x86_64-pc-windows-msvc -R ./main.ts",
|
"compile": "deno compile -o pdf-tools.exe --target x86_64-pc-windows-msvc --include ./asciiart.txt -A ./main.ts",
|
||||||
"install": "deno install -fgq --import-map ./deno.json -n checkfields -R ./main.ts",
|
"install": "deno install -fgq --import-map ./deno.json -n checkfields -R ./main.ts",
|
||||||
"debug": "deno run -A --env-file=.env --inspect-wait --watch main.ts"
|
"debug": "deno run -A --env-file=.env --inspect-wait --watch main.ts"
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@std/assert": "jsr:@std/assert@1",
|
"@std/assert": "jsr:@std/assert@1",
|
||||||
|
"@std/path": "jsr:@std/path@^1.0.9",
|
||||||
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
||||||
"util/": "./util/"
|
"util/": "./util/",
|
||||||
|
"@/": "./"
|
||||||
},
|
},
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./main.ts"
|
".": "./main.ts"
|
||||||
@@ -19,10 +22,14 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"no-explicit-any"
|
"no-explicit-any"
|
||||||
|
],
|
||||||
|
"include": [
|
||||||
|
"require-await"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"plugins": [
|
"plugins": [
|
||||||
"./no-log.ts"
|
"./no-log.ts",
|
||||||
|
"./must_await_cli_prompts.ts"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
7
deno.lock
generated
7
deno.lock
generated
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"version": "4",
|
"version": "5",
|
||||||
"specifiers": {
|
"specifiers": {
|
||||||
"jsr:@std/assert@1": "1.0.12",
|
"jsr:@std/assert@1": "1.0.12",
|
||||||
"jsr:@std/internal@^1.0.6": "1.0.6",
|
"jsr:@std/internal@^1.0.6": "1.0.6",
|
||||||
|
"jsr:@std/path@^1.0.9": "1.0.9",
|
||||||
"npm:pdf-lib@^1.17.1": "1.17.1"
|
"npm:pdf-lib@^1.17.1": "1.17.1"
|
||||||
},
|
},
|
||||||
"jsr": {
|
"jsr": {
|
||||||
@@ -14,6 +15,9 @@
|
|||||||
},
|
},
|
||||||
"@std/internal@1.0.6": {
|
"@std/internal@1.0.6": {
|
||||||
"integrity": "9533b128f230f73bd209408bb07a4b12f8d4255ab2a4d22a1fd6d87304aca9a4"
|
"integrity": "9533b128f230f73bd209408bb07a4b12f8d4255ab2a4d22a1fd6d87304aca9a4"
|
||||||
|
},
|
||||||
|
"@std/path@1.0.9": {
|
||||||
|
"integrity": "260a49f11edd3db93dd38350bf9cd1b4d1366afa98e81b86167b4e3dd750129e"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"npm": {
|
"npm": {
|
||||||
@@ -48,6 +52,7 @@
|
|||||||
"workspace": {
|
"workspace": {
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"jsr:@std/assert@1",
|
"jsr:@std/assert@1",
|
||||||
|
"jsr:@std/path@^1.0.9",
|
||||||
"npm:pdf-lib@^1.17.1"
|
"npm:pdf-lib@^1.17.1"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
1
main.ts
1
main.ts
@@ -1,3 +1,4 @@
|
|||||||
|
/// <reference types="./types.ts" />
|
||||||
import { PdfToolsCli } from "./cli/index.ts";
|
import { PdfToolsCli } from "./cli/index.ts";
|
||||||
|
|
||||||
const app = new PdfToolsCli();
|
const app = new PdfToolsCli();
|
||||||
|
45
must_await_cli_prompts.ts
Normal file
45
must_await_cli_prompts.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
const TARGET_FUNCTIONS = new Set(["cliAlert", "cliPrompt", "cliConfirm"]);
|
||||||
|
|
||||||
|
const plugin: Deno.lint.Plugin = {
|
||||||
|
name: "must-await-calls",
|
||||||
|
rules: {
|
||||||
|
"must-await-calls": {
|
||||||
|
create(context) {
|
||||||
|
return {
|
||||||
|
CallExpression(node) {
|
||||||
|
if (
|
||||||
|
node.callee.type !== "Identifier" ||
|
||||||
|
!TARGET_FUNCTIONS.has(node.callee.name)
|
||||||
|
) return;
|
||||||
|
|
||||||
|
const parent = node.parent;
|
||||||
|
|
||||||
|
// Allow `await fetchData()`
|
||||||
|
if (parent?.type === "AwaitExpression") return;
|
||||||
|
|
||||||
|
// Allow `return fetchData()` or `return await fetchData()`
|
||||||
|
if (parent?.type === "ReturnStatement") return;
|
||||||
|
|
||||||
|
// Allow `fetchData().then(...)`
|
||||||
|
if (
|
||||||
|
parent?.type === "MemberExpression" &&
|
||||||
|
parent.property.type === "Identifier" &&
|
||||||
|
parent.property.name === "then"
|
||||||
|
) return;
|
||||||
|
|
||||||
|
context.report({
|
||||||
|
node,
|
||||||
|
message:
|
||||||
|
`Call to "${node.callee.name}" must be awaited, returned, or .then-chained.`,
|
||||||
|
fix(fixer) {
|
||||||
|
return fixer.insertTextBefore(node, "await ");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default plugin;
|
@@ -0,0 +1,7 @@
|
|||||||
|
const thing: string = "";
|
||||||
|
switch (thing) {
|
||||||
|
case "Text1":
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
@@ -1,57 +1,75 @@
|
|||||||
|
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 type { ITool } from "../types.ts";
|
||||||
import { loadPdfForm } from "../util/saveLoadPdf.ts";
|
import { loadPdfForm } from "../util/saveLoadPdf.ts";
|
||||||
import { callWithArgPrompt } from "util/call.ts";
|
|
||||||
|
|
||||||
export async function checkFile(pdfPath: string, csPath: string) {
|
function getCaseSyntaxPatternByFileExtension(
|
||||||
while (!pdfPath || !pdfPath.endsWith(".pdf")) {
|
extenstion: string,
|
||||||
pdfPath = prompt("Please provide path to PDF file:") || "";
|
field: string,
|
||||||
}
|
) {
|
||||||
while (!csPath || !csPath.endsWith(".cs")) {
|
switch (extenstion.trim().toLowerCase().replace(".", "")) {
|
||||||
csPath = prompt("Please provide path to CS class file:") || "";
|
case "cs":
|
||||||
}
|
case "js":
|
||||||
|
case "ts":
|
||||||
const form = await loadPdfForm(pdfPath);
|
default:
|
||||||
|
return `(?<!//\s?)case ?"${field.replace(/\[\d\]/, `\\[\\?|\\d+\\]`)}"`;
|
||||||
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 {
|
class CheckCode implements ITool {
|
||||||
name = "checkcode";
|
name = "checkcode";
|
||||||
description = "Checks if form fields are present in CS code";
|
description = "Checks if form fields are present in a given code file";
|
||||||
help() {
|
private block?: TerminalBlock;
|
||||||
console.log("Usage: checkcode <pdfPath> <csPath>");
|
setBlock(block: TerminalBlock) {
|
||||||
|
this.block = block;
|
||||||
|
this.block.setPreserveHistory(true);
|
||||||
}
|
}
|
||||||
async run(...args: string[]) {
|
async help() {
|
||||||
await callWithArgPrompt(checkFile, [
|
cliLog("Usage: checkcode <pdfPath> <csPath>", this.block);
|
||||||
["Please provide path to PDF file:", (p) => !!p && p.endsWith(".pdf")],
|
await cliAlert("", this.block);
|
||||||
[
|
}
|
||||||
"Please provide path to CS file (comma separated for multiple):",
|
async run(pdfPath: string, codePaths: string) {
|
||||||
(p) => !!p && p.endsWith(".cs"),
|
[pdfPath, codePaths] = await forceArgs([pdfPath, codePaths], [
|
||||||
],
|
"Please provide path to PDF file:",
|
||||||
], args);
|
"Please provide path(s) to code file(s) (comma separated for multiple):",
|
||||||
|
], this.block);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,24 +4,10 @@ import { callWithArgPrompt } from "util/call.ts";
|
|||||||
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||||
import { forceArgs } from "../cli/forceArgs.ts";
|
import { forceArgs } from "../cli/forceArgs.ts";
|
||||||
import { colorize } from "../cli/style.ts";
|
import { colorize } from "../cli/style.ts";
|
||||||
import { cliLog, cliPrompt } from "../cli/prompts.ts";
|
import { cliAlert, cliLog, cliPrompt } from "../cli/prompts.ts";
|
||||||
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
||||||
|
import type { callback, ITool } from "../types.ts";
|
||||||
// const thing = PDFAcroField.prototype.getFullyQualifiedName;
|
import { toCase } from "util/caseManagement.ts";
|
||||||
// PDFAcroField.prototype.getFullyQualifiedName = function () {
|
|
||||||
// const name = thing.call(this)
|
|
||||||
// // if (name?.includes('langauge'))
|
|
||||||
// console.log(name)
|
|
||||||
// return name;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const thing = PDFHexString.prototype.copyBytesInto
|
|
||||||
// PDFHexString.prototype.copyBytesInto = function (buffer: Uint8Array, offset: number) {
|
|
||||||
// console.log((this as any).value)
|
|
||||||
|
|
||||||
// const result = thing.call(this, buffer, offset)
|
|
||||||
// return result;
|
|
||||||
// }
|
|
||||||
|
|
||||||
async function renameFields(
|
async function renameFields(
|
||||||
path: string,
|
path: string,
|
||||||
@@ -48,29 +34,10 @@ async function renameFields(
|
|||||||
if (mName) {
|
if (mName) {
|
||||||
changesMade = true;
|
changesMade = true;
|
||||||
cField.dict.set(PDFName.of("T"), PDFString.of(mName));
|
cField.dict.set(PDFName.of("T"), PDFString.of(mName));
|
||||||
// console.log(cField.getPartialName())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cField = cField.getParent();
|
cField = cField.getParent();
|
||||||
// console.log(cField?.getPartialName())
|
|
||||||
}
|
}
|
||||||
console.log(field.getName());
|
|
||||||
// const newName = name.replace(pattern, change);
|
|
||||||
// console.log("Change to: %c" + newName, "color: yellow");
|
|
||||||
// if (confirm('Ok?')) {
|
|
||||||
// let parent = field.acroField.getParent();
|
|
||||||
// field.acroField.setPartialName(segments.pop())
|
|
||||||
// while (parent && segments.length) {
|
|
||||||
// console.log(parent.getPartialName())
|
|
||||||
// parent.setPartialName(segments.pop())
|
|
||||||
// parent = parent.getParent();
|
|
||||||
// }
|
|
||||||
// changesMade = true;
|
|
||||||
// console.log(field.getName())
|
|
||||||
// // dict.set(PDFName.of("T"), PDFHexString.fromText(newName))
|
|
||||||
// console.log("%cDone!", "color: lime")
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (changesMade) {
|
if (changesMade) {
|
||||||
@@ -103,13 +70,42 @@ function applyRename(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Evaluates the change string with the match array
|
||||||
|
*
|
||||||
|
* @description The change string can include the following variables:
|
||||||
|
*
|
||||||
|
* - $<int> - capture groups, indexed from 1
|
||||||
|
* - $<int>i - capture groups, indexed from 1, transforming an integer to an index
|
||||||
|
* - $<int>s - capture groups, indexed from 1, transforming a string to snake case
|
||||||
|
* - $<int>c - capture groups, indexed from 1, transforming a string to camel case
|
||||||
|
* - $<int>l - capture groups, indexed from 1, transforming a string to lower case
|
||||||
|
* - $<int>u - capture groups, indexed from 1, transforming a string to upper case
|
||||||
|
* - $<int>t - capture groups, indexed from 1, transforming a string to title case
|
||||||
|
*/
|
||||||
function evaluateChange(change: string, match: RegExpExecArray) {
|
function evaluateChange(change: string, match: RegExpExecArray) {
|
||||||
return change.replace(
|
return change.replace(
|
||||||
/\$(\d+)(i?)/g,
|
/\$(\d+)([icslut]?)/g,
|
||||||
(_, i, indexed) =>
|
(_, i, indexed) => {
|
||||||
indexed
|
switch (indexed) {
|
||||||
? (parseInt(match[i]) ? (parseInt(match[i]) - 1).toString() : match[i])
|
case "i":
|
||||||
: match[i],
|
return (parseInt(match[i])
|
||||||
|
? (parseInt(match[i]) - 1).toString()
|
||||||
|
: match[i]);
|
||||||
|
case "s":
|
||||||
|
return toCase(match[i], "snake");
|
||||||
|
case "c":
|
||||||
|
return toCase(match[i], "camel");
|
||||||
|
case "t":
|
||||||
|
return toCase(match[i], "title");
|
||||||
|
case "l":
|
||||||
|
return match[i].toLowerCase();
|
||||||
|
case "u":
|
||||||
|
return match[i].toUpperCase();
|
||||||
|
default:
|
||||||
|
return match[i];
|
||||||
|
}
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,9 +118,9 @@ class RenameFields implements ITool {
|
|||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
help(standalone = false) {
|
async help(standalone = false) {
|
||||||
cliLog(
|
await cliAlert(
|
||||||
"Usage: renamefields <pdfPath> <pattern> <change>",
|
"Usage: rename-fields <pdfPath> <pattern> <change>\n",
|
||||||
standalone ? undefined : this.block,
|
standalone ? undefined : this.block,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -137,49 +133,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);
|
||||||
foundUpdates.push([
|
if (match) {
|
||||||
`${colorize(name, "red")} -> ${colorize(toChange, "green")}`,
|
const toChange = evaluateChange(change, match);
|
||||||
() => {
|
const preview = name.replace(new RegExp(patternRegex), toChange);
|
||||||
applyRename(field, name, patternRegex, toChange);
|
foundUpdates.push([
|
||||||
},
|
`${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();
|
||||||
|
@@ -2,6 +2,7 @@ import { forceArgs } from "../cli/forceArgs.ts";
|
|||||||
import { cliAlert } from "../cli/prompts.ts";
|
import { cliAlert } from "../cli/prompts.ts";
|
||||||
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||||
import { loadPdfForm } from "util/saveLoadPdf.ts";
|
import { loadPdfForm } from "util/saveLoadPdf.ts";
|
||||||
|
import type { ITool } from "../types.ts";
|
||||||
|
|
||||||
export class ListFormFields implements ITool {
|
export class ListFormFields implements ITool {
|
||||||
name = "listformfields";
|
name = "listformfields";
|
||||||
|
22
types.ts
22
types.ts
@@ -1,15 +1,13 @@
|
|||||||
import type { TerminalBlock } from "./cli/TerminalLayout.ts";
|
import type { TerminalBlock } from "./cli/TerminalLayout.ts";
|
||||||
|
|
||||||
declare global {
|
export type ToolFunc<T extends unknown[]> = (...args: T) => Promise<void>;
|
||||||
type ToolFunc<T extends unknown[]> = (...args: T) => Promise<void>;
|
export interface ITool {
|
||||||
interface ITool {
|
name: string;
|
||||||
name: string;
|
description: string;
|
||||||
description: string;
|
run: ToolFunc<any[]>;
|
||||||
run: ToolFunc<any[]>;
|
help?: () => Promise<void> | void;
|
||||||
help?: () => Promise<void> | void;
|
done?: () => Promise<void> | void;
|
||||||
done?: () => Promise<void> | void;
|
setBlock?: (block: TerminalBlock) => void;
|
||||||
setBlock?: (block: TerminalBlock) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
type callback = (...args: any[]) => any;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type callback = (...args: any[]) => any;
|
||||||
|
@@ -1,30 +1,31 @@
|
|||||||
export async function getAsciiArt(art: string) {
|
import { log } from "./logfile.ts";
|
||||||
const artFilePath = Deno.env.get("BEARMETAL_ASCII_PATH") ||
|
import { join } from "@std/path";
|
||||||
getBearmetalAsciiPath();
|
|
||||||
if (!artFilePath) return art;
|
|
||||||
let artFileText: string;
|
|
||||||
if (artFilePath.startsWith("http")) {
|
|
||||||
artFileText = await fetch(artFilePath).then((res) => res.text());
|
|
||||||
} else {
|
|
||||||
artFileText = await Deno.readTextFile(artFilePath);
|
|
||||||
}
|
|
||||||
const parserRX = /begin\s+(\w+)\s*\n([\s\S]*?)\s*end\s*/g;
|
|
||||||
let result = parserRX.exec(artFileText);
|
|
||||||
|
|
||||||
while (result !== null) {
|
export async function getAsciiArt(art: string) {
|
||||||
const [_, name, artText] = result;
|
try {
|
||||||
if (name === art) return artText;
|
const artFilePath =
|
||||||
result = parserRX.exec(artFileText);
|
Deno.env.get("BEARMETAL_ASCII_PATH") || import.meta.dirname
|
||||||
|
? join(import.meta.dirname || "", "../asciiart.txt")
|
||||||
|
: "https://git.cyborggrizzly.com/BearMetal/pdf-tools/raw/branch/main/asciiart.txt";
|
||||||
|
let artFileText: string;
|
||||||
|
if (artFilePath?.startsWith("http")) {
|
||||||
|
artFileText = await fetch(artFilePath).then((res) => res.text());
|
||||||
|
} else {
|
||||||
|
artFileText = await Deno.readTextFile(
|
||||||
|
artFilePath,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const parserRX = /begin\s+(\w+)\s*\n([\s\S]*?)\s*end\s*/g;
|
||||||
|
let result = parserRX.exec(artFileText);
|
||||||
|
|
||||||
|
while (result !== null) {
|
||||||
|
const [_, name, artText] = result;
|
||||||
|
if (name === art) return artText;
|
||||||
|
result = parserRX.exec(artFileText);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
alert();
|
||||||
}
|
}
|
||||||
return art;
|
return art;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBearmetalAsciiPath() {
|
|
||||||
const filenameRX = /asciiarts?\.txt$/;
|
|
||||||
for (const filename of Deno.readDirSync(".")) {
|
|
||||||
if (filename.isFile && filenameRX.test(filename.name)) {
|
|
||||||
return filename.name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
import type { ToolFunc } from "../types.ts";
|
||||||
|
|
||||||
type transformer = (arg: string) => any;
|
type transformer = (arg: string) => any;
|
||||||
interface IConfig {
|
interface IConfig {
|
||||||
multiTransform?: boolean;
|
multiTransform?: boolean;
|
||||||
|
@@ -11,9 +11,45 @@ function lowerToTrainCase(str: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function lowerToCamelCase(str: string) {
|
/**
|
||||||
return str.trim().replace(/(?:\s)\w/g, (match) => match.toUpperCase())
|
* @param str
|
||||||
.replaceAll(" ", "");
|
* @returns camelCased string (single letter words are lower cased, e.g. SSN -> ssn)
|
||||||
|
*/
|
||||||
|
function lowerToCamelCase(str: string): string {
|
||||||
|
const words = str.trim().split(/\s+/);
|
||||||
|
const result: string[] = [];
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
while (i < words.length) {
|
||||||
|
if (words[i].length === 1) {
|
||||||
|
// We’ve hit the start of a chain of single-letter words
|
||||||
|
let j = i;
|
||||||
|
while (j < words.length && words[j].length === 1) {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
const chainIsAtStart = i === 0;
|
||||||
|
// Process that entire chain
|
||||||
|
for (let k = i; k < j; k++) {
|
||||||
|
result[k] = chainIsAtStart
|
||||||
|
? words[k].toLowerCase()
|
||||||
|
: words[k].toUpperCase();
|
||||||
|
}
|
||||||
|
i = j;
|
||||||
|
} else {
|
||||||
|
// Normal multi-letter word
|
||||||
|
if (i === 0) {
|
||||||
|
// first word: all lower
|
||||||
|
result[i] = words[i].toLowerCase();
|
||||||
|
} else {
|
||||||
|
// subsequent words: capitalize first letter
|
||||||
|
result[i] = words[i][0].toUpperCase() +
|
||||||
|
words[i].slice(1).toLowerCase();
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
function lowerToSnakeCase(str: string) {
|
function lowerToSnakeCase(str: string) {
|
||||||
@@ -88,10 +124,10 @@ function coerceCaseToLower(str: string, caseType: CaseType) {
|
|||||||
case "macro":
|
case "macro":
|
||||||
case "snake":
|
case "snake":
|
||||||
case "upper":
|
case "upper":
|
||||||
return str.replace("_", " ").toLowerCase();
|
return str.replaceAll("_", " ").toLowerCase();
|
||||||
case "train":
|
case "train":
|
||||||
case "kebab":
|
case "kebab":
|
||||||
return str.replace("-", " ").toLowerCase();
|
return str.replaceAll("-", " ").toLowerCase();
|
||||||
default:
|
default:
|
||||||
return str.toLowerCase();
|
return str.toLowerCase();
|
||||||
}
|
}
|
||||||
@@ -124,3 +160,7 @@ export function toCase(str: string, toCase: CaseType) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (import.meta.main) {
|
||||||
|
console.log(toCase("SSN", "camel"));
|
||||||
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { PDFDocument } from "pdf-lib";
|
import { PDFDocument, PDFTextField } from "pdf-lib";
|
||||||
|
|
||||||
export async function loadPdfForm(path: string) {
|
export async function loadPdfForm(path: string) {
|
||||||
const pdfDoc = await loadPdf(path);
|
const pdfDoc = await loadPdf(path);
|
||||||
@@ -13,6 +13,11 @@ export async function loadPdf(path: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function savePdf(doc: PDFDocument, path: string) {
|
export async function savePdf(doc: PDFDocument, path: string) {
|
||||||
|
doc.getForm().getFields().forEach((field) => {
|
||||||
|
if (field instanceof PDFTextField) {
|
||||||
|
field.disableRichFormatting();
|
||||||
|
}
|
||||||
|
});
|
||||||
const pdfBytes = await doc.save();
|
const pdfBytes = await doc.save();
|
||||||
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
||||||
await Deno.writeFile(path, pdfBytes);
|
await Deno.writeFile(path, pdfBytes);
|
||||||
|
Reference in New Issue
Block a user