Comparing version 2.1.0 to 2.1.1
@@ -7,6 +7,18 @@ #!/usr/bin/env node | ||
var path = require('path') | ||
var argv = require('minimist')(process.argv.slice(2)) | ||
var minimist = require('minimist') | ||
var columnify = require('columnify') | ||
var bytes = require('pretty-bytes') | ||
var argv = minimist(process.argv.slice(2), { | ||
alias: { | ||
'd': 'dirs', | ||
'D': 'only-dirs', | ||
'f': 'files' | ||
}, | ||
default: { | ||
files: true, | ||
dirs: true | ||
}, | ||
boolean: ['dirs', 'files'] | ||
}) | ||
var dir = argv._[0] || process.cwd() | ||
@@ -33,13 +45,30 @@ | ||
argv.sort = argv.sort || 'size' | ||
if (argv.sort === 'name') argv.sort = 'file' | ||
if (argv['only-dirs']) { | ||
argv.dirs = true | ||
argv.files = false | ||
} | ||
pkgFiles.summary(dir, function(err, result) { | ||
console.error() | ||
if (err) return error(err) | ||
result.entries = result.entries.sort(sortBy(argv.sort)) | ||
var entries = result.entries | ||
var files = entries.filter(function(e) { | ||
return !e.isDirectory | ||
}) | ||
var dirs = entries.filter(function(e) { | ||
return e.isDirectory | ||
}) | ||
if (!argv['dirs']) entries = files | ||
if (!argv['files']) entries = dirs | ||
entries = result.entries = entries.sort(sortBy(argv.sort)) | ||
if (argv.json) { | ||
return console.info(JSON.stringify(result, null, 2)) | ||
} | ||
var total = result.entries.length | ||
var total = files.length | ||
var summary = [ | ||
@@ -52,2 +81,7 @@ { | ||
{ | ||
key: 'total', | ||
title: 'Number of Directories', | ||
value: total | ||
}, | ||
{ | ||
key: 'publishSize', | ||
@@ -63,16 +97,15 @@ title: 'Publishable Size', | ||
{ | ||
key: 'extractedSize', | ||
key: 'sizeWithDependencies', | ||
title: 'Size with Dependencies', | ||
value: "~" + bytes(result.extractedSize) | ||
value: "~" + bytes(result.sizeWithDependencies) | ||
}, | ||
{ | ||
key: 'extractedDiskSize', | ||
key: 'diskSizeWithDependencies', | ||
title: 'Size on Disk with Dependencies', | ||
value: "~" + bytes(result.extractedDiskSize) | ||
value: "~" + bytes(result.diskSizeWithDependencies) | ||
} | ||
].reverse() | ||
var entries = result.entries | ||
.map(function(entry) { | ||
entry.file = path.relative(dir, entry.file) | ||
entries = entries.map(function(entry) { | ||
entry.name = path.relative(dir, entry.name) | ||
if (!entry.exists) { | ||
@@ -85,2 +118,6 @@ entry.percent = '' | ||
} | ||
if (entry.isDirectory) entry.name += '/' | ||
if (entry.name === '/') entry.name = '.' | ||
entry.percent = percent(entry.size/result.publishSize) | ||
@@ -94,3 +131,3 @@ entry.percentDisk = percent(entry.diskSize/result.publishDiskSize) | ||
var columns = ['file', 'size', 'percent',] | ||
var columns = ['name', 'size', 'percent',] | ||
if (argv.disk) { | ||
@@ -103,4 +140,15 @@ columns.push('diskSize', 'percentDisk') | ||
} | ||
var breaker = { | ||
name: 'DIR', | ||
size: 'SIZE', | ||
percent: '%', | ||
diskSize: 'DISK SIZE', | ||
diskSizePercent: 'DISK SIZE %' | ||
} | ||
console.info(columnify(result.entries, {columnSplitter: ' ', columns: columns, headingTransform: function(header) { | ||
var rows = files | ||
if (argv.dirs) rows = rows.concat({}, breaker).concat(dirs) | ||
console.info(columnify(rows, {columnSplitter: ' ', columns: columns, headingTransform: function(header) { | ||
if (header === 'name') return 'PATH' | ||
if (header === 'percent') return '%' | ||
@@ -107,0 +155,0 @@ if (header === 'diskSize') return 'DISK SIZE' |
149
index.js
@@ -7,6 +7,7 @@ "use strict" | ||
var du = require('du') | ||
var map = require('map-limit') | ||
module.exports = pkgFiles | ||
function pkgFiles(src, fn) { | ||
function pkgFiles(dir, fn) { | ||
resolve('fstream-npm').fromGlobal('npm', function(err, fstreamPath) { | ||
@@ -16,3 +17,3 @@ if (err) return fn(err) | ||
var packages = [] | ||
var nest = path.join(path.dirname(src), 'package') | ||
var nest = path.join(path.dirname(dir), 'package') | ||
if (!fstreamPath) { | ||
@@ -25,3 +26,3 @@ console.error('Warning: Could not find fstream-npm in global npm.') | ||
var fstream = require(fstreamPath) | ||
fstream({ path: src }) | ||
fstream({ path: dir }) | ||
.on('entry', function(entry) { | ||
@@ -40,11 +41,11 @@ files.push(entry.path) | ||
files = files.map(function(file) { | ||
return path.join(src, path.relative(nest, file)) | ||
return path.join(dir, path.relative(nest, file)) | ||
}) | ||
var pending = files.length | ||
files.forEach(function(file) { | ||
map(files, Infinity, function(file, next) { | ||
fs.lstat(file, function(err, stat) { | ||
var name = path.relative(dir, file) | ||
if (err) { | ||
if (err.code === 'ENOENT') return done(null, { | ||
file: file, | ||
if (err.code === 'ENOENT') return next(null, { | ||
name: name, | ||
size: 0, | ||
@@ -54,26 +55,61 @@ diskSize: 0, | ||
}) | ||
return done(err) | ||
return next(err) | ||
} | ||
return next(null, { | ||
name: name, | ||
size: stat.size, | ||
diskSize: 512 * stat.blocks, | ||
exists: true, | ||
isDirectory: false | ||
}) | ||
}) | ||
}, function(err, entries) { | ||
if (err) return fn(err) | ||
var dirNames = entries | ||
.reduce(function(dirNames, entry) { | ||
return dirNames.concat(getAncestors(entry.name)) | ||
}, []) | ||
.filter(Boolean) | ||
.filter(unique) | ||
if (stat.isDirectory()) return done(null, { | ||
file: file, | ||
var dirEntries = dirNames.map(function(dirName) { | ||
return { | ||
name: dirName, | ||
size: 0, | ||
diskSize: 0, | ||
exists: true | ||
}) | ||
exists: true, | ||
children: [], | ||
isDirectory: true | ||
} | ||
}) | ||
return done(null, { | ||
file: file, | ||
size: stat.size, | ||
diskSize: 512 * stat.blocks, | ||
exists: true | ||
}) | ||
dirEntries.forEach(function(dirEntry) { | ||
dirEntry.children = dirEntry.children | ||
.concat(entries.filter(function(entry) { | ||
return ( | ||
dirEntry.name !== entry.name && | ||
!path.relative(dirEntry.name, path.dirname(entry.name)) | ||
) | ||
})) | ||
}) | ||
dirEntries.forEach(function(dirEntry) { | ||
dirEntry.children = dirEntry.children | ||
.concat(dirEntries.filter(function(entry) { | ||
return ( | ||
dirEntry.name !== entry.name && | ||
!path.relative(dirEntry.name, path.dirname(entry.name)) | ||
) | ||
})) | ||
}) | ||
dirEntries.forEach(function(dirEntry) { | ||
dirEntry.children = dirEntry.children.filter(unique) | ||
}) | ||
var rootDir = dirEntries.filter(function(dirEntry) { | ||
return dirEntry.name === '.' | ||
}).pop() | ||
getDirSize(rootDir) | ||
return fn(null, entries.concat(dirEntries), packages) | ||
}) | ||
var results = [] | ||
function done(err, entry) { | ||
if (err) return fn(err) | ||
results = results.concat(entry) | ||
if (!--pending) fn(null, results, packages) | ||
} | ||
}) | ||
@@ -83,2 +119,15 @@ }) | ||
function getDirSize(dirEntry) { | ||
return dirEntry.children.reduce(function(dirEntry, entry) { | ||
if (entry.isDirectory) { | ||
entry = getDirSize(entry) | ||
} | ||
dirEntry.size += entry.size | ||
dirEntry.diskSize += entry.diskSize | ||
return dirEntry | ||
}, dirEntry) | ||
dirEntry.children = [] | ||
return dirEntry | ||
} | ||
pkgFiles.summary = function summary(dir, done) { | ||
@@ -93,4 +142,4 @@ var fn = function once(err, result) { | ||
entries: [], | ||
extractedSize: 0, | ||
extractedDiskSize: 0, | ||
sizeWithDependencies: 0, | ||
diskSizeWithDependencies: 0, | ||
publishSize: 0, | ||
@@ -100,3 +149,3 @@ publishDiskSize: 0 | ||
var pending = 3 | ||
var pending = 2 | ||
@@ -106,6 +155,9 @@ pkgFiles(dir, function(err, entries, packages) { | ||
result.packages = packages | ||
result.publishSize = entries.reduce(function(t, entry) { | ||
var files = entries.filter(function(entry) { | ||
return !entry.isDirectory | ||
}) | ||
result.publishSize = files.reduce(function(t, entry) { | ||
return t + (entry.size || 0) | ||
}, 0) | ||
result.publishDiskSize = entries.reduce(function(t, entry) { | ||
result.publishDiskSize = files.reduce(function(t, entry) { | ||
return t + (entry.diskSize || 0) | ||
@@ -117,13 +169,42 @@ }, 0) | ||
du(dir, {disk: true}, function(err, extractedDiskSize) { | ||
duSizes(dir, function(err, dir) { | ||
result.diskSizeWithDependencies = dir.diskSize | ||
result.sizeWithDependencies = dir.size | ||
if (!--pending) return fn(null, result) | ||
}) | ||
} | ||
function duSizes(dir, fn) { | ||
var result = {dir: dir} | ||
var pending = 2 | ||
du(dir, {disk: true}, function(err, diskSize) { | ||
if (err) return fn(err) | ||
result.extractedDiskSize = extractedDiskSize || 0 | ||
result.diskSize = diskSize || 0 | ||
if (!--pending) return fn(null, result) | ||
}) | ||
du(dir, function(err, extractedSize) { | ||
du(dir, function(err, size) { | ||
if (err) return fn(err) | ||
result.extractedSize = extractedSize || 0 | ||
result.size = size || 0 | ||
if (!--pending) return fn(null, result) | ||
}) | ||
} | ||
function getAncestors(dirName) { | ||
var dirNames = [] | ||
dirName = path.dirname(dirName) | ||
while (dirName && dirName !== '/' && dirName !== '.') { | ||
dirNames.push(dirName) | ||
dirName = path.dirname(dirName) | ||
} | ||
if (dirName === '.') dirNames.push(dirName) | ||
return dirNames | ||
} | ||
function unique(item, index, arr) { | ||
return index === arr.lastIndexOf(item) | ||
} | ||
function dirContains(parent, child) { | ||
return parent !== child && path.relative(parent, child).slice(0, 2) !== '..' | ||
} |
{ | ||
"name": "pkgfiles", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "List all files which would be published in a package.", | ||
@@ -25,2 +25,3 @@ "main": "index.js", | ||
"fstream-npm": "~1.0.0", | ||
"map-limit": "0.0.1", | ||
"minimist": "~1.1.0", | ||
@@ -27,0 +28,0 @@ "pkgresolve": "~1.0.0", |
@@ -10,8 +10,6 @@ var test = require('tape') | ||
t.ifError(err) | ||
var expected = ['index.js', 'lib/index.js', 'package.json'].map(function(file) { | ||
return join(PKG_DIR, file) | ||
}) | ||
var expected = ['.', 'index.js', 'lib', 'lib/index.js', 'package.json'] | ||
t.equal(packages.length, 1) | ||
t.equal(packages[0].name, 'pkg') | ||
t.deepEqual(entries.map(function(e) {return e.file}).sort(), expected.sort()) | ||
t.deepEqual(entries.map(function(e) {return e.name}).sort(), expected.sort()) | ||
t.ok(entries.every(function(e) { | ||
@@ -18,0 +16,0 @@ return ( |
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
17016
354
7
+ Addedmap-limit@0.0.1
+ Addedmap-limit@0.0.1(transitive)
+ Addedonce@1.3.3(transitive)
- Removedonce@1.4.0(transitive)