g1t/apps/web/app/lib/packages.ts

73 lines2,653 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Packages, with a container registry on g1t.sh; workspaces deleted whole and kept 30 days; Members for every member1/**
2 * Words and commands for the Packages pages: sizes, digests, and how each
3 * registry's own tool logs in and installs.
4 */
5import type { Ecosystem, PackageSummary } from "@g1t/contracts";
6
7/** What each registry is called on the pages. */
8export const ECOSYSTEM_LABEL: Record<Ecosystem, string> = {
9 container: "Container",
10 npm: "npm",
11 composer: "Composer",
12 cargo: "Cargo",
13 go: "Go",
14};
15
16/** "0 B", "812 KB", "12.3 MB", "1.4 GB", in powers of 1,000 as registries show them. */
17export function formatBytes(bytes: number): string {
18 if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
19 const units = ["B", "KB", "MB", "GB", "TB"];
20 let value = bytes;
21 let unit = 0;
22 while (value >= 1000 && unit < units.length - 1) {
23 value /= 1000;
24 unit++;
25 }
26 const shown = unit === 0 || value >= 100 ? Math.round(value) : Number(value.toFixed(1));
27 return `${shown} ${units[unit]}`;
28}
29
30/** `sha256:3f2a…` shortened to `3f2a9c1b7d0e` for lists. */
31export function shortDigest(digest: string): string {
32 return digest.replace(/^sha256:/, "").slice(0, 12);
33}
34
35/** The host packages are published to and installed from. */
36export function registryHost(address: string): string {
37 return address.split("/")[0] || "g1t.sh";
38}
39
40/**
41 * How to log in and install `pkg` at `version` (a tag or version) with its
42 * tool. `you` stands in the username a login takes.
43 */
44export function installCommands(pkg: Pick<PackageSummary, "ecosystem" | "address" | "name" | "workspace">, version: string | null, you: string): { login: string; install: string } {
45 const host = registryHost(pkg.address);
46 switch (pkg.ecosystem) {
47 case "container":
48 return {
49 login: `docker login ${host} -u ${you}`,
50 install: `docker pull ${pkg.address}${version ? `:${version}` : ""}`,
51 };
52 case "npm":
53 return {
54 login: `npm config set @${pkg.workspace}:registry https://${host}/-/npm/`,
55 install: `npm install @${pkg.workspace}/${pkg.name}${version ? `@${version}` : ""}`,
56 };
57 case "composer":
58 return {
59 login: `composer config repositories.${pkg.workspace} composer https://${host}/-/composer/${pkg.workspace}/`,
60 install: `composer require ${pkg.name}${version ? `:${version}` : ""}`,
61 };
62 case "cargo":
63 return {
64 login: `cargo login --registry ${pkg.workspace}`,
65 install: `cargo add ${pkg.name} --registry ${pkg.workspace}${version ? ` --vers ${version}` : ""}`,
66 };
67 case "go":
68 return {
69 login: `go env -w GOPRIVATE=${host}/${pkg.workspace}`,
70 install: `go get ${pkg.address}${version ? `@${version}` : ""}`,
71 };
72 }
73}