# core/steps/remote.step.ts

> 31 lines of code and 6 definitions.

Tree: Bane's Lab Deploy
Language: typescript
Layer: application
Canonical: https://banes-lab.com/anatomy/deploy#file-deploy-core-steps-remote-step-ts
Source text: https://banes-lab.com/source/deploy/core/steps/remote.step.ts.txt

Listed in [core/steps](https://banes-lab.com/api/source/deploy/core/steps.md), after [core/steps/release.step.ts](https://banes-lab.com/source/deploy/core/steps/release.step.ts.md) and before [core/steps/search.step.ts](https://banes-lab.com/source/deploy/core/steps/search.step.ts.md).

## Definitions

- `pruneRemoteRoot` (lexical_declaration, line 21, exported)
- `OWNED` (lexical_declaration, line 10)
- `listStale` (lexical_declaration, line 12)
- `guards` (lexical_declaration, line 13)
- `result` (lexical_declaration, line 14)
- `stale` (lexical_declaration, line 23, exported)

## Contained in

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

## Used by

- [runtime/entrypoints/deployment.entrypoint.ts](https://banes-lab.com/source/deploy/runtime/entrypoints/deployment.entrypoint.ts.md)

## Linked from

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

## Source

```typescript
import type { Journal, Shell } from "#types/deployment.types";
import {
    PATH_SEPARATOR,
    REMOTE_ROOT,
    REMOTE_SITE,
    REMOTE_STAGING,
} from "#configuration/constants/deployment.constants";
import { ROOT_CLEAN, ROOT_PRUNED, ROOT_PRUNING, STALE_REMOVED } from "#configuration/strings/deployment.strings";

const OWNED = [REMOTE_SITE, REMOTE_STAGING].map((path) => path.slice(path.lastIndexOf(PATH_SEPARATOR) + 1));

const listStale = async function listStale(shell: Shell): Promise<string[]> {
    const guards = OWNED.map((name) => `! -name ${name}`).join(" ");
    const result = await shell.run(`find ${REMOTE_ROOT} -mindepth 1 -maxdepth 1 ${guards}`);
    return result.stdout
        .split("\n")
        .map((line) => line.trim())
        .filter((line) => line.length > 0);
};

export const pruneRemoteRoot = async function pruneRemoteRoot(shell: Shell, journal: Journal): Promise<string[]> {
    journal.log(ROOT_PRUNING);
    const stale = await listStale(shell);
    if (stale.length === 0) {
        journal.mark(ROOT_CLEAN);
        return stale;
    }
    await shell.run(`rm -rf ${stale.join(" ")}`);
    for (const path of stale) {
        journal.log(STALE_REMOVED + path);
    }
    journal.mark(ROOT_PRUNED);
    return stale;
};
```
