From 6988b578e04c320118850070f7f6d5ee6676b7e7 Mon Sep 17 00:00:00 2001 From: accoladesio Date: Mon, 25 Nov 2024 09:31:48 +0100 Subject: [PATCH 1/3] Refactor CLI tool: enhance readability and usability * Improved error handling and input validation --- bin/index.js | 98 ++++++++++++++++++++++++++--------------------- package-lock.json | 17 ++++++++ package.json | 1 + 3 files changed, 73 insertions(+), 43 deletions(-) diff --git a/bin/index.js b/bin/index.js index 172ecfb..91bd340 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,28 +1,41 @@ -#! /usr/bin/env node +#!/usr/bin/env node +import boxen from "boxen"; +import { translate } from "@vitalets/google-translate-api"; +import yargs from "yargs"; +import { hideBin } from "yargs/helpers"; +import figlet from "figlet"; +import chalk from "chalk"; + +/** + * Changes: + * 1. Used async/await for better readability and error handling. + * 2. Added default language (`en`) if none is provided. + * 3. Improved error handling with specific messages. + * 4. Added examples to the CLI usage. + * 5. Validated input arguments (non-empty strings). + * 6. Organized code for better readability and maintainability. + */ -import boxen from "boxen"; -import { translate } from "@vitalets/google-translate-api"; -import yargs from "yargs"; -import { hideBin } from "yargs/helpers"; -import figlet from "figlet"; -import chalk from "chalk"; const usage = chalk.magenta( - "\nUsage: tranpeer -l -s \n" + - boxen( - chalk.yellow("\n" + "Translates a sentence to specific language" + "\n"), - { padding: 1, borderColor: "green", dimBorder: true } - ) + - "\n" + "\nUsage: tranpeer -l -s \n" + + boxen( + chalk.yellow("\nTranslates a sentence to a specific language\n"), + { padding: 1, borderColor: "green", dimBorder: true } + ) + + "\nExamples:\n" + + chalk.cyan(" tranpeer -l fr -s 'Hello, world!'") + + "\n" + + chalk.cyan(" tranpeer --language es --sentence 'How are you?'") + + "\n" ); -const yargsInstance = yargs(hideBin(process.argv)); -yargsInstance +const yargsInstance = yargs(hideBin(process.argv)) .usage(usage) .option("l", { alias: "language", - describe: "Translate to language", + describe: "Translate to language (default: en)", type: "string", demandOption: false, }) @@ -33,45 +46,44 @@ yargsInstance demandOption: false, }) .help() - .parse(); -// console.log(yargs.argv); + .alias("h", "help"); + -function runCLI() { +async function runCLI () { const argv = yargsInstance.argv; - if (argv.language == null && argv.l == null) { + + if (!argv.language && !argv.sentence) { console.log( chalk.yellow(figlet.textSync("Tranpeer", { horizontalLayout: "full" })) ); - yargsInstance.showHelp().parse(); + yargsInstance.showHelp(); return; } - if (argv.sentence == null && argv.s == null) { - yargsInstance.showHelp().parse(); + const language = (argv.l || argv.language || "pt").toLowerCase(); + const sentence = argv.s || argv.sentence; + + // Validate inputs + if (!sentence || typeof sentence !== "string" || sentence.trim() === "") { + console.error(chalk.red("Error: A valid sentence must be provided.")); return; } - const language = argv.l || argv.language; - - const sentence = argv.s || argv.sentence; - - // console.log( language,sentence); - translate(sentence, { to: language.toLowerCase() }) - .then((res) => { - console.log( - "\n" + - boxen(chalk.green(sentence + "\n\n" + res.text), { - padding: 1, - borderColor: "green", - dimBorder: true, - }) + - "\n" - ); - }) - .catch((err) => { - console.error(err); - }); + try { + const res = await translate(sentence, { to: language }); + console.log( + "\n" + + boxen(chalk.green(`${sentence}\n\n${res.text}`), { + padding: 1, + borderColor: "green", + dimBorder: true, + }) + + "\n" + ); + } catch (err) { + console.error(chalk.red("Error translating sentence: ", err.message)); + } } runCLI(); diff --git a/package-lock.json b/package-lock.json index d92fc1d..9408074 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "boxen": "^8.0.1", "chalk": "^5.3.0", "figlet": "^1.8.0", + "tranpeer": "^2.1.0", "yargs": "^17.7.2" }, "bin": { @@ -4162,6 +4163,22 @@ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/tranpeer": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/tranpeer/-/tranpeer-2.1.0.tgz", + "integrity": "sha512-YV6HpXcup5n8jBozVBunteFNrSXyHuOZa3/7+L7XUipCL69ejlsIF11KJDm2qC/ctrQqQxiRmH3ylEd0dtFPYQ==", + "license": "ISC", + "dependencies": { + "@vitalets/google-translate-api": "^9.2.0", + "boxen": "^8.0.1", + "chalk": "^5.3.0", + "figlet": "^1.8.0", + "yargs": "^17.7.2" + }, + "bin": { + "tranpeer": "bin/index.js" + } + }, "node_modules/tslib": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", diff --git a/package.json b/package.json index 78007c3..ec7fd9b 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "boxen": "^8.0.1", "chalk": "^5.3.0", "figlet": "^1.8.0", + "tranpeer": "^2.1.0", "yargs": "^17.7.2" }, "devDependencies": { From 71bbc712e0c537d98535135e2510ead5bb1a0bff Mon Sep 17 00:00:00 2001 From: accoladesio Date: Mon, 25 Nov 2024 09:32:32 +0100 Subject: [PATCH 2/3] Refactor CLI tool: enhance readability and usability * Improved error handling and input validation --- bin/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/index.js b/bin/index.js index 91bd340..724be69 100755 --- a/bin/index.js +++ b/bin/index.js @@ -9,7 +9,7 @@ import chalk from "chalk"; /** * Changes: * 1. Used async/await for better readability and error handling. - * 2. Added default language (`en`) if none is provided. + * 2. Added default language (`pt`) if none is provided. * 3. Improved error handling with specific messages. * 4. Added examples to the CLI usage. * 5. Validated input arguments (non-empty strings). From ba97b860bc0ba6b4e4e3d9d8ec868673bf0ec768 Mon Sep 17 00:00:00 2001 From: accoladesio Date: Mon, 25 Nov 2024 09:57:11 +0100 Subject: [PATCH 3/3] Refactor CLI tool: enhance readability and usability * Improved error handling and input validation --- bin/index.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/bin/index.js b/bin/index.js index 724be69..8bc1e90 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,19 +1,18 @@ #!/usr/bin/env node -import boxen from "boxen"; -import { translate } from "@vitalets/google-translate-api"; -import yargs from "yargs"; -import { hideBin } from "yargs/helpers"; -import figlet from "figlet"; -import chalk from "chalk"; +import boxen from "boxen"; +import { translate } from "@vitalets/google-translate-api"; +import yargs from "yargs"; +import { hideBin } from "yargs/helpers"; +import figlet from "figlet"; +import chalk from "chalk"; /** * Changes: * 1. Used async/await for better readability and error handling. - * 2. Added default language (`pt`) if none is provided. - * 3. Improved error handling with specific messages. - * 4. Added examples to the CLI usage. - * 5. Validated input arguments (non-empty strings). - * 6. Organized code for better readability and maintainability. + * 2. Improved error handling with specific messages. + * 3. Added examples to the CLI usage. + * 4. Validated input arguments (non-empty strings). + * 5. Organized code for better readability and maintainability. */ @@ -35,7 +34,7 @@ const yargsInstance = yargs(hideBin(process.argv)) .usage(usage) .option("l", { alias: "language", - describe: "Translate to language (default: en)", + describe: "Translate to language", type: "string", demandOption: false, }) @@ -86,4 +85,4 @@ async function runCLI () { } } -runCLI(); +runCLI(); \ No newline at end of file