Comparing version 0.0.2 to 0.1.0
#!/usr/bin/env node | ||
var chalk = require('chalk'), | ||
argv = require('minimist')(process.argv.slice(2)), | ||
cli = require('../src/count.js')(argv._); | ||
var chalk = require('chalk'); | ||
var argv = require('minimist')(process.argv.slice(2)); | ||
var count = require('../src/count.js')(argv._); | ||
@@ -7,0 +7,0 @@ // provide clean output on exceptions rather than dumping a stack trace |
{ | ||
"name": "fcount", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Recursively count the number files in a directory given a pattern to match on.", | ||
"preferGlobal": "true", | ||
"main": "src/count.js", | ||
"homepage": "https://github.com/ConnorAtherton/fcount", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/ConnorAtherton/fcount.git" | ||
}, | ||
"scripts": { | ||
@@ -26,9 +31,7 @@ "prepublish": "npm prune", | ||
"chalk": "~0.5.1", | ||
"async": "~0.9.0", | ||
"cli-table": "~0.3.0", | ||
"mocha": "~1.20.1" | ||
"cli-table": "~0.3.0" | ||
}, | ||
"devDependencies": { | ||
"jasmine-node": "~1.14.5" | ||
"mocha": "~1.20.1" | ||
} | ||
} |
@@ -5,1 +5,50 @@ fcount | ||
Recursively count the number files in the current directory given file extensions to match on. | ||
Usage | ||
============= | ||
All extensions you want counted should be entered in a space seperated list. | ||
``` | ||
$ fcount [extensions...] | ||
``` | ||
Example | ||
======= | ||
``` | ||
$ fcount js json java | ||
``` | ||
The above will return something similiar to this in the shell | ||
``` | ||
┌───────────┬───────┐ | ||
│ File type │ Count │ | ||
├───────────┼───────┤ | ||
│ js │ 349 │ | ||
├───────────┼───────┤ | ||
│ json │ 57 │ | ||
├───────────┼───────┤ | ||
│ java │ 1 │ | ||
└───────────┴───────┘ | ||
``` | ||
Note: Any files in hidden directories such as ```.git``` will **not** be counted. | ||
TODO | ||
==== | ||
- Add in the ability to exclude certain folders from the directory traversal. For example, | ||
all files inside of ```node_modules``` should not be counted. I'm thinking to use the ```-e``` | ||
flag followed by a space seperated list of directory names. | ||
``` | ||
$ fcount js json -e node_modules | ||
``` | ||
- Document methods in count.js. | ||
- TESTS. It's 1.20am right now, need to come back and do later! | ||
@@ -1,5 +0,5 @@ | ||
var fs = require('fs'), | ||
path = require('path'), | ||
chalk = require('chalk'), | ||
Table = require('cli-table'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var chalk = require('chalk'); | ||
var Table = require('cli-table'); | ||
@@ -19,9 +19,9 @@ // | ||
copy_to_obj(extensions); | ||
walk(process.cwd(), function(err) { | ||
if (err) throw err; | ||
if (err) return console.log(err); | ||
for (file in file_counts) { | ||
if (file_counts.hasOwnProperty(file)) { | ||
table.push( [file, file_counts[file] ]); | ||
table.push([file, file_counts[file]]); | ||
} | ||
@@ -39,2 +39,3 @@ } | ||
var key; | ||
for (var i = 0, len = extensions.length; i < len; i++) { | ||
@@ -46,33 +47,65 @@ key = extensions[i]; | ||
// | ||
// Walks a path recursively calling the callback on each file. | ||
// @param {string} dir The directory path to start traversing from. | ||
// @param {Function} cb The function to call when fully traversed or an error occurred. | ||
// @returns {void} | ||
// @private | ||
// | ||
function walk(dir, cb) { | ||
var file, abs_path, i = 0; | ||
fs.readdir(dir, function(err, list) { | ||
if (err) return cb(err); | ||
var remaining = 1; | ||
(function next () { | ||
file = list[i++]; | ||
if (!file) return cb(null); | ||
(function traverse(dir) { | ||
abs_path = path.resolve(dir, file); | ||
fs.readdir(dir, function(err, list) { | ||
if (err) return cb(err); | ||
remaining--; | ||
fs.stat(file, function (err, stat) { | ||
list.forEach(function(file) { | ||
remaining++; | ||
if (stat && stat.isDirectory()) { | ||
walk(file, function (err) { | ||
next(); | ||
}); | ||
} else { | ||
slice_extension(abs_path); | ||
// console.log(chalk.blue('file'), chalk.red(file), chalk.green(abs_path)); | ||
next(); | ||
} | ||
var abs_path = path.resolve(dir, file); | ||
fs.stat(abs_path, function (err, stat) { | ||
if(err) { | ||
remaining--; | ||
return cb(err); | ||
} | ||
if(stat) { | ||
if (stat.isDirectory()){ | ||
// Don't traverse hidden dirs like .git | ||
return isHidden(file) ? --remaining : traverse(abs_path); | ||
} else { | ||
if (!isHidden(file)) slice_extension(abs_path); | ||
remaining--; | ||
// if this is the last file we are done | ||
if(!remaining) return cb(null); | ||
} | ||
} | ||
}); | ||
}); | ||
})(); | ||
}); | ||
// if we finish on an empty file we | ||
// have to escape here | ||
if(!remaining) { | ||
cb(null); | ||
} | ||
}); | ||
})(process.cwd()); | ||
} | ||
// | ||
// Checks to see whether a directory or file is hidden. | ||
// @param {string} filename The directory/file. | ||
// @returns {boolean} | ||
// @private | ||
// | ||
function isHidden(filename) { | ||
return filename.substring(0, 1) === '.'; | ||
} | ||
function slice_extension(abs_path) { | ||
var ext = path.extname(abs_path), | ||
sliced = ext.slice(1, ext.length); | ||
var ext = path.extname(abs_path); | ||
var sliced = ext.slice(1, ext.length); | ||
@@ -79,0 +112,0 @@ if (sliced in file_counts) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
5306
3
104
1
54
1
- Removedasync@~0.9.0
- Removedmocha@~1.20.1
- Removedasync@0.9.2(transitive)
- Removedcommander@0.6.12.0.0(transitive)
- Removeddebug@4.4.0(transitive)
- Removeddiff@1.0.7(transitive)
- Removedglob@3.2.3(transitive)
- Removedgraceful-fs@2.0.3(transitive)
- Removedgrowl@1.7.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedjade@0.26.3(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.2.14(transitive)
- Removedmkdirp@0.3.00.3.5(transitive)
- Removedmocha@1.20.1(transitive)
- Removedms@2.1.3(transitive)
- Removedsigmund@1.0.1(transitive)