-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathversion.js
More file actions
41 lines (36 loc) · 1000 Bytes
/
version.js
File metadata and controls
41 lines (36 loc) · 1000 Bytes
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
import path from "node:path";
import fs from "node:fs";
import child_process from "node:child_process";
export function getVersion(cwd = process.cwd()) {
const packageJsonFile = path.join(cwd, "package.json");
const packageJsonContent = fs.readFileSync(packageJsonFile, {
encoding: "utf-8"
});
const packageJson = JSON.parse(packageJsonContent);
const [major, minor, patch] = packageJson.version.split(".");
const branch = child_process
.execSync("git rev-parse --abbrev-ref HEAD", {
cwd
})
.toString()
.trim();
const build = child_process
.execSync(`git rev-list --count ${branch}`, {
cwd
})
.toString()
.trim();
const hash = child_process
.execSync("git rev-parse HEAD", { cwd })
.toString()
.trim();
return {
major,
minor,
patch,
build,
branch,
hash
};
}
export default getVersion();