Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processDir() function #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions bin/jscoverage
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,16 @@ if (!dest) {
}

if (exclude) {
exclude = exclude.split(',');
exclude.forEach(function (v, i, a) {
a[i] = new RegExp(v.replace(/\./, '\\.').replace(/\*/g, '.*'));
exclude = exclude.split(',').map(function (v) {
return new RegExp(v.replace(/\./, '\\.').replace(/\*/g, '.*'));
});
}
try {
sourceStat = fs.statSync(source);
if (sourceStat.isFile()) {
jscoverage.processFile(source, dest);
} else {
var count = 0;
fs.walk(source, function (err, file, done) {
if (err) {
console.log(err);
return done;
}
var flag = false;
if (exclude) {
exclude.forEach(function (v) {
if (v.test(file)) {
flag = true;
}
});
}
count ++;
var destFile = path.join(dest, file.substr(source.length));
if (flag) {
// copy exclude file
fs.save(destFile, fs.readFileSync(file));
} else {
jscoverage.processFile(file, destFile);
}
done();
}, function (err) {
jscoverage.processDir(source, dest, {exclude: exclude}, function(err, count) {
if (err) {
return console.log(err);
}
Expand Down
53 changes: 50 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ exports.process = jscoverage.process;
* @sync
* @param {Path} source absolute Path
* @param {Path} dest absolute Path
* @param {Object} option [description]
*/
exports.processFile = function (source, dest, option) {
exports.processFile = function (source, dest, callback) {
callback = callback || function(){}

var content;
var stats;

// test source is file or dir, or not a file
try {
stats = fs.statSync(source);
Expand Down Expand Up @@ -203,13 +205,58 @@ exports.processFile = function (source, dest, option) {
filename: source
});
}
content = this.process(source, content);
content = exports.process(source, content);
if (sheBang) {
content = sheBang + content;
}
fs.writeFileSync(dest, content);

callback()
};

/**
* processDir, instrument directory
* @sync
* @param {Path} source absolute Path
* @param {Path} dest absolute Path
* @param {Object} option [description]
*/
exports.processDir = function (source, dest, option, callback) {
if(option instanceof Function) {
callback = option
option = null
}

var count = 0;
var exclude = option && option.exclude;

fs.walk(source, function (err, file, done) {
if (err) {
return done(err);
}

var flag = false;
if (exclude) {
for(var v in exclude)
if (v.test(file)) {
flag = true;
break;
}
}
count++;
var destFile = path.join(dest, file.substr(source.length));
if (flag) {
// copy exclude file
fs.save(destFile, fs.readFileSync(file));
done();
} else {
exports.processFile(file, destFile, done);
}
}, function(err) {
callback(err, count)
});
}

function fixData(num) {
return Math.round(num * 10000) / 10000;
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"jscoverage": "./bin/jscoverage"
},
"scripts": {
"test": "./node_modules/mocha/bin/_mocha -t 60000 -r ./index.js -R spec --covinject true test/"
"test": "_mocha -t 60000 -r ./index.js -R spec --covinject true test/"
},
"engines": {
"node" : ">=0.8"
},
"dependencies" : {
"uglify-js" : "2.4.15",
"optimist" : "0.3.1",
"uglify-js": "2.7.0",
"optimist": "0.6.1",
"xfs" : "0.1.8",
"ejs": "1.0.0",
"debug": "1.0.3",
"ejs": "2.5.1",
"debug": "2.2.0",
"coffee-script": "*"
},
"devDependencies" : {
Expand Down