1 var fs = require('graceful-fs')
2 var path = require('path')
3 var ncp = require('./ncp')
4 var mkdir = require('../mkdirs')
6 function copy (src, dest, options, callback) {
7 if (typeof options === 'function' && !callback) {
10 } else if (typeof options === 'function' || options instanceof RegExp) {
11 options = {filter: options}
13 callback = callback || function () {}
14 options = options || {}
16 // Warn about using preserveTimestamps on 32-bit node:
17 if (options.preserveTimestamps && process.arch === 'ia32') {
18 console.warn('fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n' +
19 'see https://github.com/jprichardson/node-fs-extra/issues/269')
22 // don't allow src and dest to be the same
23 var basePath = process.cwd()
24 var currentPath = path.resolve(basePath, src)
25 var targetPath = path.resolve(basePath, dest)
26 if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
28 fs.lstat(src, function (err, stats) {
29 if (err) return callback(err)
32 if (stats.isDirectory()) {
33 var parts = dest.split(path.sep)
35 dir = parts.join(path.sep)
37 dir = path.dirname(dest)
40 fs.exists(dir, function (dirExists) {
41 if (dirExists) return ncp(src, dest, options, callback)
42 mkdir.mkdirs(dir, function (err) {
43 if (err) return callback(err)
44 ncp(src, dest, options, callback)