-
Notifications
You must be signed in to change notification settings - Fork 771
/
copy.js
37 lines (32 loc) · 948 Bytes
/
copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var fs = require('graceful-fs')
var path = require('path')
var ncp = require('./ncp')
var mkdir = require('../mkdirs')
function copy (src, dest, options, callback) {
if (typeof options === 'function' && !callback) {
callback = options
options = {}
} else if (typeof options === 'function' || options instanceof RegExp) {
options = {filter: options}
}
callback = callback || function () {}
fs.lstat(src, function (err, stats) {
if (err) return callback(err)
var dir = null
if (stats.isDirectory()) {
var parts = dest.split(path.sep)
parts.pop()
dir = parts.join(path.sep)
} else {
dir = path.dirname(dest)
}
fs.exists(dir, function (dirExists) {
if (dirExists) return ncp(src, dest, options, callback)
mkdir.mkdirs(dir, function (err) {
if (err) return callback(err)
ncp(src, dest, options, callback)
})
})
})
}
module.exports = copy