-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-dirs.js
40 lines (33 loc) · 1.03 KB
/
merge-dirs.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
#!/usr/bin/env node
import fs from 'node:fs'
import path from 'node:path'
function copyFile(file, location) {
fs.mkdirSync(path.dirname(file), {
mode: 0x1ed,
recursive: true,
})
fs.writeFileSync(file, fs.readFileSync(location))
}
function mergeDirs(src, dest, overwrite = true) {
const files = fs.readdirSync(src)
files.forEach((file) => {
const srcFile = path.join(src, file)
const destFile = path.join(dest, file)
const stats = fs.lstatSync(srcFile)
if(stats.isDirectory()) {
mergeDirs(srcFile, destFile)
} else {
if(overwrite || !fs.existsSync(destFile)) {
copyFile(destFile, srcFile)
}
// skip when not overwrite and file exists
}
})
}
const argv = process.argv.slice(2)
const helpString = `Usage: merge-dirs source destination [--overwrite]`
if(argv.length < 2 || argv.includes('--help')) {
console.log(helpString)
process.exit()
}
mergeDirs(argv[0], argv[1], argv[2] === '--overwrite')