chownr
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -10,2 +10,3 @@ 'use strict' | ||
/* istanbul ignore next */ | ||
const needEISDIRHandled = fs.lchown && | ||
@@ -15,3 +16,22 @@ !process.version.match(/v1[1-9]+\./) && | ||
const lchownSync = (path, uid, gid) => { | ||
try { | ||
return fs[LCHOWNSYNC](path, uid, gid) | ||
} catch (er) { | ||
if (er.code !== 'ENOENT') | ||
throw er | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
const chownSync = (path, uid, gid) => { | ||
try { | ||
return fs.chownSync(path, uid, gid) | ||
} catch (er) { | ||
if (er.code !== 'ENOENT') | ||
throw er | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
const handleEISDIR = | ||
@@ -33,10 +53,10 @@ needEISDIRHandled ? (path, uid, gid, cb) => er => { | ||
try { | ||
return fs[LCHOWNSYNC](path, uid, gid) | ||
return lchownSync(path, uid, gid) | ||
} catch (er) { | ||
if (er.code !== 'EISDIR') | ||
throw er | ||
fs.chownSync(path, uid, gid) | ||
chownSync(path, uid, gid) | ||
} | ||
} | ||
: (path, uid, gid) => fs[LCHOWNSYNC](path, uid, gid) | ||
: (path, uid, gid) => lchownSync(path, uid, gid) | ||
@@ -51,7 +71,15 @@ // fs.readdir could only accept an options object as of node v6 | ||
const chown = (cpath, uid, gid, cb) => { | ||
fs[LCHOWN](cpath, uid, gid, handleEISDIR(cpath, uid, gid, er => { | ||
// Skip ENOENT error | ||
cb(er && er.code !== 'ENOENT' ? er : null) | ||
})) | ||
} | ||
const chownrKid = (p, child, uid, gid, cb) => { | ||
if (typeof child === 'string') | ||
return fs.lstat(path.resolve(p, child), (er, stats) => { | ||
// Skip ENOENT error | ||
if (er) | ||
return cb(er) | ||
return cb(er.code !== 'ENOENT' ? er : null) | ||
stats.name = child | ||
@@ -66,7 +94,7 @@ chownrKid(p, stats, uid, gid, cb) | ||
const cpath = path.resolve(p, child.name) | ||
fs[LCHOWN](cpath, uid, gid, handleEISDIR(cpath, uid, gid, cb)) | ||
chown(cpath, uid, gid, cb) | ||
}) | ||
} else { | ||
const cpath = path.resolve(p, child.name) | ||
fs[LCHOWN](cpath, uid, gid, handleEISDIR(cpath, uid, gid, cb)) | ||
chown(cpath, uid, gid, cb) | ||
} | ||
@@ -80,6 +108,10 @@ } | ||
// or doesn't exist. give up. | ||
if (er && er.code !== 'ENOTDIR' && er.code !== 'ENOTSUP') | ||
return cb(er) | ||
if (er) { | ||
if (er.code === 'ENOENT') | ||
return cb() | ||
else if (er.code !== 'ENOTDIR' && er.code !== 'ENOTSUP') | ||
return cb(er) | ||
} | ||
if (er || !children.length) | ||
return fs[LCHOWN](p, uid, gid, handleEISDIR(p, uid, gid, cb)) | ||
return chown(p, uid, gid, cb) | ||
@@ -94,3 +126,3 @@ let len = children.length | ||
if (-- len === 0) | ||
return fs[LCHOWN](p, uid, gid, handleEISDIR(p, uid, gid, cb)) | ||
return chown(p, uid, gid, cb) | ||
} | ||
@@ -104,5 +136,12 @@ | ||
if (typeof child === 'string') { | ||
const stats = fs.lstatSync(path.resolve(p, child)) | ||
stats.name = child | ||
child = stats | ||
try { | ||
const stats = fs.lstatSync(path.resolve(p, child)) | ||
stats.name = child | ||
child = stats | ||
} catch (er) { | ||
if (er.code === 'ENOENT') | ||
return | ||
else | ||
throw er | ||
} | ||
} | ||
@@ -121,8 +160,11 @@ | ||
} catch (er) { | ||
if (er && er.code === 'ENOTDIR' && er.code !== 'ENOTSUP') | ||
if (er.code === 'ENOENT') | ||
return | ||
else if (er.code === 'ENOTDIR' || er.code === 'ENOTSUP') | ||
return handleEISDirSync(p, uid, gid) | ||
throw er | ||
else | ||
throw er | ||
} | ||
if (children.length) | ||
if (children && children.length) | ||
children.forEach(child => chownrKidSync(p, child, uid, gid)) | ||
@@ -129,0 +171,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "like `chown -R`", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"repository": { | ||
@@ -17,12 +17,15 @@ "type": "git", | ||
"mkdirp": "0.3", | ||
"rimraf": "", | ||
"tap": "^12.0.1" | ||
"rimraf": "^2.7.1", | ||
"tap": "^14.10.6" | ||
}, | ||
"tap": { | ||
"check-coverage": true | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js --cov", | ||
"test": "tap", | ||
"preversion": "npm test", | ||
"postversion": "npm publish", | ||
"postpublish": "git push origin --follow-tags" | ||
"prepublishOnly": "git push origin --follow-tags" | ||
}, | ||
"license": "ISC" | ||
} |
5709
146