@sindresorhus/df
Advanced tools
Comparing version 2.1.0 to 3.0.0
58
index.js
'use strict'; | ||
const execa = require('execa'); | ||
const run = args => execa('df', args).then(res => | ||
res.stdout.trim().split('\n').slice(1).map(x => { | ||
const cl = x.split(/\s+(?=[\d\/])/); | ||
const run = async args => { | ||
const {stdout} = await execa('df', args); | ||
return stdout.trim().split('\n').slice(1).map(line => { | ||
const cl = line.split(/\s+(?=[\d/])/); | ||
return { | ||
@@ -16,37 +18,43 @@ filesystem: cl[0], | ||
}; | ||
}) | ||
); | ||
}); | ||
}; | ||
const df = module.exports = () => run(['-kP']); | ||
const df = async () => run(['-kP']); | ||
df.fs = name => { | ||
df.fs = async name => { | ||
if (typeof name !== 'string') { | ||
return Promise.reject(new Error('name required')); | ||
throw new TypeError('The `name` parameter required'); | ||
} | ||
return run(['-kP']).then(data => { | ||
for (const x of data) { | ||
if (x.filesystem === name) { | ||
return x; | ||
} | ||
const data = await run(['-kP']); | ||
for (const item of data) { | ||
if (item.filesystem === name) { | ||
return item; | ||
} | ||
} | ||
throw new Error(`The specified filesystem \`${name}\` doesn't exist`); | ||
}); | ||
throw new Error(`The specified filesystem \`${name}\` doesn't exist`); | ||
}; | ||
df.file = file => { | ||
df.file = async file => { | ||
if (typeof file !== 'string') { | ||
return Promise.reject(new Error('file required')); | ||
throw new TypeError('The `file` parameter is required'); | ||
} | ||
return run(['-kP', file]) | ||
.then(data => data[0]) | ||
.catch(err => { | ||
if (/No such file or directory/.test(err.message)) { | ||
err = new Error(`The specified file \`${file}\` doesn't exist`); | ||
} | ||
let data; | ||
try { | ||
data = await run(['-kP', file]); | ||
} catch (error) { | ||
if (/No such file or directory/.test(error.message)) { | ||
throw new Error(`The specified file \`${file}\` doesn't exist`); | ||
} | ||
throw err; | ||
}); | ||
throw error; | ||
} | ||
return data[0]; | ||
}; | ||
module.exports = df; | ||
module.exports.default = df; |
{ | ||
"name": "@sindresorhus/df", | ||
"version": "2.1.0", | ||
"description": "Get free disk space info from `df -kP`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/df", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"df", | ||
"dfkp", | ||
"df-kp", | ||
"disk", | ||
"space", | ||
"free", | ||
"info", | ||
"data", | ||
"fs", | ||
"filesystem", | ||
"file-system", | ||
"drive", | ||
"mount", | ||
"size", | ||
"capacity" | ||
], | ||
"dependencies": { | ||
"execa": "^0.2.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "@sindresorhus/df", | ||
"version": "3.0.0", | ||
"description": "Get free disk space info from `df -kP`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/df", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"df", | ||
"dfkp", | ||
"df-kp", | ||
"disk", | ||
"space", | ||
"free", | ||
"info", | ||
"data", | ||
"fs", | ||
"filesystem", | ||
"file-system", | ||
"drive", | ||
"mount", | ||
"size", | ||
"capacity" | ||
], | ||
"dependencies": { | ||
"execa": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
# df [![Build Status](https://travis-ci.org/sindresorhus/df.svg?branch=master)](https://travis-ci.org/sindresorhus/df) | ||
> Get free disk space info from [`df -kP`](http://en.wikipedia.org/wiki/Df_\(Unix\)) | ||
> Get free disk space info from [`df -kP`](https://en.wikipedia.org/wiki/Df_\(Unix\)) | ||
Works on any Unix based system like macOS and Linux. | ||
Works on any Unix-based system like macOS and Linux. | ||
@@ -13,3 +13,3 @@ *Created because all the other `df` wrappers are terrible. This one uses simple and explicit parsing. Uses `execFile` rather than `exec`. Ensures better platform portability by using the `-P` flag. Returns sizes in bytes instead of kilobytes and the capacity as a float.* | ||
``` | ||
$ npm install --save @sindresorhus/df | ||
$ npm install @sindresorhus/df | ||
``` | ||
@@ -23,35 +23,34 @@ | ||
df().then(list => { | ||
console.log(list); | ||
(async () => { | ||
console.log(await df()); | ||
/* | ||
[{ | ||
filesystem: '/dev/disk1', | ||
size: 499046809600, | ||
used: 443222245376, | ||
available: 55562420224, | ||
capacity: 0.89, | ||
mountpoint: '/' | ||
}, ...] | ||
[ | ||
{ | ||
filesystem: '/dev/disk1', | ||
size: 499046809600, | ||
used: 443222245376, | ||
available: 55562420224, | ||
capacity: 0.89, | ||
mountpoint: '/' | ||
}, | ||
… | ||
] | ||
*/ | ||
}); | ||
df.fs('/dev/disk1').then(data => { | ||
console.log(data); | ||
console.log(await df.fs('/dev/disk1')); | ||
/* | ||
{ | ||
filesystem: '/dev/disk1', | ||
... | ||
… | ||
} | ||
*/ | ||
}); | ||
df.file(__dirname).then(data => { | ||
console.log(data); | ||
console.log(await df.file(__dirname)); | ||
/* | ||
{ | ||
filesystem: '/dev/disk1', | ||
... | ||
… | ||
} | ||
*/ | ||
}); | ||
})(); | ||
``` | ||
@@ -64,9 +63,9 @@ | ||
Returns a promise for an array of filesystems with space info. | ||
Returns a `Promise<Object[]>` with a list of space info objects for each filesystem. | ||
### df.fs(filesystem) | ||
### df.fs(path) | ||
Returns a promise for an object with the space info for the specified filesystem. | ||
Returns a `Promise<Object>` with the space info for the given filesystem path. | ||
- `filesystem` - The name of the filesystem. | ||
- `filesystem` - Name of the filesystem. | ||
- `size` - Total size in bytes. | ||
@@ -78,17 +77,21 @@ - `used` - Used size in bytes. | ||
#### filesystem | ||
#### path | ||
Type: `string` | ||
### df.file(file) | ||
Path to a [filesystem device file](https://en.wikipedia.org/wiki/Device_file). Example: `'/dev/disk1'`. | ||
Returns a promise for an object with the space info for the filesystem the supplied file is part of. | ||
### df.file(path) | ||
#### file | ||
Returns a `Promise<Object>` with the space info for the filesystem the given file is part of. | ||
#### path | ||
Type: `string` | ||
Path to a file on the filesystem to get the space info for. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
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
5964
5
91
94
3
+ Addedcross-spawn@6.0.5(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedexeca@1.0.0(transitive)
+ Addedget-stream@4.1.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addednice-try@1.0.5(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedcross-spawn-async@2.2.5(transitive)
- Removedexeca@0.2.2(transitive)
- Removedlru-cache@4.1.5(transitive)
- Removednpm-run-path@1.0.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedpath-key@1.0.0(transitive)
- Removedpseudomap@1.0.2(transitive)
- Removedyallist@2.1.2(transitive)
Updatedexeca@^1.0.0