Skip to content

Commit bbbc2db

Browse files
manidlouRyanZim
authored andcommitted
copy*(): refactor copyDirItems() to avoid passing null param (#570)
1 parent b02eea6 commit bbbc2db

2 files changed

Lines changed: 19 additions & 25 deletions

File tree

lib/copy-sync/copy-sync.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ function copySync (src, dest, opts) {
3333
}
3434

3535
function startCopy (resolvedDest, src, dest, opts) {
36-
// resovledDest is only truthy in the first call of startCopy.
37-
// when copying directory items, startCopy is called recursively and
38-
// resolvedDest is null, so we need to check paths in that case.
39-
if (resolvedDest) return resumeCopy(resolvedDest, src, dest, opts)
40-
const resolvedDestNested = checkPaths(src, dest)
41-
return resumeCopy(resolvedDestNested, src, dest, opts)
42-
}
43-
44-
function resumeCopy (resolvedDest, src, dest, opts) {
4536
if (opts.filter && !opts.filter(src, dest)) return
4637
return getStats(resolvedDest, src, dest, opts)
4738
}
@@ -133,9 +124,14 @@ function mkDirAndCopy (srcStat, src, dest, opts) {
133124
}
134125

135126
function copyDir (src, dest, opts) {
136-
fs.readdirSync(src).forEach(item => {
137-
startCopy(null, path.join(src, item), path.join(dest, item), opts)
138-
})
127+
fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
128+
}
129+
130+
function copyDirItem (item, src, dest, opts) {
131+
const srcItem = path.join(src, item)
132+
const destItem = path.join(dest, item)
133+
const resolvedDest = checkPaths(srcItem, destItem)
134+
return startCopy(resolvedDest, srcItem, destItem, opts)
139135
}
140136

141137
function onLink (resolvedDest, src, dest, opts) {

lib/copy/copy.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ function checkParentDir (resolvedDest, src, dest, opts, cb) {
4949
}
5050

5151
function startCopy (resolvedDest, src, dest, opts, cb) {
52-
// resovledDest is only truthy in the first call of startCopy.
53-
// when copying directory items, startCopy is called recursively and
54-
// resolvedDest is null, so we need to check paths in that case.
55-
if (resolvedDest) return resumeCopy(resolvedDest, src, dest, opts, cb)
56-
return checkPaths(src, dest, (err, resolvedDest) => {
57-
if (err) return cb(err)
58-
return resumeCopy(resolvedDest, src, dest, opts, cb)
59-
})
60-
}
61-
62-
function resumeCopy (resolvedDest, src, dest, opts, cb) {
6352
if (opts.filter) return handleFilter(getStats, resolvedDest, src, dest, opts, cb)
6453
return getStats(resolvedDest, src, dest, opts, cb)
6554
}
@@ -179,9 +168,18 @@ function copyDir (src, dest, opts, cb) {
179168
function copyDirItems (items, src, dest, opts, cb) {
180169
const item = items.pop()
181170
if (!item) return cb()
182-
startCopy(null, path.join(src, item), path.join(dest, item), opts, err => {
171+
return copyDirItem(items, item, src, dest, opts, cb)
172+
}
173+
174+
function copyDirItem (items, item, src, dest, opts, cb) {
175+
const srcItem = path.join(src, item)
176+
const destItem = path.join(dest, item)
177+
checkPaths(srcItem, destItem, (err, resolvedDest) => {
183178
if (err) return cb(err)
184-
return copyDirItems(items, src, dest, opts, cb)
179+
startCopy(resolvedDest, srcItem, destItem, opts, err => {
180+
if (err) return cb(err)
181+
return copyDirItems(items, src, dest, opts, cb)
182+
})
185183
})
186184
}
187185

0 commit comments

Comments
 (0)