Comparing version 0.1.1 to 1.0.0
80
index.js
/* Copyright (c) 2012 Rod Vagg <@rvagg> */ | ||
var fs = require('fs') | ||
, path = require('path') | ||
, async = require('async') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const map = require('map-async') | ||
function du (dir, options, callback) { | ||
if (typeof options == 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
dir = path.resolve(dir) | ||
fs.lstat(dir, afterLstat) | ||
fs.lstat(dir = path.resolve(dir), function (err, stat) { | ||
if (err) return callback(err) | ||
function afterLstat (err, stat) { | ||
if (err) { | ||
return callback(err) | ||
} | ||
if (!stat) return callback(null, 0) | ||
if (!stat) { | ||
return callback(null, 0) | ||
} | ||
var size = options.disk ? (512 * stat.blocks) : stat.size | ||
let size = options.disk ? (512 * stat.blocks) : stat.size | ||
if (!stat.isDirectory()) | ||
if (!stat.isDirectory()) { | ||
return callback(null, !options.filter || options.filter(dir) ? size : 0) | ||
} | ||
fs.readdir(dir, function (err, list) { | ||
if (err) return callback(err) | ||
fs.readdir(dir, afterReaddir) | ||
async.map( | ||
list.map(function (f) { | ||
return path.join(dir, f) | ||
}) | ||
, function (f, callback) { | ||
return du(f, options, callback) | ||
} | ||
, function (err, sizes) { | ||
callback( | ||
err | ||
, sizes && sizes.reduce(function (p, s) { | ||
return p + s | ||
}, size) | ||
) | ||
} | ||
function afterReaddir (err, list) { | ||
if (err) { | ||
return callback(err) | ||
} | ||
map( | ||
list.map((f) => path.join(dir, f)), | ||
(f, callback) => du(f, options, callback), | ||
(err, sizes) => callback(err, sizes && sizes.reduce((p, s) => p + s, size)) | ||
) | ||
}) | ||
} | ||
} | ||
} | ||
module.exports = function maybePromiseWrap (dir, options, callback) { | ||
if (typeof options !== 'object') { | ||
callback = options | ||
options = {} | ||
} | ||
if (typeof callback === 'function') { | ||
return du(dir, options, callback) | ||
} | ||
return new Promise((resolve, reject) => { | ||
callback = (err, data) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve(data) | ||
} | ||
du(dir, options, callback) | ||
}) | ||
} | ||
module.exports = du |
@@ -7,2 +7,3 @@ { | ||
"description": "A simple JavaScript implementation of `du -sb`", | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -12,3 +13,3 @@ "du", | ||
], | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
@@ -22,6 +23,6 @@ "bin": { | ||
"dependencies": { | ||
"async": "~0.1.22" | ||
"map-async": "~0.1.1" | ||
}, | ||
"devDependencies": { | ||
"mkfiletree": "*" | ||
"mkfiletree": "~2.0.0" | ||
}, | ||
@@ -28,0 +29,0 @@ "repository": { |
# node-du | ||
A simple JavaScript / Node.js implementation of `du -sb`. Available in npm as *du* | ||
A simple JavaScript / Node.js implementation of `du -sb`. Available in npm as *du*. | ||
[![NPM](https://nodei.co/npm/du.svg)](https://nodei.co/npm/du/) | ||
```js | ||
require('du')('/home/rvagg/.npm/', function (err, size) { | ||
console.log('The size of /home/rvagg/.npm/ is:', size, 'bytes') | ||
}) | ||
const du = require('du') | ||
let size = await du('/home/rvagg/.npm/') | ||
console.log(`The size of /home/rvagg/.npm/ is: ${size} bytes`) | ||
``` | ||
@@ -12,20 +16,18 @@ | ||
## options | ||
## API: `du(directory[, options[, callback]])` | ||
An optional `options` object may be passed as the second argument. Currently there is only two options, | ||
a `'filter'` function that is passed a full file path and is expected to return a truthy/falsy value to indicate whether the file is included in size calculations | ||
and a `disk` option. If disk is true, then block sizing is used when calculating the size. (get's you closer to real du numbers). | ||
* `options`: An optional `options` object may be passed as the second argument. Currently there is only two options, a `'filter'` function that is passed a full file path and is expected to return a truthy/falsy value to indicate whether the file is included in size calculations and a `disk` option. If disk is true, then block sizing is used when calculating the size. (get's you closer to real du numbers). | ||
* `callback`: If you supply a `callback` you'll get `(error, size)` called on it. If you don't supply a `callback`, `du()` returns a `Promise` which you can `await` on for `size`. | ||
With a filter option: | ||
```js | ||
du( | ||
'/tmp/foo.leveldb/' | ||
, { filter: function (f) { return /\.sst$/.test(f) } } | ||
, function (err, size) { | ||
console.log('The size of the sst files in /tmp/foo.leveldb/ is:', size, 'bytes') | ||
} | ||
) | ||
let size = await du('/tmp/foo.leveldb/', { filter: function (f) { return /\.sst$/.test(f) } }) | ||
console.log(`The size of the sst files in /tmp/foo.leveldb/ is: ${size} bytes`) | ||
``` | ||
*Copyright (c) 2012 [Rod Vagg](https://github.com/rvagg) ([@rvagg](https://twitter.com/rvagg))* | ||
## Licence and Copyright | ||
Copyright (c) 2012 [Rod Vagg](https://github.com/rvagg) | ||
Made available under the MIT licence: | ||
@@ -32,0 +34,0 @@ |
105
tests.js
@@ -1,56 +0,71 @@ | ||
var assert = require('assert') | ||
, mkfiletree = require('mkfiletree') | ||
, du = require('./') | ||
const assert = require('assert') | ||
const mkfiletree = require('mkfiletree') | ||
const du = require('./') | ||
const bigboy = Array.apply(null, Array(1024 * 100)).map(() => 'aaaaaaaaaa').join('') | ||
, bigboy = Array.apply(null, Array(1024 * 100)).map(function () { return 'aaaaaaaaaa' }).join('') | ||
, make = function (callback) { | ||
mkfiletree.makeTemp('du', { | ||
a: bigboy | ||
, b: { | ||
c: bigboy | ||
, d: { | ||
e: bigboy | ||
, f: bigboy | ||
} | ||
} | ||
, g: { | ||
h: bigboy | ||
, i: { | ||
j: {} | ||
} | ||
} | ||
}, callback) | ||
async function make () { | ||
return mkfiletree.makeTemp('du', { | ||
a: bigboy, | ||
b: { | ||
c: bigboy, | ||
d: { | ||
e: bigboy, | ||
f: bigboy | ||
} | ||
}, | ||
g: { | ||
h: bigboy, | ||
i: { | ||
j: {} | ||
} | ||
} | ||
}) | ||
} | ||
make(function (err, dir) { | ||
if (err) throw err | ||
du(dir, function (err, size) { | ||
mkfiletree.cleanUp(function () {}) | ||
if (err) throw err | ||
// write 5 x bigboys | ||
// account for 6 x directory entries, reasonably taking up a max of 8192b each | ||
var adjusted = size - bigboy.length * 5 | ||
, expected = 8192 * 5 | ||
async function test (asPromises) { | ||
let filter = { filter: (f) => /(a|e|f)$/.test(f) } | ||
let dir = await make() | ||
assert(adjusted <= expected, 'adjusted size (' + size + ' -> ' + adjusted + ') <= ' + expected) | ||
}) | ||
if (!asPromises) { | ||
du(dir, function (err, size) { | ||
assert.ifError(err) | ||
afterDu(size) | ||
}) | ||
du(dir, { filter: function (f) { return /(a|e|f)$/.test(f) } }, function (err, size) { | ||
mkfiletree.cleanUp(function () {}) | ||
if (err) throw err | ||
du(dir, filter, (err, size) => { | ||
assert.ifError(err) | ||
afterFilteredDu(size) | ||
}) | ||
} else { | ||
return Promise.all([ | ||
du(dir).then(afterDu), | ||
du(dir, filter).then(afterFilteredDu) | ||
]) | ||
} | ||
function afterDu (size) { | ||
mkfiletree.cleanUp() | ||
// write 5 x bigboys | ||
// account for 6 x directory entries, reasonably taking up a max of 8192b each | ||
var adjusted = size - bigboy.length * 3 | ||
, expected = 8192 * 3 | ||
let adjusted = size - bigboy.length * 5 | ||
let expected = 8192 * 5 | ||
assert(adjusted <= expected, 'adjusted size (' + size + ' -> ' + adjusted + ') <= ' + expected) | ||
console.log(`${asPromises}, ${size}, ${adjusted} <> ${expected}`) | ||
} | ||
function afterFilteredDu (size) { | ||
mkfiletree.cleanUp() | ||
let adjusted = size - bigboy.length * 3 | ||
let expected = 8192 * 3 | ||
assert(adjusted <= expected, 'leftover size (' + size + ' -> ' + adjusted + ') <= ' + expected) | ||
}) | ||
}) | ||
console.log(`${asPromises} filtered, ${size}, ${adjusted} <> ${expected}`) | ||
} | ||
} | ||
console.log('Running... no assertions means no worries') | ||
process.on('uncaughtException', function (err) { | ||
console.log('Caught exception: ' + err) | ||
}) | ||
test() | ||
.then(() => test(true)) // promises version | ||
.catch((err) => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
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
Mixed license
License(Experimental) Package contains multiple licenses.
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9373
124
1
51
1
+ Addedmap-async@~0.1.1
+ Addedmap-async@0.1.1(transitive)
- Removedasync@~0.1.22
- Removedasync@0.1.22(transitive)