Skip to content

Commit

Permalink
refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed May 25, 2018
1 parent 4b4f3cf commit 456241a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/copy-sync/copy-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ function copyLink (resolvedSrc, dest) {
function isSrcSubdir (src, dest) {
const srcArray = path.resolve(src).split(path.sep)
const destArray = path.resolve(dest).split(path.sep)

return srcArray.reduce((acc, current, i) => {
return acc && destArray[i] === current
}, true)
return srcArray.reduce((acc, current, i) => acc && destArray[i] === current, true)
}

function checkStats (src, dest) {
Expand All @@ -184,10 +181,10 @@ function checkStats (src, dest) {

function checkPaths (src, dest) {
const {srcStat, destStat} = checkStats(src, dest)
if (srcStat && destStat && destStat !== notExist && srcStat.ino === destStat.ino) {
if (destStat.ino && destStat.ino === srcStat.ino) {
throw new Error('Source and destination must not be the same.')
}
if (srcStat && srcStat.isDirectory() && isSrcSubdir(src, dest)) {
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
throw new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`)
}
return destStat
Expand Down
9 changes: 3 additions & 6 deletions lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,7 @@ function copyLink (resolvedSrc, dest, cb) {
function isSrcSubdir (src, dest) {
const srcArray = path.resolve(src).split(path.sep)
const destArray = path.resolve(dest).split(path.sep)

return srcArray.reduce((acc, current, i) => {
return acc && destArray[i] === current
}, true)
return srcArray.reduce((acc, current, i) => acc && destArray[i] === current, true)
}

function checkStats (src, dest, cb) {
Expand All @@ -236,10 +233,10 @@ function checkPaths (src, dest, cb) {
checkStats(src, dest, (err, stats) => {
if (err) return cb(err)
const {srcStat, destStat} = stats
if (srcStat && destStat && destStat !== notExist && srcStat.ino === destStat.ino) {
if (destStat.ino && destStat.ino === srcStat.ino) {
return cb(new Error('Source and destination must not be the same.'))
}
if (srcStat && srcStat.isDirectory() && isSrcSubdir(src, dest)) {
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
return cb(new Error(`Cannot copy '${src}' to a subdirectory of itself, '${dest}'.`))
}
return cb(null, destStat)
Expand Down

0 comments on commit 456241a

Please sign in to comment.