import { readFile, writeFile, stat } from 'fs' import { execFile } from 'child_process' import pify = require('pify') import rimraf = require('rimraf') const rimrafAsync = pify(rimraf) export async function each( list: T[] | Promise, action: (item: T, index: number, list: T[]) => Promise ) { const l = await list for (let i = 0; i < l.length; i++) { await action(l[i], i, l) } } function falseOnEnoent(e: any) { if (e.code === 'ENOENT') { return false } throw e } function padRight(str: string, l: number) { return (str + ' '.repeat(l)).substr(0, l) } const bound: MethodDecorator = function bound( target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor ) { const configurable = true return { configurable, get(this: T) { const value = (descriptor.value as any).bind(this) Object.defineProperty(this, propertyKey, { configurable, value, writable: true }) return value } } } function dequote(input: string) { 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: string): Promise (path: string, encoding: string): Promise } const readFileAsync = pify(readFile) const writeFileAsync = pify(writeFile) const statAsync = pify(stat) const execFileAsync = pify(execFile) const isWindows = process.platform === 'win32' function pathExistsAsync(path: string) { return statAsync(path) .then(x => true) .catch(falseOnEnoent) } function isDirectoryAsync(path: string) { return statAsync(path) .then(x => x.isDirectory()) .catch(falseOnEnoent) } export { dequote, padRight, bound, isWindows, rimrafAsync, statAsync, execFileAsync, readFileAsync, pathExistsAsync, isDirectoryAsync, writeFileAsync }