Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the CLI work again #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ typings/
# dotenv environment variables file
.env

# VSCode
.vscode/settings.json

19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ This command line tool delivers text translation capabilities to your console, p

## Install

```
$ yarn global add deepl-translator-cli
```shell
yarn global add deepl-translator-cli
```

## Usage examples

```shell
# Set your API key, or use the -k option to pass your key inline
export DEEPL_AUTH_KEY='<replace with your key>'

# Translate text into German
$ deepl translate -t 'DE' 'How do you do?'
deepl translate -t 'DE' 'How do you do?'

# Pipe text from standard input
$ echo 'How do you do?' | deepl translate -t 'DE'
echo 'How do you do?' | deepl translate -t 'DE'

# Detect language
$ deepl detect 'Wie geht es Ihnen?'
deepl detect 'Wie geht es Ihnen?'

# For help
$ deepl -h
$ deepl translate -h
$ deepl detect -h
deepl -h
deepl translate -h
deepl detect -h
```

## License
Expand Down
39 changes: 29 additions & 10 deletions deepl-detect.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
#!/usr/bin/env node
const program = require('commander');
const { detectLanguage } = require('deepl-translator');
const { version } = require('./package.json');
const chalk = require('chalk');
const program = require('commander');
const initTranslator = require('./translator');
const languages = require('./languages');

function detectLanguage(text) {
const translator = initTranslator();
if (translator.error != undefined) {
console.error(chalk.bold.red(translator.error));
return -1;
}
translator
.translateText(text, null, 'en-US')
.then(result => {
const languageCode = result.detectedSourceLang;
const languageName = result.detectedSourceLang
.split(',')
.map(code => languages[code.toUpperCase()])
.join(',');
console.log(
`${chalk.green(languageName)} (${chalk.magenta(languageCode)})`
);
})
.catch(error => console.error(chalk.bold.red(error.message)));
}

program
.version(version)
.arguments('<text>')
.action(text =>
detectLanguage(text)
.then(({ languageName, languageCode }) =>
console.log(
`${chalk.green(languageName)} (${chalk.magenta(languageCode)})`
)
)
.catch(error => console.error(chalk.bold.red(error.message)))
.option(
'-k, --authorizationKey <authorizationKey>',
'specify an authorization key or set a dedicated DEEPL_AUTH_KEY environment variable'
)
.parse(process.argv);

Expand All @@ -23,4 +40,6 @@ if (program.args.length > 2) {
program.help();
} else if (!program.args.length) {
program.help();
} else {
detectLanguage(program.args[0]);
}
36 changes: 23 additions & 13 deletions deepl-translate.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
#!/usr/bin/env node
const program = require('commander');
const { translate } = require('deepl-translator');
const { version } = require('./package.json');
const chalk = require('chalk');
const program = require('commander');
const initTranslator = require('./translator');

/**
* Reads the text to translate from the standard input.
* @return a promise resolved when the standard input
* has been read.
*/
const read = () => new Promise((resolve, reject) => {
let data = '';
process.stdin
.on('data', (chunk) => data += chunk)
.on('end', () => resolve(data.trim()));
});
const read = () =>
new Promise((resolve, reject) => {
let data = '';
process.stdin
.on('data', chunk => (data += chunk))
.on('end', () => resolve(data.trim()));
});

/**
* Executes the translation of the given text.
* @param {*} text the text to translate.
*/
const translateText = (text) => {
translate(text, program.targetLanguage, program.sourceLanguage)
.then(({ translation }) => console.log(chalk.green(translation)))
.catch(error => console.error(chalk.bold.red(error.message)))
const translateText = text => {
const translator = initTranslator();
if (translator.error != undefined)
console.error(chalk.bold.red(translator.error));
else
translator
.translateText(text, null, program.opts().targetLanguage)
.then(result => console.log(chalk.green(result.text)))
.catch(error => console.error(chalk.bold.red(error.message)));
};

program
.version(version)
.arguments('[text]')
.option(
.requiredOption(
'-t, --targetLanguage <targetLanguage>',
'target language code (EN, DE, FR, ES, IT, NL, PL, auto)'
)
.option(
'-s, --sourceLanguage [sourceLanguage]',
'source language code (EN, DE, FR, ES, IT, NL, PL, auto)'
)
.option(
'-k, --authorizationKey <authorizationKey>',
'specify an authorization key or set a dedicated DEEPL_AUTH_KEY environment variable'
)
.parse(process.argv);

if (program.rawArgs.length === 2) {
Expand Down
9 changes: 9 additions & 0 deletions languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
EN: 'English',
DE: 'German',
FR: 'French',
ES: 'Spanish',
IT: 'Italian',
NL: 'Dutch',
PL: 'Polish',
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deepl-translator-cli",
"version": "1.1.2",
"version": "1.2.0",
"description": "This module provides command line translation capabilities and is powered by DeepL (https://www.deepl.com/translator)",
"author": "Vladimir Setka <vsetka@gmail.com>",
"main": "deepl.js",
Expand Down Expand Up @@ -32,12 +32,12 @@
"cli"
],
"dependencies": {
"chalk": "2.1.0",
"commander": "2.11.0",
"deepl-translator": "^1.1.0"
"chalk": "^4.1.2",
"commander": "^9.4.1",
"deepl-node": "^1.7.2"
},
"devDependencies": {
"generate-changelog": "^1.5.0",
"prettier": "^1.6.1"
"generate-changelog": "^1.8.0",
"prettier": "^2.8.0"
}
}
18 changes: 18 additions & 0 deletions translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { version } = require('./package.json');
const chalk = require('chalk');
const program = require('commander');
const deepl = require('deepl-node');

module.exports = () => {
const authorizationKey =
program.opts().authorizationKey == undefined
? process.env.DEEPL_AUTH_KEY
: program.opts().authorizationKey;
if (authorizationKey == undefined) {
return {
error:
'Specify an authorization key or set a dedicated DEEPL_AUTH_KEY environment variable',
};
}
return new deepl.Translator(authorizationKey);
};
Loading