Skip to content
Myr

config-folder.ts

This file

Path

packages/rules/src/config-folder.ts

199 lines · 5.7 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take 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

line 1why, one step back
1import { AskInput, PolicyInput } from "@repo/protocol/domain/policy";

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

line 2why, one step back
2import { VisibilityPolicy } from "@repo/protocol/domain/visibility";

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–13why, one step back
3import { AskId, Handle, RepoPath } from "@repo/protocol/ids";
4import { Effect, Option, Schema, Struct } from "effect";56import { defaultPolicyInput } from "./defaults.ts";7import { RulesFailed } from "./rules-evaluator.ts";8import type { ConfigBundle } from "./rules-evaluator.ts";910const ASK_FILE = /\.ask\.[cm]?[jt]s$/u;1112const SCRIPT = /\.[cm]?[jt]s$/u;13

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 14why, one step back
14/** The bundle names the folder, so `rules` never spells the brand. */

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 15–25why, one step back
15const folderOf = (bundle: ConfigBundle) =>
16 bundle.entry.slice(0, bundle.entry.lastIndexOf("/") + 1);1718export const policyJsonOf = (bundle: ConfigBundle) =>19 `${folderOf(bundle)}policy.json`;2021export const projectModule = (bundle: ConfigBundle): string | undefined =>22 SCRIPT.test(bundle.entry) && Object.hasOwn(bundle.modules, bundle.entry)23 ? bundle.entry24 : undefined;25

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

lines 26–39why, one step back
26const VISIBILITY_FILE = /^visibility\.[cm]?[jt]s$/u;
2728export const visibilityModule = (bundle: ConfigBundle): string | undefined => {29 const folder = folderOf(bundle);3031 return Object.keys(bundle.modules)32 .toSorted()33 .find(34 (path) =>35 path.startsWith(folder) &&36 VISIBILITY_FILE.test(path.slice(folder.length))37 );38};39

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 40–77why, one step back
40export const askModules = (bundle: ConfigBundle): readonly string[] => {
41 const asks = `${folderOf(bundle)}asks/`;4243 return Object.keys(bundle.modules)44 .filter((path) => path.startsWith(asks) && ASK_FILE.test(path))45 .toSorted();46};4748export const failedAt =49 (where: string) =>50 (failure: Schema.SchemaError): RulesFailed =>51 new RulesFailed({52 detail: `${where}: ${failure.message}`,53 reason: "decode",54 });5556const noDefault = (path: string, what: string) =>57 new RulesFailed({58 detail: `${path}: its default export is not ${what}`,59 reason: "decode",60 });6162const fromPolicyJson = (63 bundle: ConfigBundle64): Effect.Effect<Option.Option<PolicyInput>, RulesFailed> => {65 const path = policyJsonOf(bundle);66 const source = bundle.modules[path];6768 return source === undefined69 ? Effect.succeedNone70 : Schema.decodeEffect(Schema.fromJsonString(PolicyInput))(source).pipe(71 Effect.map((policy) =>72 Option.some({ ...policy, source: "config" as const })73 ),74 Effect.mapError(failedAt(path))75 );76};77

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 78why, one step back
78/** Its source is the config folder whenever there is one, so every adapter gives one bundle the same source. */

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 79–93why, one step back
79export const statedWithoutCode = (
80 bundle: ConfigBundle81): Effect.Effect<PolicyInput, RulesFailed> =>82 Effect.map(83 fromPolicyJson(bundle),84 Option.getOrElse((): PolicyInput =>85 Object.keys(bundle.modules).length === 086 ? defaultPolicyInput87 : { ...defaultPolicyInput, source: "config" }88 )89 );9091export const Collected = Schema.Struct({92 asks: Schema.Array(Schema.Struct({ ask: Schema.Json, path: Schema.String })),93 project: Schema.optionalKey(Schema.Json),

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

line 94why, one step back
94 visibility: Schema.optionalKey(Schema.Json),

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 95–104why, one step back
95});
9697export type Collected = typeof Collected.Type;9899const AskFile = Schema.Struct({100 ...Struct.omit(AskInput.fields, ["askedBy", "blockedBy", "file", "id"]),101 askedBy: Schema.optionalKey(Handle),102 blockedBy: Schema.optionalKey(Schema.Array(AskId)),103});104

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

lines 105–107why, one step back
105const ProjectFile = PolicyInput.mapFields(
106 Struct.omit(["asks", "source", "visibility"])107);

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 108–111why, one step back
108
109const idOf = (path: string) =>110 path.slice(path.lastIndexOf("/") + 1).replace(ASK_FILE, "");111

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 112why, one step back
112/** The id and file come from the path, whatever the data says. */

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 113–135why, one step back
113const askInputOf = (path: string, data: Schema.Json, initializer: Handle) =>
114 Effect.gen(function* () {115 if (data === null) {116 return yield* noDefault(path, "an ask");117 }118119 const ask = yield* Schema.decodeUnknownEffect(AskFile)(data).pipe(120 Effect.mapError(failedAt(path))121 );122123 const id = yield* Schema.decodeEffect(AskId)(idOf(path)).pipe(124 Effect.mapError(failedAt(`${path}'s file name`))125 );126127 return {128 ...ask,129 askedBy: ask.askedBy ?? initializer,130 blockedBy: ask.blockedBy ?? [],131 file: RepoPath.make(path),132 id,133 } satisfies AskInput;134 });135

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

lines 136–153why, one step back
136const visibilityOf = (
137 data: Schema.Json | undefined,138 path: string | undefined139): Effect.Effect<Option.Option<VisibilityPolicy>, RulesFailed> => {140 if (data === undefined) {141 return Effect.succeedNone;142 }143144 const where = path ?? "visibility.ts";145146 return data === null147 ? Effect.fail(noDefault(where, "a visibility policy"))148 : Schema.decodeUnknownEffect(VisibilityPolicy)(data).pipe(149 Effect.map(Option.some),150 Effect.mapError(failedAt(where))151 );152};153

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 154–158why, one step back
154export const fromCollected = (input: {
155 readonly collected: Collected;156 readonly entry: string;157 readonly fallback: PolicyInput;158 readonly initializer: Handle;

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

line 159why, one step back
159 readonly visibility?: string | undefined;

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 160–184why, one step back
160}): Effect.Effect<PolicyInput, RulesFailed> =>
161 Effect.gen(function* () {162 const { collected, entry, fallback, initializer } = input;163 const { project } = collected;164165 if (project === null) {166 return yield* noDefault(entry, "a project");167 }168169 const base =170 project === undefined171 ? fallback172 : {173 ...(yield* Schema.decodeUnknownEffect(ProjectFile)(project).pipe(174 Effect.mapError(failedAt(entry))175 )),176 asks: [],177 };178179 const asks: AskInput[] = [...base.asks];180181 for (const { ask, path } of collected.asks) {182 asks.push(yield* askInputOf(path, ask, initializer));183 }184

Merge build/608-datekit-maintainers-layer: datekit has a maintainers layer of sealed checks, each citing the clause it enforces, and the seed pushes itoscabrielbefore adoption166f6c9

lines 185–198why, one step back
185 const visibility = yield* visibilityOf(
186 collected.visibility,187 input.visibility188 );189190 return Option.match(visibility, {191 onNone: () => ({ ...base, asks, source: "config" as const }),192 onSome: (stated) => ({193 ...base,194 asks,195 source: "config" as const,196 visibility: stated,197 }),198 });

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

line 199why, one step back
199 });