# core/coordinators/closure.coordinator.ts

> 37 lines of code and 6 definitions.

Tree: Project Scripts
Language: typescript
Layer: application
Canonical: https://banes-lab.com/anatomy/scripts#file-scripts-core-coordinators-closure-coordinator-ts
Source text: https://banes-lab.com/source/scripts/core/coordinators/closure.coordinator.ts.txt

Listed in [core/coordinators](https://banes-lab.com/api/source/scripts/core/coordinators.md), before [core/coordinators/route.coordinator.ts](https://banes-lab.com/source/scripts/core/coordinators/route.coordinator.ts.md).

## Definitions

- `buildClosureGraph` (lexical_declaration, line 11, exported)
- `files` (lexical_declaration, line 12, exported)
- `targets` (lexical_declaration, line 13, exported)
- `deltas` (lexical_declaration, line 14, exported)
- `exports` (lexical_declaration, line 15, exported)
- `graphCounts` (lexical_declaration, line 26, exported)

## Contained in

- [core/coordinators](https://banes-lab.com/anatomy/scripts/folder-scripts-core-coordinators.md)

## Uses

- [core/analyzers/barrel.analyzer.ts](https://banes-lab.com/source/scripts/core/analyzers/barrel.analyzer.ts.md)
- [core/analyzers/export.analyzer.ts](https://banes-lab.com/source/scripts/core/analyzers/export.analyzer.ts.md)
- [core/converters/closure.converter.ts](https://banes-lab.com/source/scripts/core/converters/closure.converter.ts.md)

## Linked from

- [core/analyzers](https://banes-lab.com/anatomy/scripts/folder-scripts-core-analyzers.md)
- [core/converters](https://banes-lab.com/anatomy/scripts/folder-scripts-core-converters.md)

## Source

```typescript
import { GRAPH_VERSION, TEST_MARK } from "#configuration/constants/closure.constants";
import type { ClosureGraph } from "#types/closure.types";
import { collectExternalConsumers } from "#core/analyzers/export.analyzer";
import { collectGlobEdges } from "#core/analyzers/barrel.analyzer";
import { fileDeltas } from "#core/analyzers/closure.analyzer";
import { foldDeltas } from "#core/converters/closure.converter";
import { manifestOf } from "#core/loaders/manifest.loader";
import { selfImportTargets } from "#core/resolvers/specifier.resolver";
import { sourceFilesUnder } from "#core/loaders/source.loader";

export const buildClosureGraph = function buildClosureGraph(root: string): ClosureGraph {
    const files = sourceFilesUnder(root).filter((file) => !file.includes(TEST_MARK));
    const targets = selfImportTargets(manifestOf(root));
    const deltas = files.flatMap((file) => fileDeltas(file, root, targets));
    const exports = deltas.flatMap((delta) => delta.exports ?? []);
    return {
        ...foldDeltas([
            ...deltas,
            { sideEffectImports: collectGlobEdges(root, files) },
            { externalConsumers: collectExternalConsumers(root, exports) },
        ]),
        version: GRAPH_VERSION,
    };
};

export const graphCounts = function graphCounts(graph: ClosureGraph): Readonly<Record<string, number>> {
    return {
        consumers: graph.consumers.length,
        emits: graph.emits.length,
        exports: graph.exports.length,
        icons: graph.iconsExports.length,
        "id consts": graph.idsExports.length,
        imports: graph.imports.length,
        interfaces: graph.interfaces.length,
        registers: graph.registers.length,
        strings: graph.stringsExports.length,
        subscribes: graph.subscribes.length,
    };
};
```
