-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
|
||
// Module dependencies | ||
|
||
var _request = require('request'); | ||
|
||
var _request2 = _interopRequireWildcard(_request); | ||
|
||
var _program = require('commander'); | ||
|
||
var _program2 = _interopRequireWildcard(_program); | ||
|
||
var _lib = require('./lib'); | ||
|
||
var _lib2 = _interopRequireWildcard(_lib); | ||
|
||
var _config = require('./config'); | ||
|
||
var _config2 = _interopRequireWildcard(_config); | ||
|
||
var _pkg = require('./package.json'); | ||
|
||
var _pkg2 = _interopRequireWildcard(_pkg); | ||
|
||
'use strict'; | ||
exports['default'] = function (params) { | ||
|
||
_program2['default'].version(_pkg2['default'].version).usage('[word to be translated]'); | ||
|
||
_program2['default'].on('--help', function () { | ||
console.log(' Example:'); | ||
console.log('\n $ ntrans professor\n'); | ||
}); | ||
|
||
_program2['default'].parse(process.argv); | ||
|
||
if (params.length === 0) { | ||
_program2['default'].help(); | ||
|
||
return; | ||
} | ||
|
||
_request2['default'](_config2['default'].src + encodeURIComponent(params), function (err, res, body) { | ||
var data; | ||
|
||
if (!err && res.statusCode === 200) { | ||
data = JSON.parse(body); | ||
|
||
if (data.errorCode == 0) { | ||
_lib2['default'].output(data); | ||
} else { | ||
console.log('[ERROR]'.red + ' Youdao API request error.'); | ||
} | ||
} else { | ||
console.log('[ERROR]'.red + ' Youdao API request error.'); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
"src": "http://fanyi.youdao.com/openapi.do?keyfrom=node-translator&key=2058911035&type=data&doctype=json&version=1.1&q=" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.') | ||
} | ||
}); | ||
|
||
} |