Skip to content

Commit

Permalink
Switch to yargs. (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored and Ace Nassri committed Nov 17, 2022
1 parent 6a89ecf commit fcbab53
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions translate/system-test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('translate:translate', function () {
assert.ifError(err);
assert(result, 'should have received a result');
assert.equal(result.language, 'en', 'should have detected english');
assert(console.log.calledWith('Detected %s with confidence %d', 'English', result.confidence));
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
done();
});
});
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('translate:translate', function () {
program.translateText(options, function (err, translation) {
assert.ifError(err);
assert.equal(translation, expected);
assert(console.log.calledWith('Translated text to %s', 'Russian'));
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
done();
});
});
Expand Down
18 changes: 13 additions & 5 deletions translate/test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('translate:translate', function () {
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
assert.strictEqual(callback.firstCall.args[1], sample.mocks.result, 'callback received result');
assert(console.log.calledWith('Detected %s with confidence %d', 'English', sample.mocks.result.confidence));
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', sample.mocks.result.confidence));
});

it('should handle error', function () {
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('translate:translate', function () {
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
assert.strictEqual(callback.firstCall.args[1], sample.mocks.translation, 'callback received result');
assert(console.log.calledWith('Translated text to %s', 'Russian'));
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
});

it('should handle error', function () {
Expand Down Expand Up @@ -166,23 +166,31 @@ describe('translate:translate', function () {

sinon.stub(program, 'detectLanguage');
program.main(['detect', text, '-k', apiKey]);
assert(program.detectLanguage.calledOnce);
assert.equal(program.detectLanguage.calledOnce, true);
assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [text, apiKey]);
});

it('should call listLanguages', function () {
var program = getSample().program;

sinon.stub(program, 'listLanguages');
program.main(['list', '-k', apiKey]);
assert(program.listLanguages.calledOnce);
assert.equal(program.listLanguages.calledOnce, true);
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [apiKey]);
});

it('should call translateText', function () {
var program = getSample().program;

sinon.stub(program, 'translateText');
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
assert(program.translateText.calledOnce);
assert.equal(program.translateText.calledOnce, true);
assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [{
text: text,
to: 'ru',
from: undefined,
apiKey: apiKey
}]);
});
});
});
16 changes: 11 additions & 5 deletions translate/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ function detectLanguage (text, apiKey, callback) {
return callback(err);
}

console.log('Detected %s with confidence %d', ISO6391.getName(result.language), result.confidence);
console.log(
'Detected %s (%s) with confidence %d',
ISO6391.getName(result.language),
result.language,
result.confidence
);
return callback(null, result);
});
}
Expand Down Expand Up @@ -104,7 +109,7 @@ function translateText (options, callback) {
return callback(err);
}

console.log('Translated text to %s', ISO6391.getName(options.to));
console.log('Translated text to %s:', ISO6391.getName(options.to));
return callback(null, translation);
});
}
Expand All @@ -113,6 +118,7 @@ function translateText (options, callback) {

// The command-line program
var cli = require('yargs');
var utils = require('../utils');

var program = module.exports = {
detectLanguage: detectLanguage,
Expand All @@ -127,10 +133,10 @@ var program = module.exports = {
cli
.demand(1)
.command('detect <text>', 'Detect the language of the provided text', {}, function (options) {
program.detectLanguage(options.text, options.apiKey, console.log);
program.detectLanguage(options.text, options.apiKey, utils.makeHandler(false));
})
.command('list', 'List available translation languages.', {}, function (options) {
program.listLanguages(options.apiKey, console.log);
program.listLanguages(options.apiKey, utils.makeHandler());
})
.command('translate <text>', 'Translate the provided text to the target language.', {
to: {
Expand All @@ -147,7 +153,7 @@ cli
description: 'The language of the source text.'
}
}, function (options) {
program.translateText(options, console.log);
program.translateText(utils.pick(options, ['text', 'to', 'from', 'apiKey']), utils.makeHandler());
})
.option('apiKey', {
alias: 'k',
Expand Down

0 comments on commit fcbab53

Please sign in to comment.