Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Simplified environment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
szwacz committed Jun 4, 2016
1 parent 4c32397 commit bdaad98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
22 changes: 3 additions & 19 deletions app/env.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
// Simple module exposing environment variables to rest of the code.
// Simple wrapper exposing environment variables to rest of the code.

import jetpack from 'fs-jetpack';

// Normal way of obtaining env variables: They are written to package.json file.
var env;
var app;
if (process.type === 'renderer') {
app = require('electron').remote.app;
} else {
app = require('electron').app;
}
var appDir = jetpack.cwd(app.getAppPath());
var manifest = appDir.read('package.json', 'json');

if (manifest && manifest.env) {
env = manifest.env;
} else {
// If 'normal way' failed, assume we're in test environment (where normal
// way won't work) and grab the variables in a ditry way.
env = jetpack.cwd(__dirname).read('../config/env_test.json', 'json');
}
// The variables have been written to `env.json` by the build process.
var env = jetpack.cwd(__dirname).read('env.json', 'json');

export default env;
14 changes: 14 additions & 0 deletions app/hello_world/boilerplate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Tests here are for easier maintenance of this boilerplate.
// Feel free to delete this file in your own project.

import { expect } from 'chai';
import env from '../env';

describe("boilerplate tests", function () {

it("environment variables should be on their place", function () {
expect(env.name).to.equal('test');
expect(env.description).to.equal('Add here any environment specific stuff you like.');
});

});
31 changes: 13 additions & 18 deletions tasks/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,22 @@ gulp.task('less', ['clean'], lessTask);
gulp.task('less-watch', lessTask);


gulp.task('finalize', ['clean'], function () {
gulp.task('environment', ['clean'], function () {
var configFile = 'config/env_' + utils.getEnvName() + '.json';
projectDir.copy(configFile, destDir.path('env.json'));
});


gulp.task('package-json', ['clean'], function () {
var manifest = srcDir.read('package.json', 'json');

// Add "dev" or "test" suffix to name, so Electron will write all data
// like cookies and localStorage in separate places for each environment.
switch (utils.getEnvName()) {
case 'development':
manifest.name += '-dev';
manifest.productName += ' Dev';
break;
case 'test':
manifest.name += '-test';
manifest.productName += ' Test';
break;
// Add "dev" suffix to name, so Electron will write all data like cookies
// and localStorage in separate places for production and development.
if (utils.getEnvName() === 'development') {
manifest.name += '-dev';
manifest.productName += ' Dev';
}

// Copy environment variables to package.json file for easy use
// in the running application. This is not official way of doing
// things, but also isn't prohibited ;)
manifest.env = projectDir.read('config/env_' + utils.getEnvName() + '.json', 'json');

destDir.write('package.json', manifest);
});

Expand All @@ -116,4 +111,4 @@ gulp.task('watch', function () {
});


gulp.task('build', ['bundle', 'less', 'copy', 'finalize']);
gulp.task('build', ['bundle', 'less', 'copy', 'environment', 'package-json']);

0 comments on commit bdaad98

Please sign in to comment.