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

Loading…
Cancel
Save