refs.ts
This file
Path
packages/project/src/code/refs.ts
136 lines · 3.8 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 1why, one step back1import { RepoHost } from "@repo/artifacts/repo-host";Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 2why, one step back2import { packageOf, TagName } from "@repo/protocol/domain/code";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 3–31why, one step back3import type { RefsView, TagRef } from "@repo/protocol/domain/code";4import { Array, Effect, Option, Schema } from "effect";56import type { CodeProject } from "../code.ts";7import { tagNameOf } from "./revision.ts";89const runsOf = (name: string) => name.match(/\d+|\D+/gu) ?? [];1011const isDigits = (run: string) => /^\d/u.test(run);1213const compareText = (one: string, other: string) => {14 if (one === other) {15 return 0;16 }1718 return one < other ? -1 : 1;19};2021const compareNumbers = (one: string, other: string) => {22 const left = one.replace(/^0+(?=\d)/u, "");23 const right = other.replace(/^0+(?=\d)/u, "");2425 if (left.length !== right.length) {26 return left.length < right.length ? -1 : 1;27 }2829 return compareText(left, right);30};31Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 32why, one step back32/** Newest first: digit runs compare by value (`v1.10.0` before `v1.9.0`), and a release before its pre-releases. */Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 33–54why, one step back33export const compareTags = (one: string, other: string): number => {34 const left = runsOf(one);35 const right = runsOf(other);3637 for (let at = 0; at < Math.min(left.length, right.length); at += 1) {38 const mine = left[at] ?? "";39 const theirs = right[at] ?? "";4041 const order =42 isDigits(mine) && isDigits(theirs)43 ? compareNumbers(mine, theirs)44 : compareText(mine, theirs);4546 if (order !== 0) {47 return -order;48 }49 }5051 if (left.length === right.length) {52 return 0;53 }54Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 55why, one step back55 // A pre-release (`-beta.1`) is older than its release, anything else newer.Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 56–61why, one step back56 const longer = left.length > right.length ? left : right;57 const rest = longer[Math.min(left.length, right.length)] ?? "";58 const longerIsNewer = !rest.startsWith("-");59 const leftIsLonger = left.length > right.length;6061 return leftIsLonger === longerIsNewer ? -1 : 1;Merge build/1006oscabrielbefore adoptionf155a0c
lines 62–63why, one step back62};63Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 64why, one step back64/** Unversioned tags first, then each package's tags together, most tags first; newest first within each. */Merge build/1006oscabrielbefore adoptionf155a0c
lines 65–103why, one step back65export const sortTags = <T extends { readonly name: string }>(66 tags: readonly T[]67): readonly T[] => {68 const keyed = tags.map((tag) => ({69 pkg: Option.getOrUndefined(packageOf(tag.name)),70 tag,71 }));7273 const counts = new Map<string, number>();7475 for (const { pkg } of keyed) {76 if (pkg !== undefined) {77 counts.set(pkg, (counts.get(pkg) ?? 0) + 1);78 }79 }8081 const runOrder = (one: string | undefined, other: string | undefined) => {82 if (one === other) {83 return 0;84 }8586 if (one === undefined || other === undefined) {87 return one === undefined ? -1 : 1;88 }8990 const more = (counts.get(other) ?? 0) - (counts.get(one) ?? 0);9192 return more === 0 ? compareText(one, other) : Math.sign(more);93 };9495 return keyed96 .toSorted(97 (one, other) =>98 runOrder(one.pkg, other.pkg) ||99 compareTags(one.tag.name, other.tag.name)100 )101 .map(({ tag }) => tag);102};103Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 104–105why, one step back104const decodeTag = Schema.decodeUnknownOption(TagName);105Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 106why, one step back106/** Only `refs/tags/*` is read: none of the platform's own refs and no branch but main ever shows. */Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 107–129why, one step back107export const refsView = (108 project: CodeProject109): Effect.Effect<RefsView, never, RepoHost> =>110 Effect.gen(function* () {111 const landed = yield* project.head;112 const forks = yield* project.forks;113114 const tags = yield* RepoHost.use((host) => host.tags(project.repo)).pipe(115 Effect.catchTag("RepoNotFound", () => Effect.succeed([]))116 );117118 const named = Array.getSomes(119 tags.map(({ name, sha }) =>120 Option.map(121 Option.flatMap(tagNameOf(name), decodeTag),122 (tag): TagRef => ({ name: tag, sha })123 )124 )125 );126127 return {128 forks,129 project: project.project,Merge build/1006oscabrielbefore adoptionf155a0c
line 130why, one step back130 tags: sortTags(named),Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 131–136why, one step back131 ...Option.match(landed, {132 onNone: () => ({}),133 onSome: (sha) => ({ landed: sha }),134 }),135 };136 });