# core/persistence/text.persistence.ts

> 44 lines of code and 10 definitions.

Tree: Canonical Write
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/canonical-write#file-canonical-write-core-persistence-text-persistence-ts
Source text: https://banes-lab.com/source/canonical-write/core/persistence/text.persistence.ts.txt

Listed in [core/persistence](https://banes-lab.com/api/source/canonical-write/core/persistence.md).

## Definitions

- `writeChanged` (lexical_declaration, line 6)
- `writeCanonicalText` (lexical_declaration, line 22, exported)
- `canonicalText` (lexical_declaration, line 14)
- `writeGeneratedMarkdown` (lexical_declaration, line 38, exported)
- `writeCanonicalJson` (lexical_declaration, line 30, exported)
- `writeVerbatim` (lexical_declaration, line 48, exported)
- `next` (lexical_declaration, line 7)
- `{ inferredParser }` (lexical_declaration, line 15)
- `body` (lexical_declaration, line 43, exported)
- `previous` (lexical_declaration, line 44, exported)

## Contained in

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

## Uses

- [core/formatters/mark.formatter.ts](https://banes-lab.com/source/canonical-write/core/formatters/mark.formatter.ts.md)

## Linked from

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

## Source

```typescript
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { stampGenerated, stripMark } from "#core/formatters/mark.formatter";
import type { Options } from "prettier";
import prettier from "prettier";

const writeChanged = function writeChanged(target: string, content: Uint8Array | string): void {
    const next = typeof content === "string" ? Buffer.from(content) : content;
    if (existsSync(target) && readFileSync(target).equals(next)) {
        return;
    }
    writeFileSync(target, next);
};

const canonicalText = async function canonicalText(target: string, text: string, options: Options): Promise<string> {
    const { inferredParser } = await prettier.getFileInfo(target);
    if (inferredParser === null && options.parser === undefined) {
        return text;
    }
    return prettier.format(text, { ...options, filepath: target });
};

export const writeCanonicalText = async function writeCanonicalText(
    target: string,
    text: string,
    options: Options = {},
): Promise<void> {
    writeChanged(target, await canonicalText(target, text, options));
};

export const writeCanonicalJson = async function writeCanonicalJson(
    target: string,
    data: unknown,
    options: Options = {},
): Promise<void> {
    await writeCanonicalText(target, JSON.stringify(data), options);
};

export const writeGeneratedMarkdown = async function writeGeneratedMarkdown(
    target: string,
    text: string,
    options: Options = {},
): Promise<void> {
    const body = await prettier.format(stripMark(text), { ...options, filepath: target });
    const previous = existsSync(target) ? readFileSync(target, "utf8") : "";
    writeChanged(target, stampGenerated(body, previous, new Date()));
};

export const writeVerbatim = function writeVerbatim(target: string, content: Uint8Array | string): void {
    writeChanged(target, content);
};
```
