Skip to content

Commit b9f059a

Browse files
committed
chore(wheelhouse): cascade template@4f14ec07c
Auto-applied by socket-wheelhouse sync-scaffolding into socket-cli. 4 file(s) touched: - .config/fleet/pnpm-workspace.fleet.yaml - scripts/fleet/_shared/github-raw-url.mts - scripts/fleet/gen/coverage-badge.mts - scripts/fleet/lib/coverage-badge.mts
1 parent 1f75e50 commit b9f059a

4 files changed

Lines changed: 63 additions & 17 deletions

File tree

.config/fleet/pnpm-workspace.fleet.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/fleet/_shared/github-raw-url.mts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,37 @@ export function repoGitHubSlug(repoRoot: string): string | undefined {
106106
return parseGitHubSlug(readRepositoryField(repoRoot))
107107
}
108108

109+
/**
110+
* Whether this package's README ever reaches a registry page. Only a published
111+
* package needs the absolute URL: a `private: true` package is never uploaded,
112+
* so its README is read on GitHub alone, where a relative path resolves for
113+
* anyone who can see the repo. The absolute form is actively WORSE there — a
114+
* private repo's `raw.githubusercontent.com` URL is not served anonymously, so
115+
* GitHub's image proxy gets a 404 and renders the badge broken for everyone.
116+
* Read from the manifest rather than the repo's GitHub visibility so the answer
117+
* needs no network call and is the same locally and in CI.
118+
*/
119+
export function isPublishedPackage(repoRoot: string): boolean {
120+
const pkgPath = path.join(repoRoot, 'package.json')
121+
if (!existsSync(pkgPath)) {
122+
// An absent or unreadable manifest is not evidence of a private package.
123+
// Answering "published" keeps the caller on the absolute-url path, where an
124+
// unresolvable slug is a hard stop; answering "private" would hand back a
125+
// relative path and quietly reship the broken registry image.
126+
return true
127+
}
128+
let parsed: unknown
129+
try {
130+
parsed = JSON.parse(readFileSync(pkgPath, 'utf8'))
131+
} catch {
132+
return true
133+
}
134+
if (typeof parsed !== 'object' || parsed === null) {
135+
return true
136+
}
137+
return (parsed as Record<string, unknown>)['private'] !== true
138+
}
139+
109140
/**
110141
* The four-ingredient error a generator prints when [`repoGitHubSlug`] comes
111142
* back undefined. Falling back to a relative path here would reintroduce the

scripts/fleet/gen/coverage-badge.mts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
} from '../lib/coverage-badge.mts'
3737
import { REPO_ROOT } from '../paths.mts'
3838
import {
39+
isPublishedPackage,
3940
missingGitHubSlugMessage,
4041
repoGitHubSlug,
4142
} from '../_shared/github-raw-url.mts'
@@ -79,16 +80,22 @@ export function makeCoverageBadge(config: MakeCoverageBadgeConfig): number {
7980
)
8081
return 1
8182
}
82-
// The README ref is an absolute raw-GitHub url, so the badge renders on the
83-
// npm package page too — which means the repo slug is a hard requirement, not
84-
// a nice-to-have. No relative fallback: it would silently reship the broken
85-
// npm image this url exists to fix.
86-
const slug = repoGitHubSlug(cfg.repoRoot)
87-
if (slug === undefined) {
88-
logger.error(
89-
`gen/coverage-badge: ${missingGitHubSlugMessage(cfg.repoRoot)}`,
90-
)
91-
return 1
83+
// A published package's README ref is an absolute raw-GitHub url so the badge
84+
// renders on the npm package page too, which makes the repo slug a hard
85+
// requirement there, not a nice-to-have. No relative fallback: it would
86+
// silently reship the broken npm image this url exists to fix. A private
87+
// package has no registry page, so it keeps the relative path — the absolute
88+
// form would break it, since a private repo's raw url is not served
89+
// anonymously.
90+
let slug: string | undefined
91+
if (isPublishedPackage(cfg.repoRoot)) {
92+
slug = repoGitHubSlug(cfg.repoRoot)
93+
if (slug === undefined) {
94+
logger.error(
95+
`gen/coverage-badge: ${missingGitHubSlugMessage(cfg.repoRoot)}`,
96+
)
97+
return 1
98+
}
9299
}
93100
const svgPath = badgeAssetPath(cfg.repoRoot)
94101
const nextSvg = coverageBadgeSvg(pct)

scripts/fleet/lib/coverage-badge.mts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ export function coverageBadgeUrl(slug: string): string {
7474

7575
// The current README reference to the coverage badge — a dimensioned <img> at
7676
// the badge's absolute raw-GitHub URL for `slug` (e.g. `SocketDev/socket-lib`).
77-
// Every other spelling is legacy, recognized only to migrate it.
78-
export function coverageBadgeRef(slug: string, svg: string): string {
79-
return badgeImgTag(coverageBadgeUrl(slug), 'Coverage', svg)
77+
// Every other spelling is legacy, recognized only to migrate it. An `undefined`
78+
// slug means the package is never published, so it keeps the repo-relative path
79+
// (see `isPublishedPackage`): there is no registry page to break, and a private
80+
// repo's raw URL would not resolve.
81+
export function coverageBadgeRef(
82+
slug: string | undefined,
83+
svg: string,
84+
): string {
85+
const src = slug === undefined ? BADGE_ASSET_PATH : coverageBadgeUrl(slug)
86+
return badgeImgTag(src, 'Coverage', svg)
8087
}
8188

8289
// The legacy markdown reference, kept for migration matching.
@@ -188,11 +195,12 @@ export function hasUnrecognizedCoverageBadge(readme: string): boolean {
188195
* legacy pre-badges/ path, the `![]` markdown form, the relative-src `<img>`,
189196
* AND an absolute `<img>` whose width went stale after a coverage change.
190197
* Already-current READMEs come back unchanged. `slug` is the repo's
191-
* `owner/repo`; `svg` supplies the exact width the <img> pins.
198+
* `owner/repo`, or `undefined` for a never-published package that keeps the
199+
* relative path; `svg` supplies the exact width the <img> pins.
192200
*/
193201
export function migrateReadmeBadge(
194202
readme: string,
195-
slug: string,
203+
slug: string | undefined,
196204
svg: string,
197205
): string {
198206
const ref = coverageBadgeRef(slug, svg)

0 commit comments

Comments
 (0)