Read config (#18)

* read config file

* remove homepage from package.json
This commit is contained in:
Lynn 2017-02-20 10:24:46 -08:00 committed by GitHub
parent 57f8124ba5
commit 50ca154a7e
5 changed files with 124 additions and 40 deletions

3
config/Symphony.config Normal file
View File

@ -0,0 +1,3 @@
{
"url": "https://foundation-dev.symphony.com"
}

52
js/getConfig.js Normal file
View File

@ -0,0 +1,52 @@
'use strict';
const electron = require('electron');
const app = electron.app;
const path = require('path');
const fs = require('fs');
const { isDevEnv } = require('./utils.js');
/**
* reads global configuration file: config/Symphony.config. this file is
* hold items (such as the start url) that are intended to be used as
* global (or default) values for all users running this app. for production
* this file is located relative to the executable - it is placed there by
* the installer. this makes the file easily modifable by admin (or person who
* installed app). for dev env, the file is read directly from packed asar file.
*
* @return {Object} configuration parameters (e.g., url)
*/
function getConfig() {
return new Promise(function(resolve, reject) {
let configPath;
const configFile = 'config/Symphony.config';
if (isDevEnv) {
// for dev env, get config file from asar
configPath = path.join(app.getAppPath(), configFile);
} else {
// for non-dev, config file is placed by installer relative to exe.
// this is so the config can be easily be changed post install.
let execPath = path.dirname(app.getPath('exe'));
// for mac exec is stored in subdir, for linux/windows config
// dir is in the same location.
configPath = path.join(execPath, isMac ? '..' : '', configFile)
}
fs.readFile(configPath, 'utf8', function (err, data) {
if (err) {
reject('cannot open config file: ' + configPath + ', error: ' + err);
} else {
try {
// data is the contents of the text file we just read
let config = JSON.parse(data);
resolve(config);
} catch(e) {
reject('can not parse config file data: ' + data + ', error: ' + err);
}
}
});
});
}
module.exports = getConfig

View File

@ -4,7 +4,10 @@ const electron = require('electron');
const packageJSON = require('../package.json');
const menuTemplate = require('./menuTemplate.js');
const path = require('path');
const app = electron.app
const app = electron.app;
const nodeURL = require('url');
const getConfig = require('./getConfig.js');
const { isMac, isDevEnv, getGuid } = require('./utils.js');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
@ -17,23 +20,14 @@ if (require('electron-squirrel-startup')) {
return;
}
if (isDevEnv()) {
if (isDevEnv) {
// needed for development env because local server doesn't have cert
app.commandLine.appendSwitch('--ignore-certificate-errors');
}
function isDevEnv() {
let isDev = process.env.ELECTRON_DEV ?
process.env.ELECTRON_DEV.trim().toLowerCase() === "true" : false;
return isDev;
}
function createMainWindow (url) {
let key = getGuid();
function createMainWindow () {
let key = getWindowKey();
// note: for now, turning off node integration as this is causing failure with
// onelogin, jquery can not get initialized. electron's node integration
// conflicts on the window object.
mainWindow = new electron.BrowserWindow({
title: 'Symphony',
width: 1024, height: 768,
@ -45,9 +39,14 @@ function createMainWindow () {
}
});
storeWindowKey(key, mainWindow)
mainWindow.webContents.once('did-fail-load', function() {
// ToDo: show ui when failure occurs
console.error('failed to load window');
});
mainWindow.loadURL(packageJSON.homepage);
storeWindowKey(key, mainWindow);
mainWindow.loadURL(url);
const menu = electron.Menu.buildFromTemplate(menuTemplate(app));
electron.Menu.setApplicationMenu(menu);
@ -58,7 +57,7 @@ function createMainWindow () {
return;
}
// mac should hide window when hitting x close
if (process.platform === 'darwin') {
if (isMac) {
mainWindow.hide();
e.preventDefault();
}
@ -78,21 +77,6 @@ function createMainWindow () {
});
}
/**
* Generate a key (guid).
* @return {string} guid
*/
function getWindowKey() {
// generate guid:
// http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function storeWindowKey(key, browserWin) {
windows[key] = browserWin;
}
@ -162,7 +146,7 @@ electron.ipcMain.on('symphony-msg', (event, arg) => {
let width = arg.width || 1024;
let height = arg.height || 768;
let title = arg.title || 'Symphony';
let winKey = getWindowKey();
let winKey = getGuid();
let childWindow = new electron.BrowserWindow({
title: title,
@ -187,9 +171,27 @@ electron.ipcMain.on('symphony-msg', (event, arg) => {
* initialization and is ready to create browser windows.
* Some APIs can only be used after this event occurs.
*/
app.on('ready', function() {
createMainWindow();
});
app.on('ready', getUrlAndOpenMainWindow);
function getUrlAndOpenMainWindow() {
getConfig().then(function(config) {
let protocol = '';
// add https protocol if none found.
let parsedUrl = nodeURL.parse(config.url);
if (!parsedUrl.protocol) {
protocol = 'https';
}
var url = nodeURL.format({
protocol: protocol,
slahes: true,
pathname: parsedUrl.href
});
createMainWindow(url);
}).catch(function(err) {
// ToDo: show ui when failure occurs
console.error(err);
});
}
app.on('before-quit', function() {
willQuitApp = true;
@ -198,14 +200,14 @@ app.on('before-quit', function() {
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
if (!isMac) {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createMainWindow();
getUrlAndOpenMainWindow();
} else {
mainWindow.show();
}

27
js/utils.js Normal file
View File

@ -0,0 +1,27 @@
'use strict';
const isDevEnv = process.env.ELECTRON_DEV ?
process.env.ELECTRON_DEV.trim().toLowerCase() === "true" : false;
const isMac = (process.platform === 'darwin');
/**
* Generates a guid,
* http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
*
* @return {String} guid value in string
*/
function getGuid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
module.exports = {
isDevEnv: isDevEnv,
isMac: isMac,
getGuid: getGuid
};

View File

@ -12,10 +12,11 @@
"dist-mac": "build --mac",
"dist-win": "build --win --x64",
"dist-win-x86": "build --win --ia32",
"win-unpacked": "build --win --x64 --dir",
"win-x86-unpacked": "build --win --ia32 --dir"
"unpacked-win": "build --win --x64 --dir",
"unpacked-win-x86": "build --win --ia32 --dir"
},
"build": {
"extraFiles": "config/Symphony.config",
"appId": "symphony-electron-desktop",
"mac": {
"target": "dmg",
@ -43,7 +44,6 @@
"bugs": {
"url": "https://support.symphony.com"
},
"homepage": "https://foundation-dev.symphony.com",
"devDependencies": {
"electron": "1.5.1",
"electron-builder": "^12.3.1",