Skip to content

Commit

Permalink
Use ES6 today! 😍
Browse files Browse the repository at this point in the history
  • Loading branch information
SFantasy committed Apr 21, 2015
1 parent 56a2f71 commit 2722a49
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 51 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion bin/ntrans
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

var pkg = require('../index');
var pkg = require('../compiled');

var param = process.argv[2] ? process.argv[2] : '';

Expand Down
66 changes: 66 additions & 0 deletions compiled.js
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'];
2 changes: 1 addition & 1 deletion config.js
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="
}
};
41 changes: 0 additions & 41 deletions index.js

This file was deleted.

20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}
46 changes: 46 additions & 0 deletions source.es6
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.')
}
});

}

0 comments on commit 2722a49

Please sign in to comment.