# shared/loaders/index.loader.ts

> 67 lines of code and 14 definitions.

Tree: Governance tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/governance#file-governance-shared-loaders-index-loader-ts
Source text: https://banes-lab.com/source/governance/shared/loaders/index.loader.ts.txt

Listed in [shared/loaders](https://banes-lab.com/api/source/governance/shared/loaders.md), after [shared/loaders/graph.loader.ts](https://banes-lab.com/source/governance/shared/loaders/graph.loader.ts.md) and before [shared/loaders/manifest.loader.ts](https://banes-lab.com/source/governance/shared/loaders/manifest.loader.ts.md).

## Definitions

- `entriesIn` (lexical_declaration, line 11)
- `resolvesAgainst` (lexical_declaration, line 58, exported)
- `ruleHostPaths` (lexical_declaration, line 34)
- `scanRoots` (lexical_declaration, line 15)
- `walk` (lexical_declaration, line 21)
- `topLevelOf` (lexical_declaration, line 30)
- `ROOT` (lexical_declaration, line 9)
- `child` (lexical_declaration, line 25)
- `out` (lexical_declaration, line 35)
- `buildTreeIndex` (lexical_declaration, line 48, exported)
- `paths` (lexical_declaration, line 49, exported)
- `basenames` (lexical_declaration, line 50, exported)
- `trimmed` (lexical_declaration, line 52, exported)
- `needle` (lexical_declaration, line 62, exported)

## Uses

- [shared/resolvers/anchor.resolver.ts](https://banes-lab.com/source/governance/shared/resolvers/anchor.resolver.ts.md)

## Used by

- [rules/eslint/closure-rule-declarations-resolve.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/closure-rule-declarations-resolve.eslint.rule.ts.md)

## Source

```typescript
import { RULE_HOST, WORKSPACE_ROOT, labelOf } from "../resolvers/anchor.resolver.ts";
import type { Dirent } from "node:fs";
import { MASTER_EXCLUDE_MARKERS } from "../generated/exclusions.generated.ts";
import type { TreeIndex } from "../../types/location.types.ts";
import { isExcludedPath } from "@govlab/quality/core/matchers/exclusions.matcher.ts";
import { join } from "node:path";
import { readdirSync } from "node:fs";

const ROOT = WORKSPACE_ROOT;

const entriesIn = function entriesIn(dir: string): Dirent[] {
    return readdirSync(dir, { withFileTypes: true });
};

const scanRoots = function scanRoots(): string[] {
    return entriesIn(ROOT)
        .filter((entry) => entry.isDirectory() && !isExcludedPath(entry.name, MASTER_EXCLUDE_MARKERS))
        .map((entry) => entry.name);
};

const walk = function walk(rel: string): string[] {
    return entriesIn(join(ROOT, rel))
        .filter((e) => !(e.isDirectory() && isExcludedPath(`${rel}/${e.name}`, MASTER_EXCLUDE_MARKERS)))
        .flatMap((e) => {
            const child = `${rel}/${e.name}`;
            return e.isDirectory() ? [`${child}/`, ...walk(child)] : [child];
        });
};

const topLevelOf = function topLevelOf(dir: string): string[] {
    return entriesIn(dir).map((e) => (e.isDirectory() ? `${e.name}/` : e.name));
};

const ruleHostPaths = function ruleHostPaths(): string[] {
    const out: string[] = [];
    for (const e of entriesIn(RULE_HOST)) {
        if (!e.isFile()) {
            continue;
        }
        out.push(`ruleHost/${e.name}`);
        if (e.name.endsWith(".ts")) {
            out.push(`local/${labelOf(e.name.slice(0, -3))}`);
        }
    }
    return out;
};

export const buildTreeIndex = function buildTreeIndex(): TreeIndex {
    const paths = [...topLevelOf(ROOT), ...scanRoots().flatMap(walk), ...ruleHostPaths()];
    const basenames = new Set<string>();
    for (const p of paths) {
        const trimmed = p.endsWith("/") ? p.slice(0, -1) : p;
        basenames.add(trimmed.slice(trimmed.lastIndexOf("/") + 1));
    }
    return { basenames, paths };
};

export const resolvesAgainst = function resolvesAgainst(index: TreeIndex, value: string): boolean {
    if (!value.includes("/")) {
        return index.basenames.has(value);
    }
    const needle = value.startsWith("/") ? value : `/${value}`;
    for (const p of index.paths) {
        if (p === value) {
            return true;
        }
        if (p.startsWith(value)) {
            return true;
        }
        if (`/${p}`.includes(needle)) {
            return true;
        }
    }
    return false;
};
```
