copy-sync.js
1.62 KB
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
38
39
40
41
42
43
44
45
46
47
var fs = require('graceful-fs')
var path = require('path')
var copyFileSync = require('./copy-file-sync')
var mkdir = require('../mkdirs')
function copySync (src, dest, options) {
if (typeof options === 'function' || options instanceof RegExp) {
options = {filter: options}
}
options = options || {}
options.recursive = !!options.recursive
// default to true for now
options.clobber = 'clobber' in options ? !!options.clobber : true
options.preserveTimestamps = 'preserveTimestamps' in options ? !!options.preserveTimestamps : false
options.filter = options.filter || function () { return true }
var stats = options.recursive ? fs.lstatSync(src) : fs.statSync(src)
var destFolder = path.dirname(dest)
var destFolderExists = fs.existsSync(destFolder)
var performCopy = false
if (stats.isFile()) {
if (options.filter instanceof RegExp) performCopy = options.filter.test(src)
else if (typeof options.filter === 'function') performCopy = options.filter(src)
if (performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest, {clobber: options.clobber, preserveTimestamps: options.preserveTimestamps})
}
} else if (stats.isDirectory()) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
var contents = fs.readdirSync(src)
contents.forEach(function (content) {
var opts = options
opts.recursive = true
copySync(path.join(src, content), path.join(dest, content), opts)
})
} else if (options.recursive && stats.isSymbolicLink()) {
var srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
}
module.exports = copySync