~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
68 lines · 2.5 KB Raw History
// What the pages say of a repository's releases: which is a pre-release,
// how an image is pulled, how a crate is added, and where each is.

import { repoHref, urlPath } from '#lib/format.ts';

/** Whether `version` has a pre-release part, as `1.1.0-rc.1` does: never its package's latest. */
export const prerelease = (version: string) => /^[^+]*-/.test(version);

/**
 * The command that pulls version `version` of the image `pkg` that
 * `owner`'s repository `repo` publishes, from the registry at `host`:
 * the repository's own name when the package is named as it is.
 */
export function pullCommand(
	host: string,
	owner: string,
	repo: string,
	pkg: string,
	version: string
): string {
	const name = pkg === repo ? `${owner}/${repo}` : `${owner}/${repo}/${pkg}`;
	return `docker pull ${host}/${name}:${version}`;
}

/**
 * What a crate's release, `uri`, says to Cargo, served from `origin`: the
 * lines of `.cargo/config.toml` naming its repository's registry, as
 * `registry`, and the command that adds the crate from it. The registry is
 * at the owner's DID and the repository's key, which open its record's
 * URI and key, as names can change and Cargo keeps it in `Cargo.lock`.
 */
export function cargoUse(
	origin: string,
	uri: string,
	registry: string,
	pkg: string,
	version: string
): { config: string; add: string } {
	const [did, , rkey] = uri.replace(/^at:\/\//, '').split('/');
	const key = rkey.split('.')[0];
	const name = registry.replace(/[^A-Za-z0-9_-]/g, '-').replace(/^[^A-Za-z_]/, 'r$&');
	return {
		config: `[registries.${name}]\nindex = "sparse+${origin}/cargo/${did}/${key}/"`,
		add: `cargo add ${pkg}@${version} --registry ${name}`
	};
}

/** The id a release has on the releases page, for a link to land on it. */
export const releaseAnchor = (r: { package: string; version: string }) =>
	`${r.package}@${r.version}`;

/** The releases page of `owner`'s repository `name`, of one package's if given. */
export const releasesHref = (owner: string, name: string, pkg?: string) =>
	repoHref(owner, name, pkg ? `/releases?${new URLSearchParams({ package: pkg })}` : '/releases');

/**
 * Where the file `file` of version `version` of `pkg`, which `owner`'s
 * repository `name` publishes, is downloaded from: knotless-serve reads it
 * from the owner's PDS and checks it against its release.
 */
export const downloadHref = (
	owner: string,
	name: string,
	pkg: string,
	version: string,
	file: string
) =>
	repoHref(owner, name, `/releases/download/${urlPath(pkg)}/${urlPath(version)}/${urlPath(file)}`);