Skip to content

Instantly share code, notes, and snippets.

@leommoore
Last active December 13, 2015 17:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leommoore/4951360 to your computer and use it in GitHub Desktop.
Save leommoore/4951360 to your computer and use it in GitHub Desktop.
Node - Build Automation

#Node - Build Automation

##Setup

Jake is the JavaScript equivilent of the Ruby Rake tool. It prefers to be installed globally but for build purposes it is better to install it into node_modules. For more info see http://howtonode.org/intro-to-jake

npm install jake

It can be called using:

node_modules/.bin/jake

To shorten this you can create a shell file

touch jake.sh
chmod+x jake.sh

Then edit the file and put in:

node_modules/.bin/jake $*

You should then be able to run using ./jake.sh -h from the project directory to get the help information.

For windows users you can do the same thing by creating a jake.bat with node node_modules\jake\bin\cli.js %*

##Jake Build File - Jakefile.js Please note that the filename must have a capital J or it will not pick up on it as default and you will end up specifying the file (with -f jakefile.js) each time.

desc("Example");
task('default', function (params) {
  console.log('This is the default task.');
});

To run the Jakefile use:

./jake.sh
This is the default task.

To see the tasks in the Jakefile run:

./jake.sh -T
jake default   # This is the default task. 

You can create chains of dependency, for example:

desc("Example with dependency");
task("default",["dependency"], function() {
  console.log('This is the default task.');
});

task("dependency", function() {
  console.log("dependency task");
});

In this situation, when it start to run the default task it must run the dependency task first. If there are multiple tasks depending on the same task it is still only run once.

##Running JSLint

Add a build -> lint folder with:

#####lint_runner.js

/* Copyright (c) 2012 James Shore - See README.txt for license */
"use strict";
var jshint = require("jshint").JSHINT;
var fs = require("fs");

exports.validateSource = function(sourceCode, options, globals, description) {
  description = description ? description + " " : "";
  var pass = jshint(sourceCode, options, globals);

  if (pass) {
    console.log(description + "ok");
  }
  else {
    console.log(description + "failed");
    jshint.errors.forEach(function(error) {
      console.log(error.line + ": " + error.evidence.trim());
      console.log("   " + error.reason);
    });
  }
  return pass;
};

exports.validateFile = function(filename, options, globals) {
  var sourceCode = fs.readFileSync(filename, "utf8");
  return exports.validateSource(sourceCode, options, globals, filename);
};

exports.validateFileList = function(fileList, options, globals) {
  var pass = true;
  fileList.forEach(function(filename) {
    pass = exports.validateFile(filename, options, globals) && pass;
  });
  return pass;
};

###Update the Jakefile

/*global desc, task, jake, fail, complete */
"use strict";

task("default",["lint"]);

desc("Lint everything");
task("lint",[], function() {
  var lint = require("./build/lint/lint_runner.js");

  //To validate a specific file
  //lint.validateFile("Jakefile.js", {}, {});

  //To validate a set of files
  var files = new jake.FileList();

  files.include("**/*.js");
  files.exclude("node_modules");

  var options = {
    node: true,
    es5: true
  };

  lint.validateFileList(files.toArray(), options, {});
});
@cloudhuang
Copy link

Hi, what's the meaning of using /*global desc, task, jake, fail, complete */?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment