-
Notifications
You must be signed in to change notification settings - Fork 774
/
copy.js
91 lines (77 loc) · 2.3 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var fs = require('fs')
var path = require('path')
var ncp = require('ncp').ncp
var mkdir = require('./mkdir')
var create = require('./create')
var BUF_LENGTH = 64 * 1024
var _buff = new Buffer(BUF_LENGTH)
var copyFileSync = function(srcFile, destFile) {
var fdr = fs.openSync(srcFile, 'r')
var stat = fs.fstatSync(fdr)
var fdw = fs.openSync(destFile, 'w', stat.mode)
var bytesRead = 1
var pos = 0
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
fs.writeSync(fdw, _buff, 0, bytesRead)
pos += bytesRead
}
fs.closeSync(fdr)
fs.closeSync(fdw)
}
function copy(src, dest, filter, callback) {
if( typeof filter == "function" && !callback) {
callback = filter
filter = null
}
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, {filter: filter}, callback)
mkdir.mkdirs(dir, function(err) {
if (err) return callback(err)
ncp(src, dest, {filter: filter}, callback)
})
})
})
}
function copySync(src, dest, filter, recursive) {
filter = filter || function() { return true }
recursive = recursive || false
var stats = recursive ? fs.lstatSync(src) : fs.statSync(src)
var destFolder = path.dirname(dest)
var destFolderExists = fs.existsSync(destFolder)
var performCopy = false
if (stats.isFile()) {
if (filter instanceof RegExp) performCopy = filter.test(src)
else if (typeof filter == "function") performCopy = filter(src)
if (performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
copyFileSync(src, dest)
}
}
else if (stats.isDirectory()) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
var contents = fs.readdirSync(src)
contents.forEach(function(content) {
copySync(path.join(src, content), path.join(dest, content), filter, true)
})
}
else if (recursive && stats.isSymbolicLink()) {
var srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
}
module.exports = {
copy: copy,
copySync: copySync
}