Created
October 29, 2012 16:22
-
-
Save alex-seville/3974605 to your computer and use it in GitHub Desktop.
custom loading in node
This file contains 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
var fs = require("fs"); | |
var oldLoader = require.extensions['.js']; | |
//find current scripts | |
require.extensions['.js'] = function(module, filename) { | |
if (filename.indexOf("/src/") > -1){ | |
var content = fs.readFileSync(filename, 'utf8'); | |
// Parse the file content and give to module.exports | |
var covintro = "/* automatically generated by JSCoverage - do not edit */"; | |
covintro += " if (typeof _$jscoverage === 'undefined'){ _$jscoverage = {};}"; | |
covintro += " if (! _$jscoverage['srcScript.js']) {"; | |
covintro += " _$jscoverage['srcScript.js'] = [];"; | |
covintro += " _$jscoverage['srcScript.js'][1] = 0;"; | |
covintro += " _$jscoverage['srcScript.js'][2] = 0;"; | |
covintro += " _$jscoverage['srcScript.js'][1]++;"; | |
covintro += "} "; | |
//not the actual source, just for simplicity sake | |
covintro += " _$jscoverage['srcScript.js'].source = [\"source line1\",\"source line2\"];"; | |
covintro += " global._$jscoverage = _$jscoverage;"; | |
content = covintro + content; | |
module.exports = eval(content); | |
}else{ | |
oldLoader(module,filename); | |
} | |
}; |
This file contains 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
module.exports = function sample(){ | |
return true; | |
}; |
This file contains 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
var blanket = require("../lib/blanket"), | |
assert = require("assert"), | |
scrFile = require("../src/srcScript"); | |
describe('easy test', function(){ | |
describe('simplest', function(){ | |
it('should return true when called', function(){ | |
assert.equal(scrFile(), true); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment