# runtime/entrypoints/document.entrypoint.ts

> 97 lines of code and 11 definitions.

Tree: GovLab Docs
Language: typescript
Layer: runtime
Canonical: https://banes-lab.com/anatomy/docs#file-docs-runtime-entrypoints-document-entrypoint-ts
Source text: https://banes-lab.com/source/docs/runtime/entrypoints/document.entrypoint.ts.txt

Listed in [runtime/entrypoints](https://banes-lab.com/api/source/docs/runtime/entrypoints.md), after [runtime/entrypoints/coverage.entrypoint.ts](https://banes-lab.com/source/docs/runtime/entrypoints/coverage.entrypoint.ts.md) and before [runtime/entrypoints/index.entrypoint.ts](https://banes-lab.com/source/docs/runtime/entrypoints/index.entrypoint.ts.md).

## Definitions

- `flagText` (lexical_declaration, line 25)
- `newDocument` (lexical_declaration, line 53)
- `runValidate` (lexical_declaration, line 41)
- `run` (lexical_declaration, line 69)
- `checkScripts` (lexical_declaration, line 29)
- `manifests` (lexical_declaration, line 30)
- `dead` (lexical_declaration, line 31)
- `docFindings` (lexical_declaration, line 42)
- `deadScripts` (lexical_declaration, line 43)
- `type` (lexical_declaration, line 54)
- `host` (lexical_declaration, line 70)

## Contained in

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

## Uses

- [configuration/strings/finding.strings.ts](https://banes-lab.com/source/docs/configuration/strings/finding.strings.ts.md)
- [core/coordinators/catalog.coordinator.ts](https://banes-lab.com/source/docs/core/coordinators/catalog.coordinator.ts.md)
- [core/coordinators/markdown.coordinator.ts](https://banes-lab.com/source/docs/core/coordinators/markdown.coordinator.ts.md)
- [core/coordinators/template.coordinator.ts](https://banes-lab.com/source/docs/core/coordinators/template.coordinator.ts.md)
- [core/persistence/catalog.persistence.ts](https://banes-lab.com/source/docs/core/persistence/catalog.persistence.ts.md)
- [core/reporters/base.reporter.ts](https://banes-lab.com/source/docs/core/reporters/base.reporter.ts.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/docs/folder-docs-configuration-strings.md)
- [core/coordinators](https://banes-lab.com/anatomy/docs/folder-docs-core-coordinators.md)
- [core/persistence](https://banes-lab.com/anatomy/docs/folder-docs-core-persistence.md)
- [core/reporters](https://banes-lab.com/anatomy/docs/folder-docs-core-reporters.md)
- [runtime/entrypoints](https://banes-lab.com/anatomy/docs/folder-docs-runtime-entrypoints.md)

## Source

```typescript
import { FAILURE_EXIT, FLAG_NAMES } from "#configuration/constants/invocation.constants";
import { MARKDOWN_SUFFIX, PACKAGE_FILE } from "#configuration/constants/document.constants";
import {
    deadScriptPath,
    deadScriptsFailure,
    scriptSummary,
    strictFailure,
} from "#configuration/strings/finding.strings";
import { flagValue, hasFlag, resolveArgv } from "@govlab/argv";
import { print, printErr } from "#core/reporters/base.reporter";
import { DOCUMENT_ARGV } from "#configuration/configs/invocation.config";
import { DOCUMENT_USAGE } from "#configuration/strings/invocation.strings";
import { DocValidator } from "#core/coordinators/validation.coordinator";
import type { DocsHost } from "#types/environment.types";
import type { ParsedArgv } from "@govlab/argv";
import { ROOT } from "@ssot/paths";
import { deadScriptRefs } from "#core/validators/package.validator";
import { docsHostFor } from "#core/factories/environment.factory";
import { indexDirectory } from "#core/persistence/catalog.persistence";
import process from "node:process";
import { runCatalog } from "#core/coordinators/catalog.coordinator";
import { runFix } from "#core/coordinators/markdown.coordinator";
import { runNew } from "#core/coordinators/template.coordinator";

const flagText = function flagText(argv: ParsedArgv, name: string): string {
    return flagValue(argv, name) ?? "";
};

const checkScripts = function checkScripts(host: DocsHost): number {
    const manifests = host.walk(host.root, PACKAGE_FILE);
    const dead = manifests.flatMap((pkgPath) =>
        deadScriptRefs(pkgPath).map((ref) => deadScriptPath(host.relative(pkgPath), ref.script, ref.path)),
    );
    for (const line of dead) {
        printErr(line);
    }
    print(scriptSummary(dead.length, manifests.length));
    return dead.length;
};

const runValidate = async function runValidate(host: DocsHost, strict: boolean): Promise<boolean> {
    const docFindings = await new DocValidator(host.ctx).run(host.walk(host.root, MARKDOWN_SUFFIX));
    const deadScripts = checkScripts(host);
    if (deadScripts > 0) {
        printErr(deadScriptsFailure(deadScripts));
    }
    if (strict && docFindings > 0) {
        printErr(strictFailure(docFindings));
    }
    return deadScripts === 0 && !(strict && docFindings > 0);
};

const newDocument = function newDocument(host: DocsHost, argv: ParsedArgv): void {
    const type = flagText(argv, FLAG_NAMES.type);
    runNew(
        { locationOptions: host.locationOptions, registries: host.registries, root: host.root, userReg: host.userReg },
        {
            concern: flagText(argv, FLAG_NAMES.concern),
            form: type.length > 0 ? type : flagText(argv, FLAG_NAMES.form),
            member: flagText(argv, FLAG_NAMES.member),
            name: flagText(argv, FLAG_NAMES.name),
            status: flagText(argv, FLAG_NAMES.status),
            subject: flagText(argv, FLAG_NAMES.subject),
            summary: flagText(argv, FLAG_NAMES.docSummary),
        },
    );
};

const run = async function run(argv: ParsedArgv): Promise<boolean> {
    const host = await docsHostFor(ROOT);
    if (hasFlag(argv, FLAG_NAMES.fix)) {
        runFix(host);
        return true;
    }
    if (hasFlag(argv, FLAG_NAMES.validate)) {
        return runValidate(host, hasFlag(argv, FLAG_NAMES.strict));
    }
    if (hasFlag(argv, FLAG_NAMES.catalog)) {
        await runCatalog(
            {
                docs: host.walk(host.root, MARKDOWN_SUFFIX),
                relative: host.relative,
                root: host.root,
                rootPrefix: host.rootPrefix,
            },
            indexDirectory(),
        );
        return true;
    }
    if (hasFlag(argv, FLAG_NAMES.new)) {
        newDocument(host, argv);
        return true;
    }
    printErr(DOCUMENT_USAGE);
    return false;
};

try {
    process.exitCode = (await run(resolveArgv(DOCUMENT_ARGV))) ? 0 : FAILURE_EXIT;
} catch (error) {
    printErr(error instanceof Error ? error.message : String(error));
    process.exitCode = FAILURE_EXIT;
}
```
