# core/converters/nginx.converter.ts

> 56 lines of code and 12 definitions.

Tree: Bane's Lab Deploy
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/deploy#file-deploy-core-converters-nginx-converter-ts
Source text: https://banes-lab.com/source/deploy/core/converters/nginx.converter.ts.txt

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

## Definitions

- `scriptNameOf` (lexical_declaration, line 29, exported)
- `LINE_END` (lexical_declaration, line 11)
- `SPACE` (lexical_declaration, line 12)
- `DIRECTIVE_END` (lexical_declaration, line 13)
- `errorLogOf` (lexical_declaration, line 15, exported)
- `words` (lexical_declaration, line 17, exported)
- `[directive, path]` (lexical_declaration, line 21, exported)
- `subjectEnd` (lexical_declaration, line 33, exported)
- `bareModules` (lexical_declaration, line 37)
- `visit` (lexical_declaration, line 38)
- `bare` (lexical_declaration, line 46)
- `scriptTextOf` (lexical_declaration, line 52, exported)

## Contained in

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

## Used by

- [core/steps/recovery.step.ts](https://banes-lab.com/source/deploy/core/steps/recovery.step.ts.md)

## Linked from

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

## Source

```typescript
import {
    ERROR_LOG_DIRECTIVE,
    NAME_SEPARATOR,
    NODE_PROTOCOL,
    SCRIPT_SOURCE_EXTENSION,
    SCRIPT_TARGET_EXTENSION,
} from "#configuration/constants/nginx.constants";
import { ERROR_LOG_MISSING } from "#configuration/strings/deployment.strings";
import ts from "typescript";

const LINE_END = "\n";
const SPACE = " ";
const DIRECTIVE_END = ";";

export const errorLogOf = function errorLogOf(config: string): string {
    for (const line of config.split(LINE_END)) {
        const words = line
            .trim()
            .split(SPACE)
            .filter((word) => word.length > 0);
        const [directive, path] = words;
        if (directive === ERROR_LOG_DIRECTIVE && path !== undefined) {
            return path.endsWith(DIRECTIVE_END) ? path.slice(0, -DIRECTIVE_END.length) : path;
        }
    }
    throw new Error(ERROR_LOG_MISSING);
};

export const scriptNameOf = function scriptNameOf(name: string): string {
    if (!name.endsWith(SCRIPT_SOURCE_EXTENSION)) {
        return name;
    }
    const subjectEnd = name.indexOf(NAME_SEPARATOR);
    return name.slice(0, subjectEnd) + SCRIPT_TARGET_EXTENSION;
};

const bareModules: ts.TransformerFactory<ts.SourceFile> = (context) => (file) => {
    const visit = (node: ts.Node): ts.Node => {
        if (
            !ts.isImportDeclaration(node) ||
            !ts.isStringLiteral(node.moduleSpecifier) ||
            !node.moduleSpecifier.text.startsWith(NODE_PROTOCOL)
        ) {
            return node;
        }
        const bare = context.factory.createStringLiteral(node.moduleSpecifier.text.slice(NODE_PROTOCOL.length));
        return context.factory.updateImportDeclaration(node, node.modifiers, node.importClause, bare, node.attributes);
    };
    return ts.visitEachChild(file, visit, context);
};

export const scriptTextOf = function scriptTextOf(name: string, source: string): string {
    if (!name.endsWith(SCRIPT_SOURCE_EXTENSION)) {
        return source;
    }
    return ts.transpileModule(source, {
        compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 },
        fileName: name,
        transformers: { before: [bareModules] },
    }).outputText;
};
```
