import * as Promise from "bluebird" import {readdir, stat} from "fs" import {join} from "path" import {each} from "async" import * as fs from "fs" export function allDirectories(directory: string): Promise> { return new Promise>((resolve, reject) => { readdir(directory, (err, files) => { if (err) { reject(err) return } let dirs: Array = [] each(files, (dirName, cb) => { let dirPath = join(directory, dirName) stat(dirPath, (err, stats) => { if (err) { cb(err) return } if (stats.isDirectory()) { dirs.push(dirPath) } cb() }) }, err => { if (err) reject(err) else resolve(dirs) }) }) }) } export function permutations(array1: Array, array2: Array): Array> { let permutations: Array> = [] for (var i = 0; i < array1.length; i++) { for (var j = 0; j < array2.length; j++) { permutations.push([array1[i], array2[j]]) } } return permutations } export function PromiseCache(func: () => Promise): () => Promise { let cache: Result let currentlyExecutingPromise: Promise return function (): Promise { if (cache != null) return Promise.resolve(cache) if (currentlyExecutingPromise != null) return currentlyExecutingPromise let promise = func().then((res: Result) => { if (cache == null) { cache = res } currentlyExecutingPromise = null return res }) currentlyExecutingPromise = promise return promise } } export function flatten(arr: Array>): Array { return Array.prototype.concat.apply([], arr) } export function canExecute(path: string): Promise { return new Promise(resolve => { fs.access(path, fs.X_OK, err => { if (err) resolve(false) else resolve(true) }) }) }