-
Notifications
You must be signed in to change notification settings - Fork 6
/
project.js
141 lines (129 loc) · 3.74 KB
/
project.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const fs = require("fs/promises");
const { spawnSync } = require("node:child_process");
(async () => {
const [
packageConfig,
appConfig,
cmd,
readme,
] = await Promise.all([
readFile("package.json"),
readFile("src/miui_cleaner_app/project.json"),
readFile("src/miui_cleaner_cmd/main.cmd"),
readFile("README.md"),
]);
const pkgInfo = packageConfig.json;
const appInfo = appConfig.json;
const date = new Date();
let versionCode;
if (process.env.GITHUB_RUN_NUMBER) {
versionCode = process.env.GITHUB_RUN_NUMBER - 0;
} else {
const controller = new AbortController();
versionCode = await Promise.any(
[
"https://cdn.jsdelivr.net/gh/gucong3000/MiuiCleaner/src/miui_cleaner_app/project.json",
"https://raw.fastgit.org/gucong3000/MiuiCleaner/main/src/miui_cleaner_app/project.json",
"https://raw.githubusercontent.com/gucong3000/MiuiCleaner/main/src/miui_cleaner_app/project.json",
].map(async url => {
const res = await fetch(url, { signal: controller.signal });
const data = await res.json();
return data.versionCode + 1;
}),
);
controller.abort();
}
const versionName = [
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
versionCode,
].join(".");
pkgInfo.version = versionName;
appInfo.versionCode = versionCode;
appInfo.versionName = versionName;
appInfo.launchConfig.splashText = pkgInfo.description;
cmd.constents = cmd.constents.replace(
/^title\s+.*$/im,
`title ${appInfo.name} - ${pkgInfo.description}`,
);
const distCmd = fs.writeFile(
"dist/miui_cleaner_cmd/MiuiCleaner.cmd",
spawnSync(
"iconv",
[
"--from-code=utf-8",
"--to-code=gb18030",
],
{
input: cmd.constents.replace(
/^chcp\s+\d+/im, "chcp 936",
).replace(
/^title\s+.*$/im,
`title ${appInfo.name} - ${pkgInfo.description} -v${pkgInfo.version}`,
).replace(/\r?\n/g, "\r\n"),
},
).stdout,
);
updateDoc(readme);
await Promise.all([
appConfig.update(),
packageConfig.update(),
cmd.update(),
readme.update(),
distCmd,
]);
})(
);
async function readFile (path) {
let constents = await fs.readFile(path);
constents = constents.toString("utf-8");
const json = /\.json$/.test(path) && JSON.parse(constents);
const file = {
json,
constents,
update: (...args) => {
let newContents;
if (file.json) {
newContents = JSON.stringify(file.json, 0, "\t");
} else {
newContents = file.constents;
}
const finalNewline = /\.(cmd|bat)$/i.test(path) ? "\r\n" : "\n";
newContents = newContents.replace(/\r?\n/g, finalNewline);
if (constents.trim() !== newContents.trim()) {
return fs.writeFile(path, newContents.trim() + finalNewline, ...args);
}
},
};
return file;
}
// 保持文档的描述和关闭广告的单元测试数据一致
function updateDoc (readme) {
const docResult = [];
const testCase = require("./src/miui_cleaner_app/test/services").testCase;
delete testCase["关于手机"];
delete testCase["开发者选项"];
printTestCase(testCase);
readme.constents = readme.constents.replace(/(#+\s*关闭各应用广告[\s\S]*?<\/summary>[\s\S]*?)-[\s\S]*(<\/details>)/, (s, prefix, suffix) => {
return prefix + docResult.join("\n") + "\n\n" + suffix;
});
function printTestCase (data, deep = 0) {
for (const caseName in data) {
if (data[caseName] === true) {
docResult.push("\t".repeat(deep) + "- " + caseName + ":`打开`");
} else if (data[caseName] === false) {
docResult.push("\t".repeat(deep) + "- " + caseName + ":`关闭`");
} else if (
typeof data[caseName] !== "object" ||
data[caseName] === null ||
(caseName === "广告服务" && !deep)
) {
continue;
} else {
docResult.push("\t".repeat(deep) + "- " + caseName);
printTestCase(data[caseName], deep + 1);
}
}
}
}