forked from calvinmetcalf/copyfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·120 lines (120 loc) · 2.83 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
var path = require('path');
var fs = require('fs');
var glob = require('glob');
var mkdirp = require('mkdirp');
var through = require('through2').obj;
var noms = require('noms').obj;
function toStream(array) {
var length = array.length;
var i = 0;
return noms(function (done) {
if (i >= length) {
this.push(null);
}
this.push(array[i++]);
done();
});
}
function depth(string) {
return path.normalize(string).split(path.sep).length - 1;
}
function dealWith(inPath, up) {
if (!up) {
return inPath;
}
if (up === true) {
return path.basename(inPath);
}
if (depth(inPath) < up) {
throw new Error('cant go up that far');
}
return path.join.apply(path, path.normalize(inPath).split(path.sep).slice(up));
}
module.exports = copyFiles;
function copyFiles(args, config, callback) {
if (typeof config === 'function') {
callback = config;
config = {
up:0
};
}
if (typeof config !== 'object' && config) {
config = {
up: config
};
}
var opts = config.up || 0;
var soft = config.soft;
if (typeof callback !== 'function') {
throw new Error('callback is not optional');
}
var input = args.slice();
var outDir = input.pop();
var globOpts = {};
if (config.exclude) {
globOpts.ignore = config.exclude;
}
if (config.all) {
globOpts.dot = true;
}
toStream(input)
.pipe(through(function (pathName, _, next) {
var self = this;
glob(pathName, globOpts, function (err, paths) {
if (err) {
return next(err);
}
paths.forEach(function (unglobbedPath) {
self.push(unglobbedPath);
});
next();
});
}))
.pipe(through(function (pathName, _, next) {
fs.stat(pathName, function (err, pathStat) {
if (err) {
return next(err);
}
var outName = path.join(outDir, dealWith(pathName, opts));
function done(){
mkdirp(path.dirname(outName), function (err) {
if (err) {
return next(err);
}
next(null, pathName);
});
}
if (pathStat.isDirectory()) {
return next();
}
if (!pathStat.isFile()) {
return next(new Error('how can it be neither file nor folder?'))
}
if (!soft) {
return done();
}
fs.stat(outName, function(err){
if(!err){
//file exists
return next()
}
if (err.code === 'ENOENT') {
//file does not exist
return done();
}
// other error
return next(err)
})
});
}))
.pipe(through(function (pathName, _, next) {
var outName = path.join(outDir, dealWith(pathName, opts));
fs.createReadStream(pathName)
.pipe(fs.createWriteStream(outName))
.on('error', next)
.on('finish', next);
}))
.on('error', callback)
.on('finish', callback);
}