Browse Source

lib/_copy: started cleaning up

ci/travis-osximage
JP Richardson 10 years ago
parent
commit
fe8633199e
  1. 72
      lib/_copy.js

72
lib/_copy.js

@ -4,10 +4,8 @@ var fs = require('graceful-fs')
var path = require('path')
function ncp (source, dest, options, callback) {
var cback = callback
if (!callback) {
cback = options
callback = options
options = {}
}
@ -22,9 +20,7 @@ function ncp (source, dest, options, callback) {
var errs = null
var started = 0
var finished = 0
var running = 0
var started = 0, finished = 0, running = 0
// this is pretty useless now that we're using graceful-fs
// consider removing
var limit = options.limit || 512
@ -36,11 +32,11 @@ function ncp (source, dest, options, callback) {
if (filter) {
if (filter instanceof RegExp) {
if (!filter.test(source)) {
return cb(true)
return doneOne(true)
}
} else if (typeof filter === 'function') {
if (!filter(source)) {
return cb(true)
return doneOne(true)
}
}
}
@ -57,16 +53,15 @@ function ncp (source, dest, options, callback) {
}
running++
stat(source, function (err, stats) {
var item = {}
if (err) {
return onError(err)
}
if (err) return onError(err)
// We need to get the mode from the stats object and preserve it.
item.name = source
item.mode = stats.mode
item.mtime = stats.mtime // modified time
item.atime = stats.atime // access time
var item = {
name: source,
mode: stats.mode,
mtime: stats.mtime, // modified time
atime: stats.atime // access time
}
if (stats.isDirectory()) {
return onDir(item)
@ -90,7 +85,7 @@ function ncp (source, dest, options, callback) {
copyFile(file, target)
})
} else {
cb()
doneOne()
}
}
})
@ -114,15 +109,13 @@ function ncp (source, dest, options, callback) {
// presumably old node then
var eventName = global.setImmediate ? 'finish' : 'close'
writeStream.once(eventName, function () {
cb()
doneOne()
})
}
function rmFile (file, done) {
fs.unlink(file, function (err) {
if (err) {
return onError(err)
}
if (err) return onError(err)
return done()
})
}
@ -139,31 +132,25 @@ function ncp (source, dest, options, callback) {
function mkDir (dir, target) {
fs.mkdir(target, dir.mode, function (err) {
if (err) {
return onError(err)
}
if (err) return onError(err)
copyDir(dir.name)
})
}
function copyDir (dir) {
fs.readdir(dir, function (err, items) {
if (err) {
return onError(err)
}
if (err) return onError(err)
items.forEach(function (item) {
startCopy(path.join(dir, item))
})
return cb()
return doneOne()
})
}
function onLink (link) {
var target = link.replace(currentPath, targetPath)
fs.readlink(link, function (err, resolvedPath) {
if (err) {
return onError(err)
}
if (err) return onError(err)
checkLink(resolvedPath, target)
})
}
@ -177,14 +164,13 @@ function ncp (source, dest, options, callback) {
return makeLink(resolvedPath, target)
}
fs.readlink(target, function (err, targetDest) {
if (err) {
return onError(err)
}
if (err) return onError(err)
if (dereference) {
targetDest = path.resolve(basePath, targetDest)
}
if (targetDest === resolvedPath) {
return cb()
return doneOne()
}
return rmFile(target, function () {
makeLink(resolvedPath, target)
@ -195,10 +181,8 @@ function ncp (source, dest, options, callback) {
function makeLink (linkPath, target) {
fs.symlink(linkPath, target, function (err) {
if (err) {
return onError(err)
}
return cb()
if (err) return onError(err)
return doneOne()
})
}
@ -214,7 +198,7 @@ function ncp (source, dest, options, callback) {
function onError (err) {
if (options.stopOnError) {
return cback(err)
return callback(err)
} else if (!errs && options.errs) {
errs = fs.createWriteStream(options.errs)
} else if (!errs) {
@ -225,15 +209,15 @@ function ncp (source, dest, options, callback) {
} else {
errs.write(err.stack + '\n\n')
}
return cb()
return doneOne()
}
function cb (skipped) {
function doneOne (skipped) {
if (!skipped) running--
finished++
if ((started === finished) && (running === 0)) {
if (cback !== undefined) {
return errs ? cback(errs) : cback(null)
if (callback !== undefined) {
return errs ? callback(errs) : callback(null)
}
}
}

Loading…
Cancel
Save