-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
111 lines (110 loc) · 4.7 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
import console from "../modules/console.js";
console.info("Initialization done.");
import mkdtmp from "../modules/mkdtmp.js";
import browserify from "browserify";
import minifyStream from "../modules/minify-stream.js";
import yamlModule from "../modules/yamlModule.js";
import fs from "node:fs";
import path from "node:path";
import { startGroup, endGroup, exportVariable } from "@actions/core";
import createCommit from "../modules/createCommit.js";
import exec from "../modules/exec.js";
import modulePath from "../modules/modulePath.js";
/**
* @type {{ module: string; entry: string; gadget: { name: string, fileName: string }; exportValues?: string[], removePlugins?: string[], prependCode?: string }[]}
*/
const browserifyTargets = await yamlModule.readFile("scripts/browserify/targets.yaml");
startGroup("browserifyTargets:");
console.info(browserifyTargets);
endGroup();
const tempPath = await mkdtmp({
local: true,
});
const inputPath = path.join(tempPath, "input.js");
const jsonOutput = await exec("npm ls --json");
/**
* @type {{ [name: string]: { version: string } }}
*/
const localPackageVersions = JSON.parse(jsonOutput).dependencies;
console.info("npm ls:", ["", ...Object.entries(localPackageVersions).map(([name, { version }]) => `* ${name}@${version}`)].join("\n"));
const fileList = [];
for (const browserifyTarget of browserifyTargets) {
console.info("target:", browserifyTarget);
const { module, entry, gadget: { name, fileName }, exportValues, removePlugins, prependCode, namespaceImport } = browserifyTarget;
const file = path.join("src/gadgets", name, fileName);
fileList.push(file);
await fs.promises.rm(inputPath, {
recursive: true,
force: true,
});
const hasExports = Array.isArray(exportValues) && exportValues.length > 0;
const exportReference = namespaceImport ? "* as m" : hasExports ? `{ ${exportValues.join(", ")} }` : "m";
const importReference = namespaceImport ? "m" : hasExports ? `{ ${exportValues.join(", ")} }` : "m";
await fs.promises.writeFile(inputPath, [
`import ${exportReference} from "${module}";`,
`global["${entry}"] = ${importReference};`,
].join("\n"));
const codes = await new Promise((res, rej) => {
console.info(`[${module}]`, "start generating...");
const plugins = new Set([
"esmify",
"common-shakeify",
"browser-pack-flat/plugin",
]);
if (Array.isArray(removePlugins)) {
for (const removePlugin of removePlugins) {
plugins.delete(removePlugin);
}
}
let codeObject = browserify(inputPath).transform("unassertify", { global: true }).transform("envify", { global: true });
for (const plugin of plugins) {
codeObject = codeObject.plugin(plugin);
}
const codeStream = codeObject.bundle().pipe(minifyStream({
mangle: false,
output: {
beautify: true,
width: 1024 * 10,
},
}));
const chunks = [];
codeStream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
codeStream.on("error", (err) => rej(err));
codeStream.on("end", () => res(Buffer.concat(chunks).toString("utf8")));
});
const output = [
"/**",
` * Generated by ${modulePath(import.meta)}`,
" * Options:",
];
for (const [k, v] of Object.entries(browserifyTarget)) {
output.push(` * ${k}: ${JSON.stringify(v, null, 1).replace(/\n */g, " ")}`);
}
output.push(" */");
if (typeof prependCode === "string") {
output.push(prependCode);
}
output.push(codes.trim(), "");
const code = output.join("\n");
if (code === await fs.promises.readFile(file, { encoding: "utf-8" }).catch(() => null)) {
console.info(`[${module}]`, "No change, continue to next one.");
continue;
}
await fs.promises.writeFile(file, code);
if (path.extname(file) === ".js") {
const filename = path.basename(file);
const eslintrcName = path.join(path.dirname(file), ".eslintrc.yaml");
const eslintrc = await yamlModule.readFile(eslintrcName).catch(() => ({}));
if (!Array.isArray(eslintrc.ignorePatterns)) {
eslintrc.ignorePatterns = [];
}
if (!eslintrc.ignorePatterns.includes(filename)) {
eslintrc.ignorePatterns.push(filename);
await yamlModule.writeFile(eslintrcName, eslintrc);
}
}
console.info(`[${module}]`, "generated successfully.");
await createCommit(`auto(Gadget-${name}): bump ${module} to ${localPackageVersions[module].version} by browserify`);
}
exportVariable("linguist-generated-browserify", JSON.stringify(fileList));
console.info("Done.");