-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
210 lines (189 loc) · 7.06 KB
/
index.js
File metadata and controls
210 lines (189 loc) · 7.06 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const path = require("path");
const fs = require("fs");
function parseRepo(repo) {
if (!repo) return null;
if (typeof repo === "object" && repo.url) repo = repo.url;
if (typeof repo !== "string") return null;
// strip git+ prefix and .git suffix
repo = repo.replace(/^git\+/, "").replace(/\.git$/, "");
const m = repo.match(/github\.com[:\/](.+?)\/?$/i);
if (!m) return null;
return m[1]; // owner/repo
}
module.exports = ({ transform, options, settings = {} }) => {
const defaultOptions = {
style: null,
ciWorkflow: "ci.yml",
ciBranch: "main",
excludeBadges: [],
// collapse: true,
// collapseLabel: "More badges",
// collapseVisible: 3,
};
const globalOptions =
(settings.transformDefaults && settings.transformDefaults[transform]) || {};
const opts = { ...defaultOptions, ...globalOptions, ...options };
const style = opts.style ? `?style=${encodeURIComponent(opts.style)}` : "";
// For URLs that already have a query param (eg ?branch=main) append with &
const styleAmp = opts.style ? `&style=${encodeURIComponent(opts.style)}` : "";
const pkgPath = path.join(process.cwd(), "package.json");
// const pkgPath = path.join(__dirname, "package.json");
if (!fs.existsSync(pkgPath)) return "";
const pkg = require(pkgPath);
const allBadges = [];
const name = pkg.name;
// helper to add named badges
const pushBadge = (key, md) => {
if (opts.excludeBadges && opts.excludeBadges.includes(key)) {
return; // Skip this badge if it's in the exclude list
}
allBadges.push({ key, md });
};
if (name) {
pushBadge(
"npmVersion",
`[}.svg${style})](https://www.npmjs.com/package/${encodeURIComponent(
name
)})`
);
pushBadge(
"npmDownloads",
`[}.svg${style})](https://www.npmjs.com/package/${encodeURIComponent(
name
)})`
);
}
if (pkg.license) {
pushBadge(
"license",
`[}-blue.svg${style})](https://www.npmjs.com/package/${encodeURIComponent(
name
)})`
);
}
// Maintained badge based on commit activity (yearly activity)
// Uses shields endpoint for commit-activity (y: yearly), which supports style
// Example: https://img.shields.io/github/commit-activity/y/owner/repo?style=flat
// We'll render as 'maintained' with value 'active' (visual indicator)
// The actual numeric activity isn't available via this shield, but the badge indicates activity.
// We'll add the badge unconditionally when repository is present.
const ownerRepo = parseRepo(pkg.repository);
if (ownerRepo) {
const parts = ownerRepo.split("/");
const owner = parts[0];
const repoName = parts[1];
const workflowFile = opts.ciWorkflow ?? pkg.ciWorkflow ?? "ci.yml";
const workflowBranch = opts.ciBranch ?? pkg.ciBranch ?? "main";
pushBadge(
"actions",
`[](https://github.com/${ownerRepo}/actions)`
);
pushBadge(
"codecov",
`[](https://codecov.io/gh/${ownerRepo})`
);
pushBadge(
"release",
`[](https://github.com/${ownerRepo}/releases)`
);
// Commit activity / maintained badge (yearly commits)
pushBadge(
"maintained",
`[](https://github.com/${ownerRepo}/graphs/commit-activity)`
);
pushBadge(
"stars",
`[](https://github.com/${ownerRepo}/stargazers)`
);
pushBadge(
"forks",
`[](https://github.com/${ownerRepo}/network/members)`
);
pushBadge(
"watchers",
`[](https://github.com/${ownerRepo}/watchers)`
);
pushBadge(
"lastCommit",
`[](https://github.com/${ownerRepo}/commits)`
);
pushBadge(
"contributors",
`[](https://github.com/${ownerRepo}/graphs/contributors)`
);
pushBadge(
"issues",
`[](https://github.com/${ownerRepo}/issues)`
);
pushBadge(
"pulls",
`[](https://github.com/${ownerRepo}/pulls)`
);
pushBadge(
"repoSize",
`[](https://github.com/${ownerRepo})`
);
pushBadge(
"topLanguage",
`[](https://github.com/${ownerRepo})`
);
pushBadge(
"languages",
`[](https://github.com/${ownerRepo}/search?l=)`
);
}
// Collapse logic
const collapse = opts.collapse === true || String(opts.collapse) === "true";
const collapseLabel = opts.collapseLabel || "More badges";
const collapseVisible = Number(opts.collapseVisible) || 3;
const preferredOrder = ["npmVersion", "actions", "license", "maintained"];
const visible = [];
const hidden = [];
// map for quick lookup
const byKey = allBadges.reduce((map, b) => {
map[b.key] = b.md;
return map;
}, {});
// add preferred first
for (const k of preferredOrder) {
if (byKey[k]) {
visible.push(byKey[k]);
delete byKey[k];
}
if (visible.length >= collapseVisible) break;
}
// fill remaining visible up to collapseVisible
if (visible.length < collapseVisible) {
for (const b of allBadges) {
if (visible.length >= collapseVisible) break;
if (!byKey[b.key]) continue; // already added
visible.push(b.md);
delete byKey[b.key];
}
}
// remaining go to hidden
for (const k of Object.keys(byKey)) {
hidden.push(byKey[k]);
}
// If collapse not requested or nothing hidden, return all inline, with a trailing space
if (!collapse || hidden.length === 0) {
return allBadges.map((b) => b.md).join(" ") + " ";
}
// Build collapsed output
const visibleStr = visible.join(" ");
const hiddenStr = hidden.join(" ");
return (
visibleStr +
"\n\n<details>\n<summary>" +
collapseLabel +
"</summary>\n\n" +
hiddenStr +
"\n\n</details>"
);
};