-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
55 lines (42 loc) · 1.47 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
54
55
import { execSync } from "child_process";
import { createHash } from "crypto";
import { readFileSync } from "fs";
import { globSync } from "glob";
import { resolve } from "path";
import { replaceInFileSync } from "replace-in-file";
const level = process.argv[2] || "patch";
execSync("git stash --include-untracked --quiet");
const version = execSync(`npm version ${level} --no-git-tag-version`)
.toString()
.trim();
console.log(`Building ${version}`);
console.log();
const assets = globSync("docs/**/*.{css,js}", { withFileTypes: true });
updateVersions(assets);
execSync(`git commit --all --message="Build ${version}"`);
execSync(`git tag --force ${version}`);
try {
execSync("git stash pop --index --quiet");
} catch {}
function updateVersions(assets) {
const results = assets.map((asset) => updateVersion(asset));
const anyHasChanged = results.some((result) => result.hasChanged);
if (anyHasChanged) {
updateVersions(assets);
}
}
function updateVersion(asset) {
const { parentPath, name } = asset;
const nameForRegex = name.replace(".", "\\.");
const regex = new RegExp(`(?<=${nameForRegex}\\?v=)[0-9a-f]+`, "g");
const path = resolve(parentPath, name);
const data = readFileSync(path);
const hash = createHash("md5").update(data).digest("hex").substring(0, 8);
const results = replaceInFileSync({
files: "docs/**/*.{html,js}",
from: regex,
to: hash,
});
const hasChanged = results.some((result) => result.hasChanged);
return { hasChanged };
}