Sha256: 6d65916b7da38af38f23925cadc032fa49ced91dc65bf468c8d28c4817ae3321

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

module.exports = chmodr
chmodr.sync = chmodrSync

var fs = require("fs")
, path = require("path")

function chmodr (p, mode, cb) {
  fs.readdir(p, function (er, children) {
    // any error other than ENOTDIR means it's not readable, or
    // doesn't exist.  give up.
    if (er && er.code !== "ENOTDIR")
      return cb(er)
    var isDir = !er
    var m = isDir ? dirMode(mode) : mode
    if (er || !children.length)
      return fs.chmod(p, m, cb)

    var len = children.length
    var errState = null
    children.forEach(function (child) {
      chmodr(path.resolve(p, child), mode, then)
    })
    function then (er) {
      if (errState) return
      if (er) return cb(errState = er)
      if (-- len === 0) return fs.chmod(p, dirMode(mode), cb)
    }
  })
}

function chmodrSync (p, mode) {
  var children
  try {
    children = fs.readdirSync(p)
  } catch (er) {
    if (er && er.code === "ENOTDIR") return fs.chmodSync(p, mode)
    throw er
  }
  if (!children.length) return fs.chmodSync(p, dirMode(mode))

  children.forEach(function (child) {
    chmodrSync(path.resolve(p, child), mode)
  })
  return fs.chmodSync(p, dirMode(mode))
}

// If a party has r, add x
// so that dirs are listable
function dirMode(mode) {
  if (mode & 0400) mode |= 0100
  if (mode & 040) mode |= 010
  if (mode & 04) mode |= 01
  return mode
}

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
entangled-0.0.16 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
entangled-0.0.15 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
entangled-0.0.14 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
entangled-0.0.13 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
entangled-0.0.12 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
entangled-0.0.11 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
entangled-0.0.10 spec/dummy/public/node_modules/bower/node_modules/chmodr/chmodr.js
embeditor-rails-2.0.0.beta lib/node_modules/npm/node_modules/chmodr/chmodr.js