Skip to content

Instantly share code, notes, and snippets.

@Istar-Eldritch
Last active June 18, 2016 12:46
Show Gist options
  • Save Istar-Eldritch/cc74ae658aaa4f3c6801fa94e8e63498 to your computer and use it in GitHub Desktop.
Save Istar-Eldritch/cc74ae658aaa4f3c6801fa94e8e63498 to your computer and use it in GitHub Desktop.
Deep Read function in TypeScript
import {promisify, all} from 'bluebird';
import {readdir as _rd, lstat as _ls} from 'fs'
import {flatten} from 'ramda'
let readdir = promisify(_rd)
let lstat = promisify(_ls)
export function reader<T>(path:string, f: (string) => T): Promise<T[]> {
async function iter(path: string): Promise<T[]> {
let stat = await lstat(path)
if(stat.isDirectory()) {
let paths = await readdir(path)
let routes = await all(paths.map((file) => {
return iter(`${path}/${file}`)
}))
console.log(routes)
return flatten(routes)
}
else {
let route: T = f(path)
return [route];
}
}
return iter(path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment