Security update for permissions_by_term
[yaffs-website] / node_modules / fs-extra / lib / copy / copy.js
1 var fs = require('graceful-fs')
2 var path = require('path')
3 var ncp = require('./ncp')
4 var mkdir = require('../mkdirs')
5
6 function copy (src, dest, options, callback) {
7   if (typeof options === 'function' && !callback) {
8     callback = options
9     options = {}
10   } else if (typeof options === 'function' || options instanceof RegExp) {
11     options = {filter: options}
12   }
13   callback = callback || function () {}
14   options = options || {}
15
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')
20   }
21
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.'))
27
28   fs.lstat(src, function (err, stats) {
29     if (err) return callback(err)
30
31     var dir = null
32     if (stats.isDirectory()) {
33       var parts = dest.split(path.sep)
34       parts.pop()
35       dir = parts.join(path.sep)
36     } else {
37       dir = path.dirname(dest)
38     }
39
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)
45       })
46     })
47   })
48 }
49
50 module.exports = copy