Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
atticoos committed Apr 23, 2015
2 parents d042250 + b9b15d7 commit 6edbc09
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 22 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.parser](#options.parser)

### <a id="options.environment"></a>options.environment
Type: `String` Optional
Expand Down Expand Up @@ -165,5 +166,35 @@ define(["angular"], function () {
.constant('..', '..');
});
```

### <a id="options.parser"></a>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'` 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
integer: 12345
object:
one: 2
three:
- four
```
```javascript
gulp.src("config.yml")
gulpNgConfig('myApp.config', {
parser: '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 &amp; 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.
30 changes: 25 additions & 5 deletions gulp-ng-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,7 +14,8 @@ function gulpNgConfig (moduleName, configuration) {
defaults = {
createModule: true,
wrap: false,
environment: null
environment: null,
parser: null
};

if (!moduleName) {
Expand All @@ -30,10 +32,28 @@ 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.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'));
} catch (e) {
this.emit('error', new PluginError(PLUGIN_NAME, 'invaild JSON file provided'));
}
} else if (configuration.parser === 'yml' || configuration.parser === 'yaml') {
try {
jsonObj = jsYaml.safeLoad(file.contents);
} catch (e) {
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)) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions test/mocks/input_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
one: two
2 changes: 2 additions & 0 deletions test/mocks/input_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
one:
two: three
2 changes: 2 additions & 0 deletions test/mocks/input_2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
one:
two: three
80 changes: 64 additions & 16 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,71 @@ 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', {
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', 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', {
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('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 () {
Expand Down

0 comments on commit 6edbc09

Please sign in to comment.