Last active
March 21, 2020 13:10
-
-
Save merksam/46cc0d2bf236141ef8720023e1342d9b to your computer and use it in GitHub Desktop.
Provides interface to `df -lP` and parses its result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { exec } from 'child_process'; | |
class DiskService { | |
getAvailableSpace = () => { | |
/* | |
Filesystem 1024-blocks Used Available Capacity Mounted on | |
/dev/root 3745600 2808444 752184 79% / | |
devtmpfs 470116 0 470116 0% /dev | |
tmpfs 474724 0 474724 0% /dev/shm | |
tmpfs 474724 12228 462496 3% /run | |
tmpfs 5120 0 5120 0% /run/lock | |
tmpfs 474724 0 474724 0% /sys/fs/cgroup | |
/dev/mmcblk0p1 43539 22191 21348 51% /boot | |
tmpfs 94944 0 94944 0% /run/user/0 | |
*/ | |
exec('df -lP', (error, stdout, stderr) => { | |
const rows = stdout.trim().split('\n').map(item => item.replace(/\s+/g, ' ').split(' ')); | |
const dfResult = []; | |
for (const row of rows.slice(1)) { | |
dfResult.push({ | |
fileSystem : row[0], | |
used : row[2], | |
available : row[3], | |
capacity : row[4], | |
mountedOn : row[5], | |
}); | |
} | |
// console.log(JSON.stringify(dfResult, null, 2)); | |
return dfResult; | |
}); | |
}; | |
} | |
export const diskService = new DiskService(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment