Skip to content

Commit

Permalink
refactor move, remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed Feb 6, 2018
1 parent 77ee239 commit 7149691
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions lib/move/index.js
@@ -1,15 +1,11 @@
'use strict'

// most of this code was written by Andrew Kelley
// licensed under the BSD license: see
// https://github.com/andrewrk/node-mv/blob/master/package.json

const u = require('universalify').fromCallback
const fs = require('graceful-fs')
const path = require('path')
const copy = require('../copy').copy
const remove = require('../remove').remove
const mkdirp = require('../mkdirs').mkdirs
const mkdirp = require('../mkdirs').mkdirp

const destIsFile = Symbol('destIsFile')
const destIsDir = Symbol('destIsDir')
Expand All @@ -24,26 +20,22 @@ function move (src, dest, opts, cb) {

src = path.resolve(src)
dest = path.resolve(dest)

if (src === dest) return fs.access(src, cb)

fs.stat(src, (err, st) => {
if (err) return cb(err)
if (st.isDirectory()) {
if (isSrcSubdir(src, dest)) return cb(new Error(`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`))
checkDest(dest, (err, res) => {
if (err) return cb(err)
if (res === destIsFile) return cb(new Error(`Cannot move directory '${src}' to file '${dest}'.`))
if (!overwrite && res) return cb(new Error('dest already exists.'))
return doRename(src, dest, overwrite, cb)
})
} else {
checkDest(dest, (err, res) => {
if (err) return cb(err)
if (res === destIsDir) return cb(new Error(`Cannot move file '${src}' to directory '${dest}'.`))
if (!overwrite && res) return cb(new Error('dest already exists.'))
return doRename(src, dest, overwrite, cb)
})
}

const srcIsDir = st.isDirectory()
if (srcIsDir && isSrcSubdir(src, dest)) return cb(new Error(`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`))

checkDest(dest, (err, res) => {
if (err) return cb(err)
if (!overwrite && res) return cb(new Error('dest already exists.'))
if (srcIsDir && res === destIsFile) return cb(new Error(`Cannot move directory '${src}' to file '${dest}'.`))
if (!srcIsDir && res === destIsDir) return cb(new Error(`Cannot move file '${src}' to directory '${dest}'.`))
return doRename(src, dest, overwrite, cb)
})
})
}

Expand Down Expand Up @@ -96,8 +88,7 @@ function checkDest (dest, cb) {
if (err.code === 'ENOENT') return cb()
return cb(err)
}
if (st.isDirectory()) return cb(null, destIsDir)
return cb(null, destIsFile)
return cb(null, st.isDirectory() ? destIsDir : destIsFile)
})
}

Expand Down

0 comments on commit 7149691

Please sign in to comment.