Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
miickel committed Apr 2, 2014
1 parent 1edfad9 commit f9d6b90
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
root = true

[*]
indent_style = tab
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
Expand Down
77 changes: 39 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,46 @@ var footer = require('gulp-footer');
var htmlJsStr = require('js-string-escape');

function templateCache(root, base) {
if (base && base.substr(-1) != path.sep)
base += path.sep

return es.map(function(file, cb) {
var template = '$templateCache.put("<%= url %>","<%= contents %>");';
var url = path.join(root, file.path.replace(base || file.base, ''));

if (process.platform === 'win32') {
url = url.replace(/\\/g, '/');
}

file.contents = new Buffer(gutil.template(template, {
url: url,
contents: htmlJsStr(file.contents),
file: file
}));

cb(null, file);
});
if (base && base.substr(-1) !== path.sep) {
base += path.sep;
}

return es.map(function(file, callback) {
var template = '$templateCache.put("<%= url %>","<%= contents %>");';
var url = path.join(root, file.path.replace(base || file.base, ''));

if (process.platform === 'win32') {
url = url.replace(/\\/g, '/');
}

file.contents = new Buffer(gutil.template(template, {
url: url,
contents: htmlJsStr(file.contents),
file: file
}));

callback(null, file);
});
}

module.exports = function(filename, options) {
if (typeof filename === 'string') {
options = options || {};
} else {
options = filename || {};
filename = options.filename || 'templates.js';
}

var templateHeader = 'angular.module("<%= module %>"<%= standalone %>).run(["$templateCache", function($templateCache) {';
var templateFooter = '}]);';

return es.pipeline(
templateCache(options.root || '', options.base),
concat(filename),
header(templateHeader, {
module: options.module || 'templates',
standalone: options.standalone ? ', []' : ''
}),
footer(templateFooter)
);
if (typeof filename === 'string') {
options = options || {};
} else {
options = filename || {};
filename = options.filename || 'templates.js';
}

var templateHeader = 'angular.module("<%= module %>"<%= standalone %>).run(["$templateCache", function($templateCache) {';
var templateFooter = '}]);';

return es.pipeline(
templateCache(options.root || '', options.base),
concat(filename),
header(templateHeader, {
module: options.module || 'templates',
standalone: options.standalone ? ', []' : ''
}),
footer(templateFooter)
);
};
258 changes: 129 additions & 129 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,154 +3,154 @@ var gutil = require('gulp-util');
var templateCache = require('../index');

it('should build valid $templateCache from two html-files', function(cb) {
var stream = templateCache('templates.js');

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates").run(["$templateCache", function($templateCache) {$templateCache.put("/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");\n$templateCache.put("/template-b.html","<h1 id=\\"template-b\\">I\\\'m template B!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-b.html',
contents: new Buffer('<h1 id="template-b">I\'m template B!</h1>')
}));

stream.end();
var stream = templateCache('templates.js');

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates").run(["$templateCache", function($templateCache) {$templateCache.put("/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");\n$templateCache.put("/template-b.html","<h1 id=\\"template-b\\">I\\\'m template B!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-b.html',
contents: new Buffer('<h1 id="template-b">I\'m template B!</h1>')
}));

stream.end();
});

it('should set proper template urls using options.root', function(cb) {
var stream = templateCache('templates.js', {
root: '/views'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates").run(["$templateCache", function($templateCache) {$templateCache.put("/views/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
var stream = templateCache('templates.js', {
root: '/views'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates").run(["$templateCache", function($templateCache) {$templateCache.put("/views/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
});

it('should be able to create standalone module', function(cb) {
var stream = templateCache('templates.js', {
standalone: true
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
var stream = templateCache('templates.js', {
standalone: true
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
});

it('defaults to templates.js if no filename is specified', function(cb) {
var stream = templateCache();
var stream = templateCache();

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
cb();
});
stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));
stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
stream.end();
});

it('can set options using first parameter when no filename is specified', function(cb) {
var stream = templateCache({
standalone: true,
root: '/views'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/views/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
var stream = templateCache({
standalone: true,
root: '/views'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/views/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
});

it('can set filename in options', function(cb) {
var stream = templateCache({
standalone: true,
root: '/views',
filename: 'foobar.js'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/foobar.js');
assert.equal(file.relative, 'foobar.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/views/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
var stream = templateCache({
standalone: true,
root: '/views',
filename: 'foobar.js'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/foobar.js');
assert.equal(file.relative, 'foobar.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/views/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
});

it('can override file base path in options', function(cb) {
var stream = templateCache({
standalone: true,
root: '/views',
base: '~/dev/projects/gulp-angular-templatecache'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/views/test/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
var stream = templateCache({
standalone: true,
root: '/views',
base: '~/dev/projects/gulp-angular-templatecache'
});

stream.on('data', function(file) {
assert.equal(file.path, '~/dev/projects/gulp-angular-templatecache/test/templates.js');
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module("templates", []).run(["$templateCache", function($templateCache) {$templateCache.put("/views/test/template-a.html","<h1 id=\\"template-a\\">I\\\'m template A!</h1>");}]);');
cb();
});

stream.write(new gutil.File({
base: '~/dev/projects/gulp-angular-templatecache/test',
path: '~/dev/projects/gulp-angular-templatecache/test/template-a.html',
contents: new Buffer('<h1 id="template-a">I\'m template A!</h1>')
}));

stream.end();
});

0 comments on commit f9d6b90

Please sign in to comment.