check-disk-space
Advanced tools
Comparing version 1.0.0 to 1.1.0
103
index.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
'use strict' | ||
@@ -11,20 +11,21 @@ const {exec} = require('child_process') | ||
* @param {Object} mapping - Map between column index and normalized column name | ||
* @return {Object} - {free, size} normolized object | ||
* @param {Number} coefficient - The size coefficient to get bytes instead of kB | ||
* @return {Object} - {free, size} normalized object | ||
*/ | ||
function mapOutput(stdout, filter, mapping, coefficient = 1) { | ||
const parsed = stdout.trim().split('\n').slice(1).map(line => { | ||
return line.trim().split(/\s+(?=[\d\/])/) | ||
}) | ||
const parsed = stdout.trim().split('\n').slice(1).map(line => { | ||
return line.trim().split(/\s+(?=[\d/])/) | ||
}) | ||
let filtered = parsed.filter(filter) | ||
let filtered = parsed.filter(filter) | ||
if (filtered.length === 0) { | ||
throw new Error('Path did not match any drive') | ||
} | ||
filtered = filtered[0] | ||
if (filtered.length === 0) { | ||
throw new Error('Path did not match any drive') | ||
} | ||
filtered = filtered[0] | ||
return { | ||
free: parseInt(filtered[mapping.free], 10) * coefficient, | ||
size: parseInt(filtered[mapping.size], 10) * coefficient | ||
} | ||
return { | ||
free: parseInt(filtered[mapping.free], 10) * coefficient, | ||
size: parseInt(filtered[mapping.size], 10) * coefficient | ||
} | ||
} | ||
@@ -38,19 +39,19 @@ | ||
* @param {Object} mapping - Map between column index and normalized column name | ||
* @param {String} path - The file/folder path from where we want to know disk space | ||
* @return {Promise} | ||
* @param {Number} coefficient - The size coefficient to get bytes instead of kB | ||
* @return {Promise} - | ||
*/ | ||
function check(cmd, filter, mapping, path) { | ||
return new Promise((resolve, reject) => { | ||
exec(cmd, (error, stdout) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
function check(cmd, filter, mapping, coefficient) { | ||
return new Promise((resolve, reject) => { | ||
exec(cmd, (error, stdout) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
try { | ||
resolve(mapOutput(stdout, filter, mapping)) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
try { | ||
resolve(mapOutput(stdout, filter, mapping, coefficient)) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
}) | ||
} | ||
@@ -62,17 +63,17 @@ | ||
* @param {String} path - The file/folder path from where we want to know disk space | ||
* @return {Promise} | ||
* @return {Promise} - | ||
*/ | ||
function checkWin32(path) { | ||
return check( | ||
`wmic logicaldisk get size,freespace,caption`, | ||
(driveData) => { | ||
// Only get the drive which match the path | ||
const driveLetter = driveData[0] | ||
return path.startsWith(driveLetter) | ||
}, | ||
{ | ||
free: 1, | ||
size: 2 | ||
} | ||
) | ||
return check( | ||
`wmic logicaldisk get size,freespace,caption`, | ||
driveData => { | ||
// Only get the drive which match the path | ||
const driveLetter = driveData[0] | ||
return path.startsWith(driveLetter) | ||
}, | ||
{ | ||
free: 1, | ||
size: 2 | ||
} | ||
) | ||
} | ||
@@ -84,14 +85,14 @@ | ||
* @param {String} path - The file/folder path from where we want to know disk space | ||
* @return {Promise} | ||
* @return {Promise} - | ||
*/ | ||
function checkUnix(path) { | ||
return check( | ||
`df -Pk ${path}`, | ||
(driveData) => true, // We should only get one line, so we did not need to filter | ||
{ | ||
free: 3, | ||
size: 1 | ||
}, | ||
1024 // We get sizes in kB, we need to convert that to bytes | ||
) | ||
return check( | ||
`df -Pk ${path}`, | ||
() => true, // We should only get one line, so we did not need to filter | ||
{ | ||
free: 3, | ||
size: 1 | ||
}, | ||
1024 // We get sizes in kB, we need to convert that to bytes | ||
) | ||
} | ||
@@ -98,0 +99,0 @@ |
{ | ||
"name": "check-disk-space", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Light multiplartform disk space checker without third party for Node.js", | ||
@@ -8,6 +8,15 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ava": "0.22.0" | ||
"ava": "0.22.0", | ||
"eslint-plugin-ava": "^4.2.2", | ||
"npm-run-all": "^4.1.1", | ||
"xo": "^0.18.2" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"semicolon": false | ||
}, | ||
"scripts": { | ||
"test": "ava --color" | ||
"unit-test": "ava --color", | ||
"lint": "xo", | ||
"test": "npm-run-all unit-test lint" | ||
}, | ||
@@ -14,0 +23,0 @@ "repository": { |
@@ -0,0 +0,0 @@ # Check disk space |
276
test.js
@@ -1,160 +0,158 @@ | ||
'use strict'; | ||
'use strict' | ||
const {test} = require('ava') | ||
import test from 'ava' | ||
const platformFixtures = { | ||
win32: [ | ||
{ | ||
title: 'Windows XP', | ||
path: 'D:/a/long/path', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 627703808 10725732352 | ||
D: 0 699783168 | ||
E: 25851797504 107496861696 | ||
F: 834660716544 2000396742656 | ||
`, | ||
result: { | ||
free: 0, | ||
size: 699783168 | ||
} | ||
}, | ||
{ | ||
title: 'Windows 7', | ||
path: 'C:/blah', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 1286164480 34359734272 | ||
D: 1864638464 50925137920 | ||
E: | ||
F: 77553082368 990202818560 | ||
G: | ||
L: | ||
`, | ||
result: { | ||
free: 1286164480, | ||
size: 34359734272 | ||
} | ||
}, | ||
{ | ||
title: 'Windows 8', | ||
path: 'D:/MyGames', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 498562174976 539028877312 | ||
D: 0 59494400 | ||
`, | ||
result: { | ||
free: 0, | ||
size: 59494400 | ||
} | ||
}, | ||
{ | ||
title: 'Windows 10', | ||
path: 'C:/User/toto', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 159345410048 171204145152 | ||
D: 0 4001759232 | ||
`, | ||
result: { | ||
free: 159345410048, | ||
size: 171204145152 | ||
} | ||
} | ||
], | ||
unix: [ | ||
{ | ||
title: 'bananian', | ||
path: '/dev', | ||
execOutput: ` | ||
Filesystem 1K-blocks Used Available Use% Mounted on | ||
devtmpfs 447624 0 447624 0% /dev | ||
`, | ||
result: { | ||
free: 447624, | ||
size: 447624 | ||
} | ||
}, | ||
{ | ||
title: 'Ubuntu', | ||
path: '/run/user/1000', | ||
execOutput: ` | ||
Filesystem 1024-blocks Used Available Capacity Mounted on | ||
tmpfs 89884 8 89876 1% /run/user/1000 | ||
`, | ||
result: { | ||
free: 89876, | ||
size: 89884 | ||
} | ||
}, | ||
{ | ||
title: 'Ubuntu: russian locale', | ||
path: '/media/Games', | ||
execOutput: ` | ||
Файл.система 1024-блоков Использовано Доступно Вместимость Cмонтировано в | ||
/dev/sdc 240234168 188729228 39278628 83% /media/Games | ||
`, | ||
result: { | ||
free: 39278628, | ||
size: 240234168 | ||
} | ||
}, | ||
{ | ||
title: 'macOS', | ||
path: '/home/jacquie', | ||
execOutput: ` | ||
Filesystem 1024-blocks Used Available Capacity Mounted on | ||
/dev/disk0s2 145961032 20678788 125026244 15% /home | ||
`, | ||
result: { | ||
free: 125026244, | ||
size: 145961032 | ||
} | ||
} | ||
] | ||
win32: [ | ||
{ | ||
title: 'Windows XP', | ||
path: 'D:/a/long/path', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 627703808 10725732352 | ||
D: 0 699783168 | ||
E: 25851797504 107496861696 | ||
F: 834660716544 2000396742656 | ||
`, | ||
result: { | ||
free: 0, | ||
size: 699783168 | ||
} | ||
}, | ||
{ | ||
title: 'Windows 7', | ||
path: 'C:/blah', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 1286164480 34359734272 | ||
D: 1864638464 50925137920 | ||
E: | ||
F: 77553082368 990202818560 | ||
G: | ||
L: | ||
`, | ||
result: { | ||
free: 1286164480, | ||
size: 34359734272 | ||
} | ||
}, | ||
{ | ||
title: 'Windows 8', | ||
path: 'D:/MyGames', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 498562174976 539028877312 | ||
D: 0 59494400 | ||
`, | ||
result: { | ||
free: 0, | ||
size: 59494400 | ||
} | ||
}, | ||
{ | ||
title: 'Windows 10', | ||
path: 'C:/User/toto', | ||
execOutput: ` | ||
Caption FreeSpace Size | ||
C: 159345410048 171204145152 | ||
D: 0 4001759232 | ||
`, | ||
result: { | ||
free: 159345410048, | ||
size: 171204145152 | ||
} | ||
} | ||
], | ||
unix: [ | ||
{ | ||
title: 'bananian', | ||
path: '/dev', | ||
execOutput: ` | ||
Filesystem 1K-blocks Used Available Use% Mounted on | ||
devtmpfs 447624 0 447624 0% /dev | ||
`, | ||
result: { | ||
free: 447624 * 1024, | ||
size: 447624 * 1024 | ||
} | ||
}, | ||
{ | ||
title: 'Ubuntu', | ||
path: '/run/user/1000', | ||
execOutput: ` | ||
Filesystem 1024-blocks Used Available Capacity Mounted on | ||
tmpfs 89884 8 89876 1% /run/user/1000 | ||
`, | ||
result: { | ||
free: 89876 * 1024, | ||
size: 89884 * 1024 | ||
} | ||
}, | ||
{ | ||
title: 'Ubuntu: russian locale', | ||
path: '/media/Games', | ||
execOutput: ` | ||
Файл.система 1024-блоков Использовано Доступно Вместимость Cмонтировано в | ||
/dev/sdc 240234168 188729228 39278628 83% /media/Games | ||
`, | ||
result: { | ||
free: 39278628 * 1024, | ||
size: 240234168 * 1024 | ||
} | ||
}, | ||
{ | ||
title: 'macOS', | ||
path: '/home/jacquie', | ||
execOutput: ` | ||
Filesystem 1024-blocks Used Available Capacity Mounted on | ||
/dev/disk0s2 145961032 20678788 125026244 15% /home | ||
`, | ||
result: { | ||
free: 125026244 * 1024, | ||
size: 145961032 * 1024 | ||
} | ||
} | ||
] | ||
} | ||
function mockCheckDiskSpace(platform, fixture) { | ||
// Clean some required paths | ||
delete require.cache[require.resolve('./')] | ||
// Clean some required paths | ||
delete require.cache[require.resolve('./')] | ||
// Forces the platform for test purposes | ||
Object.defineProperty(process, 'platform', {value: platform}) | ||
// Forces the platform for test purposes | ||
Object.defineProperty(process, 'platform', {value: platform}) | ||
// Mock child_process.exec | ||
require('child_process').exec = (cmd, callback) => { | ||
process.nextTick(() => { | ||
callback(fixture.execError, fixture.execOutput) | ||
}) | ||
} | ||
// Mock child_process.exec | ||
require('child_process').exec = (cmd, callback) => { | ||
process.nextTick(() => { | ||
callback(fixture.execError, fixture.execOutput) | ||
}) | ||
} | ||
const checkDiskSpace = require('./') | ||
return checkDiskSpace | ||
return require('./') | ||
} | ||
Object.keys(platformFixtures).forEach((platform) => { | ||
platformFixtures[platform].forEach((fixture) => { | ||
test(`${platform}: ${fixture.title}`, async t => { | ||
const checkDiskSpace = mockCheckDiskSpace(platform, fixture) | ||
Object.keys(platformFixtures).forEach(platform => { | ||
platformFixtures[platform].forEach(fixture => { | ||
test(`${platform}: ${fixture.title}`, async t => { | ||
const checkDiskSpace = mockCheckDiskSpace(platform, fixture) | ||
t.deepEqual(await checkDiskSpace(fixture.path), fixture.result) | ||
}) | ||
t.deepEqual(await checkDiskSpace(fixture.path), fixture.result) | ||
}) | ||
}) | ||
}) | ||
test(`win32: path did not match any disk`, async t => { | ||
const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0]) | ||
const error = await t.throws(checkDiskSpace('Z:/shouldfail')) | ||
t.is(error.message, 'Path did not match any drive') | ||
const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0]) | ||
const error = await t.throws(checkDiskSpace('Z:/shouldfail')) | ||
t.is(error.message, 'Path did not match any drive') | ||
}) | ||
test(`exec has an error`, async t => { | ||
const fixture = JSON.parse(JSON.stringify(platformFixtures.win32[0])) | ||
fixture.execError = new Error('some error') | ||
const fixture = JSON.parse(JSON.stringify(platformFixtures.win32[0])) | ||
fixture.execError = new Error('some error') | ||
const checkDiskSpace = mockCheckDiskSpace('win32', fixture) | ||
const error = await t.throws(checkDiskSpace('C:/something')) | ||
t.is(error.message, 'some error') | ||
const checkDiskSpace = mockCheckDiskSpace('win32', fixture) | ||
const error = await t.throws(checkDiskSpace('C:/something')) | ||
t.is(error.message, 'some error') | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
154712
16
4