-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathpack.js
More file actions
156 lines (134 loc) · 5.34 KB
/
Copy pathpack.js
File metadata and controls
156 lines (134 loc) · 5.34 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* global process */
import { promises as fs } from "fs";
import { createWriteStream } from "fs";
import JSZip from "jszip";
import ChromeExtension from "crx";
import { execSync } from "child_process";
import manifest from "../src/manifest.json" with { type: "json" };
import packageInfo from "../package.json" with { type: "json" };
import semver from "semver";
import { toChromeVersion } from "./version.js";
// ============================================================================
// 目前 ScriptCat MV3 未正式支持 Firefox,
// 测试人员可修改 PACK_FIREFOX 为 true 作个人测试用途
const PACK_FIREFOX = false;
// ============================================================================
const createJSZip = () => {
const currDate = new Date();
const dateWithOffset = new Date(currDate.getTime() - currDate.getTimezoneOffset() * 60000);
// replace the default date with dateWithOffset
JSZip.defaults.date = dateWithOffset;
return new JSZip();
};
// 判断是否为beta版本
const version = semver.parse(packageInfo.version);
manifest.version = toChromeVersion(packageInfo.version);
if (version.prerelease.length) {
manifest.name = `__MSG_scriptcat_beta__`;
} else {
manifest.name = `__MSG_scriptcat__`;
}
// 处理manifest version
let str = (await fs.readFile("./src/manifest.json", { encoding: "utf8" })).toString();
str = str.replace(/"version": "(.*?)"/, `"version": "${manifest.version}"`);
await fs.writeFile("./src/manifest.json", str);
// 处理configSystem version
let configSystem = (await fs.readFile("./src/app/const.ts", { encoding: "utf8" })).toString();
// 如果是由github action的分支触发的构建,在版本中再加上commit id
if (process.env.GITHUB_REF_TYPE === "branch") {
configSystem = configSystem.replace(
"ExtVersion = version;",
`ExtVersion = \`\${version}+${process.env.GITHUB_SHA.substring(0, 7)}\`;`
);
await fs.writeFile("./src/app/const.ts", configSystem);
}
execSync("npm run build", { stdio: "inherit" });
// logo 在 rspack.config.ts 处理
// 处理firefox和chrome的zip压缩包
// 浅拷贝防止后续修改
const firefoxManifest = { ...manifest, background: { ...manifest.background } };
const chromeManifest = { ...manifest, background: { ...manifest.background } };
chromeManifest.optional_permissions = chromeManifest.optional_permissions.filter((val) => val !== "userScripts");
delete chromeManifest.background.scripts;
// Firefox MV3 不支持 "background" permission
firefoxManifest.optional_permissions = firefoxManifest.optional_permissions.filter((val) => val !== "background");
delete firefoxManifest.background.service_worker;
delete firefoxManifest.sandbox;
firefoxManifest.browser_specific_settings = {
gecko: {
id: `{${
version.prerelease.length ? "44ab8538-2642-46b0-8a57-3942dbc1a33b" : "8e515334-52b5-4cc5-b4e8-675d50af677d"
}}`,
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/userScripts#browser_compatibility
// Firefox 136 (Released 2025-03-04)
strict_min_version: "136.0",
data_collection_permissions: {
required: [
"none", // 没有必须传送至第三方的资料。安装转页没有记录用户何时何地安装了什么。
],
optional: [
"authenticationInfo", // 使用 Cloud Backup / Import 时,有传送用户的资料至第三方作登入验证
"personallyIdentifyingInfo", // 使用 电邮 或 帐密 让第三方识别个人身份进行 Cloud Backup / Import
],
},
},
};
// 为 Firefox 添加激活工具栏按钮的快捷键
firefoxManifest.commands = {
// mv3 的工具栏快捷键为 `_execute_action`,mv2 则是 `_execute_browser_action`
_execute_action: {},
};
const chrome = createJSZip();
const firefox = createJSZip();
async function addDir(zip, localDir, toDir, filters) {
const sub = async (localDir, toDir) => {
const files = await fs.readdir(localDir);
for (const file of files) {
if (filters?.includes(file)) {
continue;
}
const localPath = `${localDir}/${file}`;
const toPath = `${toDir}${file}`;
const stats = await fs.stat(localPath);
if (stats.isDirectory()) {
await sub(localPath, `${toPath}/`);
} else {
zip.file(toPath, await fs.readFile(localPath));
}
}
};
await sub(localDir, toDir);
}
chrome.file("manifest.json", JSON.stringify(chromeManifest));
firefox.file("manifest.json", JSON.stringify(firefoxManifest));
await Promise.all([
addDir(chrome, "./dist/ext", "", ["manifest.json"]),
addDir(firefox, "./dist/ext", "", ["manifest.json"]),
]);
// 导出zip包
chrome
.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
compression: "DEFLATE",
})
.pipe(createWriteStream(`./dist/${packageInfo.name}-v${packageInfo.version}-chrome.zip`));
PACK_FIREFOX &&
firefox
.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
compression: "DEFLATE",
})
.pipe(createWriteStream(`./dist/${packageInfo.name}-v${packageInfo.version}-firefox.zip`));
// 处理crx
const crx = new ChromeExtension({
privateKey: await fs.readFile("./dist/scriptcat.pem", { encoding: "utf8" }),
});
await crx
.load("./dist/ext")
.then((crxFile) => crxFile.pack())
.then((crxBuffer) => fs.writeFile(`./dist/${packageInfo.name}-v${packageInfo.version}-chrome.crx`, crxBuffer))
.catch((err) => {
console.error(err);
});