import { readFile , writeFile , stat } from 'fs' ; import { execFile } from 'child_process' ; import { promisify } from 'util' ; import rimraf = require ( 'rimraf' ) ; const rimrafAsync = promisify ( rimraf ) ; export const STDIN_FLAG = '[stdin]' ; export async function each < T > ( list , action ) { const l = await list ; return Promise . all ( l . map ( action ) ) ; } export function wrap ( code ) { return '!(function () {' + code + '})();' ; } function falseOnEnoent ( e ) { if ( e . code === 'ENOENT' ) { return false ; } throw e ; } function padRight ( str , l ) { return ( str + ' ' . repeat ( l ) ) . substr ( 0 , l ) ; } const bound = function bound < T > ( target , propertyKey , descriptor ) { const configurable = true ; return { configurable , get ( this ) { const value = ( descriptor . value as any ) . bind ( this ) ; Object . defineProperty ( this , propertyKey as string , { configurable , value , writable : true , } ) ; return value ; } , } ; } ; function dequote ( input ) { input = input . trim ( ) ; const singleQuote = input . startsWith ( "'" ) && input . endsWith ( "'" ) ; const doubleQuote = input . startsWith ( '"' ) && input . endsWith ( '"' ) ; if ( singleQuote || doubleQuote ) { return input . slice ( 1 ) . slice ( 0 , - 1 ) ; } return input ; } export interface ReadFileAsync { ( path ) : Promise < Buffer > ; ( path , encoding ) : Promise < string > ; } const readFileAsync = promisify ( readFile ) ; const writeFileAsync = promisify ( writeFile ) ; const statAsync = promisify ( stat ) ; const execFileAsync = promisify ( execFile ) ; const isWindows = process . platform === 'win32' ; function pathExistsAsync ( path ) { return statAsync ( path ) . then ( ( x ) => true ) . catch ( falseOnEnoent ) ; } function isDirectoryAsync ( path ) { return statAsync ( path ) . then ( ( x ) => x . isDirectory ( ) ) . catch ( falseOnEnoent ) ; } param version param operand function semverGt ( version , operand ) { const [ cMajor , cMinor , cPatch ] = version . split ( '.' ) . map ( Number ) ; let [ major , minor , patch ] = operand . split ( '.' ) . map ( Number ) ; if ( ! minor ) minor = 0 ; if ( ! patch ) patch = 0 ; return ( cMajor > major || ( cMajor === major && cMinor > minor ) || ( cMajor === major && cMinor === minor && cPatch > patch ) ) ; } export { dequote , padRight , semverGt , bound , isWindows , rimrafAsync , statAsync , execFileAsync , readFileAsync , pathExistsAsync , isDirectoryAsync , writeFileAsync , } ;