Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
#! /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. 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.
*/


const usage = chalk.magenta(
"\nUsage: tranpeer -l <language> -s <sentence> \n" +
boxen(
chalk.yellow("\n" + "Translates a sentence to specific language" + "\n"),
{ padding: 1, borderColor: "green", dimBorder: true }
) +
"\n"
"\nUsage: tranpeer -l <language> -s <sentence>\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",
Expand All @@ -33,45 +45,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();
runCLI();
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down