# tools/core/resolvers/scope.resolver.ts

> 66 lines of code and 21 definitions.

Tree: Coordination tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-resolvers-scope-resolver-ts
Source text: https://banes-lab.com/assets/sources/source.c4cb5dac52f2d887681dd676f1b13f42af573ae6689c152b0720927080b5b97e.generated.txt

## Definitions

- `narrow` (lexical_declaration, line 68, exported)
- `once` (lexical_declaration, line 71, exported)
- `ScopeSets` (interface_declaration, line 11, exported)
- `collect` (lexical_declaration, line 16)
- `out` (lexical_declaration, line 17)
- `absolute` (lexical_declaration, line 20)
- `options` (lexical_declaration, line 21)
- `rootDocuments` (lexical_declaration, line 30)
- `own` (lexical_declaration, line 31)
- `prefix` (lexical_declaration, line 32)
- `resolveScope` (lexical_declaration, line 49, exported)
- `namingRoots` (lexical_declaration, line 54, exported)
- `artifactRoots` (lexical_declaration, line 62, exported)
- `artifactPaths` (lexical_declaration, line 63, exported)
- `authored` (lexical_declaration, line 65, exported)
- `within` (lexical_declaration, line 67, exported)
- `taxonomyPaths` (lexical_declaration, line 73, exported)
- `artifact` (lexical_declaration, line 74, exported)
- `roots` (lexical_declaration, line 75, exported)
- `tools` (lexical_declaration, line 76, exported)
- `all` (lexical_declaration, line 78, exported)

## Source

```typescript
import { AUTHORED_ROOTS, BINARY_EXTENSIONS } from "../constants/path.constants.ts";
import { existsSync, readdirSync } from "node:fs";

import { surfacePrefix, surfaceRoot } from "../../../config/surface.config.ts";
import { toPosix, walk } from "../iterators/file.iterator.ts";
import type { Jurisdiction } from "../types/rule.types.ts";
import type { TaxonomyData } from "../types/taxonomy.types.ts";
import { resolve } from "node:path";
import { resolveArtifactRoots } from "./artifact.resolver.ts";

export interface ScopeSets {
    readonly byJurisdiction: Record<Jurisdiction, string[]>;
    readonly all: string[];
}

const collect = function collect(repoRoot: string, roots: readonly string[], ignored: readonly string[]): string[] {
    const out: string[] = [];

    for (const root of roots) {
        const absolute = resolve(repoRoot, root);
        const options = { excluded: BINARY_EXTENSIONS, extensions: [], ignored, root: absolute };
        for (const file of walk(options)) {
            out.push(toPosix(repoRoot, file));
        }
    }

    return out;
};

const rootDocuments = function rootDocuments(ignored: readonly string[]): string[] {
    const own = surfaceRoot();
    const prefix = surfacePrefix();

    return readdirSync(own, { withFileTypes: true })
        .filter((entry) => entry.isFile())
        .map((entry) => (prefix.length === 0 ? entry.name : `${prefix}/${entry.name}`))
        .filter((name) => !ignored.includes(name.slice(name.lastIndexOf("/") + 1)))
        .filter((name) => {
            for (const extension of BINARY_EXTENSIONS) {
                if (name.endsWith(extension)) {
                    return false;
                }
            }
            return true;
        })
        .sort();
};

export const resolveScope = function resolveScope(
    repoRoot: string,
    taxonomy: TaxonomyData,
    scope: string | null,
): ScopeSets {
    const namingRoots = [
        ...new Set([
            ...Object.keys(taxonomy.containers),
            ...Object.keys(taxonomy.specialContainers),
            ...Object.keys(taxonomy.corpusRoots),
        ]),
    ];

    const artifactRoots = resolveArtifactRoots(repoRoot, taxonomy);
    const artifactPaths = artifactRoots.filter((root) => root.unresolved === null).map((root) => root.path);

    const authored = AUTHORED_ROOTS.filter((root) => existsSync(resolve(repoRoot, root)));

    const within = scope === null ? null : new Set(collect(repoRoot, [scope], taxonomy.ignored));
    const narrow = (paths: readonly string[]): string[] =>
        within === null ? [...paths] : paths.filter((path) => within.has(path));

    const once = (paths: readonly string[]): string[] => [...new Set(narrow(paths))];

    const taxonomyPaths = once(collect(repoRoot, namingRoots, taxonomy.ignored));
    const artifact = once(collect(repoRoot, artifactPaths, taxonomy.ignored));
    const roots = once(rootDocuments(taxonomy.ignored));
    const tools = once(collect(repoRoot, authored, taxonomy.ignored));

    const all = [...new Set([...taxonomyPaths, ...artifact, ...roots, ...tools])];

    return { all, byJurisdiction: { all, artifact, taxonomy: taxonomyPaths } };
};
```
