Skip to content

Commit cdd4ce7

Browse files
committed
use modern JavaScript
1 parent d548650 commit cdd4ce7

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

chownr.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
module.exports = chownr
2-
chownr.sync = chownrSync
1+
'use strict'
2+
const fs = require('fs')
3+
const path = require('path')
34

4-
var fs = require("fs")
5-
, path = require("path")
6-
7-
function chownr (p, uid, gid, cb) {
8-
fs.readdir(p, function (er, children) {
5+
const chownr = (p, uid, gid, cb) => {
6+
fs.readdir(p, (er, children) => {
97
// any error other than ENOTDIR means it's not readable, or
108
// doesn't exist. give up.
11-
if (er && er.code !== "ENOTDIR") return cb(er)
9+
if (er && er.code !== 'ENOTDIR') return cb(er)
1210
if (er || !children.length) return fs.chown(p, uid, gid, cb)
1311

14-
var len = children.length
15-
, errState = null
16-
children.forEach(function (child) {
17-
var pathChild = path.resolve(p, child);
18-
fs.lstat(pathChild, function(er, stats) {
12+
let len = children.length
13+
let errState = null
14+
const then = er => {
15+
if (errState) return
16+
if (er) return cb(errState = er)
17+
if (-- len === 0) return fs.chown(p, uid, gid, cb)
18+
}
19+
20+
children.forEach(child => {
21+
const pathChild = path.resolve(p, child)
22+
fs.lstat(pathChild, (er, stats) => {
1923
if (er)
2024
return cb(er)
2125
if (!stats.isSymbolicLink())
@@ -24,29 +28,28 @@ function chownr (p, uid, gid, cb) {
2428
then()
2529
})
2630
})
27-
function then (er) {
28-
if (errState) return
29-
if (er) return cb(errState = er)
30-
if (-- len === 0) return fs.chown(p, uid, gid, cb)
31-
}
3231
})
3332
}
3433

35-
function chownrSync (p, uid, gid) {
36-
var children
34+
const chownrSync = (p, uid, gid) => {
35+
let children
3736
try {
3837
children = fs.readdirSync(p)
3938
} catch (er) {
40-
if (er && er.code === "ENOTDIR") return fs.chownSync(p, uid, gid)
39+
if (er && er.code === 'ENOTDIR') return fs.chownSync(p, uid, gid)
4140
throw er
4241
}
4342
if (!children.length) return fs.chownSync(p, uid, gid)
4443

45-
children.forEach(function (child) {
46-
var pathChild = path.resolve(p, child)
47-
var stats = fs.lstatSync(pathChild)
44+
children.forEach(child => {
45+
const pathChild = path.resolve(p, child)
46+
const stats = fs.lstatSync(pathChild)
4847
if (!stats.isSymbolicLink())
4948
chownrSync(pathChild, uid, gid)
5049
})
50+
5151
return fs.chownSync(p, uid, gid)
5252
}
53+
54+
module.exports = chownr
55+
chownr.sync = chownrSync

0 commit comments

Comments
 (0)