# core/persistence/report.persistence.ts

> 28 lines of code and 5 definitions.

Tree: Bane's Lab Content
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/content#file-content-core-persistence-report-persistence-ts
Source text: https://banes-lab.com/source/content/core/persistence/report.persistence.ts.txt

Listed in [core/persistence](https://banes-lab.com/api/source/content/core/persistence.md), after [core/persistence/chapter.persistence.ts](https://banes-lab.com/source/content/core/persistence/chapter.persistence.ts.md).

## Definitions

- `ensureFolder` (lexical_declaration, line 6)
- `persistJson` (lexical_declaration, line 10, exported)
- `persistText` (lexical_declaration, line 15, exported)
- `persistGeneratedMarkdown` (lexical_declaration, line 20, exported)
- `readJsonOrNull` (lexical_declaration, line 28, exported)

## Contained in

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

## Used by

- [core/persistence/chapter.persistence.ts](https://banes-lab.com/source/content/core/persistence/chapter.persistence.ts.md)
- [runtime/entrypoints/leak.entrypoint.ts](https://banes-lab.com/source/content/runtime/entrypoints/leak.entrypoint.ts.md)
- [runtime/entrypoints/link.entrypoint.ts](https://banes-lab.com/source/content/runtime/entrypoints/link.entrypoint.ts.md)

## Linked from

- [core/persistence](https://banes-lab.com/anatomy/content/folder-content-core-persistence.md)
- [runtime/entrypoints](https://banes-lab.com/anatomy/content/folder-content-runtime-entrypoints.md)

## Source

```typescript
import { existsSync, mkdirSync, readFileSync } from "node:fs";
import { writeCanonicalJson, writeCanonicalText, writeGeneratedMarkdown } from "@govlab/canonical-write";
import { UTF8 } from "#configuration/constants/source.constants";
import { dirname } from "node:path";

const ensureFolder = function ensureFolder(target: string): void {
    mkdirSync(dirname(target), { recursive: true });
};

export const persistJson = async function persistJson(target: string, data: unknown): Promise<void> {
    ensureFolder(target);
    await writeCanonicalJson(target, data);
};

export const persistText = async function persistText(target: string, text: string): Promise<void> {
    ensureFolder(target);
    await writeCanonicalText(target, text);
};

export const persistGeneratedMarkdown = async function persistGeneratedMarkdown(
    target: string,
    text: string,
): Promise<void> {
    ensureFolder(target);
    await writeGeneratedMarkdown(target, text);
};

export const readJsonOrNull = function readJsonOrNull(target: string): unknown {
    if (!existsSync(target)) {
        return null;
    }
    return JSON.parse(readFileSync(target, UTF8));
};
```
