New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

count-files

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

count-files - npm Package Compare versions

Comparing version 2.5.0 to 2.6.0

31

index.js

@@ -6,6 +6,7 @@ var fs = require('fs')

function count (dir, opts, cb) {
if (typeof opts === 'function') return count(dir, {}, opts)
function count (src, opts, cb) {
if (typeof opts === 'function') return count(src, {}, opts)
src = parse(src)
opts = opts || {}
if (!opts.ignore) opts.ignore = function () { return false }
var totalStats = opts._stats || {

@@ -17,5 +18,6 @@ files: 0,

if (!opts._stats) opts._stats = totalStats
if (!opts.ignore) opts.ignore = function () { return false }
fs.readdir(dir, function (err, list) {
if (err && err.code === 'ENOTDIR') return countFile() // Single file
src.fs.readdir(src.name, function (err, list) {
if (err && err.code === 'ENOTDIR' || !list.length) return countFile() // Single file
if (err) return cb(err)

@@ -26,3 +28,3 @@

list.forEach(function (file) {
file = path.resolve(dir, file)
file = path.resolve(src.name, file)
if (opts.ignore(file)) {

@@ -32,3 +34,3 @@ if (!--pending) cb(null, totalStats)

}
stat(file, function (err, st) {
stat(src.fs, file, function (err, st) {
if (err) return cb(err)

@@ -38,3 +40,3 @@ if (st && st.isDirectory()) {

// Uses opts._stats to add to total
count(file, opts, function (err, cnt) {
count({name: file, fs: src.fs}, opts, function (err, cnt) {
if (err) return cb(err)

@@ -54,3 +56,3 @@ if (!--pending) cb(null, totalStats)

function stat (name, cb) {
function stat (fs, name, cb) {
if (opts.dereference) fs.stat(name, cb)

@@ -61,4 +63,4 @@ else fs.lstat(name, cb)

function countFile () {
// dir === a single file, just count that
stat(dir, function (err, st) {
// src === a single file, just count that
stat(src.fs, src.name, function (err, st) {
if (err) return cb(err)

@@ -70,2 +72,9 @@ totalStats.files++

}
function parse (name) {
if (typeof name === 'string') return {name: path.resolve(name), fs: fs}
name.name = path.resolve(name.name)
if (!name.fs) name.fs = fs
return name
}
}
{
"name": "count-files",
"version": "2.5.0",
"version": "2.6.0",
"description": "count files in a directory recursively",

@@ -22,3 +22,5 @@ "main": "index.js",

"folder-walker": "^3.0.0",
"hyperdrive": "^8.0.0",
"nanobench": "^1.0.3",
"random-access-memory": "^2.3.0",
"standard": "^8.6.0",

@@ -25,0 +27,0 @@ "stream-each": "^1.2.0",

@@ -41,5 +41,5 @@ # Count Files

### var stats = countFiles(dir, [opts], cb) { }
### var stats = countFiles(src, [opts], cb) { }
`dir` can be a directory or a single file.
`src` can be a directory or a single file path. You can also pass a custom fs, e.g. [hyperdrive](https://github.com/mafintosh/hyperdrive) and path with: `{fs: customFs, name: dir}` (see tests for examples).

@@ -46,0 +46,0 @@ Callback returns the completed `stats`: `{files, dirs, bytes}`.

var fs = require('fs')
var path = require('path')
var test = require('tape')
var hyperdrive = require('hyperdrive')
var ram = require('random-access-memory')
var count = require('..')

@@ -32,3 +34,3 @@

test('pass a file as dir', function (t) {
test('count single file', function (t) {
count(path.join(__dirname, 'fixtures', '1.txt'), function (err, stats) {

@@ -69,1 +71,75 @@ t.ifError(err, 'count error')

})
test('count with regular fs as arg', function (t) {
count({fs: fs, name: path.join(__dirname, 'fixtures')}, function (err, stats) {
t.ifError(err, 'count error')
t.ok(stats, 'stats ok')
t.same(stats.files, 2, 'two files')
t.same(stats.dirs, 1, 'one dirs')
t.same(stats.bytes, 26, 'size')
t.end()
})
})
test('count with custom fs', function (t) {
makeArchive(function (err, archive) {
t.ifError(err)
count({fs: archive, name: '/'}, function (err, stats) {
t.ifError(err, 'count error')
t.ok(stats, 'stats ok')
t.same(stats.files, 2, 'two files')
t.same(stats.dirs, 1, 'one dirs')
t.same(stats.bytes, 26, 'size')
t.end()
})
})
})
test('count with custom fs + ignore', function (t) {
makeArchive(function (err, archive) {
t.ifError(err)
count({fs: archive, name: '/'}, {
ignore: function (name) {
return name.indexOf('dir') > -1
}
}, function (err, stats) {
t.ifError(err, 'count error')
t.ok(stats, 'stats ok')
t.same(stats.files, 1, '1 files')
t.same(stats.dirs, 0, '0 dirs')
t.same(stats.bytes, 3, 'size')
t.end()
})
})
})
test('count custom fs + single file', function (t) {
makeArchive(function (err, archive) {
t.ifError(err)
count({fs: archive, name: '/1.txt'}, function (err, stats) {
t.ifError(err, 'count error')
t.ok(stats, 'stats ok')
t.same(stats.files, 1, '1 files')
t.same(stats.dirs, 0, '0 dirs')
t.same(stats.bytes, 3, 'size')
t.end()
})
})
})
function makeArchive (cb) {
var archive = hyperdrive(ram)
var fixtures = path.join(__dirname, 'fixtures')
archive.writeFile('1.txt', fs.readFileSync(path.join(fixtures, '1.txt')), function (err) {
if (err) return cb(err)
archive.writeFile('dir/2.txt', fs.readFileSync(path.join(fixtures, 'dir', '2.txt')), done)
})
function done (err) {
if (err) return cb(err)
archive.ready(function (err) {
if (err) return cb(err)
cb(null, archive)
})
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc