-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatFiles.mjs
45 lines (38 loc) · 1.05 KB
/
concatFiles.mjs
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
import { readFile, writeFile } from 'fs';
/**
* ### srcFiles 의 순서를 지키며 모든 파일의 내용을 dest 로 복사
* - 아마도 Promise, Async/Await, Array.reduce 를 사용하지 않는것이 이 문제의 의도에 포함되어있을것 같으니 사용하지 말자.
* @param {string[]} srcFiles
* @param {string} dest
* @param {(err, destData: string) => void} cb
*/
function concatFiles(srcFiles, dest, cb) {
let destData = '';
let index = 0;
next();
function next() {
if (index === srcFiles.length) {
writeFile(dest, destData, (err) => {
if (err) {
return cb(err);
};
});
return cb(null, destData);
};
readFile(srcFiles[index], 'utf8', (err, data) => {
if (err) {
return cb(err);
};
destData += data;
index++;
next();
});
}
}
concatFiles(['./srcFiles/fileA', './srcFiles/fileB', './srcFiles/fileC'], './destFile', (err, destData) => {
if (err) {
return console.log(err);
// throw err;
};
console.log(destData);
});