vue3 SCF项目组件自动添加组件名称,方便动态缓存组件

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//  vue組件自動添加 name
const fs = require("fs");

const list = [];
function cpSync(SOURCE, lvl = 1) {
// 读取源文件夹
let rd = fs.readdirSync(SOURCE);

for (const fd of rd) {
const sourceFullName = SOURCE + "/" + fd;

// 读取路径代表的信息
let lstatRes = fs.lstatSync(sourceFullName);
// // 文件:读取内容判断
if (lstatRes.isFile() && lvl > 1) {
if (!sourceFullName.endsWith(".vue")) {
continue;
// fs.writeFileSync(sourceFullName, TEMP_CONTENT);
}

const TEXT = fs.readFileSync(sourceFullName, "utf8");
let TEMP_CONTENT = TEXT;

if (/<script.*name=".*?>/.test(TEMP_CONTENT)) {
console.log("已存在", sourceFullName);
continue;
}

const componentName = sourceFullName
.replace(".vue", "")
.split("/views/")[1]
.split(/[/-]/)
.map(word => word.slice(0, 1).toLocaleUpperCase() + word.slice(1))
.join("");

if (list.includes(componentName)) console.log("------重复---------", componentName);

list.push(componentName);

TEMP_CONTENT = TEMP_CONTENT.replace("<script ", '<script name="' + componentName + '" ');

if (/<script.*name=".*?>/.test(TEMP_CONTENT)) {
console.log("已添加", sourceFullName);
fs.writeFileSync(sourceFullName, TEMP_CONTENT);
}
}
// 文件夹:递归
if (lstatRes.isDirectory()) {
cpSync(sourceFullName, lvl + 1);
}
}
}

// 获取目标地址(解析命令参数)
const args = process.argv.slice(2);

if (args.length == 0) {
console.warn("Please enter the full address after this command, for example:");
console.warn("node v2name D:/project/my-project/src/views");
} else {
const address = args[0];
if (address.includes("/")) {
cpSync(address);

// only for zheng local file
} else if (address == "all") {
cpSync("D:/project/my-project/src/views");
} else if (address == "pay") {
cpSync("D:/project/my-project/src/views/pay");
} else if (address == "base") {
cpSync("D:/project/my-project/src/views/base");
} else if (address == "business") {
cpSync("D:/project/my-project/src/views/business");
} else {
// error
console.warn("Please enter the full address after this command, for example:");
console.warn("node v2name D:/project/my-project/src/views");
}
}



编辑文章✏