-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmarkdown.ts
162 lines (140 loc) · 3.63 KB
/
markdown.ts
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
import { abbreviateCommitHash, PackageManager } from "@pkg-pr-new/utils";
import { WorkflowData } from "../types";
const installCommands: Record<PackageManager, string> = {
npm: "npm i",
pnpm: "pnpm add",
yarn: "yarn add",
bun: "bun add",
};
const binCommands: Record<PackageManager, string> = {
npm: "npx",
pnpm: "pnpm dlx",
yarn: "npx",
bun: "bunx",
};
export function generateCommitPublishMessage(
origin: string,
templates: Record<string, string>,
packages: string[],
workflowData: WorkflowData,
compact: boolean,
packageManager: PackageManager,
bin: boolean,
) {
const isMoreThanFour = packages.length > 4;
const shaMessages = packages
.map((packageName) => {
let shaUrl = generatePublishUrl(
"sha",
origin,
packageName,
workflowData,
compact,
);
if (packageManager === "yarn") {
shaUrl = shaUrl + ".tgz";
}
return `
\`\`\`
${bin ? binCommands[packageManager] : installCommands[packageManager]} ${shaUrl}
\`\`\`
`;
})
.map((message, i) =>
isMoreThanFour
? createCollapsibleBlock(`<b>${packages[i]}</b>`, message)
: message,
)
.join("\n");
const templatesStr = generateTemplatesStr(templates);
return `
${templatesStr}
${shaMessages}
`;
}
export function generatePullRequestPublishMessage(
origin: string,
templates: Record<string, string>,
packages: string[],
workflowData: WorkflowData,
compact: boolean,
onlyTemplates: boolean,
checkRunUrl: string,
packageManager: PackageManager,
base: "sha" | "ref",
bin: boolean,
) {
const isMoreThanFour = packages.length > 4;
const refMessages = packages
.map((packageName) => {
let refUrl = generatePublishUrl(
base,
origin,
packageName,
workflowData,
compact,
);
if (packageManager === "yarn") {
refUrl = refUrl + ".tgz";
}
return `
\`\`\`
${bin ? binCommands[packageManager] : installCommands[packageManager]} ${refUrl}
\`\`\`
`;
})
.map((message, i) =>
isMoreThanFour
? createCollapsibleBlock(`<b>${packages[i]}</b>`, message)
: message,
)
.join("\n");
const templatesStr = generateTemplatesStr(templates);
return `
${templatesStr}
${onlyTemplates ? "" : refMessages}
_commit: <a href="${checkRunUrl}"><code>${abbreviateCommitHash(workflowData.sha)}</code></a>_
`;
}
function generateTemplatesStr(templates: Record<string, string>) {
const entries = Object.entries(templates).filter(([k]) => k !== "default");
let str =
entries.length === 0 && templates.default
? `[Open in StackBlitz](${templates.default})`
: "";
if (entries.length > 0 && entries.length <= 2) {
str = [str, ...entries.map(([k, v]) => `[${k}](${v})`)]
.filter(Boolean)
.join(" • ");
} else if (entries.length > 2) {
str += createCollapsibleBlock(
"<b>More templates</b>",
`
${entries.map(([k, v]) => `- [${k}](${v})`).join("\n")}
`,
);
}
return str;
}
export function generatePublishUrl(
base: "sha" | "ref",
origin: string,
packageName: string,
workflowData: WorkflowData,
compact: boolean,
) {
const tag =
base === "sha" ? abbreviateCommitHash(workflowData.sha) : workflowData.ref;
const shorter = workflowData.repo === packageName;
const urlPath = compact
? `/${packageName}@${tag}`
: `/${workflowData.owner}${shorter ? "" : `/${workflowData.repo}`}/${packageName}@${tag}`;
return `${new URL(urlPath, origin)}`;
}
function createCollapsibleBlock(title: string, body: string) {
return `
<details><summary>${title}</summary><p>
${body}
</p></details>
`;
}