Separate development and production environments

Set NODE_ENV at run time or build time to switch the app between dev and
production modes.

At build time, the current NODE_ENV will be included in the packaged
app's package.json file. At runtime we read NODE_ENV from package.json,
but also allow the local environment variable to override. A query
string parsed by a preload script exposes the value to the renderer,
which then determines whether we use the staging or production server.
Additionally, different environments have different user data
directories.

// FREEBIE
This commit is contained in:
lilia 2017-04-04 13:39:24 -07:00 committed by Scott Nonnenberg
parent 45bf7330ac
commit 2bbd0d58c6
No known key found for this signature in database
GPG key ID: A4931C09644C654B
4 changed files with 33 additions and 5 deletions

18
main.js
View file

@ -3,6 +3,7 @@ const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
const fs = require('fs')
const autoUpdater = require('electron-updater').autoUpdater
const autoUpdaterInterval = 60 * 60 * 1000;
@ -22,6 +23,14 @@ if (shouldQuit) {
return;
}
// Read package.json
const package_json = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8'))
const NODE_ENV = process.env.NODE_ENV || package_json.NODE_ENV || 'development';
// use a separate data directory for development
if (NODE_ENV !== 'production') {
app.setPath('userData', path.join(app.getPath('appData'), 'Signal-' + NODE_ENV))
}
// 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.
let mainWindow
@ -31,14 +40,19 @@ function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: { nodeIntegration: false, sandbox: true }
webPreferences: {
nodeIntegration: false,
sandbox: true,
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'background.html'),
protocol: 'file:',
slashes: true
slashes: true,
query: { node_env: NODE_ENV }
}))
// Open the DevTools.