Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var exec = require('child_process').exec
var parse = require('./parse')
var parsePosix = require('./parse-posix')
var parseWin = require('./parse-win')
var path = require('path')

module.exports = function df(aOptions, aCallback) {
var options
Expand Down Expand Up @@ -32,10 +34,25 @@ module.exports = function df(aOptions, aCallback) {
// It should invoke callback with `err` but it's a breaking change

// TODO: should fail if unit is not a string

var command = 'df -kP'
if (options.file) {
command += ' ' + options.file
var command = null;
var isWin = process.platform === 'win32'
var file = options.file
if (isWin) {
var disk = null
command = 'wmic LOGICALDISK '
if (file) {
file = path.win32.normalize(file)
disk = path.parse(file).root.slice(0, -1)
command += disk
} else {
command += ' where "DRIVETYPE=3"'
}
command += ' get name, filesystem, size, freespace'
} else {
var command = 'df -kP'
if (file) {
command += ' ' + file
}
}

exec(command, function(err, stdout, stderr) {
Expand All @@ -49,7 +66,12 @@ module.exports = function df(aOptions, aCallback) {
return
}

var entries = parse(stdout, options)
var entries = null;
if (isWin) {
entries = parseWin(stdout, options)
} else {
entries = parsePosix(stdout, options)
}

callback(null, entries)
})
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions lib/parse-win.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var format = require('./format')
var calcMultiplier = require('./calcMultiplier')

module.exports = function (stdout, options) {
// TODO: this should be removed because it never happens.
// Nonetheless, I want to consider it a breaking changed that's to be made after releasing 0.1.4
if (!stdout) {
return void 0
}

// TODO: `options.prefixMultiplier` should become `options.unit`
// I should deprecate the use of `options.prefixMultiplier` in 0.1.4
var multiplier = calcMultiplier(options.prefixMultiplier)
var precision = options.precision
// TODO: `options.isDisplayPrefixMultiplier` should become `options.showUnit`
// I should deprecate the use of options.isDisplayPrefixMultiplier in 0.1.4
var unit = options.isDisplayPrefixMultiplier ? options.prefixMultiplier : null

return stdout
.trim()
.split(/\r\r\n/) // split into rows
.slice(1) // strip column headers away
.map(function (row) {
var columns = row
.trim()
// one or more whitespaces followed by one or more digits
// must be interpreted as column delimiter
.replace(/\s+(\d+)/g, '\t$1')
// one or more whitespaces followed by a letter
// must be interpreted as column delimiter
.replace(/\s+([a-zA-Z])/g, '\t$1')
// comma followed by one or more whitespaces must be
// interpreted as the last column delimiter
.replace(/(\:)\s+/g, '$1\t')
// split into columns
.split(/\t/)
var size = parseInt(columns[3], 10)
var free = parseInt(columns[1], 10)
var used = size - free
var capacity = 1 - free / size
return {
filesystem: columns[0],
size: format(size, multiplier, precision, unit),
used: format(used, multiplier, precision, unit),
available: format(free, multiplier, precision, unit),
capacity: precision ? capacity.toFixed(precision) : capacity,
mount: columns[2],
}
})
}