Last active
December 14, 2015 06:28
-
-
Save dignifiedquire/5042471 to your computer and use it in GitHub Desktop.
Testacular Handlers aka Preprocessors
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
a = -> 4 | |
b = (a,b) -> a+b |
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
// Handler aka Preprocessor concept for Testacular | |
var fs = require('fs'); | |
var util = require('util'); | |
var cs = require('coffee-script'); | |
var es = require('event-stream'); | |
// Configuration | |
var filename = '1.coffee'; | |
//var filename = 'index.html'; | |
var options = { | |
flags: 'r', | |
encoding: 'utf8' | |
}; | |
var coffeeOptions = { | |
bare: true | |
}; | |
// Sample CoffeeScript Handler | |
var coffee = function(file, opts) { | |
// Return a stream | |
return es.map(function(content, done) { | |
// compile to js | |
try { | |
content = cs.compile(content, opts); | |
done(null, content); | |
} catch (error) { | |
done(error); | |
} | |
}); | |
}; | |
// Sample Html2js Handler | |
var html2js = function(file, opts) { | |
var template = 'angular.module(\'%s\', []).run(function($templateCache) {\n' + | |
' $templateCache.put(\'%s\',\n \'%s\');\n' + | |
'});\n'; | |
var escapeContent = function(content) { | |
return content.replace(/'/g, '\\\'').replace(/\n/g, '\\n\' +\n \''); | |
}; | |
return es.map(function(content, done) { | |
var htmlPath = file + '.js'; | |
done(null, util.format(template, htmlPath, htmlPath, escapeContent(content.toString()))); | |
}); | |
}; | |
// Handlers array that is just a list of streams | |
var handlers = [ | |
fs.createReadStream(filename, options), | |
coffee(filename, coffeeOptions), | |
//html2js(filename), | |
process.stdout | |
]; | |
// Here is where all the real action happens | |
es.pipeline.apply(this, handlers); | |
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
$ node stuff.js | |
var a, b; | |
a = function() { | |
return 4; | |
}; | |
b = function(a, b) { | |
return a + b; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment