import which from 'which' ; import path from 'path' ; export function executable ( cmd ) { try { which . sync ( cmd ) ; } catch ( e ) { return false ; } return true ; } export function characterIndex ( content , byteIndex ) { let buf = Buffer . from ( content , 'utf8' ) ; return buf . slice ( 0 , byteIndex ) . toString ( 'utf8' ) . length ; } export function wait ( ms ) { return new Promise ( resolve => { setTimeout ( ( ) => { resolve ( undefined ) ; } , ms ) ; } ) ; } export function pad ( n , total ) { let l = total - n . length ; if ( l <= 0 ) return '' ; return ( ( new Array ( l ) ) . fill ( ' ' ) . join ( '' ) ) ; } /**
 * Removes duplicates from the given array. The optional keyFn allows to specify
 * how elements are checked for equalness by returning a unique string for each.
 */ export function distinct < T > ( array , keyFn ? ) { if ( ! keyFn ) { return array . filter ( ( element , position ) => { return array . indexOf ( element ) === position ; } ) ; } const seen = Object . create ( null ) ; return array . filter ( elem => { const key = keyFn ( elem ) ; if ( seen [ key ] ) { return false ; } seen [ key ] = true ; return true ; } ) ; } export function isParentFolder ( folder , filepath ) { let rel = path . relative ( folder , filepath ) ; return ! rel . startsWith ( '..' ) ; }