Skip to content

Commit

Permalink
Added database (#3)
Browse files Browse the repository at this point in the history
* Added database

* Removed console / Added TODO

* Fixed lint error
  • Loading branch information
ovekyc authored and yuda1124 committed Jan 9, 2017
1 parent 94e96c7 commit 8e7bfd4
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"react",
"stage-0"
],
"plugins": ["transform-decorators-legacy"]
"plugins": [
"transform-decorators-legacy",
"transform-regenerator"
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ jspm_packages
log.txt

text-logger-*

*.db
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ Support x64 processor only

npm run lint
npm run test

If you face error message like below
Error: Module version mismatch. Expected 50, got 48
try this

npm rebuild --runtime=electron --target=1.4.3 --disturl=https://atom.io/download/atom-shell --build-from-source
4 changes: 4 additions & 0 deletions app/core/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const SERVICE = {
GOOGLE: 'google',
GLOSBE: 'glosbe'
};
7 changes: 7 additions & 0 deletions app/core/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class {
constructor(source, google, glosbe) {
this.key = source;
this.google = google;
this.glosbe = glosbe;
}
}
43 changes: 43 additions & 0 deletions app/core/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {clipboard} from 'electron';
import levelup from 'level';
import googleTranslate from './google-translate';
import glosbeTranslate from './glosbe-translate';
import Contents from './content';

// TODO: remove, for backward compatibility
import fs from 'fs';
import setting from '../setting.json';
import {SERVICE} from './constants';
import {logPath} from './main';
export function saveContents() {
const clip = clipboard.readText();
if (setting.enableServiceHook) {
let service;
if (setting.service === SERVICE.GOOGLE) service = googleTranslate;
else if (setting.service === SERVICE.GLOSBE) service = glosbeTranslate;
else {
// TODO: ERROR handle
}
service(clip)
.then(translated => `${clip} => ${translated}\n`)
.then(contents => {
fs.appendFileSync(logPath, contents);
});
return;
}
fs.appendFileSync(logPath, clip);
}

const db = levelup('log.db', {valueEncoding: 'json'});
export async function store(cb) {
const clip = clipboard.readText();

const tranGoogle = await googleTranslate(clip);
const tranGlosbe = await glosbeTranslate(clip);

const contents = new Contents(clip, tranGoogle, tranGlosbe);
db.put(contents.key, contents, err => {
if (err) throw err;
cb(contents);
});
}
32 changes: 29 additions & 3 deletions app/core/glosbe-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,33 @@ export default function (source) {
});
res.on('end', () => resolve(buffer.toString()));
});
// }).then(buffer => {
// const json = JSON.parse(buffer);
}).catch(console.log); // eslint-disable-line no-console
}).then(buffer => {
const json = JSON.parse(buffer);
if (json.result !== 'ok') throw new Error('Request fail');
if (json.tuc.length === 0) return 'N/A';

return json.tuc[0].phrase.text;
});
}

/**
* response format
* {
* "result" : "ok",
* "tuc" : [ {
* "phrase" : {
* "text" : "돌아오다",
* "language" : "ko"
* },
* "meanings" : [ {
* "language" : "en",
* "text" : "To go there where one was before."
* }, {
* "language" : "en",
* "text" : "to come or go back"
* } ],
* "meaningId" : 8308189710182875766,
* "authors" : [ 13 ]
* }, {
* .....
*/
2 changes: 1 addition & 1 deletion app/core/google-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export default function (source) {
const str = refineJsonString(buffer);
const json = JSON.parse(str);
return json[0][0][0];
}).catch(console.log); // eslint-disable-line no-console
});
}
52 changes: 24 additions & 28 deletions app/core/main.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
import {app, globalShortcut, clipboard, ipcMain} from 'electron';
import {app, globalShortcut, ipcMain} from 'electron';
import fs from 'fs';
import menubar from 'menubar';
import {store, saveContents} from './database';
import setting from '../setting.json';
import googleTranslate from './google-translate';
import glosbeTranslate from './glosbe-translate';

const dir = process.cwd();
const logPath = `${dir}/log.txt`;
const indexPath = `file://${dir}/app/view/popup.html`;
const settingPath = `${dir}/app/setting.json`;
const SERVICE = {
GOOGLE: 'google',
GLOSBE: 'glosbe'
};

const mb = menubar({index: indexPath});

function saveContents() {
const clip = clipboard.readText();
console.log(clip); // eslint-disable-line no-console
// TODO: remove, for backward compatibility
export const logPath = `${dir}/log.txt`;

if (setting.enableServiceHook) {
let service;
if (setting.service === SERVICE.GOOGLE) service = googleTranslate;
else if (setting.service === SERVICE.GLOSBE) service = glosbeTranslate;
else {
// TODO: ERROR handle
}
service(clip)
.then(translated => `${clip} => ${translated}\n`)
.then(contents => {
fs.appendFileSync(logPath, contents);
});
return;
}
fs.appendFileSync(logPath, clip);
function notifyDone(contents) {
// TODO: impl
console.log(contents);
}

function notifyErr(err) {
// TODO: impl
console.log(err);
}

mb.on('ready', () => {
globalShortcut.register('Control+Command+S', saveContents);
globalShortcut.register('Control+Command+S', () => {
// TODO: remove, for backward compatibility
saveContents();

try {
store(notifyDone);
} catch (err) {
notifyErr(err);
}
});

if (!globalShortcut.isRegistered('Control+Command+S')) {
// TODO: alert and end process
}

ipcMain.on('delete-log', (event, arg) => {
fs.writeFile(logPath, arg);
});
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// install babel hooks in the main process
require('babel-register');
require('babel-polyfill');
require('./app/core/main.js');
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"repository": "[email protected]:ovekyc/textlogger.git",
"scripts": {
"start": "electron .",
"start": "./node_modules/.bin/electron .",
"lint": "./node_modules/.bin/eslint app/**/*.js",
"test": "npm run lint"
},
Expand All @@ -20,13 +20,16 @@
"babel-preset-stage-0": "^6.16.0",
"babel-register": "^6.18.0",
"electron": "^1.4.3",
"level": "^1.5.0",
"menubar": "^5.2.0",
"react": "^15.4.1",
"react-addons-update": "^15.4.1",
"react-dom": "^15.4.1"
},
"devDependencies": {
"babel-eslint": "^7.1.1",
"babel-polyfill": "^6.20.0",
"electron-rebuild": "^1.5.5",
"eslint": "^3.12.2",
"eslint-plugin-react": "^6.8.0",
"pre-commit": "^1.2.2"
Expand Down

0 comments on commit 8e7bfd4

Please sign in to comment.