Last active
August 29, 2015 14:07
-
-
Save OscarGodson/239739a443c99b6760e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var fs = require('fs'); | |
var glob = require("glob"); | |
var browserify = require('browserify'); | |
var watchify = require('watchify'); | |
var watch = require('watch'); | |
var exec = require('child_process').exec; | |
var manifestFilePath = './spec/built/manifest.js'; | |
var didFail; | |
var watchedBundle; | |
// This will catch all exceptions, but usually they're syntax errors | |
process.on('uncaughtException', function(error) { | |
console.log("\033[31m× BUILD FAILED"); | |
console.log("\033[31m× " + error.message); | |
didFail = true; | |
}); | |
/** | |
* This function will close any open watchify watchers, build the require() | |
* manifest, and then restart watchify | |
*/ | |
function buildManifest () { | |
// If there's already a bundle being watched, close it | |
if (watchedBundle) watchedBundle.close(); | |
// Look through the entire project for any *.spec.js file | |
glob("**/*.spec.js", {}, function (error, files) { | |
if (error) return console.log(error); | |
if (!fs.existsSync('./spec/built')) { | |
fs.mkdirSync('./spec/built/'); | |
} | |
// Empty all the previous file contents | |
fs.writeFileSync(manifestFilePath, ''); | |
// Include the helpers.js file for testing helpers | |
fs.appendFileSync(manifestFilePath, 'require("../../spec/helpers.js");\n'); | |
// For every spec.js file found add it to the manifest. This is the file that | |
// watchify will be watching | |
files.forEach(function (file) { | |
fs.appendFileSync(manifestFilePath, 'require("../../'+ file +'");\n'); | |
}); | |
startWatchify(); | |
}); | |
} | |
/** | |
* Creates the spec/built directory if it doesn't exist and then builds and | |
* saves the browserify bundle which contains all specs and components. | |
*/ | |
function startWatchify () { | |
var bundle = new browserify(manifestFilePath, { | |
cache: {}, | |
packageCache: {}, | |
fullPaths: true | |
}); | |
watchedBundle = watchify(bundle); | |
watchedBundle.on('update', function() { | |
bundleAndSave(watchedBundle); | |
}); | |
bundleAndSave(watchedBundle); | |
}; | |
/** | |
* Writes the given bundle to spec/built/tests.js | |
*/ | |
function bundleAndSave (bundle) { | |
console.log('Building specs...') | |
bundle.bundle().pipe(fs.createWriteStream('./spec/built/tests.js')); | |
var msg = didFail ? 'Fixed the' : 'Done building'; | |
console.log(msg + ' specs'); | |
didFail = false; | |
} | |
/** | |
* Simply checks if the filename passed in is a spec file and returns a boolean | |
*/ | |
function isSpecFile (filename) { | |
return /\.spec\.js$/.test(filename); | |
} | |
/** | |
* Watches the app/javascripts directory for any additions or deletions of | |
* files and if they were spec files it will rebuild the manifest and restart | |
* watchify | |
*/ | |
watch.createMonitor('./', function (monitor) { | |
monitor.on("created", function (filename) { | |
if (isSpecFile(filename)) { | |
buildManifest(); | |
} | |
}); | |
monitor.on("removed", function (filename) { | |
if (isSpecFile(filename)) { | |
buildManifest(); | |
} | |
}); | |
}); | |
buildManifest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment