From d56b9f967ec077c16d680866fb59535b866b125b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna?= Date: Tue, 29 Dec 2015 14:38:51 +0100 Subject: [PATCH 1/5] processDir() function --- bin/jscoverage | 35 +++-------------------------------- index.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/bin/jscoverage b/bin/jscoverage index a1ceaf3..381a70f 100755 --- a/bin/jscoverage +++ b/bin/jscoverage @@ -30,9 +30,8 @@ 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 { @@ -40,35 +39,7 @@ try { 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) { - if (err) { - return console.log(err); - } - console.log('process files:', count); - }); + jscoverage.processDir(source, dest, {exclude: exclude}); } } catch (e) { console.log('[ERROR]', e.toString()); diff --git a/index.js b/index.js index 3333cd8..49c6b7e 100755 --- a/index.js +++ b/index.js @@ -210,6 +210,48 @@ exports.processFile = function (source, dest, option) { fs.writeFileSync(dest, content); }; +/** + * processDir, instrument directory + * @sync + * @param {Path} source absolute Path + * @param {Path} dest absolute Path + * @param {Object} option [description] + */ +exports.processDir = function (source, dest, option) { + var count = 0; + var exclude = 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)); + } else { + exports.processFile(file, destFile); + } + + done(); + }, function (err) { + if (err) { + return console.log(err); + } + console.log('process files:', count); + }); +} + function fixData(num) { return Math.round(num * 10000) / 10000; } From ca4aaa7a6726ebf9833df4cb1ecd056830858be3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro?= Date: Wed, 13 Jul 2016 23:53:25 +0200 Subject: [PATCH 2/5] Replaced wrong `this` for correct local `exports` --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 49c6b7e..d91dd1d 100755 --- a/index.js +++ b/index.js @@ -203,7 +203,7 @@ exports.processFile = function (source, dest, option) { filename: source }); } - content = this.process(source, content); + content = exports.process(source, content); if (sheBang) { content = sheBang + content; } From bc3f5f9cf334d7cd052a8e2739a27526c6ada1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Sun, 24 Jul 2016 14:45:01 +0200 Subject: [PATCH 3/5] Fix optional parameter --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d91dd1d..82321ed 100755 --- a/index.js +++ b/index.js @@ -165,9 +165,8 @@ 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) { var content; var stats; // test source is file or dir, or not a file @@ -219,7 +218,7 @@ exports.processFile = function (source, dest, option) { */ exports.processDir = function (source, dest, option) { var count = 0; - var exclude = option.exclude; + var exclude = option && option.exclude; fs.walk(source, function (err, file, done) { if (err) { From abe0c5d8176d301f5c4c304ea50b1f83311ae784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Sun, 24 Jul 2016 23:42:16 +0200 Subject: [PATCH 4/5] Made `processFile()` and `processDir()` to use an async callback Next step, make them async themselves --- bin/jscoverage | 7 ++++++- index.js | 26 ++++++++++++++++---------- package.json | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/bin/jscoverage b/bin/jscoverage index 381a70f..71171e1 100755 --- a/bin/jscoverage +++ b/bin/jscoverage @@ -39,7 +39,12 @@ try { if (sourceStat.isFile()) { jscoverage.processFile(source, dest); } else { - jscoverage.processDir(source, dest, {exclude: exclude}); + jscoverage.processDir(source, dest, {exclude: exclude}, function(err, count) { + if (err) { + return console.log(err); + } + console.log('process files:', count); + }); } } catch (e) { console.log('[ERROR]', e.toString()); diff --git a/index.js b/index.js index 82321ed..46915fc 100755 --- a/index.js +++ b/index.js @@ -166,9 +166,12 @@ exports.process = jscoverage.process; * @param {Path} source absolute Path * @param {Path} dest absolute Path */ -exports.processFile = function (source, dest) { +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); @@ -207,6 +210,8 @@ exports.processFile = function (source, dest) { content = sheBang + content; } fs.writeFileSync(dest, content); + + callback() }; /** @@ -216,7 +221,12 @@ exports.processFile = function (source, dest) { * @param {Path} dest absolute Path * @param {Object} option [description] */ -exports.processDir = function (source, dest, option) { +exports.processDir = function (source, dest, option, callback) { + if(option instanceof Function) { + callback = option + option = null + } + var count = 0; var exclude = option && option.exclude; @@ -238,16 +248,12 @@ exports.processDir = function (source, dest, option) { if (flag) { // copy exclude file fs.save(destFile, fs.readFileSync(file)); + done(); } else { - exports.processFile(file, destFile); - } - - done(); - }, function (err) { - if (err) { - return console.log(err); + exports.processFile(file, destFile, done); } - console.log('process files:', count); + }, function(err) { + callback(err, count) }); } diff --git a/package.json b/package.json index b989bc2..b26c206 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "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" From d789adaaae27d639180f192493e023ecc033a5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Sat, 30 Jul 2016 17:49:07 +0200 Subject: [PATCH 5/5] Updated dependencies (specially `uglify-js`) --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index b26c206..4ad44fa 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,11 @@ "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" : {