workspaces.ts
This file
Path
packages/checks/src/workspaces.ts
127 lines · 3.5 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–79why, one step back1import { RepoPath } from "@repo/protocol/ids";2import { Array, Option, Predicate, Schema } from "effect";34import type { RepoFile } from "./finder.ts";56export interface Workspace {7 readonly dir: string;8 readonly exports: readonly RepoPath[];9 readonly name: string;10}1112const Manifest = Schema.fromJsonString(13 Schema.Struct({14 exports: Schema.optionalKey(Schema.Json),15 name: Schema.optionalKey(Schema.String),16 workspaces: Schema.optionalKey(Schema.Json),17 })18);1920const decodeManifest = Schema.decodeUnknownOption(Manifest);2122const isJsonArray = (value: Schema.Json): value is Schema.JsonArray =>23 globalThis.Array.isArray(value);2425const isJsonObject = (value: Schema.Json): value is Schema.JsonObject =>26 Predicate.isObject(value) && !isJsonArray(value);2728const strings = (value: Schema.Json | undefined): readonly string[] =>29 value !== undefined && isJsonArray(value)30 ? value.filter(Predicate.isString)31 : [];3233const workspaceGlobs = (value: Schema.Json | undefined): readonly string[] => {34 if (value === undefined || value === null) {35 return [];36 }3738 if (isJsonArray(value)) {39 return strings(value);40 }4142 return isJsonObject(value) ? strings(value.packages) : [];43};4445const exportTargets = (value: Schema.Json | undefined): readonly string[] => {46 if (value === undefined || value === null) {47 return [];48 }4950 if (Predicate.isString(value)) {51 return [value];52 }5354 if (isJsonArray(value)) {55 return value.flatMap(exportTargets);56 }5758 return isJsonObject(value) ? Object.values(value).flatMap(exportTargets) : [];59};6061const withoutDotSlash = (path: string) => path.replace(/^(?:\.\/)+/u, "");6263const foldersIn = (paths: readonly string[], folder: string) => {64 const prefix = folder === "" ? "" : `${folder}/`;6566 return Array.dedupe(67 paths.flatMap((path) => {68 if (!path.startsWith(prefix)) {69 return [];70 }7172 const rest = path.slice(prefix.length);73 const slash = rest.indexOf("/");7475 return slash === -1 ? [] : [`${prefix}${rest.slice(0, slash)}`];76 })77 );78};79Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 80why, one step back80// Deeper globs are read as their folder prefix's children.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 81–89why, one step back81const foldersOf = (paths: readonly string[], glob: string) => {82 const clean = withoutDotSlash(glob).replace(/\/+$/u, "");83 const star = clean.indexOf("*");8485 return star === -186 ? [clean]87 : foldersIn(paths, clean.slice(0, star).replace(/\/+$/u, ""));88};89Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
lines 90–91why, one step back90// A manifest that doesn't parse (or was too large, so its text is empty)91// declares nothing.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 92–127why, one step back92export const workspacesIn = (93 files: readonly RepoFile[]94): readonly Workspace[] => {95 const sorted = files.toSorted((left, right) =>96 left.path < right.path ? -1 : Number(left.path > right.path)97 );9899 const paths = sorted.map(({ path }) => path);100 const texts = new Map(sorted.map(({ content, path }) => [path, content]));101102 const manifest = (path: string) =>103 Option.flatMap(Option.fromUndefinedOr(texts.get(path)), decodeManifest);104105 const globs = Option.match(manifest("package.json"), {106 onNone: () => [],107 onSome: ({ workspaces }) => workspaceGlobs(workspaces),108 });109110 const dirs = Array.dedupe(globs.flatMap((glob) => foldersOf(paths, glob)));111112 return dirs.flatMap((dir) =>113 Option.toArray(114 Option.flatMap(manifest(`${dir}/package.json`), ({ exports, name }) =>115 name === undefined || name.length === 0116 ? Option.none()117 : Option.some({118 dir,119 exports: exportTargets(exports).map((target) =>120 RepoPath.make(`${dir}/${withoutDotSlash(target)}`)121 ),122 name,123 })124 )125 )126 );127};