-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
160 lines (152 loc) · 7.41 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import console from "../modules/console.js";
console.info("Initialization done.");
import fs from "node:fs";
import path from "node:path";
import { startGroup, endGroup } from "@actions/core";
import semver from "semver";
import jsonModule from "../modules/jsonModule.js";
import yamlModule from "../modules/yamlModule.js";
import createCommit from "../modules/createCommit.js";
const polyfillGadgetDefinitionPath = "src/gadgets/libPolyfill/definition.yaml";
const polyfillGadgetDefinition = await yamlModule.readFile(polyfillGadgetDefinitionPath);
const getPolyfillGadgetFiles = async () => (await fs.promises.readdir("src/gadgets/libPolyfill/")).filter((file) => path.extname(file) === ".js");
/**
* @type { { TARGET_CHROMIUM_VERSION: string | number, TARGET_ALIASES: string[] } }
*/
const { TARGET_CHROMIUM_VERSION, TARGET_ALIASES } = await yamlModule.readFile("./scripts/generatePolyfill/config.yaml");
const TARGET_VERSION = `${TARGET_CHROMIUM_VERSION}.0.0`;
const { POLYFILL_PATH } = process.env;
if (!POLYFILL_PATH) {
throw new Error("No POLYFILL_PATH env variable.");
}
startGroup("Config:");
console.info("TARGET_CHROMIUM_VERSION:", TARGET_CHROMIUM_VERSION);
console.info("TARGET_ALIASES:", TARGET_ALIASES);
console.info("TARGET_VERSION:", TARGET_VERSION);
console.info("POLYFILL_PATH:", POLYFILL_PATH);
endGroup();
const polyfillGadgetFiles = await getPolyfillGadgetFiles();
const polyfillMainJSONPath = path.join(POLYFILL_PATH, "main.json");
const customPolyfillMainJSONPath = "./scripts/generatePolyfill/customPolyfill/main.json";
const polyfillLibraryPath = path.join(POLYFILL_PATH, "library");
const customPolyfillLibraryPath = "./scripts/generatePolyfill/customPolyfill/library";
console.info("Start to delete old polyfill files:");
for (const file of polyfillGadgetFiles) {
if (file.startsWith("MediaWiki:Gadget-libPolyfill-")) {
continue;
}
console.info("\tDeleteting", file);
await fs.promises.rm(path.join("src/gadgets/libPolyfill/", file), {
force: true,
recursive: true,
});
console.info("\tDeleteting", file, "done.");
}
console.info("Start to read polyfill JSON...");
const polyfillMainJSONArray = [
...Object.entries(await jsonModule.readFile(polyfillMainJSONPath)).map(([id, v]) => ({
id,
...JSON.parse(v),
})),
...Object.entries(await jsonModule.readFile(customPolyfillMainJSONPath)).map(([id, v]) => ({
id,
...v,
})),
];
console.info("Start to merge custom polyfill...");
for (const dir of await fs.promises.readdir(customPolyfillLibraryPath)) {
await fs.promises.cp(path.join(customPolyfillLibraryPath, dir), path.join(polyfillLibraryPath, dir), { recursive: true });
}
const polyfillMainJSON = Object.fromEntries(polyfillMainJSONArray.map((v) => [v.id, v]));
console.info("Get", polyfillMainJSONArray.length, "polyfill entries.");
const polyfillList = polyfillMainJSONArray.filter(({ aliases }) => Array.isArray(aliases) && TARGET_ALIASES.some((targetAliases) => aliases.includes(targetAliases)));
console.info("Get", polyfillList.length, "polyfill entries with aliases.");
const polyfillListAllowed = polyfillList.filter(({ browsers }) => browsers?.chrome && semver.satisfies(TARGET_VERSION, browsers?.chrome));
console.info("Get", polyfillListAllowed.length, "polyfill entries with aliases and target browsers.");
const readPolyfillRawJS = async (dir) => {
const rawJSPath = path.join(dir, "raw.js");
try {
return (await fs.promises.readFile(rawJSPath, { encoding: "utf-8" })).trim().split("\n");
} catch (e) {
console.info("\t[readPolyfillRawJS]", rawJSPath, "not exist, return false:", e);
return false;
}
};
const polyfillAlreadyInjected = {};
const getPolyfillContent = async (polyfill, _rootPolyfillID = false) => {
const rootPolyfillID = _rootPolyfillID || polyfill.id;
if (!Array.isArray(polyfillAlreadyInjected[rootPolyfillID])) {
polyfillAlreadyInjected[rootPolyfillID] = [];
}
if (polyfillAlreadyInjected[rootPolyfillID].includes(polyfill.id)) {
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "Already injected, skip.");
return [];
}
polyfillAlreadyInjected[rootPolyfillID].push(polyfill.id);
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "Processing", polyfill.id);
const content = [
"",
`// Polyfill ${polyfill.id} start`,
"",
];
const detectSource = polyfill.detectSource?.trim();
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "detectSource:", detectSource);
if (detectSource) {
content.push(`if (!(${detectSource})) {`);
}
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "dependencies:", polyfill.dependencies);
if (Array.isArray(polyfill.dependencies)) {
for (const dependency of polyfill.dependencies) {
content.push(...await getPolyfillContent(polyfillMainJSON[dependency], rootPolyfillID));
}
}
const polyfillRawJSFromBaseDir = await readPolyfillRawJS(path.join(polyfillLibraryPath, polyfill.baseDir));
if (Array.isArray(polyfillRawJSFromBaseDir)) {
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "polyfillRawJSFromBaseDir exist.");
content.push(...polyfillRawJSFromBaseDir);
} else {
const polyfillRawJSFromID = await readPolyfillRawJS(path.join(polyfillLibraryPath, polyfill.id));
if (Array.isArray(polyfillRawJSFromID)) {
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "polyfillRawJSFromID exist.");
content.push(...polyfillRawJSFromID);
} else {
throw new Error(`No raw.js found in ${path.join(polyfillLibraryPath, polyfill.baseDir)} or ${path.join(polyfillLibraryPath, polyfill.id)}`);
}
}
if (detectSource) {
content.push("}");
}
content.push(
"",
`// Polyfill ${polyfill.id} end`,
"",
);
console.info("\t[getPolyfillContent]", `[${polyfill.id}@${rootPolyfillID}]`, "Done.");
return content.join("\n").split("\n").map((line) => line.trimEnd().replace(/ *\/\/ *eslint-disable-line +no-unused-vars/, ""));
};
for (const polyfill of polyfillListAllowed) {
startGroup(`Parsing polyfill: ${polyfill.id}`);
const content = [
"\"use strict\";",
"/**",
" * Generated by scripts/generatePolyfill/index.js",
" * Options:",
` * polyfillFeature: ${polyfill.id}`,
` * polyfillAliases: ${Array.isArray(polyfill.aliases) ? polyfill.aliases.join(", ") : null}`,
` * targetChromiumVersion: ${TARGET_VERSION}`,
` * polyfillVersionRange: ${polyfill.browsers?.chrome ? `${semver.validRange(polyfill.browsers.chrome)} (${polyfill.browsers.chrome})` : null}`,
" */",
"(() => {",
...await getPolyfillContent(polyfill),
"})();",
];
const gadgetFilePath = path.join("src/gadgets/libPolyfill/", `MediaWiki:Gadget-libPolyfill-${polyfill.id}.js`);
console.info("Start to write polyfill file:", polyfill.id, "@", gadgetFilePath);
await fs.promises.writeFile(gadgetFilePath, content.join("\n"));
console.info("Done.");
endGroup();
}
polyfillGadgetDefinition._files = await getPolyfillGadgetFiles();
await yamlModule.writeFile(polyfillGadgetDefinitionPath, polyfillGadgetDefinition);
await createCommit("auto: regenerated polyfill files by generatePolyfill");
console.info("Done.");