-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
140 lines (120 loc) · 3.58 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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var Transform = require('stream').Transform || require('readable-stream').Transform;
function Appender(sources, options) {
options = options || {};
Transform.call(this);
var self = this;
this._nbSkip = parseInt(options.skip) || 0;
this._inline = options.inline;
this._verbose = options.verbose;
this._files = [];
this._next = 0;
this._skippedLines = 0;
var filter;
if (options.filter) {
if (options.filter instanceof RegExp) { filter = options.filter; }
else { filter = new RegExp(options.filter); }
}
sources.forEach(function (source) {
if (!fs.existsSync(source)) { return; }
var stat = fs.statSync(source);
if (stat.isDirectory()) {
fs.readdirSync(source).forEach(function (file) {
var filepath = path.join(source, file);
if (fs.statSync(filepath).isFile()) {
if (filter && !filter.test(file)) { return; }
self._files.push(filepath);
}
});
} else {
if (filter && !filter.test(source)) { return; }
self._files.push(source);
}
});
if (options.sort) {
switch (options.sort.toLowerCase()) {
case 'asc':
// Sort files by names from Z to A (then we pop them from A to Z)
self._files.sort(function (a, b) {
if (!options['case-sensitive']) {
a = a.toLowerCase();
b = b.toLowerCase();
}
if (path.basename(a) < path.basename(b)) { return -1; }
if (path.basename(a) > path.basename(b)) { return 1; }
return 0;
});
break;
case 'desc':
// Sort files by names from A to Z (then we pop them from Z to A)
self._files.sort(function (a, b) {
if (!options['case-sensitive']) {
a = a.toLowerCase();
b = b.toLowerCase();
}
if (path.basename(a) > path.basename(b)) { return -1; }
if (path.basename(a) < path.basename(b)) { return 1; }
return 0;
});
break;
}
}
if (options.list) {
this._files.forEach(function (file) { self.push(file + '\n'); });
this.end();
return;
}
this.appendNextFile();
}
util.inherits(Appender, Transform);
module.exports = Appender;
Appender.prototype._transform = function (chunk, encoding, done) {
var data = chunk.toString();
if (this._skippedLines >= this._nbSkip) {
this.push(data);
} else {
this.push(this.checkLineBreaks(data));
}
done();
};
/**
* Remove line breaks in a string until we reach the asked number of skipped lines
* Returns the resulting string
*/
Appender.prototype.checkLineBreaks = function (data) {
var index = data.indexOf('\n');
if (index != -1) {
++this._skippedLines;
if (this._verbose) { console.error('Skipping line ' + this._skippedLines); }
data = data.substr(++index);
if (this._skippedLines >= this._nbSkip) {
return data;
} else {
return this.checkLineBreaks(data);
}
} else if (this._skippedLines < this._nbSkip) {
return '';
}
return data;
};
Appender.prototype.appendNextFile = function () {
var self = this;
var file = this._files[this._next++];
if (!file) {
if (this._verbose) { console.error('Done'); }
this.end();
return;
}
if (this._verbose) { console.error('Appending: ' + file); }
var stream = fs.createReadStream(file);
stream.on('end', function () {
self._skippedLines = 0;
if (!self._inline) { self.push('\n'); }
self.appendNextFile();
});
stream.pipe(this, { end: false });
};