From ab7bb0de516137f8c6b6da25b99b94578982474a Mon Sep 17 00:00:00 2001 From: Rangga Fajar Oktariansyah <86386385+FajarKim@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:45:00 +0700 Subject: [PATCH] chore: remove the `printf` module file and replace it with the `process.stderr.write()` function (#6) --- bin/cli.js | 70 ++++++++++++++++++++++++------------------------- lib/printf.d.ts | 18 ------------- lib/printf.js | 22 ---------------- 3 files changed, 35 insertions(+), 75 deletions(-) delete mode 100644 lib/printf.d.ts delete mode 100644 lib/printf.js diff --git a/bin/cli.js b/bin/cli.js index 32a0d5a..daf228e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -6,16 +6,16 @@ const checknet = require("@barudakrosul/internet-available"); const millify = require("millify"); const { program } = require("commander"); const gcrypt = require("../index"); -const printf = require("../lib/printf"); const latestVersion = require("../lib/latest-version"); const packageJsonPath = path.join(__dirname, "../package.json"); const packageJson = require(packageJsonPath); -const __program = String(process.argv.slice(1,2)).replace(/.+\//g, "").replace(/(\.[^.]+)?$/, ""); +const __program = String(process.argv.slice(1, 2)).replace(/.+\//g, "").replace(/(\.[^.]+)?$/, ""); const url = "https://raw.githubusercontent.com/BarudakRosul/go-crypt/master/package.json"; try { program + .name(__program) .version(`Go-crypt v${packageJson.version}`) .description("Go-crypt is simple encryption and decryption using PBKDF2, zlib, and AES-256-GCM.") .option("-f, --file ", "input file name for encryption or decryption") @@ -59,11 +59,11 @@ try { } if (verbose) { - printf("Buffer size : 8192"); - printf(`Original bytes : ${originalBytes} bytes`); - printf(`Output bytes : ${outputBytes} bytes`); - printf(`Timeout : ${timeout} sec`); - printf(`Decrypted : ${millify.millify(percentDecrypted, { precision: 2})}%`); + process.stderr.write("Buffer size : 8192\n"); + process.stderr.write(`Original bytes : ${originalBytes} bytes\n`); + process.stderr.write(`Output bytes : ${outputBytes} bytes\n`); + process.stderr.write(`Timeout : ${timeout} sec\n`); + process.stderr.write(`Decrypted : ${millify.millify(percentDecrypted, { precision: 2})}%\n`); } if (stdout) { @@ -75,8 +75,8 @@ try { } fs.unlinkSync(file); fs.writeFileSync(output, decrypted); - printf(`${__program}: decryption successfully`); - printf(`File saved as '${output}'`); + console.log(`${__program}: decryption successfully`); + console.log(`File saved as '${output}'`); process.exit(0); } } else { @@ -90,8 +90,8 @@ try { process.stdin.on("end", () => { if (inputText === null || inputText === "") { - printf(`${__program}: missing operand`); - printf(`Try '${__program} --help' for more information`); + process.stderr.write(`${__program}: missing operand\n`); + process.stderr.write(`Try '${__program} --help' for more information\n`); process.exit(1); } @@ -113,11 +113,11 @@ try { } if (verbose) { - printf("Buffer size : 8192"); - printf(`Original bytes : ${originalBytes} bytes`); - printf(`Output bytes : ${outputBytes} bytes`); - printf(`Timeout : ${timeout} sec`); - printf(`Decrypted : ${millify.millify(percentDecrypted, { precision: 2})}%`); + process.stderr.write("Buffer size : 8192\n"); + process.stderr.write(`Original bytes : ${originalBytes} bytes\n`); + process.stderr.write(`Output bytes : ${outputBytes} bytes\n`); + process.stderr.write(`Timeout : ${timeout} sec\n`); + process.stderr.write(`Decrypted : ${millify.millify(percentDecrypted, { precision: 2})}%\n`); } if (! output) { @@ -125,8 +125,8 @@ try { process.exit(0); } else { fs.writeFileSync(output, decrypted); - printf(`${__program}: decryption successfully`); - printf(`File saved as '${output}'`); + console.log(`${__program}: decryption successfully`); + console.log(`File saved as '${output}'`); process.exit(0); } }); @@ -147,11 +147,11 @@ try { } if (verbose) { - printf("Buffer size : 8192"); - printf(`Original bytes : ${originalBytes} bytes`); - printf(`Output bytes : ${outputBytes} bytes`); - printf(`Timeout : ${timeout} sec`); - printf(`Encrypted : ${millify.millify(percentEncrypted, { precision: 2})}%`); + process.stderr.write("Buffer size : 8192\n"); + process.stderr.write(`Original bytes : ${originalBytes} bytes\n`); + process.stderr.write(`Output bytes : ${outputBytes} bytes\n`); + process.stderr.write(`Timeout : ${timeout} sec\n`); + process.stderr.write(`Encrypted : ${millify.millify(percentEncrypted, { precision: 2})}%\n`); } if (stdout) { @@ -163,8 +163,8 @@ try { } fs.unlinkSync(file); fs.writeFileSync(output, encrypted); - printf(`${__program}: encryption successfully`); - printf(`File saved as '${output}'`); + console.log(`${__program}: encryption successfully`); + console.log(`File saved as '${output}'`); process.exit(0); } } else { @@ -178,8 +178,8 @@ try { process.stdin.on("end", () => { if (inputText === null || inputText === "") { - printf(`${__program}: missing operand`); - printf(`Try '${__program} --help' for more information`); + process.stderr.write(`${__program}: missing operand`); + process.stderr.write(`Try '${__program} --help' for more information`); process.exit(1); } @@ -196,11 +196,11 @@ try { } if (verbose) { - printf("Buffer size : 8192"); - printf(`Original bytes : ${originalBytes} bytes`); - printf(`Output bytes : ${outputBytes} bytes`); - printf(`Timeout : ${timeout} sec`); - printf(`Encrypted : ${millify.millify(percentEncrypted, { precision: 2})}%`); + process.stderr.write("Buffer size : 8192\n"); + process.stderr.write(`Original bytes : ${originalBytes} bytes\n`); + process.stderr.write(`Output bytes : ${outputBytes} bytes\n`); + process.stderr.write(`Timeout : ${timeout} sec\n`); + process.stderr.write(`Encrypted : ${millify.millify(percentEncrypted, { precision: 2})}%\n`); } if (! output) { @@ -208,8 +208,8 @@ try { process.exit(0); } else { fs.writeFileSync(output, encrypted); - printf(`${__program}: encryption successfully`); - printf(`File saved as '${output}'`); + console.log(`${__program}: encryption successfully`); + console.log(`File saved as '${output}'`); process.exit(0); } }); @@ -223,7 +223,7 @@ try { }) .parse(process.argv); } catch (error) { - printf(`${__program}: ${error.message}`); + process.stderr.write(`${__program}: ${error.message}\n`); const errorCode = typeof error.code === "number" ? error.code : 1; process.exit(errorCode); } diff --git a/lib/printf.d.ts b/lib/printf.d.ts deleted file mode 100644 index be2b92f..0000000 --- a/lib/printf.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Module declaration for printing text. - * - * @module printf - */ -declare module "printf" { - /** - * Interface representing the print text function. - */ - interface Printf { - (text: string): Buffer | string; - } - - const printf: Printf; - - export { printf, Printf }; - export default printf; -} diff --git a/lib/printf.js b/lib/printf.js deleted file mode 100644 index ce98a0c..0000000 --- a/lib/printf.js +++ /dev/null @@ -1,22 +0,0 @@ -const { execSync } = require("child_process"); - -/** - * Prints the specified text. - * - * @param {string} text - The text to be printed. - * @throws {Error} Throws an error if the input text is null, undefined, an empty string, or contains only a newline character. - * @throws {Error} Throws an error if there is an issue executing the printf command. - */ -function printf(text) { - if (text === null || text === undefined || text === "" || text === "\n") { - throw Error("Input your text for print"); - } - - try { - execSync(`printf >&2 '%s\\n' "${text}"`, { encoding: "utf-8" }); - } catch(error) { - throw Error(error.message); - } -} - -module.exports = printf;