Socket
Socket
Sign inDemoInstall

check-disk-space

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

check-disk-space - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

64

index.js
'use strict'
const {exec} = require('child_process')
const fs = require('fs')
const path = require('path')
const util = require('util')
// Create some errors
const InvalidPathError = function (message) {
Error.captureStackTrace(this, this.constructor)
this.name = 'InvalidPathError'
this.message = message || ''
}
util.inherits(InvalidPathError, Error)
const NoMatchError = function (message) {
Error.captureStackTrace(this, this.constructor)
this.name = 'NoMatchError'
this.message = message || ''
}
util.inherits(NoMatchError, Error)
/**

@@ -22,3 +40,3 @@ * Maps command output to a normalized object {free, size}

if (filtered.length === 0) {
throw new Error('Path did not match any drive')
throw new NoMatchError()
}

@@ -42,3 +60,3 @@ filtered = filtered[0]

*/
function check(cmd, filter, mapping, coefficient) {
function check(cmd, filter, mapping, coefficient = 1) {
return new Promise((resolve, reject) => {

@@ -62,6 +80,12 @@ exec(cmd, (error, stdout) => {

*
* @param {String} path - The file/folder path from where we want to know disk space
* @param {String} directoryPath - The file/folder path from where we want to know disk space
* @return {Promise} -
*/
function checkWin32(path) {
function checkWin32(directoryPath) {
if (directoryPath.charAt(1) !== ':') {
return new Promise((resolve, reject) => {
reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`))
})
}
return check(

@@ -72,3 +96,3 @@ `wmic logicaldisk get size,freespace,caption`,

const driveLetter = driveData[0]
return path.startsWith(driveLetter)
return directoryPath.startsWith(driveLetter)
},

@@ -85,8 +109,14 @@ {

*
* @param {String} path - The file/folder path from where we want to know disk space
* @param {String} directoryPath - The file/folder path from where we want to know disk space
* @return {Promise} -
*/
function checkUnix(path) {
function checkUnix(directoryPath) {
if (!path.normalize(directoryPath).startsWith(path.sep)) {
return new Promise((resolve, reject) => {
reject(new InvalidPathError(`The following path is invalid (should start by ${path.sep}): ${directoryPath}`))
})
}
return check(
`df -Pk ${path}`,
`df -Pk ${module.exports.getFirstExistingParentPath(directoryPath)}`,
() => true, // We should only get one line, so we did not need to filter

@@ -103,1 +133,19 @@ {

module.exports = (process.platform === 'win32') ? checkWin32 : checkUnix
/**
* Get the first existing parent path
*
* @param directoryPath
* @returns {*}
*/
module.exports.getFirstExistingParentPath = directoryPath => {
let parentDirectoryPath = directoryPath
let parentDirectoryFound = fs.existsSync(parentDirectoryPath)
while (!parentDirectoryFound) {
parentDirectoryPath = path.normalize(parentDirectoryPath + '/..')
parentDirectoryFound = fs.existsSync(parentDirectoryPath)
}
return parentDirectoryPath
}

2

package.json
{
"name": "check-disk-space",
"version": "1.1.0",
"version": "1.2.0",
"description": "Light multiplartform disk space checker without third party for Node.js",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -5,2 +5,3 @@ # Check disk space

[![License MIT](https://img.shields.io/github/license/Alex-D/check-disk-space.svg)](LICENCE)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)

@@ -7,0 +8,0 @@ ## Install

@@ -66,3 +66,3 @@ 'use strict'

],
unix: [
linux: [
{

@@ -103,3 +103,5 @@ title: 'bananian',

}
},
}
],
darwin: [
{

@@ -137,2 +139,15 @@ title: 'macOS',

function mockGetFirstExistingParentPath(existingDirectoryPath) {
// Clean some required paths
delete require.cache[require.resolve('./')]
// Forces the platform for test purposes
Object.defineProperty(process, 'platform', {value: 'linux'})
// Mock child_process.exec
require('fs').existsSync = directoryPath => directoryPath === existingDirectoryPath
return require('./').getFirstExistingParentPath
}
Object.keys(platformFixtures).forEach(platform => {

@@ -151,5 +166,17 @@ platformFixtures[platform].forEach(fixture => {

const error = await t.throws(checkDiskSpace('Z:/shouldfail'))
t.is(error.message, 'Path did not match any drive')
t.is(error.name, 'NoMatchError')
})
test(`win32: invalid path`, async t => {
const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0])
const error = await t.throws(checkDiskSpace('an invalid path'))
t.is(error.name, 'InvalidPathError')
})
test(`unix: invalid path`, async t => {
const checkDiskSpace = mockCheckDiskSpace('linux', platformFixtures.linux[0])
const error = await t.throws(checkDiskSpace('an invalid path'))
t.is(error.name, 'InvalidPathError')
})
test(`exec has an error`, async t => {

@@ -163,1 +190,13 @@ const fixture = JSON.parse(JSON.stringify(platformFixtures.win32[0]))

})
test(`unix: get first existing parent path`, t => {
const parentPath = '/home/Lisa'
const getFirstExistingParentPath = mockGetFirstExistingParentPath(parentPath)
t.is(getFirstExistingParentPath('/home/Lisa/games/Ankama/Dofus'), parentPath)
})
test(`unix: get first parent can be the path itself`, t => {
const parentPath = '/home/Lisa'
const getFirstExistingParentPath = mockGetFirstExistingParentPath(parentPath)
t.is(getFirstExistingParentPath(parentPath), parentPath)
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc