Skip to content

Commit

Permalink
Moed out code from bookeye
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Lawson committed Jul 22, 2012
0 parents commit 91a5fe9
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
node_modules
4 changes: 4 additions & 0 deletions bolt
@@ -0,0 +1,4 @@
var bolt = require("./lib/bolt");
bolt.task = require("./build/tasks");

bolt.execute(process.argv);
9 changes: 9 additions & 0 deletions build/tasks.js
@@ -0,0 +1,9 @@
var spawn = require('child_process').spawn;
var path = require('path');
var bolt = require("../lib/bolt");

this.tests = function() {
var jasmineScript = path.join("node_modules", "jasmine-node", "bin", "jasmine-node");
var specsFolder = path.join("specs");
return bolt.captureOutput("Tests", spawn("node", [jasmineScript, specsFolder]));
};
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/bolt');
30 changes: 30 additions & 0 deletions lib/bolt.js
@@ -0,0 +1,30 @@
var q = require('q');

this.task = {};
this.captureOutput = function(name, spawnedProcess) {
console.log("Executing " + name);
var deferred = q.defer();
spawnedProcess.stdout.on('data', function (data) {
process.stdout.write('' + data);
});
spawnedProcess.stderr.on('data', function (data) {
process.stdout.write('' + data);
});
spawnedProcess.on('exit', function (code) {
console.log(name + ' exited with: ' + code);
deferred.resolve();
});
return deferred.promise;
};
this.execute = function(args) {
var argv = args.slice(2);
var bolt = this;
if (argv.length > 0) {
argv.forEach(function (taskName, index) {
bolt.task[taskName]();
});
} else {
bolt.task.default();
}
return 0;
};
37 changes: 37 additions & 0 deletions package.json
@@ -0,0 +1,37 @@
{
"name": "bolt",
"description": "A simple task runner tool",
"version": "0.0.1",
"author": {
"name": "Henry Lawson",
"email": "hen3rz@gmail.com"
},
"dependencies": {
"q" : "0.x"
},
"devDependencies": {
"jasmine-node" : "1.x"
},
"keywords": [
"bolt",
"runner",
"nant",
"ant",
"build"
],
"repository": {
"type": "git",
"url": "git://github.com/henrylawson/bolt.git"
},
"main": "index",
"engines": {
"node": "*"
},
"_id": "bolt@0.0.1",
"optionalDependencies": {},
"_engineSupported": true,
"_npmVersion": "1.1.21",
"_nodeVersion": "v0.6.18",
"_defaultsLoaded": true,
"_from": "bolt@0.0"
}
91 changes: 91 additions & 0 deletions specs/bolt.spec.js
@@ -0,0 +1,91 @@
var bolt = require("../lib/bolt");

describe("Bolt", function() {
describe("captureOutput", function() {
var taskName;
var spawnedProcess;

beforeEach(function() {
spyOn(process.stdout, 'write');
taskName = "A Task Name";
spawnedProcess = {
stdout: {
on: jasmine.createSpy()
},
stderr: {
on: jasmine.createSpy()
},
on: jasmine.createSpy()
};
});

it("should write executing message to stdout", function() {
bolt.captureOutput(taskName, spawnedProcess);

expect(process.stdout.write.mostRecentCall.args[0]).toContain(taskName);
});

it("should write spawned process stdout messages to the hosting process stdout", function() {
var data = "standard out message";

bolt.captureOutput(taskName, spawnedProcess);
spawnedProcess.stdout.on.mostRecentCall.args[1](data);

expect(spawnedProcess.stdout.on.mostRecentCall.args[0]).toEqual("data");
expect(process.stdout.write.mostRecentCall.args[0]).toEqual(data);
});

it("should write spawned process stderr messages to the hosting process stdout", function() {
var data = "standard error message";

bolt.captureOutput(taskName, spawnedProcess);
spawnedProcess.stderr.on.mostRecentCall.args[1](data);

expect(spawnedProcess.stderr.on.mostRecentCall.args[0]).toEqual("data");
expect(process.stdout.write.mostRecentCall.args[0]).toEqual(data);
});

it("should write exit code to stdout", function() {
var exitCode = 0;

bolt.captureOutput(taskName, spawnedProcess);
spawnedProcess.on.mostRecentCall.args[1](exitCode);

expect(spawnedProcess.on.mostRecentCall.args[0]).toEqual("exit");
expect(process.stdout.write.mostRecentCall.args[0]).toContain('exited with: ' + exitCode);
expect(process.stdout.write.mostRecentCall.args[0]).toContain(taskName);
});

it("should resolve promise when spawned process exits", function() {
var promiseResolvedSpy = jasmine.createSpy("Promise resolved");
bolt.captureOutput(taskName, spawnedProcess)
.then(promiseResolvedSpy());

spawnedProcess.on.mostRecentCall.args[1](0);

expect(promiseResolvedSpy).toHaveBeenCalled();
});
});

describe("execute", function() {
it("should execute default when no arguments provided", function() {
var args = ["temp", "temp"];
bolt.task.default = jasmine.createSpy("Default task");

bolt.execute(args);

expect(bolt.task.default).toHaveBeenCalled();
});

it("should execute the tasks provided by the command line", function() {
var args = ["temp", "temp", "task1", "task2"];
bolt.task.task1 = jasmine.createSpy("Task 1");
bolt.task.task2 = jasmine.createSpy("Task 2");

bolt.execute(args);

expect(bolt.task.task1).toHaveBeenCalled();
expect(bolt.task.task2).toHaveBeenCalled();
});
});
});

0 comments on commit 91a5fe9

Please sign in to comment.