Skip to content

Commit

Permalink
Merge branch 'develop' for v1.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
atticoos committed Mar 29, 2015
2 parents 59a762f + 3428955 commit 919366e
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 6 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Atticus White

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,44 @@ angular.module('myApp', ['myApp.config']).run(function (string) {

## Configuration
Currently there are a few configurable options to control the output of your configuration file:
- [options.environment](#options.environment)
- [options.constants](#options.constants)
- [options.createModule](#options.createModule)
- [options.wrap](#options.wrap)

### <a id="options.environment"></a>options.environment
Type: `String` Optional

If your configuration contains multiple environments, you can supply the key you want the plugin to load from your configuration file.

Example `config.json` file with multiple environments:
```json
{
"local": {
"EnvironmentConfig": {
"api": "http://localhost/"
}
},
"production": {
"EnvironmentConfig": {
"api": "https://api.production.com/"
}
}
}
```

Usage of the plugin:
```js
gulpNgConfig('myApp.config', {
environment: 'production'
})
```

Expected output:
```js
angular.module('myApp.config', [])
.contant('EnvironmentConfig', {"api": "https://api.production.com/"});
```

### <a id="options.constants"></a>options.constants
Type: `Object` Optional
Expand Down
12 changes: 9 additions & 3 deletions gulp-ng-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function gulpNgConfig (moduleName, configuration) {
var templateFile, stream, defaults;
defaults = {
createModule: true,
wrap: false
wrap: false,
environment: null
};

if (!moduleName) {
Expand Down Expand Up @@ -41,14 +42,19 @@ function gulpNgConfig (moduleName, configuration) {

jsonObj = _.merge({}, jsonObj, configuration.constants || {});

// select the environment in the configuration
if (configuration.environment && jsonObj.hasOwnProperty(configuration.environment)) {
jsonObj = jsonObj[configuration.environment];
}

_.each(jsonObj, function (value, key) {
constants.push({
name: key,
value: JSON.stringify(value)
});
});

templateOutput = _.template(templateFile, {
templateOutput = _.template(templateFile)({
createModule: configuration.createModule,
moduleName: moduleName,
constants: constants
Expand All @@ -60,7 +66,7 @@ function gulpNgConfig (moduleName, configuration) {
} else {
wrapTemplate = WRAP_TEAMPLTE;
}
templateOutput = _.template(wrapTemplate, {
templateOutput = _.template(wrapTemplate)({
module: templateOutput
});
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-ng-config",
"version": "1.0.0",
"version": "1.1.0",
"description": "AngularJS configuration generator for a module of constants",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -29,13 +29,13 @@
"homepage": "https://github.com/ajwhite/gulp-ng-config",
"dependencies": {
"gulp-util": "^3.0.0",
"lodash": "^2.4.1",
"lodash": "^3.0.1",
"through2": "^1.1.1"
},
"devDependencies": {
"chai": "^1.9.1",
"chai-spies": "^0.5.1",
"event-stream": "^3.1.7",
"event-stream": "^3.3.0",
"gulp": "^3.8.7",
"gulp-jscs": "^1.3.1",
"gulp-jshint": "^1.8.4",
Expand Down
12 changes: 12 additions & 0 deletions test/mocks/input_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"environmentA": {
"one": {
"two": "three"
}
},
"environmentB": {
"four": {
"five": "six"
}
}
}
3 changes: 3 additions & 0 deletions test/mocks/output_10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
angular.module('gulp-ng-config', [])
.constant('environmentA', {"one":{"two":"three"}})
.constant('environmentB', {"four":{"five":"six"}});
2 changes: 2 additions & 0 deletions test/mocks/output_8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
angular.module('gulp-ng-config', [])
.constant('one', {"two":"three"});
2 changes: 2 additions & 0 deletions test/mocks/output_9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
angular.module('gulp-ng-config', [])
.constant('four', {"five":"six"});
37 changes: 37 additions & 0 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,42 @@ describe('gulp-ng-config', function () {
done();
}));
});
it ('should select an embedded json object if an environment key is supplied and the key exists', function (done) {
var expectedOutputA = fs.readFileSync(path.normalize(__dirname + '/mocks/output_8.js')), // match envA
expectedOutputB = fs.readFileSync(path.normalize(__dirname + '/mocks/output_9.js')), // match envB
expectedOutputC = fs.readFileSync(path.normalize(__dirname + '/mocks/output_10.js')), // no match
streamA = gulp.src(path.normalize(__dirname + '/mocks/input_3.json')),
streamB = gulp.src(path.normalize(__dirname + '/mocks/input_3.json')),
streamC = gulp.src(path.normalize(__dirname + '/mocks/input_3.json'));

// tests output with `environmentA`
streamA.pipe(plugin('gulp-ng-config', {
environment: 'environmentA'
}))
.pipe(through.obj(function (file) {
expect(file.contents.toString()).to.equal(expectedOutputA.toString());
}));

// tests output with `environmentB`
streamB.pipe(plugin('gulp-ng-config', {
environment: 'environmentB'
}))
.pipe(through.obj(function (file) {
expect(file.contents.toString()).to.equal(expectedOutputB.toString());
}));

// tests output with no matching environment key
streamC.pipe(plugin('gulp-ng-config', {
environment: 'nonExistant'
}))
.pipe(through.obj(function (file) {
expect(file.contents.toString()).to.equal(expectedOutputC.toString());
}));

es.merge(streamA, streamB, streamC)
.pipe(through.obj(function () {
done();
}));
});
});
});

0 comments on commit 919366e

Please sign in to comment.