Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / 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   // don't allow src and dest to be the same
17   var basePath = process.cwd()
18   var currentPath = path.resolve(basePath, src)
19   var targetPath = path.resolve(basePath, dest)
20   if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
21
22   fs.lstat(src, function (err, stats) {
23     if (err) return callback(err)
24
25     var dir = null
26     if (stats.isDirectory()) {
27       var parts = dest.split(path.sep)
28       parts.pop()
29       dir = parts.join(path.sep)
30     } else {
31       dir = path.dirname(dest)
32     }
33
34     fs.exists(dir, function (dirExists) {
35       if (dirExists) return ncp(src, dest, options, callback)
36       mkdir.mkdirs(dir, function (err) {
37         if (err) return callback(err)
38         ncp(src, dest, options, callback)
39       })
40     })
41   })
42 }
43
44 module.exports = copy