-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
53 lines (45 loc) · 1.95 KB
/
build.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
import fs from "node:fs/promises";
import {createHash} from "node:crypto";
import {rollup} from "rollup";
import {nodeResolve} from "@rollup/plugin-node-resolve";
const bundle = await rollup({
input: "./src/main.js",
plugins: [nodeResolve()]
});
const bundles = await bundle.generate({});
const code = bundles.output[0].code;
const hash = createHash("sha1").update(code).digest("hex").slice(0, 4);
const version = await bumpSemVerIfHashChanged(hash);
console.log(version);
const banner = `// ==UserScript==
// @name HrefTaker
// @version ${version}
// @namespace gh.alttiri
// @description URL grabber popup
// @license GPL-3.0
// @homepageURL https://github.com/AlttiRi/href-taker
// @supportURL https://github.com/AlttiRi/href-taker/issues
// @downloadURL https://github.com/AlttiRi/href-taker/raw/master/dist/href-taker.user.js
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_addElement
// @noframes
// ==/UserScript==\n\n\n`;
await fs.stat("./dist").catch(() => fs.mkdir("./dist"));
await fs.writeFile("./dist/href-taker.user.js", banner + code, {});
async function bumpSemVerIfHashChanged(hash) {
const packageJsonText = await fs.readFile("./package.json", {encoding: "utf8"});
const packageJson = JSON.parse(packageJsonText);
const {
major = "", minor = "", patch = "", other = ""
} = packageJson.version.match(/^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?<other>.*)$/)?.groups || {};
if (!packageJson.version.includes(hash)) {
const date = new Date();
const dateText = `${date.getUTCFullYear()}.${date.getUTCMonth() + 1}.${date.getUTCDate()}`;
const newVersion = `${[major, minor, Number(patch) + 1].join(".")}-${dateText}${hash ? "-" + hash : ""}`;
const newPackageJsonText = packageJsonText.replace(`"version": "${packageJson.version}"`, `"version": "${newVersion}"`);
await fs.writeFile("./package.json", newPackageJsonText);
return newVersion;
}
return packageJson.version;
}