Skip to content

Instantly share code, notes, and snippets.

@lysenko-sergey-developer
Created July 23, 2019 16:35
Show Gist options
  • Save lysenko-sergey-developer/3997e0697727ee0fb4443c72c92ecbaf to your computer and use it in GitHub Desktop.
Save lysenko-sergey-developer/3997e0697727ee0fb4443c72c92ecbaf to your computer and use it in GitHub Desktop.
dd
const fs = require('fs')
const fsPromises = require('fs').promises
const path = require('path')
const promisifyStat = (path) => new Promise((resolve, reject) => {
fs.stat(path, (err, stat) => {
if (err) reject(err)
resolve(stat)
})
})
const extractComponents = async (extractPath = '') => {
try {
const directories = await fsPromises.readdir(extractPath)
return directories.reduce(async (returnedOutput, output) => {
const stat = await promisifyStat(path.join(extractPath, output))
console.log(output)
const isFile = await stat.isFile()
const filePath = path.join(extractPath, output)
console.log('>>>', stat)
const nextReturnedOutput = await isFile ?
{ filePath: output } :
{ filePath: await extractComponents(path.join(extractPath, output)) }
return { ...returnedOutput, ...nextReturnedOutput }
})
} catch (err) {
console.log('error:', err)
}
}
extractComponents(path.join(__dirname, '..', 'stories')).then(x => console.log('end', x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment