# core/steps/tree.step.ts

> 37 lines of code and 6 definitions.

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

Listed in [core/steps](https://banes-lab.com/api/source/build-scripts/core/steps.md), after [core/steps/text.step.ts](https://banes-lab.com/source/build-scripts/core/steps/text.step.ts.md).

## Definitions

- `run` (method_definition, line 17)
- `pruned` (lexical_declaration, line 21)
- `published` (lexical_declaration, line 22)
- `key` (lexical_declaration, line 23)
- `lines` (lexical_declaration, line 26)
- `sync` (lexical_declaration, line 28)

## Contained in

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

## Uses

- [core/coordinators/config.coordinator.ts](https://banes-lab.com/source/build-scripts/core/coordinators/config.coordinator.ts.md)

## Linked from

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

## Source

```typescript
import { ROOT, absolutePath } from "@ssot/paths";
import { publishedKeyOf, publishedPaths, treeDeclarations } from "#core/loaders/tree.loader";
import { defineStep } from "#core/factories/step.factory";
import { excludeMatcher } from "@govlab/quality/config";
import { existsSync } from "node:fs";
import { extractConfigs } from "#core/coordinators/config.coordinator";
import { join } from "node:path";
import { publishTree } from "#core/persistence/tree.persistence";
import { syncedLine } from "#configuration/strings/folder.strings";

defineStep({
    cache: null,
    modes: ["build"],
    name: "trees",
    needs: ["chapters"],
    phase: "close",
    async run() {
        if (!existsSync(absolutePath("app.configs"))) {
            await extractConfigs();
        }
        const pruned = await excludeMatcher(ROOT);
        const published = treeDeclarations().flatMap((declaration) => {
            const key = publishedKeyOf(declaration.tab);
            return key === null ? [] : [{ declaration, key }];
        });
        const lines = await Promise.all(
            published.map(async ({ declaration, key }) => {
                const sync = await publishTree(
                    join(ROOT, declaration.folder),
                    absolutePath(key),
                    publishedPaths(declaration, pruned),
                );
                return syncedLine(declaration.tab, sync);
            }),
        );
        return { gives: {}, line: lines.join("") };
    },
});
```
