diff --git a/lib/index.js b/lib/index.js index 767584b..11c05e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 @@ -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) { @@ -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) }) diff --git a/lib/parse.js b/lib/parse-posix.js similarity index 100% rename from lib/parse.js rename to lib/parse-posix.js diff --git a/lib/parse-win.js b/lib/parse-win.js new file mode 100644 index 0000000..d335475 --- /dev/null +++ b/lib/parse-win.js @@ -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], + } + }) +}