diff --git a/README.md b/README.md index d98f4d3..671d3a5 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,14 @@ $ ntrans chrome 翻译:n. 铬,铬合金;铬黄;谷歌浏览器 ``` +## Develop + +`node-translator` has been refactored with ECMAScript 6 using [babeljs](https://babeljs.io). + +1. Clone the project from github +2. Run `npm install` to install dependencies +3. Run `npm run build` to build package from ES6, or just run `npm run build-watch` + ## License The MIT License diff --git a/bin/ntrans b/bin/ntrans index 5f3dfc9..ed1b067 100644 --- a/bin/ntrans +++ b/bin/ntrans @@ -1,6 +1,6 @@ #!/usr/bin/env node -var pkg = require('../index'); +var pkg = require('../compiled'); var param = process.argv[2] ? process.argv[2] : ''; diff --git a/compiled.js b/compiled.js new file mode 100644 index 0000000..74e691c --- /dev/null +++ b/compiled.js @@ -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']; diff --git a/config.js b/config.js index d1d04e5..645fb48 100644 --- a/config.js +++ b/config.js @@ -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=" -} +}; \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index 2b69733..0000000 --- a/index.js +++ /dev/null @@ -1,41 +0,0 @@ -// Entrance of the package -// -// Module dependencies -var request = require('request'), - program = require('commander'); - -var lib = require('./lib'), - url = require('./config').src; - -module.exports = function(param) { - - program - .version('0.1.3') - .usage('[word to be translated]'); - - program.on('--help', function() { - console.log(' Example:'); - console.log('\n $ ntrans professor\n'); - }); - - program.parse(process.argv); - - if (param.length === 0) { - program.help(); - - return; - } - - request(url + encodeURIComponent(param), function(err, res, body) { - var data; - - if (!err && res.statusCode === 200) { - data = JSON.parse(body); - if (data.errorCode == 0) { - lib.output(data); - } else { - console.error('[ERROR]'.red + ' Youdao API request error.'); - } - } - }); -}; diff --git a/package.json b/package.json index 5c395f1..bfa536e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ntrans", - "version": "0.2.2", + "version": "0.3.0", "description": "Yet another Chinese-English translation tool written in Node.", "main": "index.js", "repository": { @@ -10,26 +10,30 @@ "keywords": [ "translate", "fanyi", - "youdao" + "youdao", + "dictionary" ], "author": [ { "name": "fantasy", - "email": "fantasyshao@icloud.com", - "url": "http://fantasy.codes" - }, - { - "name": "galeno", - "email": "lenghan1991@gmail.com" + "email": "forever.fantasy27@gmail.com", + "url": "http://blog.fantasy.codes" } ], "bin": { "ntrans": "./bin/ntrans" }, + "scripts": { + "build": "./node_modules/.bin/babel source.es6 --out-file compiled.js", + "build-watch": "./node_modules/.bin/babel source.es6 --out-file compiled.js --watch" + }, "license": "MIT", "dependencies": { "colors": "^1.0.3", "commander": "^2.5.0", "request": "^2.51.0" + }, + "devDependencies": { + "babel": "^5.1.11" } } diff --git a/source.es6 b/source.es6 new file mode 100644 index 0000000..4f3c6ae --- /dev/null +++ b/source.es6 @@ -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.') + } + }); + +}