-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (33 loc) · 1.12 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
const fs = require('fs');
const getBuffersForAllFilesInDirectory = (dir) => {
const buffers = fs.readdirSync(dir).map(file => fs.readFileSync(`${dir}/${file}`)).reduce((acc, buffer) => {
acc.push(buffer);
return acc;
}, []);
return buffers;
};
const calculateBufferLength = (buffers) => {
return buffers.reduce((acc, buffer) => {
acc += buffer.length;
return acc;
}, 0);
}
const fuseFiles = (inputFilesPath, outputFilePath) => {
try {
let buffers = [];
for (const inputFilePath of inputFilesPath) {
const stats = fs.statSync(inputFilePath);
if (stats.isDirectory()) {
buffers = buffers.concat(getBuffersForAllFilesInDirectory(inputFilePath));
} else {
buffers.push(fs.readFileSync(inputFilePath));
}
}
const bufferLength = calculateBufferLength(buffers);
const concatenatedBuffer = Buffer.concat(buffers, bufferLength);
fs.writeFileSync(outputFilePath, concatenatedBuffer);
} catch(error) {
throw error;
}
}
module.exports = fuseFiles;