forked from FLB-Music/FLB-Music-Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob.js
41 lines (38 loc) · 1.13 KB
/
glob.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
import path from 'path';
import glob from 'glob';
import consola from 'consola';
import fs from 'fs/promises';
const oldDirs = [];
glob(`./src/renderer/**/*`, {}, (err, files) => {
files.forEach(file => {
if (file.indexOf('App.vue') === -1) {
rename(file).catch(consola.error);
}
});
});
async function rename (file) {
const { dir: oldDir } = path.parse(file);
oldDirs.push(oldDir);
const newPath = file.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
const { dir: newDir } = path.parse(newPath);
await fs.mkdir(newDir, {
recursive: true
});
consola.success(`created: ${newDir}`);
await fs.rename(file, newPath);
consola.success(`renamed: ${file} > ${newPath}`);
await truncate(newPath);
}
async function truncate (file) {
if (file.endsWith('.vue')) {
const { name } = path.parse(file);
let buffer = await fs.readFile(file, 'utf8');
buffer = buffer.replace(/name:(?:\s'(.+)'),\n/g, '\n');
buffer = buffer.replace(
'export default {',
`export default {\n\tname: '${name}',`
);
await fs.writeFile(file, buffer, 'utf8');
consola.success(`modified: ${file}`);
}
}