-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcatFiles.js
47 lines (43 loc) · 933 Bytes
/
concatFiles.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
const fs = require('fs');
function each(items, iterate, done) {
function nextCb(index) {
if (index === items.length) {
return done();
}
iterate(items[index], (err) => {
if (err) {
return done(err);
}
nextCb(index + 1);
});
}
nextCb(0);
}
function eachFile(files, cb, done) {
each(files, (file, nextCb) => {
fs.readFile(file, 'utf-8', (err, content) => {
if (err) {
return nextCb(err);
}
cb(content, nextCb);
});
}, done );
}
module.exports = function concatFiles(...args) {
const cb = args.pop();
const dest = args.pop();
const files = args;
return fs.open(dest, 'w', (err, fd) => {
if (err) {
return cb(err);
}
eachFile(files, (content, nextCb) => {
fs.appendFile(fd, content, (err) => {
if (err) {
return nextCb(err);
}
return nextCb();
});
}, cb );
});
};