-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
188 lines (173 loc) · 6.25 KB
/
utils.ts
File metadata and controls
188 lines (173 loc) · 6.25 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { execSync } from "child_process";
export function coerceArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}
export function isPrerelease(input: { branch: string; preidBranches: string[]; forcePreid?: boolean; forceStable?: boolean }): boolean {
if (input.forceStable) {
return false;
}
if (input.forcePreid) {
return true;
}
return input.preidBranches.includes(input.branch);
}
export interface PreidBranchEntry {
branch: string;
/** When undefined, falls back to the global default preid. */
preid?: string;
}
/**
* Parses entries in the form `"branch"` or `"branch:preid"`.
* e.g. `["main:rc", "develop:dev", "vnext:next", "feature"]`
*/
export function parsePreidBranches(entries: string[]): PreidBranchEntry[] {
return entries.map(entry => {
const colonIdx = entry.indexOf(":");
if (colonIdx === -1) {
return { branch: entry };
}
return { branch: entry.slice(0, colonIdx), preid: entry.slice(colonIdx + 1) };
});
}
/**
* Returns true when the branch matches any of the given regex patterns using RegExp.test.
* Note: patterns are not auto-anchored; use ^ and $ in the pattern if full-string matches are required.
*/
export function matchesBranchPattern(branch: string, patterns: string[]): boolean {
return patterns.some(pattern => new RegExp(pattern).test(branch));
}
/**
* Resolves the preid string for the current branch.
* Returns `null` when the version should be stable.
*
* Resolution order:
* 1. `forceStable` → stable (null)
* 2. `forcePreid` → mapped preid or `defaultPreid`
* 3. Exact match in `preidBranches` → mapped preid or `defaultPreid`
* 4. Matches a `stableBranches` pattern → stable (null)
* 5. Fallback → `defaultPreid` (any other branch is treated as pre-release)
*/
export function resolvePreid(input: {
branch: string;
preidBranches: PreidBranchEntry[];
stableBranches: string[];
defaultPreid: string;
forcePreid?: boolean;
forceStable?: boolean;
}): string | null {
if (input.forceStable) {
return null;
}
if (input.forcePreid) {
const match = input.preidBranches.find(e => e.branch === input.branch);
return match?.preid ?? input.defaultPreid;
}
const match = input.preidBranches.find(e => e.branch === input.branch);
if (match) {
return match.preid ?? input.defaultPreid;
}
if (matchesBranchPattern(input.branch, input.stableBranches)) {
return null;
}
return input.defaultPreid;
}
/**
* Strips any pre-release suffix from a version string.
* e.g. `"1.0.0-rc.0"` → `"1.0.0"`, `"1.0.0"` → `"1.0.0"`.
*/
export function stripPreid(version: string): string {
const idx = version.indexOf("-");
return idx === -1 ? version : version.slice(0, idx);
}
/**
* Parses a stable branch name into a numeric version array for comparison.
* Strips a leading `v` and treats `.x` as a terminal segment (dropped).
* Returns `null` when no numeric version can be parsed.
* e.g. `"v1"` → `[1]`, `"2.x"` → `[2]`, `"v3.1"` → `[3, 1]`, `"main"` → `null`.
*/
export function parseBranchVersion(branch: string): number[] | null {
let normalized = branch.startsWith("v") ? branch.slice(1) : branch;
normalized = normalized.replace(/\.x$/, "");
if (!normalized) {
return null;
}
const parts = normalized.split(".");
if (parts.some(p => p === "" || !/^\d+$/.test(p))) {
return null;
}
return parts.map(Number);
}
function compareVersionArrays(a: number[], b: number[]): number {
const len = Math.max(a.length, b.length);
for (let i = 0; i < len; i++) {
const diff = (a[i] ?? 0) - (b[i] ?? 0);
if (diff !== 0) {
return diff;
}
}
return 0;
}
/**
* Resolves the dist-tag string for the current build.
* - Pre-release builds → returns the `resolvedPreid` value (e.g. `"rc"`, `"dev"`).
* - Stable builds → compares the current branch against all detected stable branches;
* the branch with the highest semver version emits `"latest"`, all others emit `"v{major}-lts"`.
* Falls back to `"latest"` when no branch versions can be parsed.
*/
export function resolveTag(input: { resolvedPreid: string | null; branch: string; stableBranchNames: string[] }): string {
if (input.resolvedPreid !== null) {
return input.resolvedPreid;
}
const versioned = input.stableBranchNames
.map(name => ({ name, version: parseBranchVersion(name) }))
.filter((e): e is { name: string; version: number[] } => e.version !== null);
if (versioned.length === 0) {
return "latest";
}
const highest = versioned.reduce((best, cur) => (compareVersionArrays(cur.version, best.version) > 0 ? cur : best));
const currentVersion = parseBranchVersion(input.branch);
if (currentVersion !== null && compareVersionArrays(currentVersion, highest.version) === 0) {
return "latest";
}
const major = currentVersion?.[0];
return major !== undefined ? `v${major}-lts` : "latest";
}
/**
* Counts commits on HEAD since the last commit that touched `filePath`.
* When `diffPattern` is provided (a regex passed to git's `-G` flag), only commits where the diff
* contains a line matching the pattern are considered — e.g., pass `'"version":'` to reset only
* when the `version` property itself changed, not just when the file was touched.
* Returns 0 when no matching commit is found, when HEAD is that commit, or if git is unavailable.
*/
export function getCommitCountSinceFileChange(
filePath: string,
execFn: (cmd: string) => string = cmd => execSync(cmd, { encoding: "utf8" }),
diffPattern?: string,
): number {
try {
const patternFlag = diffPattern ? ` -G '${diffPattern}'` : "";
const sha = execFn(`git log --follow -n 1 --pretty=format:%H${patternFlag} -- ${filePath}`).trim();
if (!sha) {
return 0;
}
const count = execFn(`git rev-list --count ${sha}..HEAD`).trim();
return parseInt(count, 10) || 0;
} catch {
return 0;
}
}
/**
* Lists all remote branch names from `origin` via `git ls-remote --heads origin`.
* Returns an empty array if git is unavailable or the remote cannot be reached.
*/
export function listRemoteBranchNames(execFn: (cmd: string) => string = cmd => execSync(cmd, { encoding: "utf8" })): string[] {
try {
const output = execFn("git ls-remote --heads origin");
return output
.split("\n")
.map(line => /refs\/heads\/(.+)$/.exec(line)?.[1]?.trim() ?? null)
.filter((name): name is string => name !== null && name.length > 0);
} catch {
return [];
}
}