-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrename.js
39 lines (31 loc) · 967 Bytes
/
rename.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
import { resolve } from "path";
import { promises } from "fs";
const { readdir, rename } = promises;
const DIRECTORY = "src/lib/sprites/objects";
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const files = [];
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isFile()) {
files.push(res);
}
}
return files.reverse();
}
const files = await getFiles(DIRECTORY);
for (let idx = 0; idx < files.length; idx++) {
const originalFilePath = files[idx];
/** @type {string[]} */
const parts = originalFilePath.split(".");
const pathParts = parts[0].split("/");
/**
* @type {string}
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const fileName = pathParts.pop()?.replace("tile", "");
parts[0] = `${pathParts.join("/")}/${parseInt(fileName) + 1}`;
const newFilePath = parts.join(".");
await rename(originalFilePath, newFilePath);
}