2016-04-20 06:09:46 +00:00
|
|
|
'use strict';
|
2016-03-22 18:20:05 +00:00
|
|
|
|
2016-07-18 19:44:46 +00:00
|
|
|
if (require('electron-squirrel-startup')) {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
var raven = require('raven');
|
|
|
|
var ravenClient = new raven.Client(
|
|
|
|
'https://fb3242f902b54cdd934b8ffa204426c0:23430fbe203a' +
|
2016-08-19 23:12:33 +00:00
|
|
|
'4189a68efb63c38fc50b@app.getsentry.com/88289', {
|
|
|
|
environment: process.env.INSOMNIA_ENV || 'production',
|
|
|
|
release: require('./app.json').version,
|
|
|
|
});
|
2016-08-17 22:44:03 +00:00
|
|
|
|
2016-08-19 23:12:33 +00:00
|
|
|
ravenClient.patchGlobal();
|
2016-08-17 22:44:03 +00:00
|
|
|
|
2016-03-22 18:20:05 +00:00
|
|
|
// Don't npm install this (it breaks). Rely on the global one.
|
2016-04-20 06:09:46 +00:00
|
|
|
const electron = require('electron');
|
2016-08-20 20:41:44 +00:00
|
|
|
const request = require('request');
|
2016-07-29 17:18:04 +00:00
|
|
|
const path = require('path');
|
2016-08-22 19:05:17 +00:00
|
|
|
const {version: appVersion, productName: appName} = require('./app.json');
|
2016-08-20 20:41:44 +00:00
|
|
|
const {LocalStorage} = require('node-localstorage');
|
2016-08-15 17:04:36 +00:00
|
|
|
const {
|
|
|
|
app,
|
|
|
|
dialog,
|
2016-08-20 20:41:44 +00:00
|
|
|
shell,
|
2016-08-15 17:04:36 +00:00
|
|
|
ipcMain,
|
|
|
|
autoUpdater,
|
|
|
|
Menu,
|
|
|
|
BrowserWindow,
|
|
|
|
webContents
|
|
|
|
} = electron;
|
2016-07-29 17:18:04 +00:00
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
const IS_DEV = process.env.INSOMNIA_ENV === 'development';
|
2016-03-23 18:34:39 +00:00
|
|
|
const IS_MAC = process.platform === 'darwin';
|
2016-08-20 20:41:44 +00:00
|
|
|
const IS_WINDOWS = process.platform === 'win32';
|
|
|
|
const IS_LINUX = !IS_MAC && !IS_WINDOWS;
|
2016-07-18 19:44:46 +00:00
|
|
|
|
|
|
|
const UPDATE_URLS = {
|
2016-08-19 23:12:33 +00:00
|
|
|
darwin: `https://updates.insomnia.rest/builds/check/mac?v=${appVersion}`,
|
2016-07-18 19:44:46 +00:00
|
|
|
win32: 'https://s3.amazonaws.com/builds-insomnia-rest/win',
|
2016-08-20 20:41:44 +00:00
|
|
|
linux: `https://updates.insomnia.rest/builds/check/linux?v=${appVersion}`
|
2016-07-18 19:44:46 +00:00
|
|
|
};
|
2016-07-14 01:12:42 +00:00
|
|
|
|
2016-08-20 20:41:44 +00:00
|
|
|
const DOWNLOAD_URL = 'http://download.insomnia.rest';
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
let mainWindow = null;
|
2016-08-03 16:35:43 +00:00
|
|
|
let localStorage = null;
|
2016-08-23 20:54:11 +00:00
|
|
|
let hasPromptedForUpdates = false;
|
2016-03-22 18:20:05 +00:00
|
|
|
|
2016-05-01 19:56:30 +00:00
|
|
|
// Enable this for CSS grid layout :)
|
2016-07-16 07:22:08 +00:00
|
|
|
app.commandLine.appendSwitch('enable-experimental-web-platform-features');
|
2016-05-01 19:56:30 +00:00
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
autoUpdater.on('error', e => {
|
|
|
|
// Failed to launch auto updater
|
2016-08-23 20:54:11 +00:00
|
|
|
ravenClient.captureError(e, {});
|
2016-08-17 22:44:03 +00:00
|
|
|
});
|
2016-07-14 01:12:42 +00:00
|
|
|
|
|
|
|
autoUpdater.on('update-not-available', () => {
|
|
|
|
console.log('-- Update Not Available --')
|
|
|
|
});
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', () => {
|
|
|
|
console.log('-- Update Available --');
|
|
|
|
});
|
|
|
|
|
2016-07-21 21:34:38 +00:00
|
|
|
autoUpdater.on('update-downloaded', (e, releaseNotes, releaseName, releaseDate, updateUrl) => {
|
|
|
|
console.log(`-- Update Downloaded ${releaseName} --`);
|
2016-07-22 22:27:04 +00:00
|
|
|
showUpdateModal();
|
2016-07-14 01:12:42 +00:00
|
|
|
});
|
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
function checkForUpdates () {
|
2016-08-23 20:54:11 +00:00
|
|
|
if (hasPromptedForUpdates) {
|
|
|
|
// We've already prompted for updates. Don't bug the user anymore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-20 20:41:44 +00:00
|
|
|
if (IS_LINUX) {
|
|
|
|
try {
|
|
|
|
request.get(UPDATE_URLS.linux, null, (err, response) => {
|
|
|
|
if (err) {
|
|
|
|
ravenClient.captureError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
showDownloadModal(response.body);
|
|
|
|
} else {
|
|
|
|
// No update available (should be STATUS=204)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
ravenClient.captureException(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
autoUpdater.setFeedURL(UPDATE_URLS[process.platform]);
|
|
|
|
autoUpdater.checkForUpdates();
|
|
|
|
} catch (e) {
|
|
|
|
// This will fail in development
|
|
|
|
}
|
2016-08-17 22:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showUpdateModal () {
|
2016-08-23 20:54:11 +00:00
|
|
|
hasPromptedForUpdates = true;
|
|
|
|
|
2016-07-22 22:27:04 +00:00
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
buttons: [
|
|
|
|
'Install and Restart',
|
|
|
|
'Later',
|
|
|
|
],
|
|
|
|
defaultId: 0,
|
|
|
|
cancelId: 1,
|
2016-08-22 21:58:24 +00:00
|
|
|
title: 'Insomnia Update Available',
|
|
|
|
message: 'Exciting news!\n\nA new version of Insomnia has been downloaded and is ready to install\n\n\n~Gregory'
|
2016-07-22 22:27:04 +00:00
|
|
|
}, id => {
|
|
|
|
if (id === 0) {
|
|
|
|
console.log('-- Installing Update --');
|
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
} else {
|
|
|
|
console.log('-- Cancel Update --');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-07-21 21:34:38 +00:00
|
|
|
|
2016-08-20 20:41:44 +00:00
|
|
|
function showDownloadModal (version) {
|
2016-08-23 20:54:11 +00:00
|
|
|
hasPromptedForUpdates = true;
|
|
|
|
|
2016-08-20 20:41:44 +00:00
|
|
|
dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
buttons: [
|
|
|
|
'Download',
|
|
|
|
'Later',
|
|
|
|
],
|
|
|
|
defaultId: 0,
|
|
|
|
cancelId: 1,
|
2016-08-22 21:58:24 +00:00
|
|
|
title: 'Insomnia Update Available',
|
|
|
|
message: `Exciting news!\n\nVersion ${version} of Insomnia is now available.\n\n\n~Gregory`
|
2016-08-20 20:41:44 +00:00
|
|
|
}, id => {
|
|
|
|
if (id === 0) {
|
|
|
|
console.log('-- Installing Update --');
|
|
|
|
shell.openExternal(DOWNLOAD_URL);
|
|
|
|
} else {
|
|
|
|
console.log('-- Cancel Update --');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:34:38 +00:00
|
|
|
ipcMain.on('check-for-updates', () => {
|
|
|
|
console.log('-- Checking for Updates --');
|
2016-08-17 22:44:03 +00:00
|
|
|
checkForUpdates();
|
2016-07-21 21:34:38 +00:00
|
|
|
});
|
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
function saveBounds () {
|
2016-09-09 17:57:26 +00:00
|
|
|
const fullscreen = mainWindow.isFullScreen();
|
|
|
|
if (!fullscreen) {
|
|
|
|
// Only save the size if we're not in fullscreen
|
|
|
|
localStorage.setItem('bounds', JSON.stringify(mainWindow.getBounds()));
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem('fullscreen', JSON.stringify(mainWindow.isFullScreen()));
|
2016-07-29 17:18:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
function getBounds () {
|
2016-07-29 17:18:04 +00:00
|
|
|
let bounds = {};
|
2016-09-09 17:57:26 +00:00
|
|
|
let fullscreen = false;
|
2016-07-29 17:18:04 +00:00
|
|
|
try {
|
|
|
|
bounds = JSON.parse(localStorage.getItem('bounds') || '{}');
|
2016-09-09 17:57:26 +00:00
|
|
|
fullscreen = JSON.parse(localStorage.getItem('fullscreen') || 'false')
|
2016-07-29 17:18:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
// This should never happen, but if it does...!
|
|
|
|
console.error('Failed to parse window bounds', e);
|
|
|
|
}
|
|
|
|
|
2016-09-09 17:57:26 +00:00
|
|
|
return {bounds, fullscreen};
|
2016-07-29 17:18:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
function saveZoomFactor (zoomFactor) {
|
2016-07-29 17:18:04 +00:00
|
|
|
localStorage.setItem('zoomFactor', JSON.stringify(zoomFactor));
|
|
|
|
}
|
|
|
|
|
2016-08-17 22:44:03 +00:00
|
|
|
function getZoomFactor () {
|
2016-07-29 17:18:04 +00:00
|
|
|
let zoomFactor = 1;
|
|
|
|
try {
|
|
|
|
zoomFactor = JSON.parse(localStorage.getItem('zoomFactor') || '1');
|
|
|
|
} catch (e) {
|
|
|
|
// This should never happen, but if it does...!
|
|
|
|
console.error('Failed to parse zoomFactor', e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return zoomFactor;
|
|
|
|
}
|
|
|
|
|
2016-07-09 04:49:09 +00:00
|
|
|
// Quit when all windows are closed (except on Mac).
|
2016-04-20 02:14:53 +00:00
|
|
|
app.on('window-all-closed', () => {
|
2016-03-23 18:34:39 +00:00
|
|
|
if (!IS_MAC) {
|
2016-03-22 18:20:05 +00:00
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-09 17:57:26 +00:00
|
|
|
// Mac-only, when the user clicks the doc icon
|
|
|
|
app.on('activate', (e, hasVisibleWindows) => {
|
|
|
|
if (!hasVisibleWindows) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// When the app is first launched
|
2016-04-20 02:14:53 +00:00
|
|
|
app.on('ready', () => {
|
2016-08-17 22:44:03 +00:00
|
|
|
checkForUpdates();
|
2016-09-09 17:57:26 +00:00
|
|
|
createWindow();
|
|
|
|
});
|
2016-08-17 22:44:03 +00:00
|
|
|
|
2016-09-09 17:57:26 +00:00
|
|
|
function createWindow () {
|
2016-08-03 16:35:43 +00:00
|
|
|
localStorage = new LocalStorage(path.join(app.getPath('userData'), 'localStorage'));
|
|
|
|
|
2016-07-29 17:18:04 +00:00
|
|
|
const zoomFactor = getZoomFactor();
|
2016-09-09 17:57:26 +00:00
|
|
|
const {bounds, fullscreen} = getBounds();
|
2016-08-15 17:04:36 +00:00
|
|
|
const {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
} = bounds;
|
2016-03-22 18:20:05 +00:00
|
|
|
|
2016-07-29 17:18:04 +00:00
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
x: x,
|
|
|
|
y: y,
|
2016-09-09 17:57:26 +00:00
|
|
|
fullscreen: fullscreen,
|
|
|
|
fullscreenable: true,
|
2016-07-29 17:18:04 +00:00
|
|
|
width: width || 1200,
|
|
|
|
height: height || 600,
|
|
|
|
minHeight: 500,
|
|
|
|
minWidth: 500,
|
|
|
|
acceptFirstMouse: true,
|
|
|
|
webPreferences: {
|
|
|
|
zoomFactor: zoomFactor
|
2016-07-27 20:07:50 +00:00
|
|
|
}
|
2016-07-29 17:18:04 +00:00
|
|
|
});
|
2016-07-25 22:27:29 +00:00
|
|
|
|
2016-07-29 17:18:04 +00:00
|
|
|
mainWindow.on('resize', e => saveBounds());
|
|
|
|
mainWindow.on('move', e => saveBounds());
|
2016-04-20 16:15:19 +00:00
|
|
|
|
2016-07-29 17:18:04 +00:00
|
|
|
// and load the app.html of the app.
|
|
|
|
mainWindow.loadURL(`file://${__dirname}/app.html`);
|
2016-07-27 20:07:50 +00:00
|
|
|
|
2016-07-29 17:18:04 +00:00
|
|
|
// Emitted when the window is closed.
|
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
// Dereference the window object, usually you would store windows
|
|
|
|
// in an array if your app supports multi windows, this is the time
|
|
|
|
// when you should delete the corresponding element.
|
|
|
|
mainWindow = null;
|
|
|
|
});
|
2016-07-27 20:07:50 +00:00
|
|
|
|
2016-08-25 19:44:51 +00:00
|
|
|
require('electron-context-menu')({});
|
|
|
|
|
2016-08-22 20:08:22 +00:00
|
|
|
var template = [
|
|
|
|
{
|
2016-08-17 20:58:44 +00:00
|
|
|
label: "Application",
|
2016-08-22 19:05:17 +00:00
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: `About ${appName}`,
|
2016-08-22 20:05:42 +00:00
|
|
|
role: 'about',
|
|
|
|
visible: IS_MAC
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
visible: IS_MAC
|
2016-08-22 19:05:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Preferences",
|
|
|
|
accelerator: "CmdOrCtrl+,",
|
2016-08-22 20:05:42 +00:00
|
|
|
click: function (menuItem, window, e) {
|
2016-08-23 20:54:11 +00:00
|
|
|
// NOTE: Checking for window because it might be closed
|
|
|
|
if (window && window.webContents) {
|
|
|
|
window.webContents.send('toggle-preferences');
|
|
|
|
}
|
2016-08-22 19:05:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-08-23 20:54:11 +00:00
|
|
|
type: "separator",
|
|
|
|
visible: IS_MAC
|
2016-08-22 19:05:17 +00:00
|
|
|
},
|
|
|
|
{
|
2016-08-23 20:54:11 +00:00
|
|
|
role: "hide",
|
|
|
|
visible: IS_MAC
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "hideothers",
|
|
|
|
visible: IS_MAC
|
2016-08-22 19:05:17 +00:00
|
|
|
},
|
|
|
|
{type: "separator"},
|
|
|
|
{
|
|
|
|
label: "Quit",
|
|
|
|
accelerator: "Command+Q",
|
|
|
|
click: function () {
|
|
|
|
app.quit();
|
|
|
|
}
|
2016-08-17 20:58:44 +00:00
|
|
|
}
|
2016-08-22 19:05:17 +00:00
|
|
|
]
|
2016-08-22 20:08:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Edit",
|
|
|
|
submenu: [{
|
|
|
|
label: "Undo",
|
|
|
|
accelerator: "CmdOrCtrl+Z",
|
|
|
|
selector: "undo:"
|
|
|
|
}, {
|
|
|
|
label: "Redo",
|
|
|
|
accelerator: "Shift+CmdOrCtrl+Z",
|
|
|
|
selector: "redo:"
|
|
|
|
}, {
|
|
|
|
type: "separator"
|
|
|
|
}, {
|
|
|
|
label: "Cut",
|
|
|
|
accelerator: "CmdOrCtrl+X",
|
|
|
|
selector: "cut:"
|
|
|
|
}, {
|
|
|
|
label: "Copy",
|
|
|
|
accelerator: "CmdOrCtrl+C",
|
|
|
|
selector: "copy:"
|
|
|
|
}, {
|
|
|
|
label: "Paste",
|
|
|
|
accelerator: "CmdOrCtrl+V",
|
|
|
|
selector: "paste:"
|
|
|
|
}, {
|
|
|
|
label: "Select All",
|
|
|
|
accelerator: "CmdOrCtrl+A",
|
|
|
|
selector: "selectAll:"
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "View",
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'togglefullscreen'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Actual Size",
|
|
|
|
accelerator: "CmdOrCtrl+0",
|
|
|
|
click: () => {
|
|
|
|
const window = BrowserWindow.getFocusedWindow();
|
|
|
|
const zoomFactor = 1;
|
|
|
|
window.webContents.setZoomFactor(zoomFactor);
|
|
|
|
saveZoomFactor(zoomFactor);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Zoom In",
|
|
|
|
accelerator: IS_MAC ? "CmdOrCtrl+Plus" : "CmdOrCtrl+=",
|
|
|
|
click: () => {
|
|
|
|
let zoomFactor = getZoomFactor();
|
|
|
|
zoomFactor = Math.min(1.8, zoomFactor + 0.1);
|
2016-08-17 20:58:44 +00:00
|
|
|
|
2016-08-22 20:08:22 +00:00
|
|
|
const window = BrowserWindow.getFocusedWindow();
|
|
|
|
window.webContents.setZoomFactor(zoomFactor);
|
|
|
|
|
|
|
|
saveZoomFactor(zoomFactor);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Zoom Out",
|
|
|
|
accelerator: "CmdOrCtrl+-",
|
|
|
|
click: () => {
|
|
|
|
let zoomFactor = getZoomFactor();
|
|
|
|
zoomFactor = Math.max(0.5, zoomFactor - 0.1);
|
|
|
|
|
|
|
|
const window = BrowserWindow.getFocusedWindow();
|
|
|
|
window.webContents.setZoomFactor(zoomFactor);
|
|
|
|
|
|
|
|
saveZoomFactor(zoomFactor);
|
|
|
|
}
|
2016-09-12 20:04:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Toggle Sidebar",
|
|
|
|
accelerator: "CmdOrCtrl+\\",
|
|
|
|
click: () => {
|
|
|
|
const window = BrowserWindow.getFocusedWindow();
|
|
|
|
window && window.webContents.send('toggle-sidebar');
|
|
|
|
}
|
2016-08-22 19:05:17 +00:00
|
|
|
}
|
2016-08-22 20:08:22 +00:00
|
|
|
]
|
|
|
|
}, {
|
|
|
|
label: "Window",
|
|
|
|
role: "window",
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'minimize'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'close'
|
2016-08-22 19:05:17 +00:00
|
|
|
}
|
2016-08-22 20:08:22 +00:00
|
|
|
]
|
|
|
|
}, {
|
|
|
|
label: "Help",
|
|
|
|
role: "help",
|
|
|
|
id: "help",
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: "Report an Issue...",
|
|
|
|
click: () => {
|
|
|
|
electron.shell.openExternal('mailto:support@insomnia.rest');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Insomnia Help",
|
|
|
|
accelerator: "CmdOrCtrl+?",
|
|
|
|
click: () => {
|
|
|
|
electron.shell.openExternal('http://insomnia.rest');
|
|
|
|
}
|
2016-08-22 19:05:17 +00:00
|
|
|
}
|
2016-08-22 20:08:22 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
2016-04-20 17:38:27 +00:00
|
|
|
|
2016-07-29 17:18:04 +00:00
|
|
|
if (IS_DEV) {
|
|
|
|
template.push({
|
|
|
|
label: 'Developer',
|
|
|
|
position: 'before=help',
|
|
|
|
submenu: [{
|
|
|
|
label: 'Reload',
|
|
|
|
accelerator: 'Command+R',
|
2016-08-17 22:44:03 +00:00
|
|
|
click: function () {
|
2016-07-29 17:18:04 +00:00
|
|
|
mainWindow.reload();
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
label: 'Toggle DevTools',
|
|
|
|
accelerator: 'Alt+Command+I',
|
2016-08-17 22:44:03 +00:00
|
|
|
click: function () {
|
2016-07-29 17:18:04 +00:00
|
|
|
mainWindow.toggleDevTools();
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
|
2016-09-09 17:57:26 +00:00
|
|
|
}
|