-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathutils.ts
More file actions
115 lines (103 loc) · 2.87 KB
/
utils.ts
File metadata and controls
115 lines (103 loc) · 2.87 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
import unified from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import fs from "node:fs/promises";
import type { Root } from "mdast";
// @ts-ignore
import mdastToString from "mdast-util-to-string";
import { getPackages, type Package } from "@manypkg/get-packages";
export const BumpLevels = {
dep: 0,
patch: 1,
minor: 2,
major: 3,
} as const;
export async function getVersionsByDirectory(cwd: string) {
let { packages } = await getPackages(cwd);
return new Map(packages.map((x) => [x.dir, x.packageJson.version]));
}
export async function getChangedPackages(
cwd: string,
previousVersions: Map<string, string>
) {
let { packages } = await getPackages(cwd);
let changedPackages = new Set<Package>();
for (let pkg of packages) {
const previousVersion = previousVersions.get(pkg.dir);
if (previousVersion !== pkg.packageJson.version) {
changedPackages.add(pkg);
}
}
return [...changedPackages];
}
export function getChangelogEntry(changelog: string, version: string) {
let ast = unified().use(remarkParse).parse(changelog) as Root;
let highestLevel: number = BumpLevels.dep;
let nodes = ast.children;
let headingStartInfo:
| {
index: number;
depth: number;
}
| undefined;
let endIndex: number | undefined;
for (let i = 0; i < nodes.length; i++) {
let node = nodes[i];
if (node.type === "heading") {
let stringified: string = mdastToString(node);
let match = stringified.toLowerCase().match(/(major|minor|patch)/);
if (match !== null) {
let level = BumpLevels[match[0] as "major" | "minor" | "patch"];
highestLevel = Math.max(level, highestLevel);
}
if (headingStartInfo === undefined && stringified.startsWith(version)) {
headingStartInfo = {
index: i,
depth: node.depth,
};
continue;
}
if (
endIndex === undefined &&
headingStartInfo !== undefined &&
headingStartInfo.depth === node.depth
) {
endIndex = i;
break;
}
}
}
if (headingStartInfo) {
ast.children = ast.children.slice(headingStartInfo.index + 1, endIndex);
}
return {
content: unified().use(remarkStringify).stringify(ast),
highestLevel: highestLevel,
};
}
export function sortTheThings(
a: { private: boolean; highestLevel: number },
b: { private: boolean; highestLevel: number }
) {
if (a.private === b.private) {
return b.highestLevel - a.highestLevel;
}
if (a.private) {
return 1;
}
return -1;
}
export function isErrorWithCode(err: unknown, code: string) {
return (
typeof err === "object" &&
err !== null &&
"code" in err &&
err.code === code
);
}
export function fileExists(filePath: string) {
return fs.access(filePath, fs.constants.F_OK).then(
() => true,
() => false
);
}