@deeplx/cli
Version:
The cli for [`@deeplx/core`](https://github.com/un-ts/deeplx/blob/master/packages/@deeplx/core), a powerful and easy-to-use yet free DeepL API client for Node.js using [DeepL](https://www.deepl.com) by porting [OwO-Network/DeepLX](https://github.com/OwO-N
38 lines • 1.75 kB
JavaScript
import './fetch.js';
import fs from 'node:fs/promises';
import { fileURLToPath, URL } from 'node:url';
import { translate, } from '@deeplx/core';
import { cjsRequire } from '@pkgr/core';
import { program } from 'commander';
const { version, description } = cjsRequire(fileURLToPath(new URL('../package.json', import.meta.url)));
const FALSY_VALUES = new Set(['0', 'false', 'n', 'no', 'off']);
program
.name('deeplx')
.version(version)
.description(description)
.option('-s, --source <text>', 'Source language of your text')
.requiredOption('-t, --target <text>', 'Target language of your desired text')
.option('--text <text>', 'Text to be translated')
.option('-f, --file <path>', 'File to be translated')
.option('--formal [boolean]', 'Whether to use formal (true) or informal (false) tone in translation. Default `undefined` respects source text tone.', (formal) => (formal == null ? formal : !FALSY_VALUES.has(formal)))
.option('--no-formal')
.action(async function () {
const { source, target, text, file, formal } = this.opts();
const isTextNil = text == null || text.trim() === '';
const isFileNil = file == null || file.trim() === '';
if (isTextNil && isFileNil) {
throw new Error('One of `text` or `file` option must be specified');
}
if (!isTextNil && !isFileNil) {
console.warn('Both `text` and `file` options provided, `text` will take precedence');
}
const translated = await translate(isTextNil ? await fs.readFile(file, 'utf8') : text, target, source, formal);
console.log(translated);
})
.parseAsync(process.argv)
.catch((err) => {
process.exitCode = 1;
console.error(err);
});
//# sourceMappingURL=cli.js.map