# core/coordinators/ontology.coordinator.ts

> 37 lines of code and 13 definitions.

Tree: Bane's Lab Build Scripts
Language: typescript
Layer: application
Canonical: https://banes-lab.com/anatomy/build-scripts#file-build-scripts-core-coordinators-ontology-coordinator-ts
Source text: https://banes-lab.com/source/build-scripts/core/coordinators/ontology.coordinator.ts.txt

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

## Definitions

- `composeOntology` (lexical_declaration, line 14, exported)
- `buildOntology` (lexical_declaration, line 20, exported)
- `context` (lexical_declaration, line 15, exported)
- `snapshot` (lexical_declaration, line 16, exported)
- `ontology` (lexical_declaration, line 21, exported)
- `{ context, snapshot }` (lexical_declaration, line 22, exported)
- `vocabulary` (lexical_declaration, line 23, exported)
- `references` (lexical_declaration, line 24, exported)
- `loaderPath` (lexical_declaration, line 25, exported)
- `faces` (lexical_declaration, line 26, exported)
- `collections` (lexical_declaration, line 27, exported)
- `file` (lexical_declaration, line 33, exported)
- `records` (lexical_declaration, line 37, exported)

## Contained in

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

## Uses

- [core/converters/reference.converter.ts](https://banes-lab.com/source/build-scripts/core/converters/reference.converter.ts.md)
- [core/formatters/ontology.formatter.ts](https://banes-lab.com/source/build-scripts/core/formatters/ontology.formatter.ts.md)
- [core/formatters/reference.formatter.ts](https://banes-lab.com/source/build-scripts/core/formatters/reference.formatter.ts.md)
- [core/formatters/vocabulary.formatter.ts](https://banes-lab.com/source/build-scripts/core/formatters/vocabulary.formatter.ts.md)

## Used by

- [core/plugins/site.plugin.ts](https://banes-lab.com/source/build-scripts/core/plugins/site.plugin.ts.md)

## Linked from

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

## Source

```typescript
import type { BuiltOntology, OntologyBuild } from "#types/ontology.types";
import { dirname, join } from "node:path";
import { renderReferenceFace, renderReferenceLoader } from "#core/formatters/reference.formatter";
import { absolutePath } from "@ssot/paths";
import { createGovlabContext } from "@govlab/context";
import { referenceFileOf } from "#core/resolvers/reference.resolver";
import { referencesOf } from "#core/converters/reference.converter";
import { renderSnapshot } from "#core/formatters/ontology.formatter";
import { renderVocabulary } from "#core/formatters/vocabulary.formatter";
import { snapshotOf } from "#core/converters/ontology.converter";
import { vocabularyOf } from "#core/converters/vocabulary.converter";
import { writeCanonicalText } from "@govlab/canonical-write";

export const composeOntology = function composeOntology(): BuiltOntology {
    const context = createGovlabContext();
    const snapshot = snapshotOf(context);
    return { collections: referencesOf(snapshot), context, snapshot };
};

export const buildOntology = async function buildOntology(): Promise<OntologyBuild> {
    const ontology = composeOntology();
    const { context, snapshot } = ontology;
    const vocabulary = vocabularyOf(context);
    const references = ontology.collections;
    const loaderPath = absolutePath("app.reference");
    const faces = [...references.entries()];
    const collections = faces.map(([face]) => face);
    await Promise.all([
        writeCanonicalText(absolutePath("app.ontology"), renderSnapshot(snapshot)),
        writeCanonicalText(absolutePath("app.vocabulary"), renderVocabulary(vocabulary)),
        writeCanonicalText(loaderPath, renderReferenceLoader(collections)),
        ...faces.map(async ([face, index]) => {
            const file = join(dirname(loaderPath), referenceFileOf(face));
            return writeCanonicalText(file, renderReferenceFace(index));
        }),
    ]);
    const records = faces.reduce((total, [, index]) => total + Object.keys(index).length, 0);
    return { ontology, phrases: vocabulary.length, references: records };
};
```
