-
Notifications
You must be signed in to change notification settings - Fork 6
/
massRename.ts
37 lines (32 loc) · 1.26 KB
/
massRename.ts
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
import * as fs from 'fs';
import * as process from 'process';
console.log(`
This function is for interal use only.
Don't use it if you don't know what you're doing!
Press Ctrl + C to exit.
`);
const onFileReadFunction = (filepath): string => {
return `${filepath}-scarf.png`;
};
const targetDir = process.argv[2] ? process.argv[2] : './src/assets/icons/scarf/';
const __onFileRead__ = process.argv[3] ? process.argv[3] : onFileReadFunction;
function readFiles(dirname: string, onFileRead: any = __onFileRead__) {
fs.readdir(dirname, (err, filenames) => {
if (err) throw err;
filenames.forEach((filename) => {
const __path__ = dirname + filename;
fs.readFile(__path__, 'utf-8', (err, _) => {
if (err) throw err;
const pathStart = filename.split('.')[0];
if (pathStart === '') throw err;
fs.rename(__path__, onFileReadFunction(dirname + pathStart), (err) => {
if (err) throw err;
console.log(
`Renamed ${filename} to ${onFileReadFunction(dirname + pathStart)}`,
);
});
});
});
});
}
setTimeout(() => readFiles(targetDir), 3000);