Skip to content

Commit

Permalink
Env config added
Browse files Browse the repository at this point in the history
  • Loading branch information
szwacz committed Apr 26, 2015
1 parent a1ccc2e commit 5301ed1
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<link href="./stylesheets/main.css" rel="stylesheet" type="text/css">

<script src="vendor/electron_boilerplate/env_config.js"></script>
<script src="vendor/electron_boilerplate/context_menu.js"></script>
<script src="vendor/electron_boilerplate/external_links.js"></script>

Expand All @@ -21,6 +22,9 @@ <h1 id="greet"></h1>
<p class="subtitle">
Welcome to <a href="http://electron.atom.io" class="js-external-link">Electron</a> app running on this magnificent <strong id="platform-info"></strong> machine.
</p>
<p class="subtitle">
You are in <strong id="env-name"></strong> environment.
</p>
</div>

</body>
Expand Down
9 changes: 5 additions & 4 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { greet } from './hello_world/hello_world';
// Node modules are required the same way as always.
var os = require('os');

var greetElement = document.getElementById('greet');
greetElement.innerHTML = greet();
// window.env contains data from config/env_XXX.json file.
var envName = window.env.name;

var platform = document.getElementById('platform-info');
platform.innerHTML = os.platform();
document.getElementById('greet').innerHTML = greet();
document.getElementById('platform-info').innerHTML = os.platform();
document.getElementById('env-name').innerHTML = envName;
4 changes: 4 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var app = require('app');
var BrowserWindow = require('browser-window');
var env = require('./vendor/electron_boilerplate/env_config');
var windowStateKeeper = require('./vendor/electron_boilerplate/window_state');

var mainWindow;
Expand All @@ -12,6 +13,9 @@ var mainWindowState = windowStateKeeper('main', {
height: 600
});

// You have data from config/env_XXX.json file loaded here in case you need it.
// console.log(env.name);

app.on('ready', function () {

mainWindow = new BrowserWindow({
Expand Down
15 changes: 15 additions & 0 deletions app/vendor/electron_boilerplate/env_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Loads config/env_XXX.json file and puts it
// in proper place for given Electron context.

'use strict';

(function () {
var jetpack = require('fs-jetpack');
if (typeof window === 'object') {
// Web browser context, __dirname points to folder where app.html file is.
window.env = jetpack.read(__dirname + '/env_config.json', 'json');
} else {
// Node context
module.exports = jetpack.read(__dirname + '/../../env_config.json', 'json');
}
}());
4 changes: 4 additions & 0 deletions config/env_development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "development",
"description": "Add here any environment specific stuff you like."
}
4 changes: 4 additions & 0 deletions config/env_production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "production",
"description": "Add here any environment specific stuff you like."
}
4 changes: 4 additions & 0 deletions config/env_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test",
"description": "Add here any environment specific stuff you like."
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"scripts": {
"postinstall": "cd ./app && npm install",
"build": "./node_modules/.bin/gulp build",
"release": "./node_modules/.bin/gulp release --target=release",
"release": "./node_modules/.bin/gulp release --env=production",
"start": "node ./tasks/start",
"test": "node ./tasks/start --target=test"
"test": "node ./tasks/start --env=test"
}
}
6 changes: 4 additions & 2 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ gulp.task('less', ['clean'], lessTask);
gulp.task('less-watch', lessTask);


// Add and customize OS-specyfic and target-specyfic stuff.
gulp.task('finalize', ['clean'], function () {
var manifest = srcDir.read('package.json', 'json');
switch (utils.getBuildTarget()) {
switch (utils.getEnvName()) {
case 'development':
// Add "dev" suffix to name, so Electron will write all
// data like cookies and localStorage into separate place.
Expand All @@ -91,6 +90,9 @@ gulp.task('finalize', ['clean'], function () {
break;
}
destDir.write('package.json', manifest);

var configFilePath = projectDir.path('config/env_' + utils.getEnvName() + '.json');
destDir.copy(configFilePath, 'env_config.json');
});


Expand Down
4 changes: 2 additions & 2 deletions tasks/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var runBuild = function () {

var build = childProcess.spawn(gulpPath, [
'build',
'--target=' + utils.getBuildTarget(),
'--env=' + utils.getEnvName(),
'--color'
]);

Expand All @@ -33,7 +33,7 @@ var runBuild = function () {
var runGulpWatch = function () {
var watch = childProcess.spawn(gulpPath, [
'watch',
'--target=' + utils.getBuildTarget(),
'--env=' + utils.getEnvName(),
'--color'
]);

Expand Down
4 changes: 2 additions & 2 deletions tasks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ module.exports.replace = function (str, patterns) {
return str;
};

module.exports.getBuildTarget = function () {
return argv.target || 'development';
module.exports.getEnvName = function () {
return argv.env || 'development';
};

0 comments on commit 5301ed1

Please sign in to comment.