This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupdate-beta-version.mjs
50 lines (45 loc) · 1.58 KB
/
update-beta-version.mjs
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
import { readFile, writeFile, readdir, stat } from "node:fs/promises";
import { createHash } from "node:crypto";
import path from "node:path";
async function computeMetaHash(folder, inputHash = null) {
const hash = inputHash ? inputHash : createHash("sha256");
const info = await readdir(folder, { withFileTypes: true });
// construct a string from the modification date, the filename and the filesize
for (let item of info) {
const fullPath = path.join(folder, item.name);
if (item.isFile()) {
const statInfo = await stat(fullPath);
// compute hash string name:size:mtime
const fileInfo = `${fullPath}:${statInfo.size}:${statInfo.mtimeMs}`;
hash.update(fileInfo);
} else if (item.isDirectory()) {
// recursively walk sub-folders
await computeMetaHash(fullPath, hash);
}
}
// if not being called recursively, get the digest and return it as the hash result
if (!inputHash) {
return hash.digest();
}
}
async function updateVersion() {
const packageJson = JSON.parse(
await readFile(
path.join(process.cwd(), "packages", "cli", "package.json"),
"utf8"
)
);
const hash = await computeMetaHash(
path.join(process.cwd(), "packages", "cli", "src")
);
// Convert the hash to a hex string
const hashString = hash.toString("hex");
// Update the version
packageJson.version = `${packageJson.version}-next-${hashString}`;
console.log(`${packageJson.version}`);
await writeFile(
path.join(process.cwd(), "packages", "cli", "package.json"),
JSON.stringify(packageJson, null, 2)
);
}
updateVersion();