2017-01-21 09:54:32 -09:00
|
|
|
// Print diagnostic information for a few arguments instead of running Hyper.
|
|
|
|
|
if (['--help', '-v', '--version'].includes(process.argv[1])) {
|
|
|
|
|
const {version} = require('./package');
|
|
|
|
|
const configLocation = process.platform === 'win32' ? process.env.userprofile + '\\.hyper.js' : '~/.hyper.js';
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2017-01-21 09:54:32 -09:00
|
|
|
console.log(`Hyper version ${version}`);
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2017-01-21 09:54:32 -09:00
|
|
|
console.log('Hyper does not accept any command line arguments. Please modify the config file instead.');
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2017-01-21 09:54:32 -09:00
|
|
|
console.log(`Hyper configuration file located at: ${configLocation}`);
|
|
|
|
|
// eslint-disable-next-line unicorn/no-process-exit
|
|
|
|
|
process.exit();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-12 21:03:35 -08:00
|
|
|
const checkSquirrel = () => {
|
|
|
|
|
let squirrel;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
squirrel = require('electron-squirrel-startup');
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-empty
|
2017-08-12 21:03:35 -08:00
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
|
|
if (squirrel) {
|
|
|
|
|
// eslint-disable-next-line unicorn/no-process-exit
|
|
|
|
|
process.exit();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-22 03:06:03 -09:00
|
|
|
// handle startup squirrel events
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
// eslint-disable-next-line import/order
|
|
|
|
|
const systemContextMenu = require('./system-context-menu');
|
|
|
|
|
|
|
|
|
|
switch (process.argv[1]) {
|
|
|
|
|
case '--squirrel-install':
|
|
|
|
|
case '--squirrel-updated':
|
|
|
|
|
systemContextMenu.add(() => {
|
2017-08-12 21:03:35 -08:00
|
|
|
checkSquirrel();
|
2016-11-22 03:06:03 -09:00
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case '--squirrel-uninstall':
|
|
|
|
|
systemContextMenu.remove(() => {
|
2017-08-12 21:03:35 -08:00
|
|
|
checkSquirrel();
|
2016-11-22 03:06:03 -09:00
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-08-12 21:03:35 -08:00
|
|
|
checkSquirrel();
|
2016-11-22 03:06:03 -09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
// Native
|
2017-08-02 11:20:03 -08:00
|
|
|
const {resolve} = require('path');
|
2016-09-21 06:27:11 -08:00
|
|
|
|
|
|
|
|
// Packages
|
2017-08-02 11:20:03 -08:00
|
|
|
const {app, BrowserWindow, Menu} = require('electron');
|
2016-10-12 17:35:44 -08:00
|
|
|
const {gitDescribe} = require('git-describe');
|
2016-07-01 12:26:51 -08:00
|
|
|
const isDev = require('electron-is-dev');
|
2016-09-21 06:27:11 -08:00
|
|
|
|
2017-06-02 16:03:47 -08:00
|
|
|
const config = require('./config');
|
2016-07-07 12:49:34 -08:00
|
|
|
|
|
|
|
|
// set up config
|
2017-06-02 16:03:47 -08:00
|
|
|
config.setup();
|
2016-09-21 06:27:11 -08:00
|
|
|
|
2016-07-07 12:49:34 -08:00
|
|
|
const plugins = require('./plugins');
|
2018-01-09 06:05:19 -09:00
|
|
|
const {addSymlink, addBinToUserPath} = require('./utils/cli-install');
|
2017-11-02 18:51:18 -08:00
|
|
|
const AppMenu = require('./menus/menu');
|
2017-08-02 11:20:03 -08:00
|
|
|
const Window = require('./ui/window');
|
2018-03-02 12:11:33 -09:00
|
|
|
const windowUtils = require('./utils/window-utils');
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2016-07-18 16:11:30 -08:00
|
|
|
const windowSet = new Set([]);
|
|
|
|
|
|
2016-07-16 10:58:21 -08:00
|
|
|
// expose to plugins
|
|
|
|
|
app.config = config;
|
|
|
|
|
app.plugins = plugins;
|
2016-07-18 16:11:30 -08:00
|
|
|
app.getWindows = () => new Set([...windowSet]); // return a clone
|
2016-07-16 10:58:21 -08:00
|
|
|
|
2016-10-10 02:26:47 -08:00
|
|
|
// function to retrieve the last focused window in windowSet;
|
2016-08-01 14:52:21 -08:00
|
|
|
// added to app object in order to expose it to plugins.
|
|
|
|
|
app.getLastFocusedWindow = () => {
|
2016-09-21 06:27:11 -08:00
|
|
|
if (!windowSet.size) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-08-01 14:52:21 -08:00
|
|
|
return Array.from(windowSet).reduce((lastWindow, win) => {
|
|
|
|
|
return win.focusTime > lastWindow.focusTime ? win : lastWindow;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-01 12:26:51 -08:00
|
|
|
if (isDev) {
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-07-01 12:26:51 -08:00
|
|
|
console.log('running in dev mode');
|
2016-09-20 14:51:15 -08:00
|
|
|
|
|
|
|
|
// Overide default appVersion which is set from package.json
|
|
|
|
|
gitDescribe({customArguments: ['--tags']}, (error, gitInfo) => {
|
2016-09-21 06:27:11 -08:00
|
|
|
if (!error) {
|
|
|
|
|
app.setVersion(gitInfo.raw);
|
|
|
|
|
}
|
2016-09-20 14:51:15 -08:00
|
|
|
});
|
2016-06-30 22:01:04 -08:00
|
|
|
} else {
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-07-01 12:26:51 -08:00
|
|
|
console.log('running in prod mode');
|
2018-01-09 06:05:19 -09:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
addBinToUserPath().catch(err => console.error('Failed to add Hyper CLI path to user PATH', err));
|
|
|
|
|
} else {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
addSymlink().catch(err => console.error('Failed to symlink Hyper CLI', err));
|
|
|
|
|
}
|
2016-06-30 22:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const url = 'file://' + resolve(isDev ? __dirname : app.getAppPath(), 'index.html');
|
|
|
|
|
//eslint-disable-next-line no-console
|
2016-07-01 12:26:51 -08:00
|
|
|
console.log('electron will open', url);
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
app.on('ready', () =>
|
|
|
|
|
installDevExtensions(isDev)
|
|
|
|
|
.then(() => {
|
|
|
|
|
function createWindow(fn, options = {}) {
|
|
|
|
|
const cfg = plugins.getDecoratedConfig();
|
|
|
|
|
|
|
|
|
|
const winSet = config.getWin();
|
|
|
|
|
let [startX, startY] = winSet.position;
|
|
|
|
|
|
|
|
|
|
const [width, height] = options.size ? options.size : cfg.windowSize || winSet.size;
|
|
|
|
|
const {screen} = require('electron');
|
|
|
|
|
|
|
|
|
|
const winPos = options.position;
|
|
|
|
|
|
|
|
|
|
// Open the new window roughly the height of the header away from the
|
|
|
|
|
// previous window. This also ensures in multi monitor setups that the
|
|
|
|
|
// new terminal is on the correct screen.
|
|
|
|
|
const focusedWindow = BrowserWindow.getFocusedWindow() || app.getLastFocusedWindow();
|
|
|
|
|
// In case of options defaults position and size, we should ignore the focusedWindow.
|
|
|
|
|
if (winPos !== undefined) {
|
|
|
|
|
[startX, startY] = winPos;
|
|
|
|
|
} else if (focusedWindow) {
|
|
|
|
|
const points = focusedWindow.getPosition();
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (biggestX > currentScreen.size.width) {
|
|
|
|
|
startX = 50;
|
|
|
|
|
} else {
|
|
|
|
|
startX = points[0] + 34;
|
|
|
|
|
}
|
|
|
|
|
if (biggestY > currentScreen.size.height) {
|
|
|
|
|
startY = 50;
|
|
|
|
|
} else {
|
|
|
|
|
startY = points[1] + 34;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-01 16:00:49 -08:00
|
|
|
|
2018-03-02 12:11:33 -09:00
|
|
|
if (!windowUtils.positionIsValid([startX, startY])) {
|
|
|
|
|
[startX, startY] = config.windowDefaults.windowPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const hwin = new Window({width, height, x: startX, y: startY}, cfg, fn);
|
|
|
|
|
windowSet.add(hwin);
|
|
|
|
|
hwin.loadURL(url);
|
2016-07-18 08:44:33 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// the window can be closed by the browser process itself
|
|
|
|
|
hwin.on('close', () => {
|
|
|
|
|
hwin.clean();
|
|
|
|
|
windowSet.delete(hwin);
|
|
|
|
|
});
|
2016-08-01 14:52:21 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
hwin.on('closed', () => {
|
|
|
|
|
if (process.platform !== 'darwin' && windowSet.size === 0) {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-07-29 20:12:21 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
return hwin;
|
2016-07-26 19:11:54 -08:00
|
|
|
}
|
2017-08-14 17:29:50 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// when opening create a new window
|
2016-07-01 16:02:08 -08:00
|
|
|
createWindow();
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// expose to plugins
|
|
|
|
|
app.createWindow = createWindow;
|
|
|
|
|
|
2018-03-08 05:45:49 -09:00
|
|
|
if (!isDev) {
|
|
|
|
|
// check if should be set/removed as default ssh protocol client
|
|
|
|
|
if (config.getConfig().defaultSSHApp && !app.isDefaultProtocolClient('ssh')) {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.log('Setting Hyper as default client for ssh:// protocol');
|
|
|
|
|
app.setAsDefaultProtocolClient('ssh');
|
|
|
|
|
} else if (!config.getConfig().defaultSSHApp && app.isDefaultProtocolClient('ssh')) {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.log('Removing Hyper from default client for ssh:// protocl');
|
|
|
|
|
app.removeAsDefaultProtocolClient('ssh');
|
|
|
|
|
}
|
2018-02-12 11:20:45 -09:00
|
|
|
}
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
// mac only. when the dock icon is clicked
|
|
|
|
|
// and we don't have any active windows open,
|
|
|
|
|
// we open one
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
|
if (!windowSet.size) {
|
2016-09-21 06:27:11 -08:00
|
|
|
createWindow();
|
|
|
|
|
}
|
2017-09-10 05:35:10 -08:00
|
|
|
});
|
2016-07-07 16:16:44 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const makeMenu = () => {
|
2017-11-02 18:51:18 -08:00
|
|
|
const menu = plugins.decorateMenu(AppMenu.createMenu(createWindow, plugins.getLoadedPluginVersions));
|
2017-09-10 05:35:10 -08:00
|
|
|
|
|
|
|
|
// If we're on Mac make a Dock Menu
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
|
const dockMenu = Menu.buildFromTemplate([
|
|
|
|
|
{
|
|
|
|
|
label: 'New Window',
|
|
|
|
|
click() {
|
|
|
|
|
createWindow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
app.dock.setMenu(dockMenu);
|
|
|
|
|
}
|
2016-07-07 16:16:44 -08:00
|
|
|
|
2017-11-02 18:51:18 -08:00
|
|
|
Menu.setApplicationMenu(AppMenu.buildMenu(menu));
|
2017-09-10 05:35:10 -08:00
|
|
|
};
|
|
|
|
|
|
2017-11-14 14:55:21 -09:00
|
|
|
plugins.onApp(app);
|
|
|
|
|
makeMenu();
|
|
|
|
|
plugins.subscribe(plugins.onApp.bind(undefined, app));
|
|
|
|
|
config.subscribe(makeMenu);
|
2017-09-10 05:35:10 -08:00
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.error('Error while loading devtools extensions', err);
|
|
|
|
|
})
|
|
|
|
|
);
|
2016-06-30 22:01:04 -08:00
|
|
|
|
2016-08-01 14:52:21 -08:00
|
|
|
app.on('open-file', (event, path) => {
|
|
|
|
|
const lastWindow = app.getLastFocusedWindow();
|
2016-09-21 06:27:11 -08:00
|
|
|
const callback = win => win.rpc.emit('open file', {path});
|
2016-08-01 14:52:21 -08:00
|
|
|
if (lastWindow) {
|
|
|
|
|
callback(lastWindow);
|
2016-09-21 06:27:11 -08:00
|
|
|
} else if (!lastWindow && {}.hasOwnProperty.call(app, 'createWindow')) {
|
2016-08-01 14:52:21 -08:00
|
|
|
app.createWindow(callback);
|
|
|
|
|
} else {
|
2016-10-10 02:26:47 -08:00
|
|
|
// If createWindow doesn't exist yet ('ready' event was not fired),
|
2016-08-01 14:52:21 -08:00
|
|
|
// sets his callback to an app.windowCallback property.
|
|
|
|
|
app.windowCallback = callback;
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-09-21 14:11:42 -08:00
|
|
|
|
2018-02-12 11:20:45 -09:00
|
|
|
app.on('open-url', (event, sshUrl) => {
|
|
|
|
|
const lastWindow = app.getLastFocusedWindow();
|
|
|
|
|
const callback = win => win.rpc.emit('open ssh', sshUrl);
|
|
|
|
|
if (lastWindow) {
|
|
|
|
|
callback(lastWindow);
|
|
|
|
|
} else if (!lastWindow && {}.hasOwnProperty.call(app, 'createWindow')) {
|
|
|
|
|
app.createWindow(callback);
|
|
|
|
|
} else {
|
|
|
|
|
// If createWindow doesn't exist yet ('ready' event was not fired),
|
|
|
|
|
// sets his callback to an app.windowCallback property.
|
|
|
|
|
app.windowCallback = callback;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
function installDevExtensions(isDev_) {
|
|
|
|
|
if (!isDev_) {
|
2016-09-21 14:11:42 -08:00
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
2016-10-12 17:35:44 -08:00
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
|
|
|
const installer = require('electron-devtools-installer');
|
2016-09-21 14:11:42 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS'];
|
2016-09-21 14:11:42 -08:00
|
|
|
const forceDownload = Boolean(process.env.UPGRADE_EXTENSIONS);
|
|
|
|
|
|
|
|
|
|
return Promise.all(extensions.map(name => installer.default(installer[name], forceDownload)));
|
|
|
|
|
}
|