env-schema.ts
This file
Path
packages/visibility/src/env-schema.ts
134 lines · 3.6 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
lines 1–2why, one step back1// Parsing reads text and nothing else: no value expression is evaluated, no2// plugin is loaded, and no import outside the repo is followed.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 3–5why, one step back3import { RepoPath } from "@repo/protocol/ids";4import { Array, Context, Effect, Predicate, Result, Schema } from "effect";5Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 6why, one step back6/** Never evaluated; a `$APP_ENV` reference arrives as the call `ref(APP_ENV)`. */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 7–47why, one step back7export type ParsedValue =8 | string9 | number10 | boolean11 | readonly ParsedValue[]12 | ParsedCall13 | ParsedArgs;1415export interface ParsedNamedArg {16 readonly key: string;17 readonly value: ParsedValue;18}1920export interface ParsedArgs {21 readonly positional: readonly ParsedValue[];22 readonly named: readonly ParsedNamedArg[];23}2425export interface ParsedCall extends ParsedArgs {26 readonly fn: string;27 readonly text: string;28}2930export interface ParsedDecorator {31 readonly name: string;32 readonly value: ParsedValue;33}3435export interface ParsedItem {36 readonly key: string;37 readonly description?: string;38 readonly decorators: readonly ParsedDecorator[];39 readonly value?: string;40}4142export interface ParsedEnvSchema {43 readonly header: readonly ParsedDecorator[];44 readonly items: readonly ParsedItem[];45 readonly imports: readonly string[];46}47Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
line 48why, one step back48/** Names the file and why, never a value. */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 49–106why, one step back49export class EnvSchemaUnreadable extends Schema.TaggedError<EnvSchemaUnreadable>()(50 "EnvSchemaUnreadable",51 { file: RepoPath, reason: Schema.String }52) {}5354export class EnvSchemaParser extends Context.Service<55 EnvSchemaParser,56 {57 readonly parse: (58 file: RepoPath,59 text: string60 ) => Effect.Effect<ParsedEnvSchema, EnvSchemaUnreadable>;61 }62>()("visibility/EnvSchemaParser") {}6364export const isCall = (value: ParsedValue): value is ParsedCall =>65 Predicate.hasProperty(value, "fn");6667export const isArgs = (value: ParsedValue): value is ParsedArgs =>68 Predicate.hasProperty(value, "named") && !isCall(value);6970export const isList = (value: ParsedValue): value is readonly ParsedValue[] =>71 Array.isArray(value);7273const OUTSIDE = /^(?:~|[/\\]|[A-Za-z][\w+.-]*:)/u;7475const repoImport = (76 file: RepoPath,77 written: string78): Result.Result<RepoPath, string> => {79 if (written === "") {80 return Result.fail("an @import names no path");81 }8283 if (OUTSIDE.test(written)) {84 return Result.fail(`imports ${written}, outside the repo`);85 }8687 const folder: string[] = file.split("/").slice(0, -1);8889 for (const segment of written.split(/[/\\]/u)) {90 if (segment === "..") {91 if (folder.pop() === undefined) {92 return Result.fail(`imports ${written}, above the repo's root`);93 }94 } else if (segment !== "" && segment !== ".") {95 folder.push(segment);96 }97 }9899 const path = folder.join("/") || ".";100101 return Result.succeed(102 RepoPath.make(written.endsWith("/") && path !== "." ? `${path}/` : path)103 );104};105106/**Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
lines 107–109why, one step back107 * An import from outside the repo makes the schema unreadable: the platform108 * never follows one. Both parsers end here, so the rule is the same whichever109 * reads the text.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 110–134why, one step back110 */111export const withImports = (112 file: RepoPath,113 parsed: Pick<ParsedEnvSchema, "header" | "items">114): Effect.Effect<ParsedEnvSchema, EnvSchemaUnreadable> =>115 Effect.forEach(116 parsed.header.filter(({ name }) => name === "import"),117 ({ value }) => {118 const written = isArgs(value) ? value.positional[0] : undefined;119120 return Effect.fromResult(121 Predicate.isString(written)122 ? repoImport(file, written)123 : Result.fail("an @import names no path")124 ).pipe(125 Effect.mapError((reason) => new EnvSchemaUnreadable({ file, reason }))126 );127 }128 ).pipe(129 Effect.map((imports) => ({130 header: parsed.header,131 imports,132 items: parsed.items,133 }))134 );