Skip to content

Commit 2c74e64

Browse files
authored
Merge pull request #65 from ajwhite/template-file-cleanup
Template file path cleanup
2 parents c1f781d + b8159c6 commit 2c74e64

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Currently there are a few configurable options to control the output of your con
6868
- [options.wrap](#options.wrap)
6969
- [options.parser](#options.parser)
7070
- [options.pretty](#options.pretty)
71+
- [options.templateFilePath](#options.templateFilePath)
7172

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

322+
### <a id="options.templateFilePath"></a>options.templateFilePath
323+
Type: `String` Optional
324+
325+
This allows the developer to provide a custom output template.
326+
327+
Sample template:
328+
`angularConfigTemplate.html`
329+
```html
330+
var foo = 'bar';
331+
332+
angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %>
333+
.<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>;
334+
```
335+
336+
Configuration:
337+
```json
338+
{
339+
"Foo": "bar"
340+
}
341+
```
342+
343+
Gulp task:
344+
```js
345+
gulp.src('config.json')
346+
.pipe(gulpNgConfig('myApp.config', {
347+
templateFilePath: path.normalize(path.join(__dirname, 'templateFilePath.html'))
348+
}));
349+
```
350+
351+
Sample output:
352+
```js
353+
var foo = 'bar';
354+
355+
angular.module('myApp.config', [])
356+
.constant('Foo', 'bar');
357+
```
321358

322359
## Additional Usages
323360

gulp-ng-config.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var through = require('through2'),
1212
ES6_TEMPLATE = 'import angular from \'angular\';\nexport default <%= module %>';
1313

1414
function gulpNgConfig (moduleName, configuration) {
15-
var templateFile, stream, defaults;
15+
var stream, defaults;
1616
defaults = {
1717
type: 'constant',
1818
createModule: true,
@@ -30,16 +30,22 @@ function gulpNgConfig (moduleName, configuration) {
3030
configuration = configuration || {};
3131
configuration = _.merge({}, defaults, configuration);
3232

33-
templateFile = fs.readFileSync(configuration.templateFilePath || defaults.templateFilePath, 'utf8');
34-
3533
stream = through.obj(function (file, encoding, callback) {
3634
var constants = [],
35+
templateFile,
3736
templateOutput,
3837
jsonObj,
3938
wrapTemplate,
4039
spaces,
4140
environmentKeys;
4241

42+
try {
43+
templateFile = fs.readFileSync(configuration.templateFilePath || defaults.templateFilePath, 'utf8');
44+
} catch (error) {
45+
this.emit('error', new PluginError(PLUGIN_NAME, 'invalid templateFilePath option, file not found'));
46+
return callback();
47+
}
48+
4349
if (!configuration.parser && (_.endsWith(file.path, 'yml') || _.endsWith(file.path, 'yaml'))) {
4450
configuration.parser = 'yml';
4551
}

test/mocks/customTemplate.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var foo = 'bar';
2+
3+
angular.module("<%= moduleName %>"<% if (createModule) { %>, []<% } %>)<% _.forEach(constants, function (constant) { %>
4+
.<%= type %>("<%= constant.name %>", <%= constant.value %>)<% }); %>;

test/mocks/output_21.js

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

test/stream.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,5 +478,39 @@ describe('gulp-ng-config', function () {
478478
});
479479
});
480480
});
481+
describe('templateFilePath', function () {
482+
it('should load a custom template file', function (done) {
483+
var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_21.js')));
484+
gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json')))
485+
.pipe(plugin('gulp-ng-config', {
486+
templateFilePath: path.normalize(path.join(__dirname, 'mocks/customTemplate.html'))
487+
}))
488+
.pipe(through.obj(function (file) {
489+
expect(file.contents.toString()).to.equal(expectedOutput.toString());
490+
done();
491+
}));
492+
});
493+
it('should generate an error if the template file does not exist', function (done) {
494+
gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json')))
495+
.pipe(plugin('gulp-ng-config', {
496+
templateFilePath: 'non/existant/path.js'
497+
}))
498+
.on('error', function (error) {
499+
expect(error.message).to.equal('invalid templateFilePath option, file not found');
500+
done();
501+
});
502+
});
503+
it('should use the default template path if a falsy value is provided', function (done) {
504+
var expectedOutput = fs.readFileSync(path.normalize(path.join(__dirname, 'mocks/output_2.js')));
505+
gulp.src(path.normalize(path.join(__dirname, '/mocks/input_2.json')))
506+
.pipe(plugin('gulp-ng-config', {
507+
templateFilePath: null
508+
}))
509+
.pipe(through.obj(function (file) {
510+
expect(file.contents.toString()).to.equal(expectedOutput.toString());
511+
done();
512+
}));
513+
});
514+
});
481515
});
482516
});

0 commit comments

Comments
 (0)