Skip to content

Commit

Permalink
Convert entire library to 2 space indents
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz committed Feb 2, 2023
1 parent ff22b90 commit 1b8caf7
Show file tree
Hide file tree
Showing 16 changed files with 1,747 additions and 1,757 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
22 changes: 11 additions & 11 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
extends: ["@sparticuz/eslint-config"],
parserOptions: {
ecmaVersion: 2022,
project: "./tsconfig.json",
sourceType: "module",
},
root: true,
rules: {
"dot-notation": "off",
"no-console": "off",
},
extends: ["@sparticuz/eslint-config"],
parserOptions: {
ecmaVersion: 2022,
project: "./tsconfig.json",
sourceType: "module",
},
root: true,
rules: {
"dot-notation": "off",
"no-console": "off",
},
};
24 changes: 12 additions & 12 deletions src/convert-field-json-to-fdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import type { FormField } from "./generate-field-json.js";
* @returns An object with converted Form Fields
*/
export default (formFields: FormField[]): Record<string, string> => {
const fields: Record<string, string> = {};
for (const row of formFields) {
if (typeof row.fieldValue === "boolean") {
if (row.fieldValue) {
fields[row.title] = "Yes";
} else {
fields[row.title] = "Off";
}
} else {
fields[row.title] = row.fieldValue;
}
const fields: Record<string, string> = {};
for (const row of formFields) {
if (typeof row.fieldValue === "boolean") {
if (row.fieldValue) {
fields[row.title] = "Yes";
} else {
fields[row.title] = "Off";
}
} else {
fields[row.title] = row.fieldValue;
}
return fields;
}
return fields;
};
12 changes: 6 additions & 6 deletions src/generate-fdf-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import generateFieldJson from "./generate-field-json.js";
* @returns A json object
*/
export default async (sourceFile: string): Promise<Record<string, string>> => {
const formFields = await generateFieldJson(sourceFile);
const json: Record<string, string> = {};
for (const row of formFields) {
json[row.title] = row.fieldValue as string;
}
return json;
const formFields = await generateFieldJson(sourceFile);
const json: Record<string, string> = {};
for (const row of formFields) {
json[row.title] = row.fieldValue as string;
}
return json;
};
109 changes: 50 additions & 59 deletions src/generate-field-json.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { spawn } from "node:child_process";

export interface FormField {
fieldDefault: string;
fieldFlags: string;
fieldMaxLength: string | number;
fieldOptions: string[];
fieldType: string;
fieldValue: string | boolean;
title: string;
fieldDefault: string;
fieldFlags: string;
fieldMaxLength: string | number;
fieldOptions: string[];
fieldType: string;
fieldValue: string | boolean;
title: string;
}

const getFieldOptions = (field: string): string[] => {
const regOptions = /(FieldStateOption: ([^\n]*))/g;
const matches = field.match(regOptions);
const options: string[] = [];
if (matches) {
for (const match of matches) {
options.push(
/FieldStateOption: ([^\n]*)/.exec(match)?.[1].trim() as string
);
}
const regOptions = /(FieldStateOption: ([^\n]*))/g;
const matches = field.match(regOptions);
const options: string[] = [];
if (matches) {
for (const match of matches) {
options.push(
/FieldStateOption: ([^\n]*)/.exec(match)?.[1]?.trim() as string
);
}
return options.sort();
}
return options.sort();
};

/**
Expand All @@ -30,52 +30,43 @@ const getFieldOptions = (field: string): string[] => {
* @returns A FormField object
*/
export default (sourceFile: string): Promise<FormField[]> => {
const regName = /FieldName: ([^\n]*)/;
const regType = /FieldType: ([\t .A-Za-z]+)/;
const regFlags = /FieldFlags: ([\d\t .]+)/;
const regMaxLength = /FieldMaxLength: ([\d\t .]+)/;
const regValue = /FieldValue: ([^\n]*)/;
const regDefault = /FieldValueDefault: ([^\n]*)/;
const regOptions = /(FieldStateOption: ([^\n]*))/g;
const fieldArray: FormField[] = [];
const regName = /FieldName: ([^\n]*)/;
const regType = /FieldType: ([\t .A-Za-z]+)/;
const regFlags = /FieldFlags: ([\d\t .]+)/;
const regMaxLength = /FieldMaxLength: ([\d\t .]+)/;
const regValue = /FieldValue: ([^\n]*)/;
const regDefault = /FieldValueDefault: ([^\n]*)/;
const regOptions = /(FieldStateOption: ([^\n]*))/g;
const fieldArray: FormField[] = [];

return new Promise((resolve, reject) => {
const childProcess = spawn("pdftk", [
sourceFile,
"dump_data_fields_utf8",
]);
let output = "";
return new Promise((resolve, reject) => {
const childProcess = spawn("pdftk", [sourceFile, "dump_data_fields_utf8"]);
let output = "";

childProcess.on("error", (error) => reject(error));
childProcess.stdout.on("error", (error) => reject(error));
childProcess.stderr.on("error", (error) => reject(error));
childProcess.stdin.on("error", (error) => reject(error));
childProcess.on("error", (error) => reject(error));
childProcess.stdout.on("error", (error) => reject(error));
childProcess.stderr.on("error", (error) => reject(error));
childProcess.stdin.on("error", (error) => reject(error));

childProcess.stdout.on("data", (data) => {
output += data;
});
childProcess.stdout.on("data", (data) => {
output += data;
});

childProcess.stdout.on("end", () => {
const fields = output.split("---").slice(1);
for (const field of fields) {
fieldArray.push({
fieldDefault:
(regDefault.exec(field)?.[1].trim() as string) ?? "",
fieldFlags:
(regFlags.exec(field)?.[1].trim() as string) ?? "",
fieldMaxLength:
(regMaxLength.exec(field)?.[1].trim() as string) ?? "",
fieldOptions: regOptions.test(field)
? getFieldOptions(field)
: [],
fieldType:
(regType.exec(field)?.[1].trim() as string) ?? "",
fieldValue:
(regValue.exec(field)?.[1].trim() as string) ?? "",
title: (regName.exec(field)?.[1].trim() as string) ?? "",
});
}
resolve(fieldArray);
childProcess.stdout.on("end", () => {
const fields = output.split("---").slice(1);
for (const field of fields) {
fieldArray.push({
fieldDefault: (regDefault.exec(field)?.[1]?.trim() as string) ?? "",
fieldFlags: (regFlags.exec(field)?.[1]?.trim() as string) ?? "",
fieldMaxLength:
(regMaxLength.exec(field)?.[1]?.trim() as string) ?? "",
fieldOptions: regOptions.test(field) ? getFieldOptions(field) : [],
fieldType: (regType.exec(field)?.[1]?.trim() as string) ?? "",
fieldValue: (regValue.exec(field)?.[1]?.trim() as string) ?? "",
title: (regName.exec(field)?.[1]?.trim() as string) ?? "",
});
}
resolve(fieldArray);
});
});
};
105 changes: 52 additions & 53 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import createFdf from "./fdf.js";
* @returns a boolean
* */
const toFile = (
promised: Promise<Readable>,
path: string
promised: Promise<Readable>,
path: string
): Promise<boolean> => {
return new Promise((resolve, reject) => {
promised
// eslint-disable-next-line promise/always-return
.then((outputStream) => {
const output = createWriteStream(path);
outputStream.pipe(output);
outputStream.on("close", () => {
output.end();
resolve(true);
});
})
.catch((error) => reject(error));
});
return new Promise((resolve, reject) => {
promised
// eslint-disable-next-line promise/always-return
.then((outputStream) => {
const output = createWriteStream(path);
outputStream.pipe(output);
outputStream.on("close", () => {
output.end();
resolve(true);
});
})
.catch((error) => reject(error));
});
};

/**
Expand All @@ -36,54 +36,53 @@ const toFile = (
* @returns A promise of a Readable stream
*/
export default (
sourceFile: string,
fieldValues: any,
extraArguments: string[] | false = ["flatten"]
sourceFile: string,
fieldValues: any,
extraArguments: string[] | false = ["flatten"]
): Promise<Readable> => {
const promised: Promise<Readable> = new Promise((resolve, reject) => {
// Check to see if sourceFile exists!
access(sourceFile, constants.F_OK || constants.R_OK, (error) => {
if (error)
reject(new Error("File does not exist or is not readable"));
});
const promised: Promise<Readable> = new Promise((resolve, reject) => {
// Check to see if sourceFile exists!
access(sourceFile, constants.F_OK || constants.R_OK, (error) => {
if (error) reject(new Error("File does not exist or is not readable"));
});

// Generate the data from the field values.
const FDFinput = createFdf(fieldValues);
// Generate the data from the field values.
const FDFinput = createFdf(fieldValues);

const runArguments = [sourceFile, "fill_form", "-", "output", "-"];
const runArguments = [sourceFile, "fill_form", "-", "output", "-"];

if (typeof extraArguments === "object") {
for (const argument of extraArguments) {
runArguments.push(argument);
}
}
if (typeof extraArguments === "object") {
for (const argument of extraArguments) {
runArguments.push(argument);
}
}

const childProcess = spawn("pdftk", runArguments);
const childProcess = spawn("pdftk", runArguments);

childProcess.on("error", (error) => reject(error));
childProcess.stdout.on("error", (error) => reject(error));
childProcess.stderr.on("error", (error) => reject(error));
childProcess.stdin.on("error", (error) => reject(error));
childProcess.on("error", (error) => reject(error));
childProcess.stdout.on("error", (error) => reject(error));
childProcess.stderr.on("error", (error) => reject(error));
childProcess.stdin.on("error", (error) => reject(error));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sendData = (chunk: any) => {
childProcess.stdout.pause();
childProcess.stdout.unshift(chunk);
resolve(childProcess.stdout);
childProcess.stdout.removeListener("data", sendData);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const sendData = (chunk: any) => {
childProcess.stdout.pause();
childProcess.stdout.unshift(chunk);
resolve(childProcess.stdout);
childProcess.stdout.removeListener("data", sendData);
};

childProcess.stdout.on("data", sendData);
childProcess.stdout.on("data", sendData);

// now pipe FDF to pdftk
childProcess.stdin.write(FDFinput);
childProcess.stdin.end();
});
// now pipe FDF to pdftk
childProcess.stdin.write(FDFinput);
childProcess.stdin.end();
});

// bind convenience method toFile for chaining
// @ts-expect-error We are adding toFile to the promise
promised.toFile = toFile.bind(undefined, promised);
return promised;
// bind convenience method toFile for chaining
// @ts-expect-error We are adding toFile to the promise
promised.toFile = toFile.bind(undefined, promised);
return promised;
};

export { default as generateFDFTemplate } from "./generate-fdf-template.js";
Expand Down
Loading

0 comments on commit 1b8caf7

Please sign in to comment.