paths.ts
This file
Path
packages/project/src/code/paths.ts
106 lines · 2.8 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 1–14why, one step back1import type {2 RepoHost,3 RepoNotFound,4 TreeEntry,5} from "@repo/artifacts/repo-host";6import type { PathList, Rev } from "@repo/protocol/domain/code";7import { RepoPath } from "@repo/protocol/ids";8import type { RepoName } from "@repo/protocol/ids";9import type { UnknownRevision } from "@repo/protocol/refusal";10import type { Option } from "effect";11import { Array, Effect, Order } from "effect";1213import type { CodeProject } from "../code.ts";14import { CodeCache } from "./cache.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 15why, one step back15import { commitAt, resolveAt } from "./revision.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 16–33why, one step back16import { entriesOf } from "./tree.ts";1718export const PATHS_CAP = 50_000;1920const KEPT_PATHS = 10_000;2122const STORE_CALLS = 8;2324export interface FileAt {25 readonly path: RepoPath;26 readonly entry: TreeEntry;27}2829interface Folder {30 readonly prefix: string;31 readonly tree: string;32}33Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 34why, one step back34/** One depth at a time, so the list past `cap` depends on the tree alone, never on which read came back first. */Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 35–89why, one step back35export const filesUnder = (36 repo: RepoName,37 root: string,38 cap: number39): Effect.Effect<40 { readonly files: readonly FileAt[]; readonly truncated: boolean },41 RepoNotFound,42 RepoHost43> =>44 Effect.gen(function* () {45 const found: FileAt[] = [];46 let depth: readonly Folder[] = [{ prefix: "", tree: root }];4748 while (depth.length > 0 && found.length <= cap) {49 const read = yield* Effect.forEach(50 depth,51 ({ prefix, tree }) =>52 Effect.map(entriesOf(repo, tree), (entries) => ({ entries, prefix })),53 { concurrency: STORE_CALLS }54 );5556 const deeper: Folder[] = [];5758 for (const { entries, prefix } of read) {59 for (const entry of entries) {60 if (entry.type === "tree") {61 deeper.push({ prefix: `${prefix}${entry.name}/`, tree: entry.sha });62 } else {63 found.push({64 entry,65 path: RepoPath.make(`${prefix}${entry.name}`),66 });67 }68 }69 }7071 depth = deeper;72 }7374 const all = Array.sort(75 found,76 Order.mapInput(Order.String, (file: FileAt) => file.path)77 );7879 return {80 files: all.slice(0, cap),81 truncated: all.length > cap || depth.length > 0,82 };83 });8485export const pathList = (86 project: CodeProject,87 at: Option.Option<Rev>88): Effect.Effect<PathList, UnknownRevision, RepoHost> =>89 Effect.gen(function* () {Merge redesign into mainoscabrielbefore adoptioncaedd21
line 90why, one step back90 const { commit, repo } = yield* resolveAt(project, at);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 91–92why, one step back91 const cache = yield* CodeCache;92Merge redesign into mainoscabrielbefore adoptioncaedd21
line 93why, one step back93 const { tree } = yield* commitAt(repo, commit).pipe(Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 94–98why, one step back94 Effect.flatMap(Effect.fromOption),95 Effect.orDie96 );9798 const { files, truncated } = yield* cacheMerge redesign into mainoscabrielbefore adoptioncaedd21
lines 99–102why, one step back99 .through(`${repo}:paths:${tree}`, filesUnder(repo, tree, PATHS_CAP), {100 heavy: true,101 keep: (list) => list.files.length <= KEPT_PATHS,102 })Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 103–106why, one step back103 .pipe(Effect.orDie);104105 return { at: commit, paths: files.map(({ path }) => path), truncated };106 });