-
Notifications
You must be signed in to change notification settings - Fork 4
/
source.es6
46 lines (34 loc) · 922 Bytes
/
source.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
// Module dependencies
import request from 'request';
import program from 'commander';
import lib from './lib';
import config from './config';
import pkg from './package.json';
export default function (params) {
program
.version(pkg.version)
.usage('[word to be translated]');
program.on('--help', () => {
console.log(' Example:');
console.log('\n $ ntrans professor\n');
});
program.parse(process.argv);
if (params.length === 0) {
program.help();
return;
}
request(config.src + encodeURIComponent(params), (err, res, body) => {
var data;
if (!err && res.statusCode === 200) {
data = JSON.parse(body);
if (data.errorCode == 0) {
lib.output(data);
} else {
console.log('[ERROR]'.red + ' Youdao API request error.');
}
} else {
console.log('[ERROR]'.red + ' Youdao API request error.')
}
});
}