import * as os from "os" ; import * as crypto from "crypto" ; import * as fse from "fs-extra" ; const originalCwd = process . cwd ( ) ; const createdDirectories = [ ] ; process . on ( "exit" , ( ) => { createdDirectories . forEach ( path => { fse . removeSync ( path ) ; } ) ; } ) ; const setCleanTestCwd = ( ) => { const random = crypto . randomBytes ( 16 ) . toString ( "hex" ) ; const path = `${ os . tmpdir ( ) }/fs-jetpack-test-${ random }` ; fse . mkdirSync ( path ) ; createdDirectories . push ( path ) ; process . chdir ( path ) ; } ; const switchBackToCorrectCwd = ( ) => { const path = createdDirectories . pop ( ) ; process . chdir ( originalCwd ) ; try { fse . removeSync ( path ) ; } catch ( err ) { fse . removeSync ( path ) ; } } ; const parseMode = ( modeAsNumber ) => { const mode = modeAsNumber . toString ( 8 ) ; return mode . substring ( mode . length - 3 ) ; } ; function osSep ( path ) ; function osSep ( path ) ; function osSep ( path ) { if ( Array . isArray ( path ) ) { return path . map ( osSep ) ; } if ( process . platform === "win32" ) { return path . replace ( /\//g , "\\" ) ; } return path . replace ( /\\/g , "/" ) ; } export default { setCleanTestCwd , switchBackToCorrectCwd , parseMode , osSep } ;