Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated bbb to have build tasks embedded

  • Loading branch information...
commit 801eba9de30525755adeabf50061b7113253b752 1 parent d42bda9
@tbranyen tbranyen authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
20 bin/bbb
@@ -1,21 +1,3 @@
#!/usr/bin/env node
-var grunt = require("grunt");
-var path = require("path");
-var fs = require("fs");
-// Shorten the path name and resolve function
-var buildTasks = "/../node_modules/build-tasks/";
-var resolve = path.resolve;
-
-// Auto-load tasks
-var taskList = fs.readdirSync(__dirname + buildTasks).filter(function(task) {
- return fs.statSync(__dirname + buildTasks + task).isDirectory();
-}).map(function(task) {
- return resolve(__dirname + buildTasks + task);
-}).concat([resolve(__dirname + buildTasks), resolve(__dirname + "/../tasks")]);
-
-// Initialize the CLI
-grunt.npmTasks("bbb").cli({
- // Load the BBB tasks
- tasks: taskList
-}, function() {});
+require("grunt").npmTasks("bbb").cli();
View
10 package.json
@@ -1,7 +1,7 @@
{
"name": "bbb",
"description": "Backbone Boilerplate",
- "version": "0.0.4",
+ "version": "0.0.5",
"homepage": "https://github.com/tbranyen/bbb",
"author": {
"name": "Tim Branyen (@tbranyen)",
@@ -17,7 +17,13 @@
},
"dependencies": {
"grunt": "0.3.6",
- "build-tasks": "0.1.1"
+
+ "rimraf": "1.0.9",
+ "underscore": "1.2.4",
+ "less": "1.2.1",
+ "clean-css": "0.3.2",
+ "requirejs": "1.0.7",
+ "express": "2.5.4"
},
"devDependencies": {
"grunt": "~0.3.0"
View
34 tasks/clean.js
@@ -0,0 +1,34 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: Clean
+ * Description: Remove the contents of a given folder
+ * Dependencies: rimraf@1.0.9
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var rimraf = require("rimraf");
+ var log = grunt.log;
+ var file = grunt.file;
+
+ grunt.registerMultiTask("clean",
+ "Deletes out all contents in a directory", function() {
+
+ var files = file.expand(this.data);
+
+ files.forEach(function(file) {
+ grunt.helper("clean", file);
+ });
+
+ return grunt.errors ? false : true;
+ });
+
+ grunt.registerHelper("clean", function(path) {
+ log.writeln('Removing: ' + path);
+ rimraf.sync(path);
+ });
+
+};
View
51 tasks/handlebars.js
@@ -0,0 +1,51 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: Handlebars
+ * Description: Compile handlebars templates to JST file
+ * Dependencies: None
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var config = grunt.config;
+ var file = grunt.file;
+ var log = grunt.log;
+
+ grunt.registerMultiTask("handlebars",
+ "Compile underscore templates to JST file", function() {
+
+ // If namespace is specified use that, otherwise fallback
+ var namespace = config("meta.handlebars.namespace") || "JST";
+
+ // Create JST file.
+ var files = file.expand(this.data);
+ file.write(this.target, grunt.helper("handlebars", files, namespace));
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("File \"" + this.target + "\" created.");
+ });
+
+ grunt.registerHelper("handlebars", function(files, namespace) {
+ namespace = "this['" + namespace + "']";
+
+ // Comes out looking like this["JST"] = this["JST"] || {};
+ var contents = namespace + " = " + namespace + " || {};\n\n";
+
+ // Compile the template and get the function source
+ contents += files ? files.map(function(filepath) {
+ var templateFunction =
+ require("handlebars").precompile(file.read(filepath));
+
+ return namespace + "['" + filepath + "'] = " + templateFunction;
+ }).join("\n\n") : "";
+
+ return contents;
+ });
+
+};
View
94 tasks/jst.js
@@ -0,0 +1,94 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: JST
+ * Description: Compile underscore templates to JST file.
+ * Dependencies: underscore@1.2.4
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var file = grunt.file;
+ var log = grunt.log;
+ var config = grunt.config;
+
+ grunt.registerMultiTask("jst",
+ "Compile underscore templates to JST file", function() {
+
+ // If namespace is specified use that, otherwise fallback
+ var namespace = config("options.jst.namespace") || "JST";
+ // If template settings are available use those
+ var templateSettings = config("options.jst.templateSettings") || null;
+ // Expand files to full paths
+ var files = file.expand(this.data);
+
+ // Create JST file.
+ file.write(this.target, grunt.helper("jst", files, namespace,
+ templateSettings));
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("File \"" + this.target + "\" created.");
+ });
+
+ grunt.registerHelper("jst", function(files, namespace, templateSettings) {
+ // Ensure we get the underscore from the node_modules folder
+ var _ = require("underscore");
+
+ // Pulled from underscore 1.2.4
+ function underscoreTemplating(str) {
+ // Merge in the templateSettings that may be passed
+ var c = _.extend({}, _.templateSettings, templateSettings) ||
+ _.templateSettings;
+
+ var tmpl = '' +
+ 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
+ 'with(obj||{}){__p.push(\'' +
+ str.replace(/\\/g, '\\\\')
+ .replace(/'/g, "\\'")
+ .replace(c.escape || noMatch, function(match, code) {
+ return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
+ })
+ .replace(c.interpolate || noMatch, function(match, code) {
+ return "'," + code.replace(/\\'/g, "'") + ",'";
+ })
+ .replace(c.evaluate || noMatch, function(match, code) {
+ return "');" + code.replace(/\\'/g, "'")
+ .replace(/[\r\n\t]/g, ' ')
+ .replace(/\\\\/g, '\\') + ";__p.push('";
+ })
+ .replace(/\r/g, '\\r')
+ .replace(/\n/g, '\\n')
+ .replace(/\t/g, '\\t')
+ + "');}return __p.join('');";
+
+ return new Function('obj', '_', tmpl).toString();
+ };
+
+ namespace = "this['" + namespace + "']";
+
+ // Comes out looking like this["JST"] = this["JST"] || {};
+ var contents = namespace + " = " + namespace + " || {};\n\n";
+
+ // Compile the template and get the function source
+ contents += files ? files.map(function(filepath) {
+ var templateFunction = [
+ "function(data) { ",
+
+ "return ",
+ underscoreTemplating(file.read(filepath)).replace("anonymous", ""),
+ "(data, _)",
+
+ "};"].join("");
+
+ return namespace + "['" + filepath + "'] = " + templateFunction;
+ }).join("\n\n") : "";
+
+ return contents;
+ });
+
+};
View
49 tasks/less.js
@@ -0,0 +1,49 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: less
+ * Description: Compile LESS files to CSS and minify.
+ * Dependencies: less@1.2.1 / clean-css@0.3.2
+ *
+ */
+
+module.exports = function(grunt) {
+
+ grunt.registerMultiTask("less",
+ "Compile LESS files to CSS and minify.", function() {
+
+ // load libraries
+ var less = require("less");
+ var cleanCSS = require("clean-css");
+ var data = this.data;
+
+ // initialize LESS parser
+ var parser = new(less.Parser)(data.options);
+
+ // iterate over files to compile/compress
+ Object.keys(data.files).forEach(function(dest) {
+ // grab src file to compile dest to
+ var src = data.files[dest];
+
+ // run less compiler
+ parser.parse(file.read(src), function (e, tree) {
+ var css = tree.toCSS();
+
+ // if config specified minify, do so with clean-css
+ if(data.options.compress) {
+ css = cleanCSS.process(css);
+ }
+
+ file.write(dest,css);
+ });
+ });
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("LESS compiling / minification complete.");
+ });
+
+};
View
29 tasks/mincss.js
@@ -0,0 +1,29 @@
+module.exports = function(grunt) {
+
+ var log = grunt.log;
+ var file = grunt.file;
+
+ grunt.registerMultiTask("mincss",
+ "Compress down CSS files cleanly.", function() {
+
+ // Minify CSS.
+ var files = file.expand(this.data);
+ file.write(this.target, grunt.helper('mincss', files));
+
+ // Fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // Otherwise, print a success message.
+ log.writeln("File \"" + this.target + "\" created.");
+ });
+
+ grunt.registerHelper("mincss", function(files) {
+ var cleanCSS = require("clean-css");
+
+ // Minify and combine all CSS
+ return files ? files.map(function(filepath) {
+ return cleanCSS.process(file.read(filepath));
+ }).join("") : "";
+ });
+
+};
View
145 tasks/requirejs.js
@@ -0,0 +1,145 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: RequireJS
+ * Description: Tasks for working with RequireJS.
+ * Dependencies: requirejs@1.0.7
+ *
+ */
+
+module.exports = function(grunt) {
+
+ // Node.js modules
+ var fs = require("fs");
+ var path = require("path");
+ // Include requirejs
+ var requirejs = require("requirejs");
+ var _ = grunt.utils._;
+ // Shorthand Grunt functions
+ var log = grunt.log;
+
+ grunt.registerTask("requirejs", "Build a RequireJS project.", function(prop) {
+ var options = grunt.config("requirejs") || {};
+
+ // Merge passed options into defaults
+ options = _.extend({}, {
+ // Do not optimize
+ optimize: "none",
+
+ // Show warnings
+ logLevel: 2,
+
+ // Ensure modules are inserted
+ skipModuleInsertion: false,
+ }, options);
+
+ // Run the r.js helper
+ grunt.helper("r.js", options, function(response) {
+ // Print out response
+ log.writeln(response);
+ });
+ });
+
+ grunt.registerTask("list", "Show module dependencies", function(prop) {
+ grunt.helper("list");
+ });
+
+ grunt.registerHelper("r.js", function(options, done) {
+ requirejs.optimize(options, done);
+ });
+
+ grunt.registerHelper("list", function() {
+ var jsRegExp = /\.js$/;
+
+ requirejs.tools.useLib(function(require) {
+ require(["parse"], function(parse) {
+ var deps = {};
+ var files = [];
+
+ // Recursively find all files inside the application directory
+ function recurse(dir) {
+ fs.readdirSync(dir).forEach(function(name) {
+ var subdir = path.resolve(dir, name);
+ var stats = fs.statSync(subdir);
+
+ if (stats.isDirectory()) {
+ recurse(subdir);
+ } else if (jsRegExp.test(name)) {
+ files.push(subdir);
+ }
+ });
+ }
+
+ // Start with the app directory
+ recurse("app/");
+
+ files.forEach(function(name) {
+ var contents = fs.readFileSync(name, "utf8");
+ var shortname = name.slice(name.indexOf("app/"));
+
+ deps[shortname] = parse.findDependencies(name,
+ contents);
+ });
+
+ //console.log({ deps: deps });
+ console.log(grunt.helper("tree", deps));
+ });
+ });
+ });
+
+ grunt.registerHelper("tree", function(obj) {
+ var tree = [""];
+
+ function spaces(len, end, start) {
+ start = start || " ";
+ end = end || " ";
+
+ if (!start) {
+ return Array(len+1).join(Array(3).join(end));
+ } else {
+ return Array(len+1).join(start + Array(2).join(end));
+ }
+ }
+
+ function traverse(obj, depth) {
+ _.each(obj, function(val, key) {
+ var len;
+
+ if (_.isArray(val)) {
+ tree.push("\u251c" + spaces(depth) + " " + key);
+
+ len = val.length;
+
+ _.each(val, function(val, i) {
+ if (_.isString(val)) {
+ if (i == len-1) {
+ tree.push("\u2502 " + spaces(depth+1, "\u2500", "\u2514") + " " + val);
+ } else {
+ tree.push("\u2502 " + spaces(depth+1, "\u2500", "\u251c") + " " + val);
+ }
+ } else if (_.isObject(val)) {
+ traverse(obj, depth+1);
+ }
+ });
+
+ tree.push("\u2502");
+
+ } else if (_.isObject(val)) {
+ tree.push(spaces(depth) + key);
+ traverse(val, depth+1);
+ } else {
+ tree.push(spaces(depth) + key);
+ }
+
+ });
+ }
+
+ traverse(obj, 0);
+
+ tree.pop();
+
+ return tree.join("\n");
+ });
+
+};
View
58 tasks/templatize.js
@@ -0,0 +1,58 @@
+/*
+ * Grunt Task File
+ * ---------------
+ *
+ * Task: templatize
+ * Description: Compile javascript templates with any templating enginej
+ * Dependencies: variable
+ *
+ */
+
+module.exports = function(grunt) {
+
+ var log = grunt.log;
+ var file = grunt.file;
+
+ grunt.registerMultiTask("templatize",
+ "Compile javascript templates with any templating enginej", function() {
+
+ var compiler = data.method;
+ var params = data.params||[];
+ var namespace = data.namespace || "JST";
+ var wrapper = data.wrapper||function(text) { return text; };
+ var data = this.data;
+
+ // iterate over files to compile
+ Object.keys(data.files).forEach(function(dest) {
+
+ // initialize template namespace
+ var output = ["this['"+namespace+"'] = this['"+namespace+"'] || {};"];
+
+ // add supporting library (if required)
+ if(data.library) output.push(file.read(data.library));
+
+ // grab src file to compile
+ var files = file.expand(data.files[dest]);
+
+ // iterate templates
+ files.forEach(function(filepath) {
+ // prepare arguments to pass to compiler
+ var args = params.slice();
+ args.unshift(file.read(filepath));
+ // call compiler function and save to string
+ output.push("this['"+namespace+"']['"+filepath+"'] = "+wrapper(compiler.apply(null,args))+";");
+ });
+
+ // write output to destination
+ file.write(dest,output.join("\n"));
+ });
+
+ // fail task if errors were logged.
+ if (grunt.errors) { return false; }
+
+ // otherwise, print a success message.
+ log.writeln("Javascript templatizing complete.");
+
+ });
+
+};
Please sign in to comment.
Something went wrong with that request. Please try again.