/**
 * Various system related utilities
 */ import { spawn } from 'child_process' ; import fs , { mkdirSync } from 'fs' ; import path from 'path' ; import rimraf from 'rimraf' ; export const LOCAL_DIR = path . join ( __dirname , '../../../../../local' ) ; param file export const removeFileSync = ( file ) : void => { rimraf ( file , ( err ) => { } ) ; } ; param file export const removeFile = ( file ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { rimraf ( file , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; export const processIsRunning = ( pid ) : boolean => { try { return < boolean > ( < unknown > process . kill ( pid , 0 ) ) ; } catch ( e ) { return e . code === 'EPERM' ; } } ; param dir export const removeDirectorySync = ( dir ) : void => { rimraf ( dir , ( ) => { } ) ; } ; param dir export const removeDirectory = ( dir ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { rimraf ( dir , ( err ) => { if ( err ) { reject ( err ) ; } else { resolve ( ) ; } } ) ; } ) ; } ; param file param destination export const writeFileToDestination = async ( file , destination ) : Promise < void > => { return new Promise ( ( resolve ) => { if ( ! fs . existsSync ( path . dirname ( destination ) ) ) { mkdirSync ( path . dirname ( destination ) , { recursive : true , } ) ; } fs . createReadStream ( file ) . pipe ( fs . createWriteStream ( destination ) ) . on ( 'close' , ( ) => { resolve ( ) ; } ) ; } ) ; } ; param fileOrDirectoryPath param container param targetPath export const dockerCopy = ( fileOrDirectoryPath , containerID , targetPath ) : Promise < void > => { return new Promise ( ( resolve , reject ) => { const p = spawn ( 'docker' , [ 'cp' , fileOrDirectoryPath , `${ containerID }:${ targetPath }` , ] ) ; p . on ( 'error' , ( err ) => { reject ( err ) ; } ) ; p . on ( 'close' , ( code ) => { if ( code === 0 ) { resolve ( ) ; } else { reject ( `dockerCopy from ${ fileOrDirectoryPath } to ${ containerID }:${ targetPath } exited with code ${ code }` ) ; } } ) ; } ) ; } ;