Skip to content

Instantly share code, notes, and snippets.

@andykais
Last active January 10, 2017 00:55
Show Gist options
  • Save andykais/62802badef3d9faa6bac7b48386f09ea to your computer and use it in GitHub Desktop.
Save andykais/62802badef3d9faa6bac7b48386f09ea to your computer and use it in GitHub Desktop.
let listFiles = (folder: string) => listContents(folder, (f) => fs.statSync(f).isFile())
let listFolders = (folder: string) => listContents(folder, (f) => fs.statSync(f).isDirectory())
let listContents = (folder: string, filterFunc) => {
return new Promise(function (resolve, reject) {
fs.readdir(folder, function (error, result) {
if (error) {
reject(error)
} else {
resolve(result.map(file => path.join(folder, file)).filter(filterFunc))
}
})
})
}
const flatten = arr => arr.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), [])
// a list of folders
let folders = await listFiles(ASSET_FOLDER)
// a list of lists of files
let files = await Promise.all(folders.map(async (name) => await listFiles(name)))
// a list of files
let flatList = flatten(files) // adding this line causes errors on the first two
log(flatList)
// make map equivilant of this
let mappedList = []
for (let file in flatList) {
let shouldConvert = await checkFile(file)
let newFile = shouldConvert ? await convertFile(file) : file
webList.push(newFile)
}
// not working, returns a promise
let clothes = await temperatures.map(async (temp) => {
let isCold = await check(temp)
if (isCold)
return await wearClothes("cold")
return dontChange()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment