l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

find-made.js (763B)


      1 const {dirname} = require('path')
      2 
      3 const findMade = (opts, parent, path = undefined) => {
      4   // we never want the 'made' return value to be a root directory
      5   if (path === parent)
      6     return Promise.resolve()
      7 
      8   return opts.statAsync(parent).then(
      9     st => st.isDirectory() ? path : undefined, // will fail later
     10     er => er.code === 'ENOENT'
     11       ? findMade(opts, dirname(parent), parent)
     12       : undefined
     13   )
     14 }
     15 
     16 const findMadeSync = (opts, parent, path = undefined) => {
     17   if (path === parent)
     18     return undefined
     19 
     20   try {
     21     return opts.statSync(parent).isDirectory() ? path : undefined
     22   } catch (er) {
     23     return er.code === 'ENOENT'
     24       ? findMadeSync(opts, dirname(parent), parent)
     25       : undefined
     26   }
     27 }
     28 
     29 module.exports = {findMade, findMadeSync}