Skip to content

Commit 919366e

Browse files
committed
Merge branch 'develop' for v1.1.0 release
2 parents 59a762f + 3428955 commit 919366e

File tree

9 files changed

+123
-6
lines changed

9 files changed

+123
-6
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Atticus White
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,44 @@ angular.module('myApp', ['myApp.config']).run(function (string) {
5454

5555
## Configuration
5656
Currently there are a few configurable options to control the output of your configuration file:
57+
- [options.environment](#options.environment)
5758
- [options.constants](#options.constants)
5859
- [options.createModule](#options.createModule)
5960
- [options.wrap](#options.wrap)
6061

62+
### <a id="options.environment"></a>options.environment
63+
Type: `String` Optional
64+
65+
If your configuration contains multiple environments, you can supply the key you want the plugin to load from your configuration file.
66+
67+
Example `config.json` file with multiple environments:
68+
```json
69+
{
70+
"local": {
71+
"EnvironmentConfig": {
72+
"api": "http://localhost/"
73+
}
74+
},
75+
"production": {
76+
"EnvironmentConfig": {
77+
"api": "https://api.production.com/"
78+
}
79+
}
80+
}
81+
```
82+
83+
Usage of the plugin:
84+
```js
85+
gulpNgConfig('myApp.config', {
86+
environment: 'production'
87+
})
88+
```
89+
90+
Expected output:
91+
```js
92+
angular.module('myApp.config', [])
93+
.contant('EnvironmentConfig', {"api": "https://api.production.com/"});
94+
```
6195

6296
### <a id="options.constants"></a>options.constants
6397
Type: `Object` Optional

gulp-ng-config.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function gulpNgConfig (moduleName, configuration) {
1212
var templateFile, stream, defaults;
1313
defaults = {
1414
createModule: true,
15-
wrap: false
15+
wrap: false,
16+
environment: null
1617
};
1718

1819
if (!moduleName) {
@@ -41,14 +42,19 @@ function gulpNgConfig (moduleName, configuration) {
4142

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

45+
// select the environment in the configuration
46+
if (configuration.environment && jsonObj.hasOwnProperty(configuration.environment)) {
47+
jsonObj = jsonObj[configuration.environment];
48+
}
49+
4450
_.each(jsonObj, function (value, key) {
4551
constants.push({
4652
name: key,
4753
value: JSON.stringify(value)
4854
});
4955
});
5056

51-
templateOutput = _.template(templateFile, {
57+
templateOutput = _.template(templateFile)({
5258
createModule: configuration.createModule,
5359
moduleName: moduleName,
5460
constants: constants
@@ -60,7 +66,7 @@ function gulpNgConfig (moduleName, configuration) {
6066
} else {
6167
wrapTemplate = WRAP_TEAMPLTE;
6268
}
63-
templateOutput = _.template(wrapTemplate, {
69+
templateOutput = _.template(wrapTemplate)({
6470
module: templateOutput
6571
});
6672
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-ng-config",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "AngularJS configuration generator for a module of constants",
55
"main": "index.js",
66
"scripts": {
@@ -29,13 +29,13 @@
2929
"homepage": "https://github.com/ajwhite/gulp-ng-config",
3030
"dependencies": {
3131
"gulp-util": "^3.0.0",
32-
"lodash": "^2.4.1",
32+
"lodash": "^3.0.1",
3333
"through2": "^1.1.1"
3434
},
3535
"devDependencies": {
3636
"chai": "^1.9.1",
3737
"chai-spies": "^0.5.1",
38-
"event-stream": "^3.1.7",
38+
"event-stream": "^3.3.0",
3939
"gulp": "^3.8.7",
4040
"gulp-jscs": "^1.3.1",
4141
"gulp-jshint": "^1.8.4",

test/mocks/input_3.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"environmentA": {
3+
"one": {
4+
"two": "three"
5+
}
6+
},
7+
"environmentB": {
8+
"four": {
9+
"five": "six"
10+
}
11+
}
12+
}

test/mocks/output_10.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
angular.module('gulp-ng-config', [])
2+
.constant('environmentA', {"one":{"two":"three"}})
3+
.constant('environmentB', {"four":{"five":"six"}});

test/mocks/output_8.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
angular.module('gulp-ng-config', [])
2+
.constant('one', {"two":"three"});

test/mocks/output_9.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
angular.module('gulp-ng-config', [])
2+
.constant('four', {"five":"six"});

test/stream.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,42 @@ describe('gulp-ng-config', function () {
166166
done();
167167
}));
168168
});
169+
it ('should select an embedded json object if an environment key is supplied and the key exists', function (done) {
170+
var expectedOutputA = fs.readFileSync(path.normalize(__dirname + '/mocks/output_8.js')), // match envA
171+
expectedOutputB = fs.readFileSync(path.normalize(__dirname + '/mocks/output_9.js')), // match envB
172+
expectedOutputC = fs.readFileSync(path.normalize(__dirname + '/mocks/output_10.js')), // no match
173+
streamA = gulp.src(path.normalize(__dirname + '/mocks/input_3.json')),
174+
streamB = gulp.src(path.normalize(__dirname + '/mocks/input_3.json')),
175+
streamC = gulp.src(path.normalize(__dirname + '/mocks/input_3.json'));
176+
177+
// tests output with `environmentA`
178+
streamA.pipe(plugin('gulp-ng-config', {
179+
environment: 'environmentA'
180+
}))
181+
.pipe(through.obj(function (file) {
182+
expect(file.contents.toString()).to.equal(expectedOutputA.toString());
183+
}));
184+
185+
// tests output with `environmentB`
186+
streamB.pipe(plugin('gulp-ng-config', {
187+
environment: 'environmentB'
188+
}))
189+
.pipe(through.obj(function (file) {
190+
expect(file.contents.toString()).to.equal(expectedOutputB.toString());
191+
}));
192+
193+
// tests output with no matching environment key
194+
streamC.pipe(plugin('gulp-ng-config', {
195+
environment: 'nonExistant'
196+
}))
197+
.pipe(through.obj(function (file) {
198+
expect(file.contents.toString()).to.equal(expectedOutputC.toString());
199+
}));
200+
201+
es.merge(streamA, streamB, streamC)
202+
.pipe(through.obj(function () {
203+
done();
204+
}));
205+
});
169206
});
170207
});

0 commit comments

Comments
 (0)