Skip to content

Commit

Permalink
Code style: Standard => XO ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuss committed Sep 20, 2016
1 parent c7c5780 commit 8a8fdac
Show file tree
Hide file tree
Showing 47 changed files with 847 additions and 720 deletions.
23 changes: 13 additions & 10 deletions app/auto-updater.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const { autoUpdater } = require('electron');
const { version } = require('./package');
const notify = require('./notify'); // eslint-disable-line no-unused-vars
const {autoUpdater} = require('electron');
const ms = require('ms');

const notify = require('./notify'); // eslint-disable-line no-unused-vars
const {version} = require('./package');

// accepted values: `osx`, `win32`
// https://nuts.gitbook.com/update-windows.html
const platform = 'darwin' === process.platform
? 'osx'
: process.platform;
const platform = process.platform === 'darwin' ?
'osx' :
process.platform;
const FEED_URL = `https://hyperterm-updates.now.sh/update/${platform}`;
let isInit = false;

function init () {
function init() {
autoUpdater.on('error', (err, msg) => {
console.error('Error fetching updates', msg + ' (' + err.stack + ')');
});
Expand All @@ -30,12 +31,14 @@ function init () {
}

module.exports = function (win) {
if (!isInit) init();
if (!isInit) {
init();
}

const { rpc } = win;
const {rpc} = win;

const onupdate = (ev, releaseNotes, releaseName) => {
rpc.emit('update available', { releaseNotes, releaseName });
rpc.emit('update available', {releaseNotes, releaseName});
};

autoUpdater.on('update-downloaded', onupdate);
Expand Down
27 changes: 16 additions & 11 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
const { dialog } = require('electron');
const { homedir } = require('os');
const { resolve } = require('path');
const { readFileSync, writeFileSync } = require('fs');
const gaze = require('gaze');
const {homedir} = require('os');
const {readFileSync, writeFileSync} = require('fs');
const {resolve} = require('path');
const vm = require('vm');

const {dialog} = require('electron');
const gaze = require('gaze');
const notify = require('./notify');

const path = resolve(homedir(), '.hyperterm.js');
const watchers = [];

let cfg = {};

function watch () {
function watch() {
gaze(path, function (err) {
if (err) throw err;
if (err) {
throw err;
}
this.on('changed', () => {
try {
if (exec(readFileSync(path, 'utf8'))) {
notify('HyperTerm configuration reloaded!');
watchers.forEach((fn) => fn());
watchers.forEach(fn => fn());
}
} catch (err) {
dialog.showMessageBox({
Expand All @@ -31,12 +34,14 @@ function watch () {
}

let _str; // last script
function exec (str) {
if (str === _str) return false;
function exec(str) {
if (str === _str) {
return false;
}
_str = str;
const script = new vm.Script(str);
const module = {};
script.runInNewContext({ module });
script.runInNewContext({module});
if (!module.exports) {
throw new Error('Error reading configuration: `module.exports` not set');
}
Expand Down
84 changes: 47 additions & 37 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
const { app, BrowserWindow, shell, Menu } = require('electron');
const createRPC = require('./rpc');
const createMenu = require('./menu');
const {parse: parseUrl} = require('url');
const {resolve} = require('path');

const {app, BrowserWindow, shell, Menu} = require('electron');
const uuid = require('uuid');
const { resolve } = require('path');
const { parse: parseUrl } = require('url');
const fileUriToPath = require('file-uri-to-path');
const isDev = require('electron-is-dev');
const AutoUpdater = require('./auto-updater');
const toElectronBackgroundColor = require('./utils/to-electron-background-color');

const createMenu = require('./menu');
const createRPC = require('./rpc');
const notify = require('./notify');

app.commandLine.appendSwitch('js-flags', '--harmony');

// set up config
const config = require('./config');

config.init();

const plugins = require('./plugins');
const Session = require('./session');

Expand All @@ -28,7 +32,9 @@ app.getWindows = () => new Set([...windowSet]); // return a clone
// function to retrive the last focused window in windowSet;
// added to app object in order to expose it to plugins.
app.getLastFocusedWindow = () => {
if (!windowSet.size) return null;
if (!windowSet.size) {
return null;
}
return Array.from(windowSet).reduce((lastWindow, win) => {
return win.focusTime > lastWindow.focusTime ? win : lastWindow;
});
Expand All @@ -48,11 +54,11 @@ const url = 'file://' + resolve(
console.log('electron will open', url);

app.on('ready', () => {
function createWindow (fn) {
function createWindow(fn) {
let cfg = plugins.getDecoratedConfig();

const [width, height] = cfg.windowSize || [540, 380];
const { screen } = require('electron');
const {screen} = require('electron');

let startX = 50;
let startY = 50;
Expand All @@ -63,7 +69,7 @@ app.on('ready', () => {
const focusedWindow = BrowserWindow.getFocusedWindow() || app.getLastFocusedWindow();
if (focusedWindow) {
const points = focusedWindow.getPosition();
const currentScreen = screen.getDisplayNearestPoint({ x: points[0], y: points[1] });
const currentScreen = screen.getDisplayNearestPoint({x: points[0], y: points[1]});

const biggestX = ((points[0] + 100 + width) - currentScreen.bounds.x);
const biggestY = ((points[1] + 100 + height) - currentScreen.bounds.y);
Expand Down Expand Up @@ -132,7 +138,9 @@ app.on('ready', () => {

// If no callback is passed to createWindow,
// a new session will be created by default.
if (!fn) fn = (win) => win.rpc.emit('session add req');
if (!fn) {
fn = win => win.rpc.emit('session add req');
}

// app.windowCallback is the createWindow callback
// that can be setted before the 'ready' app event
Expand All @@ -149,29 +157,29 @@ app.on('ready', () => {
}
});

rpc.on('new', ({ rows = 40, cols = 100, cwd = process.env.HOME }) => {
rpc.on('new', ({rows = 40, cols = 100, cwd = process.env.HOME}) => {
const shell = cfg.shell;
const shellArgs = cfg.shellArgs && Array.from(cfg.shellArgs);

initSession({ rows, cols, cwd, shell, shellArgs }, (uid, session) => {
initSession({rows, cols, cwd, shell, shellArgs}, (uid, session) => {
sessions.set(uid, session);
rpc.emit('session add', {
uid,
shell: session.shell,
pid: session.pty.pid
});

session.on('data', (data) => {
rpc.emit('session data', { uid, data });
session.on('data', data => {
rpc.emit('session data', {uid, data});
});

session.on('title', (title) => {
session.on('title', title => {
win.setTitle(title);
rpc.emit('session title', { uid, title });
rpc.emit('session title', {uid, title});
});

session.on('exit', () => {
rpc.emit('session exit', { uid });
rpc.emit('session exit', {uid});
sessions.delete(uid);
});
});
Expand All @@ -180,7 +188,7 @@ app.on('ready', () => {
// TODO: this goes away when we are able to poll
// for the title ourseleves, instead of relying
// on Session and focus/blur to subscribe
rpc.on('focus', ({ uid }) => {
rpc.on('focus', ({uid}) => {
const session = sessions.get(uid);
if (typeof session !== 'undefined' && typeof session.lastTitle !== 'undefined') {
win.setTitle(session.lastTitle);
Expand All @@ -191,7 +199,7 @@ app.on('ready', () => {
console.log('session not found by', uid);
}
});
rpc.on('blur', ({ uid }) => {
rpc.on('blur', ({uid}) => {
const session = sessions.get(uid);

if (session) {
Expand All @@ -201,7 +209,7 @@ app.on('ready', () => {
}
});

rpc.on('exit', ({ uid }) => {
rpc.on('exit', ({uid}) => {
const session = sessions.get(uid);

if (session) {
Expand All @@ -219,17 +227,17 @@ app.on('ready', () => {
win.maximize();
});

rpc.on('resize', ({ cols, rows }) => {
sessions.forEach((session) => {
session.resize({ cols, rows });
rpc.on('resize', ({cols, rows}) => {
sessions.forEach(session => {
session.resize({cols, rows});
});
});

rpc.on('data', ({ uid, data }) => {
rpc.on('data', ({uid, data}) => {
sessions.get(uid).write(data);
});

rpc.on('open external', ({ url }) => {
rpc.on('open external', ({url}) => {
shell.openExternal(url);
});

Expand Down Expand Up @@ -257,11 +265,11 @@ app.on('ready', () => {
// If file is dropped onto the terminal window, navigate event is prevented
// and his path is added to active session.
win.webContents.on('will-navigate', (event, url) => {
var protocol = typeof url === 'string' && parseUrl(url).protocol;
const protocol = typeof url === 'string' && parseUrl(url).protocol;
if (protocol === 'file:') {
event.preventDefault();
let path = fileUriToPath(url).replace(/ /g, '\\ ');
rpc.emit('session data send', { data: path });
const path = fileUriToPath(url).replace(/ /g, '\\ ');
rpc.emit('session data send', {data: path});
}
});

Expand All @@ -276,7 +284,7 @@ app.on('ready', () => {
// load plugins
load();

const pluginsUnsubscribe = plugins.subscribe((err) => {
const pluginsUnsubscribe = plugins.subscribe(err => {
if (!err) {
load();
win.webContents.send('plugins change');
Expand Down Expand Up @@ -331,16 +339,18 @@ app.on('ready', () => {
const tpl = plugins.decorateMenu(createMenu({
createWindow,
updatePlugins: () => {
plugins.updatePlugins({ force: true });
plugins.updatePlugins({force: true});
}
}));

// If we're on Mac make a Dock Menu
if (process.platform === 'darwin') {
const { app, Menu } = require('electron');
const dockMenu = Menu.buildFromTemplate([
{label: 'New Window', click () { createWindow(); }}
]);
const dockMenu = Menu.buildFromTemplate([{
label: 'New Window',
click() {
createWindow();
}
}]);
app.dock.setMenu(dockMenu);
}

Expand All @@ -356,16 +366,16 @@ app.on('ready', () => {
plugins.subscribe(load);
});

function initSession (opts, fn) {
function initSession(opts, fn) {
fn(uuid.v4(), new Session(opts));
}

app.on('open-file', (event, path) => {
const lastWindow = app.getLastFocusedWindow();
const callback = win => win.rpc.emit('open file', { path });
const callback = win => win.rpc.emit('open file', {path});
if (lastWindow) {
callback(lastWindow);
} else if (!lastWindow && app.hasOwnProperty('createWindow')) {
} else if (!lastWindow && {}.hasOwnProperty.call(app, 'createWindow')) {
app.createWindow(callback);
} else {
// if createWindow not exists yet ('ready' event was not fired),
Expand Down
Loading

0 comments on commit 8a8fdac

Please sign in to comment.