strof
Version:
a cli-tool for quering phone-books
99 lines (98 loc) • 3.09 kB
JavaScript
// src/strof.ts
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { exit } from "process";
import fs from "fs/promises";
import path from "path";
var argv = yargs(hideBin(process.argv)).scriptName("strof").usage("Usage: $0 -d <my_directory.txt> [options] <key_word_1> <key_word_2>").example([
["$0 -d ../path/to/my_book.txt media", 'search for "media"'],
["$0 -d ../path/to/my_book.txt media journal", 'seach for "media" and "journal"'],
["$0 -d ../path/to/my_book.txt media\\|ardennes", 'search for "media" or "ardennes"'],
[
"$0 -d ../path/to/my_book.txt media\\|ardennes journal",
'search for ("media" or "ardennes") and "journal"'
]
]).option("directory", {
alias: "d",
type: "string",
nargs: 1,
description: "Path to a phone-book",
array: true,
default: []
}).option("grep_v", {
alias: "v",
type: "string",
nargs: 1,
description: "Words used to unselect the displayed paragraph.",
array: true,
default: []
}).option("append_filename", {
alias: "a",
type: "boolean",
description: "Append at the end of each entry its filename. This is useful when working with several directory files to get more filter possibilities.",
default: false
}).command("$0 [keyWords..]", "Words used to select the displayed paragraph").strict().parseSync();
var addFilename = false;
if (argv.append_filename) {
addFilename = true;
}
if (argv.directory.length < 1) {
console.log("err520: Error no path to directory text files provided!");
exit(1);
}
var entries = [];
for (const pdf of argv.directory) {
const fileName = path.basename(pdf);
try {
const ftxt = await fs.readFile(pdf, { encoding: "utf8" });
const txt1 = ftxt.replace(/\n(\s*#.*\n)+/g, "\n");
const txt2 = txt1.replace(/#.*\n/g, "\n");
const txt3 = txt2.replace(/[ \t]+\n/g, "\n");
const txt4 = txt3.replace(/\n\n+/g, "\n\n");
const txt5 = txt4.replace(/^\n+/g, "");
const txt6 = txt5.replace(/\n+$/g, "");
const ltxt = txt6.split(/\n\n/);
if (addFilename) {
for (let idx = 0; idx < ltxt.length; idx++) {
ltxt[idx] += `
${fileName}`;
}
}
entries.push(...ltxt);
} catch (err) {
console.log(err);
console.log(`err560: Error while reading the directory file ${pdf}!`);
exit(1);
}
}
var seltxt = [];
if (Object.hasOwn(argv, "keyWords")) {
for (let idx = 0; idx < entries.length; idx++) {
let selectIt = true;
for (const kw of argv.keyWords) {
const regex = new RegExp(kw, "i");
if (!regex.test(entries[idx])) {
selectIt = false;
}
}
for (const gv of argv.grep_v) {
const regex = new RegExp(gv, "i");
if (regex.test(entries[idx])) {
selectIt = false;
}
}
if (selectIt) {
seltxt.push(entries[idx]);
}
}
} else {
console.log("err598: Error no key-words provided");
exit(1);
}
for (let idx = 0; idx < seltxt.length; idx++) {
console.log(`${idx + 1}:`);
console.log(seltxt[idx] + "\n");
}
console.log(`Found entries: ${seltxt.length}`);
//# sourceMappingURL=strof.js.map