-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.js
45 lines (36 loc) · 1.2 KB
/
cover.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
const fs = require("fs");
const path = require("path");
const basePath = process.argv[2];
if (!basePath) {
console.error("Needs base path.");
process.exit(1);
}
const pattern = /(\d+)\.png$/i;
const files = fs.readdirSync(basePath)
.filter(file => pattern.test(file))
.map(file => path.join(basePath, file))
.sort();
if (files.length === 0) {
console.info("No matching files found.");
process.exit(0);
}
const oldLastFile = files[files.length - 1];
const match = oldLastFile.match(pattern);
let newLastSeq = parseInt(match[1]) + 1;
const digits = newLastSeq.toString().length;
let newLastFile = "";
// move everybody one up
for (const file of files.reverse()) {
const oldSeq = parseInt(file.match(pattern)[1]);
const newSeq = oldSeq + 1;
const newFile = oldLastFile.replace(pattern, newSeq.toString().padStart(digits, "0") + ".png");
console.info(file, newFile);
fs.renameSync(file, newFile);
if (newLastFile.length === 0) {
newLastFile = newFile;
}
}
// copy last to position 0
const firstFile = newLastFile.replace(pattern, "0".toString().padStart(digits, "0") + ".png");
console.info(newLastFile, firstFile);
fs.copyFileSync(newLastFile, firstFile);