l0bsterssg

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

opts-arg.js (784B)


      1 const { promisify } = require('util')
      2 const fs = require('fs')
      3 const optsArg = opts => {
      4   if (!opts)
      5     opts = { mode: 0o777, fs }
      6   else if (typeof opts === 'object')
      7     opts = { mode: 0o777, fs, ...opts }
      8   else if (typeof opts === 'number')
      9     opts = { mode: opts, fs }
     10   else if (typeof opts === 'string')
     11     opts = { mode: parseInt(opts, 8), fs }
     12   else
     13     throw new TypeError('invalid options argument')
     14 
     15   opts.mkdir = opts.mkdir || opts.fs.mkdir || fs.mkdir
     16   opts.mkdirAsync = promisify(opts.mkdir)
     17   opts.stat = opts.stat || opts.fs.stat || fs.stat
     18   opts.statAsync = promisify(opts.stat)
     19   opts.statSync = opts.statSync || opts.fs.statSync || fs.statSync
     20   opts.mkdirSync = opts.mkdirSync || opts.fs.mkdirSync || fs.mkdirSync
     21   return opts
     22 }
     23 module.exports = optsArg