import { readdir , rmdir as fsRmdir , stat , unlink } from 'fs' ; import { join } from 'path' ; import { promisify } from 'util' ; export param { string } path returns { Promise < void > } export async function rmdir ( path ) { let contents ; try { contents = await promisify ( readdir ) ( path ) ; } catch ( error ) { if ( error . code === 'ENOENT' ) { return ; } throw error ; } await Promise . all ( contents . map ( content => { const subPath = join ( path , content ) ; return promisify ( stat ) ( subPath ) . then ( stats => { if ( stats . isDirectory ( ) ) { return rmdir ( subPath ) ; } return promisify ( unlink ) ( subPath ) ; } ) ; } ) ) ; await promisify ( fsRmdir ) ( path ) ; }