Skip to content

Commit

Permalink
templateFilePath docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atticoos committed Dec 30, 2016
1 parent c1f781d commit b9079bd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
37 changes: 37 additions & 0 deletions README.md
Expand Up @@ -68,6 +68,7 @@ Currently there are a few configurable options to control the output of your con
- [options.wrap](#options.wrap)
- [options.parser](#options.parser)
- [options.pretty](#options.pretty)
- [options.templateFilePath](#options.templateFilePath)

### <a id="options.environment"></a>options.environment
Type: `String` Optional
Expand Down Expand Up @@ -318,6 +319,42 @@ angular.module("gulp-ng-config", [])
});
```

### <a id="options.templateFilePath"></a>options.templateFilePath
Type: `String` Default value: `null` Optional

This allows the developer to provide a custom output template.

Sample template:
`angularConfigTemplate.html`
```html
var foo = 'bar';

angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %>
.<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>;
```

Configuration:
```json
{
"Foo": "bar"
}
```

Gulp task:
```js
gulp.src('config.json')
.pipe(gulpNgConfig('myApp.config', {
templateFilePath: path.normalize(path.join(__dirname, 'templateFilePath.html'))
}));
```

Sample output:
```js
var foo = 'bar';

angular.module('myApp.config', [])
.constant('Foo', 'bar');
```

## Additional Usages

Expand Down
12 changes: 9 additions & 3 deletions gulp-ng-config.js
Expand Up @@ -12,7 +12,7 @@ var through = require('through2'),
ES6_TEMPLATE = 'import angular from \'angular\';\nexport default <%= module %>';

function gulpNgConfig (moduleName, configuration) {
var templateFile, stream, defaults;
var stream, defaults;
defaults = {
type: 'constant',
createModule: true,
Expand All @@ -30,16 +30,22 @@ function gulpNgConfig (moduleName, configuration) {
configuration = configuration || {};
configuration = _.merge({}, defaults, configuration);

templateFile = fs.readFileSync(configuration.templateFilePath || defaults.templateFilePath, 'utf8');

stream = through.obj(function (file, encoding, callback) {
var constants = [],
templateFile,
templateOutput,
jsonObj,
wrapTemplate,
spaces,
environmentKeys;

try {
templateFile = fs.readFileSync(configuration.templateFilePath || defaults.templateFilePath, 'utf8');
} catch (error) {
this.emit('error', new PluginError(PLUGIN_NAME, 'invalid templateFilePath option, file not found'));
return callback();
}

if (!configuration.parser && (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml'))) {
configuration.parser = 'yml';
}
Expand Down
4 changes: 4 additions & 0 deletions test/mocks/customTemplate.html
@@ -0,0 +1,4 @@
var foo = 'bar';

angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %>
.<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>;
4 changes: 4 additions & 0 deletions test/mocks/output_21.js
@@ -0,0 +1,4 @@
var foo = 'bar';

angular.module("gulp-ng-config", [])
.constant("one", {"two":"three"});
34 changes: 34 additions & 0 deletions test/stream.js
Expand Up @@ -478,5 +478,39 @@ describe('gulp-ng-config', function () {
});
});
});
describe('templateFilePath', function () {
it('should load a custom template file', function (done) {
var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_21.js')));
gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json')))
.pipe(plugin('gulp-ng-config', {
templateFilePath: path.normalize(path.join(__dirname, 'mocks/customTemplate.html'))
}))
.pipe(through.obj(function (file) {
expect(file.contents.toString()).to.equal(expectedOutput.toString());
done();
}));
});
it('should generate an error if the template file does not exist', function (done) {
gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json')))
.pipe(plugin('gulp-ng-config', {
templateFilePath: 'non/existant/path.js'
}))
.on('error', function (error) {
expect(error.message).to.equal('invalid templateFilePath option, file not found');
done();
});
});
it('should use the default template path if a falsy value is provided', function (done) {
var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_2.js')));
gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json')))
.pipe(plugin('gulp-ng-config', {
templateFilePath: null
}))
.pipe(through.obj(function (file) {
expect(file.contents.toString()).to.equal(expectedOutput.toString());
done();
}));
});
});
});
});

0 comments on commit b9079bd

Please sign in to comment.