-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-src.js
54 lines (51 loc) · 1.78 KB
/
copy-src.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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const mkdir = require('mkdirp');
const readDir = dir => fs.promises.readdir(path.join(__dirname, dir), {
withFileTypes: true
});
const copyIfNewer = async (src, dest) => {
if (!fs.existsSync(dest)) {
console.debug(new Date().toLocaleString(), 'copy', src, dest);
return fs.promises.copyFile(src, dest);
}
let srcStat, destStat;
srcStat = await fs.promises.stat(src);
try {
destStat = await fs.promises.stat(dest);
} catch {
console.debug(new Date().toLocaleString(), 'copy', src, dest);
return fs.promises.copyFile(src, dest);
}
if (destStat && srcStat.mtimeMs <= destStat.mtimeMs) {
return;
}
console.debug(new Date().toLocaleString(), 'copy', src, dest);
return fs.promises.copyFile(src, dest);
};
(async () => {
let dir = [''], queue = [], pending = [];
queue.push(readDir('src'));
let currentDir, currentContent, nextDir, srcFile, destFile;
while (dir.length) {
currentDir = dir.shift();
currentContent = await queue.shift();
for (let dirent of currentContent) {
if (dirent.isDirectory()) {
nextDir = path.join(currentDir, dirent.name);
dir.push(nextDir);
queue.push(readDir(path.join('src', nextDir)));
} else if (dirent.isFile() && !dirent.name.endsWith('.ts')) {
if (!fs.existsSync(nextDir = path.join(__dirname, 'lib', currentDir))) {
console.debug(new Date().toLocaleString(), 'mkdir', nextDir);
mkdir.mkdirpSync(nextDir);
}
srcFile = path.join(__dirname, 'src', currentDir, dirent.name);
destFile = path.join(nextDir, dirent.name);
pending.push(copyIfNewer(srcFile, destFile));
}
}
}
return Promise.all(pending);
})();