-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
155 lines (124 loc) · 3.5 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* archiver-utils
*
* Copyright (c) 2015 Chris Talkington.
* Licensed under the MIT license.
* https://github.com/archiverjs/archiver-utils/blob/master/LICENSE
*/
var fs = require('graceful-fs');
var path = require('path');
var isStream = require('is-stream');
var lazystream = require('lazystream');
var normalizePath = require('normalize-path');
var defaults = require('lodash/defaults');
var Stream = require('stream').Stream;
var PassThrough = require('readable-stream').PassThrough;
var utils = module.exports = {};
utils.file = require('./file.js');
utils.collectStream = function(source, callback) {
var collection = [];
var size = 0;
source.on('error', callback);
source.on('data', function(chunk) {
collection.push(chunk);
size += chunk.length;
});
source.on('end', function() {
var buf = Buffer.alloc(size);
var offset = 0;
collection.forEach(function(data) {
data.copy(buf, offset);
offset += data.length;
});
callback(null, buf);
});
};
utils.dateify = function(dateish) {
dateish = dateish || new Date();
if (dateish instanceof Date) {
dateish = dateish;
} else if (typeof dateish === 'string') {
dateish = new Date(dateish);
} else {
dateish = new Date();
}
return dateish;
};
// this is slightly different from lodash version
utils.defaults = function(object, source, guard) {
var args = arguments;
args[0] = args[0] || {};
return defaults(...args);
};
utils.isStream = function(source) {
return isStream(source);
};
utils.lazyReadStream = function(filepath) {
return new lazystream.Readable(function() {
return fs.createReadStream(filepath);
});
};
utils.normalizeInputSource = function(source) {
if (source === null) {
return Buffer.alloc(0);
} else if (typeof source === 'string') {
return Buffer.from(source);
} else if (utils.isStream(source)) {
// Always pipe through a PassThrough stream to guarantee pausing the stream if it's already flowing,
// since it will only be processed in a (distant) future iteration of the event loop, and will lose
// data if already flowing now.
return source.pipe(new PassThrough());
}
return source;
};
utils.sanitizePath = function(filepath) {
return normalizePath(filepath, false).replace(/^\w+:/, '').replace(/^(\.\.\/|\/)+/, '');
};
utils.trailingSlashIt = function(str) {
return str.slice(-1) !== '/' ? str + '/' : str;
};
utils.unixifyPath = function(filepath) {
return normalizePath(filepath, false).replace(/^\w+:/, '');
};
utils.walkdir = function(dirpath, base, callback) {
var results = [];
if (typeof base === 'function') {
callback = base;
base = dirpath;
}
fs.readdir(dirpath, function(err, list) {
var i = 0;
var file;
var filepath;
if (err) {
return callback(err);
}
(function next() {
file = list[i++];
if (!file) {
return callback(null, results);
}
filepath = path.join(dirpath, file);
fs.stat(filepath, function(err, stats) {
results.push({
path: filepath,
relative: path.relative(base, filepath).replace(/\\/g, '/'),
stats: stats
});
if (stats && stats.isDirectory()) {
utils.walkdir(filepath, base, function(err, res) {
if(err){
return callback(err);
}
res.forEach(function(dirEntry) {
results.push(dirEntry);
});
next();
});
} else {
next();
}
});
})();
});
};