import { execFile } from 'child_process' ; import { existsSync } from 'fs' ; import { normalize , sep } from 'path' ; import InvalidPathError from '@/src/errors/invalidPathError' ; import NoMatchError from '@/src/errors/noMatchError' ; import getFirstExistingParentPath from '@/src/functions/getFirstExistingParentPath' ; import Dependencies from '@/src/types/dependencies' ; import DiskSpace from '@/src/types/diskSpace' ; param directoryPath param dependencies function checkDiskSpace ( directoryPath , dependencies = { platform : process . platform , fsExistsSync : existsSync , pathNormalize : normalize , pathSep : sep , cpExecFile : execFile , } ) { param stdout param filter param mapping param coefficient function mapOutput ( stdout , filter , mapping , coefficient ) { const parsed = stdout . trim ( ) . split ( '\n' ) . slice ( 1 ) . map ( line => { return line . trim ( ) . split ( /\s+(?=[\d/])/ ) ; } ) ; const filtered = parsed . filter ( filter ) ; if ( filtered . length === 0 ) { throw new NoMatchError ( ) ; } const diskData = filtered [ 0 ] ; return { diskPath : diskData [ mapping . diskPath ] , free : parseInt ( diskData [ mapping . free ] , 10 ) * coefficient , size : parseInt ( diskData [ mapping . size ] , 10 ) * coefficient , } ; } param cmd param filter param mapping param coefficient function check ( cmd , filter , mapping , coefficient = 1 ) { return new Promise ( ( resolve , reject ) => { const [ file , ... args ] = cmd ; if ( file === undefined ) { return Promise . reject ( 'cmd must contain at least one item' ) ; } dependencies . cpExecFile ( file , args , ( error , stdout ) => { if ( error ) { reject ( error ) ; } try { resolve ( mapOutput ( stdout , filter , mapping , coefficient ) ) ; } catch ( error2 ) { reject ( error2 ) ; } } ) ; } ) ; } param directoryPath 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 ( [ 'wmic' , 'logicaldisk' , 'get' , 'size,freespace,caption' ] , driveData => { const driveLetter = driveData [ 0 ] ; return directoryPath . toUpperCase ( ) . startsWith ( driveLetter . toUpperCase ( ) ) ; } , { diskPath : 0 , free : 1 , size : 2 , } ) ; } param directoryPath function checkUnix ( directoryPath ) { if ( ! dependencies . pathNormalize ( directoryPath ) . startsWith ( dependencies . pathSep ) ) { return new Promise ( ( resolve , reject ) => { reject ( new InvalidPathError ( `The following path is invalid (should start by ${ dependencies . pathSep }): ${ directoryPath }` ) ) ; } ) ; } const pathToCheck = getFirstExistingParentPath ( directoryPath , dependencies ) ; return check ( [ 'df' , '-Pk' , '--' , pathToCheck ] , ( ) => true , { diskPath : 5 , free : 3 , size : 1 , } , 1024 ) ; } if ( dependencies . platform === 'win32' ) { return checkWin32 ( directoryPath ) ; } return checkUnix ( directoryPath ) ; } export default checkDiskSpace ; export { Dependencies , DiskSpace , getFirstExistingParentPath , InvalidPathError , NoMatchError , } ;