├── .gitignore ├── .jscsrc ├── .jshintrc ├── .travis.yml ├── LICENSE.md ├── README.md ├── gulp-ng-config.js ├── gulpfile.js ├── index.js ├── package.json ├── template.html └── test ├── mocks ├── customTemplate.html ├── input_1.json ├── input_1.yml ├── input_2.json ├── input_2.yaml ├── input_2.yml ├── input_3.json ├── input_4.json ├── input_5.json ├── output_1.js ├── output_10.js ├── output_11.js ├── output_12.js ├── output_13.js ├── output_14.js ├── output_15.js ├── output_16.js ├── output_17.js ├── output_18.js ├── output_19.js ├── output_2.js ├── output_20.js ├── output_21.js ├── output_22.js ├── output_3.js ├── output_4.js ├── output_5.js ├── output_6.js ├── output_7.js ├── output_8.js └── output_9.js └── stream.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build 3 | *.log 4 | .idea 5 | coverage/ 6 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "requireCurlyBraces": [ 3 | "if", 4 | "else", 5 | "for", 6 | "while", 7 | "do", 8 | "try", 9 | "catch" 10 | ], 11 | "requireSpaceAfterKeywords": [ 12 | "if", 13 | "else", 14 | "for", 15 | "while", 16 | "do", 17 | "switch", 18 | "return", 19 | "try", 20 | "catch" 21 | ], 22 | "requireSpaceBeforeBlockStatements": true, 23 | "requireParenthesesAroundIIFE": true, 24 | "requireSpacesInConditionalExpression": { 25 | "afterTest": true, 26 | "beforeConsequent": true, 27 | "afterConsequent": true, 28 | "beforeAlternate": true 29 | }, 30 | "requireSpacesInFunctionExpression": { 31 | "beforeOpeningRoundBrace": true, 32 | "beforeOpeningCurlyBrace": true 33 | }, 34 | "requireSpacesInAnonymousFunctionExpression": { 35 | "beforeOpeningRoundBrace": true, 36 | "beforeOpeningCurlyBrace": true 37 | }, 38 | "requireSpacesInNamedFunctionExpression": { 39 | "beforeOpeningRoundBrace": true, 40 | "beforeOpeningCurlyBrace": true 41 | }, 42 | "requireSpacesInFunctionDeclaration": { 43 | "beforeOpeningRoundBrace": true, 44 | "beforeOpeningCurlyBrace": true 45 | }, 46 | "requireMultipleVarDecl": true, 47 | "requireBlocksOnNewline": true, 48 | "disallowPaddingNewlinesInBlocks": true, 49 | "disallowEmptyBlocks": true, 50 | "disallowSpacesInsideObjectBrackets": "all", 51 | "disallowSpacesInsideParentheses": true, 52 | "disallowSpaceAfterObjectKeys": true, 53 | "requireCommaBeforeLineBreak": true, 54 | 55 | "requireSpaceBeforeBinaryOperators": [ 56 | "=", 57 | "==", 58 | "===", 59 | "!=", 60 | "!==", 61 | ">", 62 | ">=", 63 | "<", 64 | "<=" 65 | ], 66 | "requireSpaceAfterBinaryOperators": [ 67 | "=", 68 | "==", 69 | "===", 70 | "!=", 71 | "!==", 72 | ">", 73 | ">=", 74 | "<", 75 | "<=" 76 | ], 77 | "requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties", 78 | "disallowMultipleLineBreaks": true, 79 | "validateQuoteMarks": { 80 | "mark": "'", 81 | "escape": true 82 | }, 83 | "validateIndentation": 2, 84 | "disallowMixedSpacesAndTabs": true, 85 | "disallowTrailingWhitespace": true, 86 | "disallowTrailingComma": true, 87 | "disallowKeywordsOnNewLine": [ 88 | "else" 89 | ], 90 | "requireLineFeedAtFileEnd": true, 91 | "maximumLineLength": 120, 92 | "requireCapitalizedConstructors": true, 93 | "disallowYodaConditions": true, 94 | "jsDoc": { 95 | "checkParamNames": true, 96 | "checkRedundantParams": true, 97 | "requireParamTypes": true 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "camelcase": false, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 2, 10 | "newcap": true, 11 | "noarg": true, 12 | "regexp": true, 13 | "quotmark": "single", 14 | "unused": "vars", 15 | "undef": true, 16 | "trailing": true, 17 | "smarttabs": true, 18 | "sub": true, 19 | "globals": { 20 | "describe": true, 21 | "before": true, 22 | "it": true 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | node_js: 5 | - '0.10' 6 | - '0.11' 7 | - '0.12' 8 | - '4.0' 9 | - '4.1' 10 | - '4.2' 11 | - '5' 12 | - '6' 13 | - iojs 14 | 15 | cache: 16 | directories: 17 | - node_modules 18 | 19 | after_script: 'gulp coveralls' 20 | 21 | notifications: 22 | slack: 23 | secure: AcgM4v8ZbWRW0jTGE+1L5HMSkW6D+f69JdoO+duCTHIzkIXtH2MHbO2tLczFUPVoSq7u+VL8+9gV+HHp4nn7IK12GTEYsd6SfCNZBSGLw3kK7tuupsdMAg0tIl+3zyhyQSwGG3sr/ZXQ7Tce8qDKSJ37YzT3R6EZSqSaACdE708= 24 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-ng-config 2 | 3 | [![License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://npmjs.org/package/gulp-ng-config) 4 | [![NPM version](http://img.shields.io/npm/v/gulp-ng-config.svg?style=flat)](https://npmjs.org/package/gulp-ng-config) 5 | [![NPM version](http://img.shields.io/npm/dm/gulp-ng-config.svg?style=flat)](https://npmjs.org/package/gulp-ng-config) 6 | [![Build Status](http://img.shields.io/travis/ajwhite/gulp-ng-config.svg?style=flat)](http://travis-ci.org/ajwhite/gulp-ng-config) 7 | [![Coverage Status](https://coveralls.io/repos/ajwhite/gulp-ng-config/badge.svg?branch=develop&service=github)](https://coveralls.io/github/ajwhite/gulp-ng-config?branch=develop) 8 | [![Code Climate](https://codeclimate.com/github/ajwhite/gulp-ng-config/badges/gpa.svg)](https://codeclimate.com/github/ajwhite/gulp-ng-config) 9 | [![Dependency Status](http://img.shields.io/gemnasium/ajwhite/gulp-ng-config.svg?style=flat)](https://gemnasium.com/ajwhite/gulp-ng-config) 10 | 11 | [![NPM](https://nodei.co/npm/gulp-ng-config.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/gulp-ng-config/) 12 | 13 | It's often useful to generate a file of constants, usually as environment variables, for your Angular apps. 14 | This Gulp plugin will allow you to provide an object of properties and will generate an Angular module of constants. 15 | 16 | ## To Install: 17 | `npm install gulp-ng-config` 18 | 19 | ## How it works 20 | It's pretty simple: 21 | `gulpNgConfig(moduleName)` 22 | 23 | 24 | ## Example Usage 25 | We start with our task. Our source file is a JSON file containing our configuration. We will pipe this through `gulpNgConfig` and out will come an angular module of constants. 26 | ```javascript 27 | var gulp = require('gulp'); 28 | var gulpNgConfig = require('gulp-ng-config'); 29 | 30 | gulp.task('test', function () { 31 | gulp.src('configFile.json') 32 | .pipe(gulpNgConfig('myApp.config')) 33 | .pipe(gulp.dest('.')) 34 | }); 35 | ``` 36 | Assume that `configFile.json` contains: 37 | ```json 38 | { 39 | "string": "my string", 40 | "integer": 12345, 41 | "object": {"one": 2, "three": ["four"]}, 42 | "array": ["one", 2, {"three": "four"}, [5, "six"]] 43 | } 44 | ``` 45 | Running `gulp test` will take `configFile.json` and produce `configFile.js` with the following content: 46 | 47 | ```js 48 | angular.module('myApp.config', []) 49 | .constant('string', "my string") 50 | .constant('integer', 12345) 51 | .constant('object', {"one":2,"three":["four"]}) 52 | .constant('array', ["one",2,{"three":"four"},[5,"six"]]); 53 | ``` 54 | We now can include this configuration module in our main app and access the constants 55 | ```js 56 | angular.module('myApp', ['myApp.config']).run(function (string) { 57 | console.log("The string constant!", string) // outputs "my string" 58 | }); 59 | ``` 60 | 61 | 62 | ## Configuration 63 | Currently there are a few configurable options to control the output of your configuration file: 64 | - [options.environment](#options.environment) 65 | - [options.constants](#options.constants) 66 | - [options.createModule](#options.createModule) 67 | - [options.type](#options.type) 68 | - [options.wrap](#options.wrap) 69 | - [options.parser](#options.parser) 70 | - [options.pretty](#options.pretty) 71 | - [options.keys](#options.keys), 72 | - [options.templateFilePath](#options.templateFilePath) 73 | 74 | ### options.environment 75 | Type: `String` Optional 76 | 77 | If your configuration contains multiple environments, you can supply the key you want the plugin to load from your configuration file. 78 | 79 | Example `config.json` file with multiple environments: 80 | ```json 81 | { 82 | "local": { 83 | "EnvironmentConfig": { 84 | "api": "http://localhost/" 85 | } 86 | }, 87 | "production": { 88 | "EnvironmentConfig": { 89 | "api": "https://api.production.com/" 90 | } 91 | } 92 | } 93 | ``` 94 | 95 | Usage of the plugin: 96 | ```js 97 | gulpNgConfig('myApp.config', { 98 | environment: 'production' 99 | }) 100 | ``` 101 | 102 | Expected output: 103 | ```js 104 | angular.module('myApp.config', []) 105 | .constant('EnvironmentConfig', {"api": "https://api.production.com/"}); 106 | ``` 107 | 108 | #### Nested Environment 109 | If the configuration is nested it can be accessed by the namespace, for example 110 | ```json 111 | { 112 | "version": "0.1.0", 113 | "env": { 114 | "local": { 115 | "EnvironmentConfig": { 116 | "api": "http://localhost/" 117 | } 118 | }, 119 | "production": { 120 | "EnvironmentConfig": { 121 | "api": "https://api.production.com/" 122 | } 123 | } 124 | } 125 | } 126 | ``` 127 | 128 | Usage of the plugin: 129 | ```js 130 | gulpNgConfig('myApp.config', { 131 | environment: 'env.production' 132 | }) 133 | ``` 134 | 135 | Expected output: 136 | ```js 137 | angular.module('myApp.config', []) 138 | .constant('EnvironmentConfig', {"api": "https://api.production.com/"}); 139 | ``` 140 | 141 | #### Multiple Environment keys 142 | Multiple environment keys can be supplied in an array, for example for global and environmental constants 143 | ```json 144 | { 145 | "global": { 146 | "version": "0.1.0" 147 | }, 148 | "env": { 149 | "local": { 150 | "EnvironmentConfig": { 151 | "api": "http://localhost/" 152 | } 153 | }, 154 | "production": { 155 | "EnvironmentConfig": { 156 | "api": "https://api.production.com/" 157 | } 158 | } 159 | } 160 | } 161 | ``` 162 | 163 | Usage of the plugin: 164 | ```js 165 | gulpNgConfig('myApp.config', { 166 | environment: ['env.production', 'global'] 167 | }) 168 | ``` 169 | 170 | Expected output: 171 | ```js 172 | angular.module('myApp.config', []) 173 | .constant('EnvironmentConfig', {"api": "https://api.production.com/"}); 174 | .constant('version', '0.1.0'); 175 | ``` 176 | 177 | ### options.constants 178 | Type: `Object` Optional 179 | 180 | You can also override properties from your json file or add more by including them in the gulp tasks: 181 | ```javascript 182 | gulpNgConfig('myApp.config', { 183 | constants: { 184 | string: 'overridden', 185 | random: 'value' 186 | } 187 | }); 188 | ``` 189 | Generating `configFile.js` 190 | ```js 191 | angular.module('myApp.config', []) 192 | .constant('string', "overridden") 193 | .constant('integer', 12345) 194 | .constant('object', {"one":2,"three":["four"]}) 195 | .constant('array', ["one",2,{"three":"four"},[5,"six"]]) 196 | .constant('random', "value"); 197 | 198 | ``` 199 | 200 | ### options.type 201 | Type: `String` Default value: `'constant'` Optional 202 | 203 | This allows configuring the type of service that is created -- a `constant` or a `value`. By default, a `constant` is created, but a `value` can be overridden. Possible types: 204 | 205 | - `'constant'` 206 | - `'value'` 207 | 208 | ```javascript 209 | gulpNgConfig('myApp.config', { 210 | type: 'value' 211 | }); 212 | ``` 213 | 214 | This will produce `configFile.js` with a `value` service. 215 | ```javascript 216 | angular.module('myApp.config', []) 217 | .value('..', '..'); 218 | ``` 219 | 220 | ### options.createModule 221 | Type: `Boolean` Default value: `true` Optional 222 | 223 | By default, a new module is created with the name supplied. You can access an existing module, rather than creating one, by setting `createModule` to false. 224 | ```javascript 225 | gulpNgConfig('myApp.config', { 226 | createModule: false 227 | }); 228 | ``` 229 | 230 | This will produce `configFile.js` with an existing angular module 231 | ```javascript 232 | angular.module('myApp.config') 233 | .constant('..', '..'); 234 | ``` 235 | 236 | ### options.wrap 237 | Type: `Boolean` or `String` Default value: `false` Optional 238 | 239 | Presets: 240 | - `ES6` 241 | - `ES2015` 242 | 243 | Wrap the configuration module in an IIFE or your own wrapper. 244 | 245 | ```js 246 | gulpNgConfig('myApp.config', { 247 | wrap: true 248 | }) 249 | ``` 250 | 251 | Will produce an IIFE wrapper for your configuration module: 252 | ```javascript 253 | (function () { 254 | return angular.module('myApp.config') // [] has been removed 255 | .constant('..', '..'); 256 | })(); 257 | ``` 258 | 259 | You can provide a custom wrapper. Provide any string you want, just make sure to include `<%= module %>` for where you want to embed the angular module. 260 | ```js 261 | gulpNgConfig('myApp.config', { 262 | wrap: 'define(["angular"], function () {\n return <%= module %> \n});' 263 | }); 264 | ``` 265 | 266 | The reuslting file will contain: 267 | ```js 268 | define(["angular"], function () { 269 | return angular.module('myApp.config', []) 270 | .constant('..', '..'); 271 | }); 272 | ``` 273 | 274 | ### options.parser 275 | Type: `String` Default value: 'json' Optional 276 | 277 | By default, json file is used to generate the module. You can provide yml file to generate the module. Just set `parser` to `'yml'` or `'yaml'`. If your file type is yml and you have not defined `parser`, your file will still be parsed and js be generated correctly. 278 | For example, you have a `config.yml` file, 279 | ```yml 280 | string: my string 281 | integer: 12345 282 | object: 283 | one: 2 284 | three: 285 | - four 286 | ``` 287 | 288 | ```javascript 289 | gulp.src("config.yml") 290 | .pipe(gulpNgConfig('myApp.config', { 291 | parser: 'yml' 292 | })); 293 | ``` 294 | 295 | Generating, 296 | ```js 297 | angular.module('myApp.config', []) 298 | .constant('string', "my string") 299 | .constant('integer', 12345) 300 | .constant('object', {"one":2,"three":["four"]}); 301 | ``` 302 | 303 | ### options.pretty 304 | Type: `Number|Boolean` Default value: `false` Optional 305 | 306 | This allows `JSON.stringify` to produce a `pretty` formatted output string. 307 | 308 | ```js 309 | gulp.src('config.json') 310 | .pipe(gulpNgConfig('myApp.config', { 311 | pretty: true // or 2, 4, etc -- all representing the number of spaces to indent 312 | })); 313 | ``` 314 | 315 | Will output a formatted `JSON` object in the constants, instead of inline. 316 | ```js 317 | angular.module("gulp-ng-config", []) 318 | .constant("one", { 319 | "two": "three" 320 | }); 321 | ``` 322 | 323 | ### options.keys 324 | Type: `Array` Optional 325 | 326 | If you only want some of the keys from the object imported, you can supply the keys you want the plugin to load. 327 | 328 | Example `config.json` file with unwanted keys: 329 | ```json 330 | { 331 | "version": "0.0.1", 332 | "wanted key": "wanted value", 333 | "unwanted key": "unwanted value" 334 | } 335 | ``` 336 | 337 | Usage of the plugin: 338 | ```js 339 | gulpNgConfig("myApp.config", { 340 | keys: ["version", "wanted key"] 341 | }) 342 | ``` 343 | 344 | Expected output: 345 | ```js 346 | angular.module("myApp.config", []) 347 | .constant("version", "0.0.1") 348 | .constant("wanted key", "wanted value"); 349 | ``` 350 | 351 | ### options.templateFilePath 352 | Type: `String` Optional 353 | 354 | This allows the developer to provide a custom output template. 355 | 356 | Sample template: 357 | `angularConfigTemplate.html` 358 | ```html 359 | var foo = 'bar'; 360 | 361 | angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %> 362 | .<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>; 363 | ``` 364 | 365 | Configuration: 366 | ```json 367 | { 368 | "Foo": "bar" 369 | } 370 | ``` 371 | 372 | Gulp task: 373 | ```js 374 | gulp.src('config.json') 375 | .pipe(gulpNgConfig('myApp.config', { 376 | templateFilePath: path.normalize(path.join(__dirname, 'templateFilePath.html')) 377 | })); 378 | ``` 379 | 380 | Sample output: 381 | ```js 382 | var foo = 'bar'; 383 | 384 | angular.module('myApp.config', []) 385 | .constant('Foo', 'bar'); 386 | ``` 387 | 388 | ## Additional Usages 389 | 390 | ### Without a json/yaml file on disk 391 | Use `buffer-to-vinyl` to create and stream a vinyl file into `gulp-ng-config`. Now config values can come from environment variables, command-line arguments or anywhere else. 392 | 393 | ```js 394 | var b2v = require('buffer-to-vinyl'); 395 | var gulpNgConfig = require('gulp-ng-config'); 396 | 397 | gulp.task('make-config', function() { 398 | var json = JSON.stringify({ 399 | // your config here 400 | }); 401 | 402 | return b2v.stream(new Buffer(json), 'config.js') 403 | .pipe(gulpNgConfig('myApp.config')) 404 | .pipe(gulp.dest('build')); 405 | }); 406 | ``` 407 | 408 | ### ES6/ES2015 409 | An ES6/ES2015 template can be generated by passing `wrap: true` as a configuration to the plugin 410 | 411 | ## Contributing 412 | Contributions, issues, suggestions, and all other remarks are welcomed. To run locally just fork & clone the project and run `npm install`. Before submitting a Pull Request, make sure that your changes pass `gulp test`, and if you are introducing or changing a feature, that you add/update any tests involved. 413 | -------------------------------------------------------------------------------- /gulp-ng-config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var through = require('through2'), 4 | _ = require('lodash'), 5 | fs = require('fs'), 6 | jsYaml = require('js-yaml'), 7 | replaceExt = require('replace-ext'), 8 | PluginError = require('plugin-error'), 9 | VALID_TYPES = ['constant', 'value'], 10 | PLUGIN_NAME = 'gulp-ng-config', 11 | WRAP_TEMPLATE = '(function () { \n return <%= module %>\n})();\n', 12 | ES6_TEMPLATE = 'import angular from \'angular\';\nexport default <%= module %>'; 13 | 14 | function gulpNgConfig (moduleName, configuration) { 15 | var stream, defaults; 16 | defaults = { 17 | type: 'constant', 18 | createModule: true, 19 | wrap: false, 20 | environment: null, 21 | parser: null, 22 | pretty: false, 23 | keys: null, 24 | templateFilePath: __dirname + '/template.html' 25 | }; 26 | 27 | if (!moduleName) { 28 | throw new PluginError(PLUGIN_NAME, 'Missing required moduleName option for gulp-ng-config'); 29 | } 30 | 31 | configuration = configuration || {}; 32 | configuration = _.merge({}, defaults, configuration); 33 | 34 | stream = through.obj(function (file, encoding, callback) { 35 | var constants = [], 36 | templateFile, 37 | templateOutput, 38 | jsonObj, 39 | wrapTemplate, 40 | spaces, 41 | environmentKeys; 42 | 43 | try { 44 | templateFile = fs.readFileSync(configuration.templateFilePath || defaults.templateFilePath, 'utf8'); 45 | } catch (error) { 46 | this.emit('error', new PluginError(PLUGIN_NAME, 'invalid templateFilePath option, file not found')); 47 | return callback(); 48 | } 49 | 50 | if (!configuration.parser && (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml'))) { 51 | configuration.parser = 'yml'; 52 | } 53 | 54 | if (!configuration.parser) { 55 | configuration.parser = 'json'; 56 | } 57 | 58 | if (configuration.parser === 'json') { 59 | try { 60 | jsonObj = file.isNull() ? {} : JSON.parse(file.contents.toString('utf8')); 61 | } catch (e) { 62 | this.emit('error', new PluginError(PLUGIN_NAME, 'invalid JSON file provided')); 63 | return callback(); 64 | } 65 | } else if (configuration.parser === 'yml' || configuration.parser === 'yaml') { 66 | try { 67 | jsonObj = jsYaml.safeLoad(file.contents); 68 | } catch (e) { 69 | this.emit('error', new PluginError(PLUGIN_NAME, 'invaild YML file provided')); 70 | return callback(); 71 | } 72 | } else { 73 | this.emit('error', new PluginError(PLUGIN_NAME, configuration.parser + ' is not supported as a valid parser')); 74 | return callback(); 75 | } 76 | 77 | if (!_.isPlainObject(jsonObj)) { 78 | this.emit('error', new PluginError(PLUGIN_NAME, 'configuration file contains invalid JSON')); 79 | return callback(); 80 | } 81 | 82 | // select the environment in the configuration 83 | if (configuration.environment) { 84 | // transform values into a flat array 85 | environmentKeys = [].concat(configuration.environment); 86 | 87 | // build the output based on the specifid keys 88 | jsonObj = environmentKeys.reduce(_.bind(function (obj, environmentKey) { 89 | var value = _.get(jsonObj, environmentKey); 90 | 91 | // if the key does not exist, raise an error. 92 | if (value === undefined) { 93 | this.emit('error', new PluginError(PLUGIN_NAME, 'invalid \'environment\' value')); 94 | return callback(); 95 | } 96 | 97 | // add the value to the output object 98 | _.merge(obj, value); 99 | return obj; 100 | }, this), {}); 101 | } 102 | 103 | if (!_.includes(VALID_TYPES, configuration.type)) { 104 | this.emit('error', new PluginError(PLUGIN_NAME, 'invalid \'type\' value')); 105 | return callback(); 106 | } 107 | 108 | if (configuration.keys) { 109 | if (_.isArray(configuration.keys)) { 110 | jsonObj = _.pick(jsonObj, configuration.keys); 111 | } else { 112 | this.emit('error', new PluginError(PLUGIN_NAME, 'invalid \'keys\' value')); 113 | return callback(); 114 | } 115 | } 116 | 117 | jsonObj = _.merge({}, jsonObj, configuration.constants || {}); 118 | 119 | if (_.isUndefined(configuration.pretty) || configuration.pretty === false) { 120 | spaces = 0; 121 | } else if (configuration.pretty === true) { 122 | spaces = 2; 123 | } else if (!isNaN(configuration.pretty) && Number.isFinite(configuration.pretty)) { 124 | spaces = parseInt(configuration.pretty); 125 | } else { 126 | this.emit('error', new PluginError( 127 | PLUGIN_NAME, 128 | 'invalid \'pretty\' value. Should be boolean value or an integer number' 129 | )); 130 | return callback(); 131 | } 132 | 133 | _.each(jsonObj, function (value, key) { 134 | constants.push({ 135 | name: key, 136 | value: JSON.stringify(value, null, spaces) 137 | }); 138 | }); 139 | 140 | templateOutput = _.template(templateFile)({ 141 | createModule: configuration.createModule, 142 | moduleName: moduleName, 143 | type: configuration.type, 144 | constants: constants 145 | }); 146 | 147 | if (configuration.wrap) { 148 | if (typeof configuration.wrap === 'string' && 149 | (configuration.wrap.toUpperCase() === 'ES6' || configuration.wrap.toUpperCase() === 'ES2015')) { 150 | wrapTemplate = ES6_TEMPLATE; 151 | } else if (typeof configuration.wrap === 'string') { 152 | wrapTemplate = configuration.wrap; 153 | } else { 154 | wrapTemplate = WRAP_TEMPLATE; 155 | } 156 | templateOutput = _.template(wrapTemplate)({ 157 | module: templateOutput 158 | }); 159 | } 160 | 161 | file.path = replaceExt(file.path, '.js'); 162 | file.contents = new Buffer(templateOutput); 163 | this.push(file); 164 | callback(); 165 | }); 166 | 167 | return stream; 168 | } 169 | 170 | module.exports = gulpNgConfig; 171 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'), 2 | mocha = require('gulp-mocha'), 3 | jscs = require('gulp-jscs'), 4 | jshint = require('gulp-jshint'), 5 | stylish = require('jshint-stylish'), 6 | istanbul = require('gulp-istanbul'), 7 | coveralls = require('gulp-coveralls'); 8 | 9 | gulp.task('lint', function () { 10 | return gulp.src(['gulp-ng-config.js', 'test/stream.js']) 11 | .pipe(jshint('.jshintrc')) 12 | .pipe(jshint.reporter('jshint-stylish')) 13 | .pipe(jshint.reporter('fail')) 14 | }); 15 | 16 | gulp.task('style', function () { 17 | return gulp.src(['gulp-ng-config.js', 'test/stream.js']) 18 | .pipe(jscs()) 19 | .pipe(jscs.reporter('console')) 20 | .pipe(jscs.reporter('fail')); 21 | }); 22 | 23 | gulp.task('pre-test', function () { 24 | return gulp.src('gulp-ng-config.js') 25 | .pipe(istanbul()) 26 | .pipe(istanbul.hookRequire()); 27 | }); 28 | 29 | gulp.task('unittest', ['pre-test'], function () { 30 | return gulp.src('test/stream.js') 31 | .pipe(mocha({reporter: 'spec'})) 32 | .pipe(istanbul.writeReports()); 33 | }); 34 | 35 | gulp.task('coveralls', function () { 36 | return gulp.src('coverage/lcov.info') 37 | .pipe(coveralls()); 38 | }); 39 | 40 | gulp.task('test', ['lint', 'style', 'unittest']); 41 | gulp.task('default', ['test']); 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./gulp-ng-config'); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-ng-config", 3 | "version": "1.5.1", 4 | "description": "AngularJS configuration generator for a module of constants", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "gulp test" 8 | }, 9 | "keywords": [ 10 | "gulp", 11 | "grunt", 12 | "config", 13 | "constant", 14 | "angular", 15 | "angularjs", 16 | "ng", 17 | "gulpfriendly", 18 | "gulpplugin" 19 | ], 20 | "repository": { 21 | "type": "git", 22 | "url": "https://github.com/ajwhite/gulp-ng-config.git" 23 | }, 24 | "author": "Atticus White", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/ajwhite/gulp-ng-config/issues" 28 | }, 29 | "homepage": "https://github.com/ajwhite/gulp-ng-config", 30 | "dependencies": { 31 | "js-yaml": "^3.7.0", 32 | "lodash": "^4.17.3", 33 | "plugin-error": "^0.1.2", 34 | "replace-ext": "^1.0.0", 35 | "through2": "^2.0.3" 36 | }, 37 | "devDependencies": { 38 | "chai": "^3.5.0", 39 | "chai-spies": "^0.7.1", 40 | "event-stream": "^3.3.4", 41 | "gulp": "^3.9.1", 42 | "gulp-coveralls": "^0.1.4", 43 | "gulp-istanbul": "^1.1.1", 44 | "gulp-jscs": "^4.0.0", 45 | "gulp-jshint": "^2.0.4", 46 | "gulp-mocha": "^3.0.1", 47 | "jshint": "^2.9.4", 48 | "jshint-stylish": "^2.2.1", 49 | "mocha": "^3.2.0", 50 | "vinyl": "^2.0.1" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /template.html: -------------------------------------------------------------------------------- 1 | angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %> 2 | .<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>; 3 | -------------------------------------------------------------------------------- /test/mocks/customTemplate.html: -------------------------------------------------------------------------------- 1 | var foo = 'bar'; 2 | 3 | angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %> 4 | .<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>; 5 | -------------------------------------------------------------------------------- /test/mocks/input_1.json: -------------------------------------------------------------------------------- 1 | { 2 | "one": "two" 3 | } 4 | -------------------------------------------------------------------------------- /test/mocks/input_1.yml: -------------------------------------------------------------------------------- 1 | one: two -------------------------------------------------------------------------------- /test/mocks/input_2.json: -------------------------------------------------------------------------------- 1 | { 2 | "one": { 3 | "two": "three" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/mocks/input_2.yaml: -------------------------------------------------------------------------------- 1 | one: 2 | two: three -------------------------------------------------------------------------------- /test/mocks/input_2.yml: -------------------------------------------------------------------------------- 1 | one: 2 | two: three -------------------------------------------------------------------------------- /test/mocks/input_3.json: -------------------------------------------------------------------------------- 1 | { 2 | "environmentA": { 3 | "one": { 4 | "two": "three" 5 | } 6 | }, 7 | "environmentB": { 8 | "four": { 9 | "five": "six" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/mocks/input_4.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.0.x", 3 | "env": { 4 | "environmentA": { 5 | "one": { 6 | "two": "three" 7 | } 8 | }, 9 | "environmentB": { 10 | "four": { 11 | "five": "six" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/mocks/input_5.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.0.x", 3 | "unwanted": "value", 4 | "wanted" : "another value" 5 | } 6 | -------------------------------------------------------------------------------- /test/mocks/output_1.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", "two"); 3 | -------------------------------------------------------------------------------- /test/mocks/output_10.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("environmentA", {"one":{"two":"three"}}) 3 | .constant("environmentB", {"four":{"five":"six"}}); 4 | -------------------------------------------------------------------------------- /test/mocks/output_11.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}) 3 | .constant("constant", "value"); 4 | -------------------------------------------------------------------------------- /test/mocks/output_12.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", { 3 | "two": "three" 4 | }); 5 | -------------------------------------------------------------------------------- /test/mocks/output_13.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", { 3 | "two": "three" 4 | }); 5 | -------------------------------------------------------------------------------- /test/mocks/output_14.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_15.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_16.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .value("one", {"two":"three"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_17.js: -------------------------------------------------------------------------------- 1 | import angular from 'angular'; 2 | export default angular.module("gulp-ng-config", []) 3 | .constant("one", {"two":"three"}); 4 | -------------------------------------------------------------------------------- /test/mocks/output_18.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}) 3 | .constant("four", {"five":"six"}); 4 | -------------------------------------------------------------------------------- /test/mocks/output_19.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("constant", "value"); 3 | -------------------------------------------------------------------------------- /test/mocks/output_2.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_20.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}) 3 | .constant("five", "six"); 4 | -------------------------------------------------------------------------------- /test/mocks/output_21.js: -------------------------------------------------------------------------------- 1 | var foo = 'bar'; 2 | 3 | angular.module("gulp-ng-config", []) 4 | .constant("one", {"two":"three"}); 5 | -------------------------------------------------------------------------------- /test/mocks/output_22.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("version", "0.0.x") 3 | .constant("wanted", "another value"); 4 | -------------------------------------------------------------------------------- /test/mocks/output_3.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"four"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_4.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three","three":"four"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_5.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config") 2 | .constant("one", {"two":"three"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_6.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | return angular.module("gulp-ng-config", []) 3 | .constant("one", {"two":"three"}); 4 | 5 | })(); 6 | -------------------------------------------------------------------------------- /test/mocks/output_7.js: -------------------------------------------------------------------------------- 1 | define(['angular', function () { 2 | return angular.module("gulp-ng-config", []) 3 | .constant("one", {"two":"three"}); 4 | }]); 5 | -------------------------------------------------------------------------------- /test/mocks/output_8.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("one", {"two":"three"}); 3 | -------------------------------------------------------------------------------- /test/mocks/output_9.js: -------------------------------------------------------------------------------- 1 | angular.module("gulp-ng-config", []) 2 | .constant("four", {"five":"six"}); 3 | -------------------------------------------------------------------------------- /test/stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | describe('gulp-ng-config', function () { 4 | var expect, 5 | should, 6 | plugin = require('../gulp-ng-config'), 7 | gulp = require('gulp'), 8 | path = require('path'), 9 | chai = require('chai'), 10 | spies = require('chai-spies'), 11 | File = require('vinyl'), 12 | es = require('event-stream'), 13 | through = require('through2'), 14 | fs = require('fs'), 15 | Readable = require('stream').Readable; 16 | 17 | before(function () { 18 | chai.use(spies); 19 | expect = chai.expect; 20 | should = chai.should; 21 | }); 22 | 23 | describe('error handling', function () { 24 | it('should throw an error if a module name is missing', function () { 25 | expect(function () { 26 | plugin(); 27 | }).to.throw(Error); 28 | }); 29 | 30 | it ('should only accept files in JSON format', function () { 31 | var mockFiles = [ 32 | new File({ 33 | path: 'mock/path.json', 34 | contents: new Readable({objectMode: true}).wrap(es.readArray(['one', 'two'])) 35 | }), 36 | new File({ 37 | path: 'mock/path.json', 38 | contents: new Buffer('a string') 39 | }), 40 | new File({ 41 | path: 'mock/path.json', 42 | contents: new Buffer(123) 43 | }) 44 | ]; 45 | mockFiles.forEach(function (file) { 46 | var stream = plugin('outputConfigurationName'); 47 | stream.on('error', function (error) { 48 | expect(error.message).to.be.eql('invalid JSON file provided'); 49 | }); 50 | expect(function () { 51 | stream.write(file); 52 | }).not.to.throw(); 53 | }); 54 | }); 55 | 56 | it('should emit an error on malformed JSON', function () { 57 | var file, 58 | stream; 59 | stream = plugin('asdf'); 60 | stream.on('error', function (error) { 61 | expect(error.message).to.be.equal('invalid JSON file provided'); 62 | }); 63 | file = new File({ 64 | path: 'mock/path.json', 65 | contents: new Buffer('{a:b}') 66 | }); 67 | expect(function () { 68 | stream.write(file); 69 | }).to.not.throw(); 70 | }); 71 | it ('should emit an error when supplying an invalid parser', function () { 72 | var file, 73 | stream; 74 | 75 | file = new File({ 76 | path: 'mock/path.json', 77 | contents: new Buffer('{"foo": "bar"}') 78 | }); 79 | 80 | stream = plugin('asdf', { 81 | parser: 'invalidParser' 82 | }); 83 | stream.on('error', function (error) { 84 | expect(error.message).to.be.eql('invalidParser' + ' is not supported as a valid parser'); 85 | }); 86 | expect(function () { 87 | stream.write(file); 88 | }).not.to.throw(); 89 | }); 90 | it ('should emit an error if the configuration exposes an invalid JSON object', function (done) { 91 | var file, 92 | stream; 93 | 94 | file = new File({ 95 | path: 'mock/path.json', 96 | contents: new Buffer('1') 97 | }); 98 | 99 | stream = plugin('asdf') 100 | .on('error', function (error) { 101 | expect(error.message).to.equal('configuration file contains invalid JSON'); 102 | done(); 103 | }); 104 | 105 | expect(function () { 106 | stream.write(file); 107 | }).not.to.throw(); 108 | }); 109 | }); 110 | 111 | describe('config generation', function () { 112 | describe('json', function () { 113 | it('should generate the angular template with scalar properties', function (done) { 114 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_1.js')); 115 | gulp.src(path.normalize(__dirname + '/mocks/input_1.json')) 116 | .pipe(plugin('gulp-ng-config')) 117 | .pipe(through.obj(function (file) { 118 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 119 | done(); 120 | })); 121 | }); 122 | it('should generate the angular template with object properties', function (done) { 123 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); 124 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 125 | .pipe(plugin('gulp-ng-config')) 126 | .pipe(through.obj(function (file) { 127 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 128 | done(); 129 | })); 130 | }); 131 | }); 132 | describe('yml', function () { 133 | it('should generate the angular template with scalar properties', function (done) { 134 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_1.js')); 135 | gulp.src(path.normalize(__dirname + '/mocks/input_1.yml')) 136 | .pipe(plugin('gulp-ng-config', { 137 | parser: 'yml' 138 | })) 139 | .pipe(through.obj(function (file) { 140 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 141 | done(); 142 | })); 143 | }); 144 | it('should generate the angular template with object properties', function (done) { 145 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); 146 | gulp.src(path.normalize(__dirname + '/mocks/input_2.yml')) 147 | .pipe(plugin('gulp-ng-config', { 148 | parser: 'yml' 149 | })) 150 | .pipe(through.obj(function (file) { 151 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 152 | done(); 153 | })); 154 | }); 155 | it('should generate the angular template with object properties with no parser', function (done) { 156 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); 157 | gulp.src(path.normalize(__dirname + '/mocks/input_2.yml')) 158 | .pipe(plugin('gulp-ng-config')) 159 | .pipe(through.obj(function (file) { 160 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 161 | done(); 162 | })); 163 | }); 164 | it('should emit an error if an invalid yml file is provided', function (done) { 165 | var file, 166 | stream; 167 | 168 | file = new File({ 169 | path: 'mock/path.yml', 170 | contents: new Buffer('[}]') 171 | }); 172 | 173 | stream = plugin('name') 174 | .on('error', function (error) { 175 | expect(error.message).to.equal('invaild YML file provided'); 176 | done(); 177 | }); 178 | expect(function () { 179 | stream.write(file); 180 | }).not.to.throw(); 181 | }); 182 | }); 183 | describe('yaml', function () { 184 | it('should generate the angular template with object properties', function (done) { 185 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); 186 | gulp.src(path.normalize(__dirname + '/mocks/input_2.yaml')) 187 | .pipe(plugin('gulp-ng-config', { 188 | parser: 'yaml' 189 | })) 190 | .pipe(through.obj(function (file) { 191 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 192 | done(); 193 | })); 194 | }); 195 | it('should emit an error if an invalid yaml file is provided', function (done) { 196 | var file, 197 | stream; 198 | 199 | file = new File({ 200 | path: 'mock/path.yaml', 201 | contents: new Buffer('[}]') 202 | }); 203 | 204 | stream = plugin('name') 205 | .on('error', function (error) { 206 | expect(error.message).to.equal('invaild YML file provided'); 207 | done(); 208 | }); 209 | expect(function () { 210 | stream.write(file); 211 | }).not.to.throw(); 212 | }); 213 | }); 214 | }); 215 | describe('plugin options', function () { 216 | it('should generate the angular template with overridable properties', function (done) { 217 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_3.js')); 218 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 219 | .pipe(plugin('gulp-ng-config', { 220 | constants: { 221 | one: { 222 | two: 'four' 223 | } 224 | } 225 | })) 226 | .pipe(through.obj(function (file) { 227 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 228 | done(); 229 | })); 230 | }); 231 | it('should generate the angular template with overridable and mergable properties', function (done) { 232 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_4.js')); 233 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 234 | .pipe(plugin('gulp-ng-config', { 235 | constants: { 236 | one: { 237 | three: 'four' 238 | } 239 | } 240 | })) 241 | .pipe(through.obj(function (file) { 242 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 243 | done(); 244 | })); 245 | }); 246 | it('should generate the angular template without declaring a new module', function (done) { 247 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_5.js')); 248 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 249 | .pipe(plugin('gulp-ng-config', { 250 | createModule: false 251 | })) 252 | .pipe(through.obj(function (file) { 253 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 254 | done(); 255 | })); 256 | }); 257 | it('should merge environment keys with constant keys', function (done) { 258 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_11.js')), 259 | streamA = gulp.src(path.normalize(__dirname + '/mocks/input_3.json')); 260 | 261 | streamA 262 | .pipe(plugin('gulp-ng-config', { 263 | environment: 'environmentA', 264 | constants: { 265 | constant: 'value' 266 | } 267 | })) 268 | .pipe(through.obj(function (file) { 269 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 270 | done(); 271 | })); 272 | }); 273 | describe('wrap', function () { 274 | it('should generate the angular template with an IFFE if options.wrap', function (done) { 275 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_6.js')); 276 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 277 | .pipe(plugin('gulp-ng-config', { 278 | wrap: true 279 | })) 280 | .pipe(through.obj(function (file) { 281 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 282 | done(); 283 | })); 284 | }); 285 | it('should generate the angular template with a custom wrap function if options.wrap is a string', 286 | function (done) { 287 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_7.js')); 288 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 289 | .pipe(plugin('gulp-ng-config', { 290 | wrap: 'define([\'angular\', function () {\n return <%= module %>}]);\n' 291 | })) 292 | .pipe(through.obj(function (file) { 293 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 294 | done(); 295 | })); 296 | }); 297 | it ('should generate an ES6 template when ES6 is specified for wrap', function (done) { 298 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_17.js'))); 299 | gulp.src(path.normalize(path.join(__dirname, 'mocks/input_2.json'))) 300 | .pipe(plugin('gulp-ng-config', { 301 | wrap: 'ES6' 302 | })) 303 | .pipe(through.obj(function (file) { 304 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 305 | done(); 306 | })); 307 | }); 308 | it ('should generate an ES6 template when ES2015 is specified for wrap', function (done) { 309 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_17.js'))); 310 | gulp.src(path.normalize(path.join(__dirname, 'mocks/input_2.json'))) 311 | .pipe(plugin('gulp-ng-config', { 312 | wrap: 'ES2015' 313 | })) 314 | .pipe(through.obj(function (file) { 315 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 316 | done(); 317 | })); 318 | }); 319 | }); 320 | describe('environment', function () { 321 | it ('should select an embedded json object if an environment key is supplied', function (done) { 322 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_8.js'))); 323 | gulp.src(path.normalize(path.join(__dirname, 'mocks/input_3.json'))) 324 | .pipe(plugin('gulp-ng-config', { 325 | environment: 'environmentA' 326 | })) 327 | .pipe(through.obj(function (file) { 328 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 329 | done(); 330 | })); 331 | }); 332 | it ('should select an embedded json object if a namespaced environment key is supplied', function (done) { 333 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_15.js'))); 334 | gulp.src(path.normalize(path.join(__dirname, 'mocks/input_4.json'))) 335 | .pipe(plugin('gulp-ng-config', { 336 | environment: 'env.environmentA' 337 | })) 338 | .pipe(through.obj(function (file) { 339 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 340 | done(); 341 | })); 342 | }); 343 | it('should emit an error if an environment key is supplied and the key does not exist', function (done) { 344 | gulp.src(path.normalize(__dirname + '/mocks/input_3.json')).pipe(plugin('gulp-ng-config', { 345 | environment: 'nonExistingEnvironment' 346 | })).on('error', function (error) { 347 | expect(error.message).to.be.eql('invalid \'environment\' value'); 348 | done(); 349 | }); 350 | }); 351 | it('should select specified embedded json objects if an array of environment keys is suplied', function (done) { 352 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_18.js'))); 353 | gulp.src(path.normalize(__dirname + '/mocks/input_3.json')).pipe(plugin('gulp-ng-config', { 354 | environment: ['environmentA', 'environmentB'] 355 | })).pipe(through.obj(function (file) { 356 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 357 | done(); 358 | })); 359 | }); 360 | it('should only include constans keys if an empty array is suplied', function (done) { 361 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_19.js'))); 362 | gulp.src(path.normalize(__dirname + '/mocks/input_3.json')).pipe(plugin('gulp-ng-config', { 363 | environment: [], 364 | constants: { 365 | constant: 'value' 366 | } 367 | })).pipe(through.obj(function (file) { 368 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 369 | done(); 370 | })); 371 | }); 372 | it('should select specified embedded json objects from array with namespaced environment keys', function (done) { 373 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_20.js'))); 374 | gulp.src(path.normalize(__dirname + '/mocks/input_3.json')).pipe(plugin('gulp-ng-config', { 375 | environment: ['environmentA', 'environmentB.four'] 376 | })).pipe(through.obj(function (file) { 377 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 378 | done(); 379 | })); 380 | }); 381 | }); 382 | describe('type', function () { 383 | it('should generate a `value` module if `type` is specified with `value`', function (done) { 384 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_16.js')); 385 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 386 | .pipe(plugin('gulp-ng-config', { 387 | type: 'value' 388 | })) 389 | .pipe(through.obj(function (file) { 390 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 391 | done(); 392 | })); 393 | }); 394 | it ('should generate a `constant` module if `type` is specified with a `constant`', function (done) { 395 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_15.js')); 396 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 397 | .pipe(plugin('gulp-ng-config', { 398 | type: 'constant' 399 | })) 400 | .pipe(through.obj(function (file) { 401 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 402 | done(); 403 | })); 404 | }); 405 | it ('should generate a `constant` module by default if `moduleTye` is not supplied', function (done) { 406 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_15.js')); 407 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 408 | .pipe(plugin('gulp-ng-config', { 409 | type: undefined 410 | })) 411 | .pipe(through.obj(function (file) { 412 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 413 | done(); 414 | })); 415 | }); 416 | it ('should emit an error if an invalid `type` is supplied', function (done) { 417 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 418 | .pipe(plugin('gulp-ng-config', { 419 | type: 'invalid' 420 | })) 421 | .on('error', function (error) { 422 | expect(error.message).to.be.eql('invalid \'type\' value'); 423 | done(); 424 | }); 425 | }); 426 | }); 427 | describe('pretty', function () { 428 | it('should generate pretty-looked content with default spaces', function (done) { 429 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_12.js')); 430 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 431 | .pipe(plugin('gulp-ng-config', { 432 | pretty: true 433 | })) 434 | .pipe(through.obj(function (file) { 435 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 436 | done(); 437 | })); 438 | }); 439 | it('should generate common-looked content with pretty set to false', function (done) { 440 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_14.js')); 441 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 442 | .pipe(plugin('gulp-ng-config', { 443 | pretty: false 444 | })) 445 | .pipe(through.obj(function (file) { 446 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 447 | done(); 448 | })); 449 | }); 450 | it('should generate pretty-looked content with number of spaces', function (done) { 451 | var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_13.js')); 452 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 453 | .pipe(plugin('gulp-ng-config', { 454 | pretty: 4 455 | })) 456 | .pipe(through.obj(function (file) { 457 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 458 | done(); 459 | })); 460 | }); 461 | it('should generate an error if an invalid value is provided', function (done) { 462 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 463 | .pipe(plugin('gulp-ng-config', { 464 | pretty: 'foobar' 465 | })) 466 | .on('error', function (error) { 467 | expect(error.message).to.equal('invalid \'pretty\' value. Should be boolean value or an integer number'); 468 | done(); 469 | }); 470 | }); 471 | it('should generate an error if an inifite value is provided', function (done) { 472 | gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) 473 | .pipe(plugin('gulp-ng-config', { 474 | pretty: Infinity 475 | })) 476 | .on('error', function (error) { 477 | expect(error.message).to.equal('invalid \'pretty\' value. Should be boolean value or an integer number'); 478 | done(); 479 | }); 480 | }); 481 | }); 482 | describe('keys', function () { 483 | it ('should select some keys if a keys option is supplied', function (done) { 484 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_22.js'))); 485 | gulp.src(path.normalize(path.join(__dirname, 'mocks/input_5.json'))) 486 | .pipe(plugin('gulp-ng-config', { 487 | keys: ['version', 'wanted'] 488 | })) 489 | .pipe(through.obj(function (file) { 490 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 491 | done(); 492 | })); 493 | }); 494 | it('should emit an error if keys option is not an array', function (done) { 495 | gulp.src(path.normalize(__dirname + '/mocks/input_5.json')).pipe(plugin('gulp-ng-config', { 496 | keys: 'non-array value' 497 | })).on('error', function (error) { 498 | expect(error.message).to.be.eql('invalid \'keys\' value'); 499 | done(); 500 | }); 501 | }); 502 | }); 503 | describe('templateFilePath', function () { 504 | it('should load a custom template file', function (done) { 505 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_21.js'))); 506 | gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json'))) 507 | .pipe(plugin('gulp-ng-config', { 508 | templateFilePath: path.normalize(path.join(__dirname, 'mocks/customTemplate.html')) 509 | })) 510 | .pipe(through.obj(function (file) { 511 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 512 | done(); 513 | })); 514 | }); 515 | it('should generate an error if the template file does not exist', function (done) { 516 | gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json'))) 517 | .pipe(plugin('gulp-ng-config', { 518 | templateFilePath: 'non/existant/path.js' 519 | })) 520 | .on('error', function (error) { 521 | expect(error.message).to.equal('invalid templateFilePath option, file not found'); 522 | done(); 523 | }); 524 | }); 525 | it('should use the default template path if a falsy value is provided', function (done) { 526 | var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_2.js'))); 527 | gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json'))) 528 | .pipe(plugin('gulp-ng-config', { 529 | templateFilePath: null 530 | })) 531 | .pipe(through.obj(function (file) { 532 | expect(file.contents.toString()).to.equal(expectedOutput.toString()); 533 | done(); 534 | })); 535 | }); 536 | }); 537 | }); 538 | }); 539 | --------------------------------------------------------------------------------