-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 1.16 KB
/
index.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
#!/usr/bin/env node
const fs = require ('fs');
const path = require ('path');
const pinyin = require ('pinyin');
const _ = require ('pumelo');
function renameFile (dir) {
const files = fs.readdirSync (dir);
for (let index = 0; index < files.length; index++) {
let item = files[index];
let fullPath = path.join (dir, item);
let stat = fs.statSync (fullPath);
if (stat.isDirectory ()) {
renameFile (path.join (dir, item));
} else {
let lastIndex = item.lastIndexOf ('.');
if (lastIndex === -1) {
continue;
}
let file_name = item.slice (0, lastIndex);
let ext = item.slice (lastIndex);
let arr = pinyin (file_name, {
style: pinyin.STYLE_NORMAL,
heteronym: false,
});
let new_file_name = _.flat () (arr).join ('_').replace (' ', '_');
try {
fs.renameSync (fullPath, path.join (dir, new_file_name + ext));
} catch (err) {
throw err;
}
}
}
}
const main = () => {
if (process.argv.length < 3) {
console.error('缺少文件夹参数');
return void 0;
}
let dir = process.argv[2];
renameFile (dir);
};
(() => {
main ();
}) ();