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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ $ npm install node-df

### Basic

It will return a promise if the callback isn't passed.

```javascript
var
df = require('node-df');

// with callback
df(function (error, response) {
if (error) { throw error; }

console.log(JSON.stringify(response, null, 2));
});
// with promises:
df()
.then((response) => console.log(JSON.stringify(response, null, 2)));
```

Output from `df` looks like this:
Expand Down
59 changes: 38 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
var exec = require('child_process').exec
var parse = require('./parse')
var utils = require('./utils')
var parsePosix = require('./parse-posix')
var parseWin = require('./parse-win')
var path = require('path')

module.exports = function df(aOptions, aCallback) {
var options
var callback

if (typeof aOptions === 'function') {
if (!aCallback && !aOptions) { // df()
callback = utils.createPromiseCallback()
options = {}
} else if (!aCallback && typeof aOptions === 'function') { // df(cb)
callback = aOptions
} else {
options = {}
} else if (!aCallback && typeof aOptions !== 'function') {// cb(options)
callback = utils.createPromiseCallback()
options = aOptions
} else {// df(options, cb)
callback = aCallback
options = aOptions
}

// TODO: this should throw an error because invoking df without a callback function is pointless.
// It's a breaking change to be made after releasing 0.1.4
if (typeof callback !== 'function') {
callback = function() {}
}

// TODO: this should invoke callback with an error
// It's a breaking change to be made after releasing 0.1.4
if (typeof options !== 'object') {
options = {}
}

// TODO: snould validate options and merge with defaults
// TODO: should validate options and merge with defaults
// It's a breaking change to be made after releasing 0.1.4

// TODO: should throw if prefixMultiplier is not a string
// 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 or DRIVETYPE=4"'
}
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 +61,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],
}
})
}
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
// From loopback
// https://github.com/strongloop/loopback/blob/77a35231dc0e8a480feb5b8e9e48429ab5bdd037/lib/utils.js#L16
exports.createPromiseCallback = function createPromiseCallback() {
var cb = null;
var promise = new Promise(function(resolve, reject) {
cb = function(err, data) {
if (err) {
return reject(err);
}
return resolve(data);
};
});
cb.promise = promise;
return cb;
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "node-df",
"version": "0.1.4",
"name": "node-df-win-posix",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be included in the pull request

"version": "0.3.0",
"description":
"A cross-platform Node.js wrapper around the standard Unix computer program, df.",
"main": "lib/index.js",
"scripts": {
"precommit": "lint-staged",
"test:coverage": "npm test -- --coverage",
"test": "jest"
"test": "jest --env=node"
},
"author": "Adriano Di Giovanni <[email protected]> (http://adrianodigiovanni.com/)",
"license": "MIT",
Expand All @@ -31,15 +31,15 @@
},
"repository": {
"type": "git",
"url": "https://github.com/adriano-di-giovanni/node-df.git"
"url": "https://github.com/dreamdevil00/node-df.git"
},
"keywords": ["node", "df", "disk", "free", "diskfree"],
"bugs": {
"url": "https://github.com/adriano-di-giovanni/node-df/issues"
"url": "https://github.com/dreamdevil00/node-df/issues"
},
"lint-staged": {
"*.js": ["eslint --fix", "git add"],
"*.json": ["prettier --write", "git add"]
},
"homepage": "https://github.com/adriano-di-giovanni/node-df"
"homepage": "https://github.com/dreamdevil00/node-df.git"
}