# runtime/entrypoints/target.entrypoint.ts

> 82 lines of code and 20 definitions.

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

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

## Definitions

- `report` (lexical_declaration, line 68)
- `inspectFile` (lexical_declaration, line 52)
- `REPLACEMENT` (lexical_declaration, line 25)
- `Offender` (interface_declaration, line 27)
- `argv` (lexical_declaration, line 33)
- `fix` (lexical_declaration, line 34)
- `[rootArg]` (lexical_declaration, line 35)
- `root` (lexical_declaration, line 36)
- `isExcluded` (lexical_declaration, line 37)
- `collectConfigs` (lexical_declaration, line 39)
- `entries` (lexical_declaration, line 40)
- `here` (lexical_declaration, line 41)
- `subdirs` (lexical_declaration, line 44)
- `nested` (lexical_declaration, line 48)
- `text` (lexical_declaration, line 53)
- `stale` (lexical_declaration, line 54)
- `rel` (lexical_declaration, line 58)
- `tokens` (lexical_declaration, line 59)
- `result` (lexical_declaration, line 60)
- `inspected` (lexical_declaration, line 85)

## Contained in

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

## Uses

- [configuration/strings/target.strings.ts](https://banes-lab.com/source/quality/configuration/strings/target.strings.ts.md)

## Enforces

- [Standardization](https://banes-lab.com/records/architecture/standardization.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/quality/folder-quality-configuration-strings.md)

## Source

```typescript
import { ENFORCED_ES, TARGET_CONFIG_BASENAMES } from "#configuration/constants/target.constants";
import { FAILURE_EXIT, FLAGS } from "#configuration/constants/invocation.constants";
import { hasFlag, resolveArgv } from "@govlab/argv";
import {
    targetClean,
    targetFatal,
    targetFixed,
    targetLine,
    targetStale,
    targetSummary,
} from "#configuration/strings/target.strings";
import { TARGET_ARGV } from "#configuration/configs/invocation.config";
import { bumpEsTokens } from "#core/converters/target.converter";
import { defineCheck } from "@govlab/context/check";
import { excludeMatcher } from "#core/factories/exclusions.factory";
import { findStaleEsTokens } from "#core/matchers/target.matcher";
import { promises as fs } from "node:fs";
import path from "node:path";
import process from "node:process";
import { safeReaddir } from "#core/loaders/source.loader";
import { writeVerbatim } from "@govlab/canonical-write";

defineCheck({ detects: [], enforces: ["architecture:standardization"] });

const REPLACEMENT = `ES${String(ENFORCED_ES)}`;

interface Offender {
    fixed: boolean;
    rel: string;
    tokens: string[];
}

const argv = resolveArgv(TARGET_ARGV);
const fix = hasFlag(argv, FLAGS.fix);
const [rootArg] = argv.positionals;
const root = rootArg === undefined ? process.cwd() : path.resolve(rootArg);
const isExcluded = await excludeMatcher(process.cwd());

const collectConfigs = async function collectConfigs(dir: string): Promise<string[]> {
    const entries = safeReaddir(dir);
    const here = entries
        .filter((entry) => !entry.isDirectory() && TARGET_CONFIG_BASENAMES.has(entry.name))
        .map((entry) => path.join(dir, entry.name));
    const subdirs = entries
        .filter((entry) => entry.isDirectory())
        .map((entry) => path.join(dir, entry.name))
        .filter((subdir) => !isExcluded(subdir));
    const nested = await Promise.all(subdirs.map(async (subdir) => collectConfigs(subdir)));
    return [...here, ...nested.flat()];
};

const inspectFile = async function inspectFile(file: string): Promise<Offender | null> {
    const text = await fs.readFile(file, "utf8");
    const stale = findStaleEsTokens(text, ENFORCED_ES);
    if (stale.length === 0) {
        return null;
    }
    const rel = path.relative(root, file).split("\\").join("/");
    const tokens = stale.map((entry) => entry.token);
    const result = fix ? bumpEsTokens(text, ENFORCED_ES, REPLACEMENT) : { changed: false, text };
    if (result.changed) {
        writeVerbatim(file, result.text);
        process.stdout.write(targetFixed(rel, tokens.join(", "), REPLACEMENT));
    }
    return { fixed: result.changed, rel, tokens };
};

const report = function report(offenders: readonly Offender[]): void {
    if (fix) {
        process.stdout.write(targetSummary(offenders.filter((offender) => offender.fixed).length, REPLACEMENT));
        return;
    }
    if (offenders.length === 0) {
        process.stdout.write(targetClean(REPLACEMENT));
        return;
    }
    process.stderr.write(targetStale(offenders.length, REPLACEMENT));
    for (const offender of offenders) {
        process.stderr.write(targetLine(offender.rel, offender.tokens.join(", ")));
    }
    process.exitCode = FAILURE_EXIT;
};

try {
    const inspected = await Promise.all((await collectConfigs(root)).map(inspectFile));
    report(inspected.filter((result): result is Offender => result !== null));
} catch (error: unknown) {
    process.stderr.write(targetFatal(error instanceof Error ? error.message : String(error)));
    process.exitCode = FAILURE_EXIT;
}
```
