Skip to content

Commit

Permalink
refact(grunt) upgrade to grunt 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Feb 28, 2013
1 parent 7c26602 commit d30f275
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 214 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- npm install --quiet -g grunt@0.3.x testacular@0.4.x
- npm install --quiet -g grunt-cli@0.1.x testacular@0.6.x
- cd server
- npm install --quiet
- node server.js &
Expand Down
92 changes: 72 additions & 20 deletions client/build/grunt-html2js.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,87 @@
module.exports = function (grunt) {
/*
* grunt-html2js
* https://github.com/karlgoldstein/grunt-html2js
*
* Copyright (c) 2013 Karl Goldstein
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// HTML-2-JS Templates
var path = require('path');
var TPL = 'angular.module("<%= id %>", []).run(["$templateCache", function($templateCache) {\n $templateCache.put("<%= id %>",\n "<%= content %>");\n}]);\n';
var templateModule = "angular.module('templates', [<%= templates %>]);";

var escapeContent = function(content) {
return content.replace(/"/g, '\\"').replace(/\r?\n/g, '" +\n "');
};

// convert Windows file separator URL path separator
var normalizePath = function(p) {
if ( path.sep !== '/' ) {
p = p.replace(/\\/g, '/');
}
return p;
};

grunt.registerTask('html2js', 'Generate js version of html template.', function() {
this.requiresConfig('html2js.src');
var files = grunt.file.expandFiles(grunt.config.process('html2js.src'));
var base = grunt.config.process('html2js.base') || '.';
var dest = grunt.config.process('html2js.dest') || '.';
var templates = [];
files.forEach(function(file) {
var id = normalizePath(path.relative(base, file));
templates.push("'" + id + "'");
grunt.file.write(path.resolve(dest, id + '.js'), grunt.template.process(TPL, {
id: id,
content: escapeContent(grunt.file.read(file))
}));
// Warn on and remove invalid source files (if nonull was set).
var existsFilter = function(filepath) {

if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
};

// compile a template to an angular module
var compileTemplate = function(moduleName, filepath) {

var content = escapeContent(grunt.file.read(filepath));

var module = 'angular.module("' + moduleName +
'", []).run(["$templateCache", function($templateCache) ' +
'{\n $templateCache.put("' + moduleName + '",\n "' + content +
'");\n}]);\n';

return module;
};

grunt.registerMultiTask('html2js', 'Compiles Angular-JS templates to JavaScript.', function() {

var options = this.options({
base: 'src',
module: 'templates-' + this.target
});

// generate a separate module
this.files.forEach(function(f) {

var moduleNames = [];

var modules = f.src.filter(existsFilter).map(function(filepath) {

var moduleName = normalizePath(path.relative(options.base, filepath));
moduleNames.push("'" + moduleName + "'");

return compileTemplate(moduleName, filepath);

}).join(grunt.util.normalizelf('\n'));

if (moduleNames.length) {

var target = f.module || options.module;

var bundle = "angular.module('" + target + "', [" +
moduleNames.join(', ') + "]);\n\n" + modules;
grunt.file.write(f.dest, bundle);
grunt.log.writeln('File "' + f.dest + '" created.');

} else {

grunt.log.warn('No source templates found, not creating ' + f.dest);
}
});
grunt.file.write(path.resolve(dest,'templates.js'), grunt.template.process(templateModule, {
templates: templates.join(', ')
}));
});
};
42 changes: 0 additions & 42 deletions client/build/grunt-testacular.js

This file was deleted.

139 changes: 0 additions & 139 deletions client/grunt.js

This file was deleted.

Loading

0 comments on commit d30f275

Please sign in to comment.