Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
| /*! | |
| * jscover - lib/jscover.js | |
| * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com> | |
| * MIT Licensed | |
| */ | |
| "use strict"; | |
| /** | |
| * Module dependencies. | |
| */ | |
| var exec = require('child_process').exec; | |
| var path = require('path'); | |
| var ndir = require('ndir'); | |
| var fse = require('fs-extra'); | |
| var fs = require('fs'); | |
| var root = path.dirname(__dirname); | |
| var JSCoverPath = path.join(root, 'bin', 'JSCover-all.jar'); | |
| var JSCoverCommand = 'java -Dfile.encoding=UTF-8 -jar "' + JSCoverPath + '" -fs'; | |
| /** | |
| * Pedding codes to support nodejs | |
| * @type {String} | |
| */ | |
| var PEDDING = "/* ****** automatically generated by jscover - do not edit ******/\n\ | |
| if (typeof _$jscoverage === 'undefined') { _$jscoverage = {}; }\n\ | |
| /* ****** end - do not edit ******/\n"; | |
| module.exports = function jscover(source, target, options, callback) { | |
| source = source || ''; | |
| target = target || ''; | |
| options = options || []; | |
| var tmpTargetDir = path.join(path.dirname(target), '__cov__'); | |
| var tmpTarget = path.join(tmpTargetDir, path.basename(target)); | |
| var cmd = JSCoverCommand; | |
| if (options && options.length > 0) { | |
| cmd += ' ' + options.join(' '); | |
| } | |
| cmd += ' --exclude=node_modules --exclude=.git/ --exclude=.svn/'; | |
| cmd += ' --exclude="' + tmpTarget + '" --exclude="' + target + '"'; | |
| cmd += ' "' + source + '" "' + tmpTarget + '"'; | |
| var child = exec(cmd, function (err, stdout, stderr) { | |
| var output = ''; | |
| if (stdout) { | |
| output += stdout; | |
| } | |
| if (stderr) { | |
| output += stderr; | |
| if (!err) { | |
| err = new Error(stderr.trim()); | |
| err.name = 'JSCoverError'; | |
| } | |
| } | |
| if (err) { | |
| return callback(err, output); | |
| } | |
| var success = !stdout && !stderr; | |
| if (!success) { | |
| return callback(null, output); | |
| } | |
| ndir.walk(tmpTarget, function onDir(dirpath, items) { | |
| var todir = dirpath.replace(tmpTarget, target); | |
| fse.mkdirpSync(todir); | |
| for (var i = 0; i < items.length; i++) { | |
| var info = items[i]; | |
| var from = info[0]; | |
| var name = path.basename(from); | |
| if (name === '.git' || name === '.svn' || name.indexOf('jscoverage') === 0) { | |
| continue; | |
| } | |
| var to = path.join(todir, path.basename(from)); | |
| if (info[1].isDirectory()) { | |
| fse.mkdirpSync(to); | |
| } else if (info[1].isFile()) { | |
| var content = fs.readFileSync(from); | |
| if (path.extname(to).toLowerCase() === '.js') { | |
| content = PEDDING + content.toString(); | |
| } | |
| fs.writeFileSync(to, content); | |
| } | |
| } | |
| }, function end() { | |
| fse.removeSync(tmpTargetDir); | |
| fse.removeSync(path.join(target, '__cov__')); | |
| callback(); | |
| }, function error(err, errPath) { | |
| console.error('%s error: %s', errPath, err); | |
| callback(err); | |
| }); | |
| }); | |
| }; |