From eb33e6ba071bcfb9c6af703b92f7c2da15042fdd Mon Sep 17 00:00:00 2001 From: vikasgarghb Date: Tue, 7 Apr 2015 15:40:52 -0400 Subject: [PATCH 1/5] Add YML support. --- README.md | 31 ++++++++++++++++++++++ gulp-ng-config.js | 20 +++++++++++---- package.json | 3 ++- test/mocks/input_1.yml | 1 + test/mocks/input_2.yml | 2 ++ test/stream.js | 58 ++++++++++++++++++++++++++++++------------ 6 files changed, 93 insertions(+), 22 deletions(-) create mode 100644 test/mocks/input_1.yml create mode 100644 test/mocks/input_2.yml diff --git a/README.md b/README.md index 1b4c8ac..27187f6 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Currently there are a few configurable options to control the output of your con - [options.constants](#options.constants) - [options.createModule](#options.createModule) - [options.wrap](#options.wrap) +- [options.configType](#options.configType) ### options.environment Type: `String` Optional @@ -165,5 +166,35 @@ define(["angular"], function () { .constant('..', '..'); }); ``` + +### options.configType +Type: `String` Default value: 'json' Optional + +By default, json file is used to generate the module. You can provide yml file to generate the module. Just set `configType` to `"yml"`. +For example, you have a `config.yml` file, +```yml +string: my string +integer: 12345 +object: + one: 2 + three: + - four +``` + +```javascript +gulp.src("config.yml") +gulpNgConfig('myApp.config', { + configType: "yml" +}); +``` + +Generating, +```js +angular.module('myApp.config', []) +.constant('string', "my string") +.constant('integer', 12345) +.constant('object', {"one":2,"three":["four"]}); +``` + ## Contributing 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. diff --git a/gulp-ng-config.js b/gulp-ng-config.js index c170b0b..f7ed5f3 100644 --- a/gulp-ng-config.js +++ b/gulp-ng-config.js @@ -2,6 +2,7 @@ var through = require('through2'), gutil = require('gulp-util'), _ = require('lodash'), fs = require('fs'), + jsYaml = require('js-yaml'), templateFilePath = __dirname + '/template.html', PluginError = gutil.PluginError; @@ -13,7 +14,8 @@ function gulpNgConfig (moduleName, configuration) { defaults = { createModule: true, wrap: false, - environment: null + environment: null, + configType: 'json' }; if (!moduleName) { @@ -30,10 +32,18 @@ function gulpNgConfig (moduleName, configuration) { jsonObj, wrapTemplate; - try { - jsonObj = JSON.parse(file.contents.toString('utf8')); - } catch (e) { - this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided')); + if (configuration.configType === 'json') { + try { + jsonObj = file.isNull() ? {} : JSON.parse(file.contents.toString('utf8')); + } catch (e) { + this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided')); + } + } else if (configuration.configType === 'yml') { + try { + jsonObj = jsYaml.safeLoad(file.contents); + } catch (e) { + this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided')); + } } if (!_.isPlainObject(jsonObj)) { diff --git a/package.json b/package.json index 3ed83fb..a595fe8 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "dependencies": { "gulp-util": "^3.0.0", "lodash": "^3.0.1", - "through2": "^1.1.1" + "through2": "^1.1.1", + "js-yaml": "3.2.7" }, "devDependencies": { "chai": "^1.9.1", diff --git a/test/mocks/input_1.yml b/test/mocks/input_1.yml new file mode 100644 index 0000000..5ce57e4 --- /dev/null +++ b/test/mocks/input_1.yml @@ -0,0 +1 @@ +one: two \ No newline at end of file diff --git a/test/mocks/input_2.yml b/test/mocks/input_2.yml new file mode 100644 index 0000000..71172f1 --- /dev/null +++ b/test/mocks/input_2.yml @@ -0,0 +1,2 @@ +one: + two: three \ No newline at end of file diff --git a/test/stream.js b/test/stream.js index 97bfca7..0fb5ce6 100644 --- a/test/stream.js +++ b/test/stream.js @@ -82,23 +82,49 @@ describe('gulp-ng-config', function () { }); describe('config generation', function () { - it ('should generate the angular template with scalar properties', function (done) { - var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_1.js')); - gulp.src(path.normalize(__dirname + '/mocks/input_1.json')) - .pipe(plugin('gulp-ng-config')) - .pipe(through.obj(function (file) { - expect(file.contents.toString()).to.equal(expectedOutput.toString()); - done(); - })); + describe('json', function () { + it ('should generate the angular template with scalar properties', function (done) { + var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_1.js')); + gulp.src(path.normalize(__dirname + '/mocks/input_1.json')) + .pipe(plugin('gulp-ng-config')) + .pipe(through.obj(function (file) { + expect(file.contents.toString()).to.equal(expectedOutput.toString()); + done(); + })); + }); + it ('should generate the angular template with object properties', function (done) { + var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); + gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) + .pipe(plugin('gulp-ng-config')) + .pipe(through.obj(function (file) { + expect(file.contents.toString()).to.equal(expectedOutput.toString()); + done(); + })); + }); }); - it ('should generate the angular template with object properties', function (done) { - var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); - gulp.src(path.normalize(__dirname + '/mocks/input_2.json')) - .pipe(plugin('gulp-ng-config')) - .pipe(through.obj(function (file) { - expect(file.contents.toString()).to.equal(expectedOutput.toString()); - done(); - })); + describe('yml', function () { + it ('should generate the angular template with scalar properties', function (done) { + var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_1.js')); + gulp.src(path.normalize(__dirname + '/mocks/input_1.yml')) + .pipe(plugin('gulp-ng-config', { + configType: 'yml' + })) + .pipe(through.obj(function (file) { + expect(file.contents.toString()).to.equal(expectedOutput.toString()); + done(); + })); + }); + it ('should generate the angular template with object properties', function (done) { + var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); + gulp.src(path.normalize(__dirname + '/mocks/input_2.yml')) + .pipe(plugin('gulp-ng-config', { + configType: 'yml' + })) + .pipe(through.obj(function (file) { + expect(file.contents.toString()).to.equal(expectedOutput.toString()); + done(); + })); + }); }); }); describe('plugin options', function () { From 92a10d0834fbad6699267c53f1171341ddfae268 Mon Sep 17 00:00:00 2001 From: vikasgarghb Date: Tue, 7 Apr 2015 16:17:27 -0400 Subject: [PATCH 2/5] Update the property name and add the functionality to use the parser based on filetype. --- README.md | 8 ++++---- gulp-ng-config.js | 10 +++++++--- test/stream.js | 13 +++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 27187f6..9ae0125 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Currently there are a few configurable options to control the output of your con - [options.constants](#options.constants) - [options.createModule](#options.createModule) - [options.wrap](#options.wrap) -- [options.configType](#options.configType) +- [options.parser](#options.parser) ### options.environment Type: `String` Optional @@ -167,10 +167,10 @@ define(["angular"], function () { }); ``` -### options.configType +### options.parser Type: `String` Default value: 'json' Optional -By default, json file is used to generate the module. You can provide yml file to generate the module. Just set `configType` to `"yml"`. +By default, json file is used to generate the module. You can provide yml file to generate the module. Just set `parser` to `"yml"`. If your file type is yml and you have not defined `parser`, your file will still be parsed and js be generated correctly. For example, you have a `config.yml` file, ```yml string: my string @@ -184,7 +184,7 @@ object: ```javascript gulp.src("config.yml") gulpNgConfig('myApp.config', { - configType: "yml" + parser: "yml" }); ``` diff --git a/gulp-ng-config.js b/gulp-ng-config.js index f7ed5f3..2e64336 100644 --- a/gulp-ng-config.js +++ b/gulp-ng-config.js @@ -15,7 +15,7 @@ function gulpNgConfig (moduleName, configuration) { createModule: true, wrap: false, environment: null, - configType: 'json' + parser: 'json' }; if (!moduleName) { @@ -32,13 +32,17 @@ function gulpNgConfig (moduleName, configuration) { jsonObj, wrapTemplate; - if (configuration.configType === 'json') { + if (_.endsWith(file.path, 'yml')) { + configuration.parser = 'yml'; + } + + if (configuration.parser === 'json') { try { jsonObj = file.isNull() ? {} : JSON.parse(file.contents.toString('utf8')); } catch (e) { this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided')); } - } else if (configuration.configType === 'yml') { + } else if (configuration.parser === 'yml') { try { jsonObj = jsYaml.safeLoad(file.contents); } catch (e) { diff --git a/test/stream.js b/test/stream.js index 0fb5ce6..f8a4bb8 100644 --- a/test/stream.js +++ b/test/stream.js @@ -107,7 +107,7 @@ describe('gulp-ng-config', function () { var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_1.js')); gulp.src(path.normalize(__dirname + '/mocks/input_1.yml')) .pipe(plugin('gulp-ng-config', { - configType: 'yml' + parser: 'yml' })) .pipe(through.obj(function (file) { expect(file.contents.toString()).to.equal(expectedOutput.toString()); @@ -118,13 +118,22 @@ describe('gulp-ng-config', function () { var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); gulp.src(path.normalize(__dirname + '/mocks/input_2.yml')) .pipe(plugin('gulp-ng-config', { - configType: 'yml' + parser: 'yml' })) .pipe(through.obj(function (file) { expect(file.contents.toString()).to.equal(expectedOutput.toString()); done(); })); }); + it ('should generate the angular template with object properties with no parser', function (done) { + var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); + gulp.src(path.normalize(__dirname + '/mocks/input_2.yml')) + .pipe(plugin('gulp-ng-config')) + .pipe(through.obj(function (file) { + expect(file.contents.toString()).to.equal(expectedOutput.toString()); + done(); + })); + }); }); }); describe('plugin options', function () { From 58ee6e42fa0399c986395555286ac9ee9b340867 Mon Sep 17 00:00:00 2001 From: vikasgarghb Date: Tue, 7 Apr 2015 16:29:24 -0400 Subject: [PATCH 3/5] Add support for yaml and also improve messaging. --- README.md | 4 ++-- gulp-ng-config.js | 6 ++++-- test/mocks/input_2.yaml | 2 ++ test/stream.js | 13 +++++++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 test/mocks/input_2.yaml diff --git a/README.md b/README.md index 9ae0125..6746961 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ define(["angular"], function () { ### options.parser Type: `String` Default value: 'json' Optional -By default, json file is used to generate the module. You can provide yml file to generate the module. Just set `parser` to `"yml"`. If your file type is yml and you have not defined `parser`, your file will still be parsed and js be generated correctly. +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. For example, you have a `config.yml` file, ```yml string: my string @@ -184,7 +184,7 @@ object: ```javascript gulp.src("config.yml") gulpNgConfig('myApp.config', { - parser: "yml" + parser: 'yml' }); ``` diff --git a/gulp-ng-config.js b/gulp-ng-config.js index 2e64336..a502dad 100644 --- a/gulp-ng-config.js +++ b/gulp-ng-config.js @@ -32,7 +32,7 @@ function gulpNgConfig (moduleName, configuration) { jsonObj, wrapTemplate; - if (_.endsWith(file.path, 'yml')) { + if (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml')) { configuration.parser = 'yml'; } @@ -46,8 +46,10 @@ function gulpNgConfig (moduleName, configuration) { try { jsonObj = jsYaml.safeLoad(file.contents); } catch (e) { - this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided')); + this.emit('error', new PluginError(PLUGIN_NAME, 'invaild YML file provided')); } + } else { + this.emit('error', new PluginError(PLUGIN_NAME, configuration.parser + ' is not supported as a valid parser')); } if (!_.isPlainObject(jsonObj)) { diff --git a/test/mocks/input_2.yaml b/test/mocks/input_2.yaml new file mode 100644 index 0000000..71172f1 --- /dev/null +++ b/test/mocks/input_2.yaml @@ -0,0 +1,2 @@ +one: + two: three \ No newline at end of file diff --git a/test/stream.js b/test/stream.js index f8a4bb8..6c414f6 100644 --- a/test/stream.js +++ b/test/stream.js @@ -135,6 +135,19 @@ describe('gulp-ng-config', function () { })); }); }); + describe('yaml', function () { + it ('should generate the angular template with object properties', function (done) { + var expectedOutput = fs.readFileSync(path.normalize(__dirname + '/mocks/output_2.js')); + gulp.src(path.normalize(__dirname + '/mocks/input_2.yaml')) + .pipe(plugin('gulp-ng-config', { + parser: 'yaml' + })) + .pipe(through.obj(function (file) { + expect(file.contents.toString()).to.equal(expectedOutput.toString()); + done(); + })); + }); + }); }); describe('plugin options', function () { it ('should generate the angular template with overridable properties', function (done) { From 594825091cc5ca70e1534c2db7b6b70e08479460 Mon Sep 17 00:00:00 2001 From: vikasgarghb Date: Tue, 7 Apr 2015 16:31:01 -0400 Subject: [PATCH 4/5] Add yaml parser. --- gulp-ng-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gulp-ng-config.js b/gulp-ng-config.js index a502dad..5f07e40 100644 --- a/gulp-ng-config.js +++ b/gulp-ng-config.js @@ -42,7 +42,7 @@ function gulpNgConfig (moduleName, configuration) { } catch (e) { this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided')); } - } else if (configuration.parser === 'yml') { + } else if (configuration.parser === 'yml' || configuration.parser === 'yaml') { try { jsonObj = jsYaml.safeLoad(file.contents); } catch (e) { From b9b15d7c389dcc5f62933a740c238f045e1efdc7 Mon Sep 17 00:00:00 2001 From: vikasgarghb Date: Tue, 7 Apr 2015 16:58:36 -0400 Subject: [PATCH 5/5] Some refactoring for the fallback parser. --- gulp-ng-config.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gulp-ng-config.js b/gulp-ng-config.js index 5f07e40..62a843e 100644 --- a/gulp-ng-config.js +++ b/gulp-ng-config.js @@ -15,7 +15,7 @@ function gulpNgConfig (moduleName, configuration) { createModule: true, wrap: false, environment: null, - parser: 'json' + parser: null }; if (!moduleName) { @@ -32,10 +32,14 @@ function gulpNgConfig (moduleName, configuration) { jsonObj, wrapTemplate; - if (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml')) { + if (!configuration.parser && (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml'))) { configuration.parser = 'yml'; } + if (!configuration.parser) { + configuration.parser = 'json'; + } + if (configuration.parser === 'json') { try { jsonObj = file.isNull() ? {} : JSON.parse(file.contents.toString('utf8'));