-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathmodify_filename.js
47 lines (41 loc) · 1.52 KB
master
/
modify_filename.js
File metadata and controls
- Code
- Blame
47 lines (41 loc) · 1.52 KB
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
const fs = require('fs');
const path = require('path');
const directoryPath = path.join(__dirname, '../i18n/zh-CN/docusaurus-plugin-content-docs/version-1.2');
const allNeedReplacePath = [];
function getAllNeedReplacePath(rootPath) {
const statsObj = fs.statSync(rootPath, (err, stats) => {
if (err) {
console.log('err', err);
return;
}
});
if (statsObj.isFile()) {
allNeedReplacePath.push(rootPath);
} else if (statsObj.isDirectory()) {
const files = fs.readdirSync(rootPath);
for (let filename of files) {
const curPath = `${rootPath}/${filename}`;
getAllNeedReplacePath(curPath);
}
}
}
function modifyFileName(rootPath) {
getAllNeedReplacePath(rootPath);
allNeedReplacePath.forEach(file => {
const suffix = file.includes('versioned_docs') ? file.split('versioned_docs')[1] : '';
if ((suffix && suffix.includes('_')) || (!suffix && file.includes('_'))) {
const oldPath = file;
const newPath = suffix
? file.split('versioned_docs')[0] + 'versioned_docs' + suffix.replace(/_/g, '-')
: file.replace(/_/g, '-');
fs.rename(oldPath, newPath, err => {
if (err) {
console.error(`Unable to rename file ${file}:`, err);
} else {
console.log(`File renamed successfully: ${file} -> ${newPath}`);
}
});
}
});
}
modifyFileName(directoryPath);