# All files / src/helpers insert-code.helper.ts

97.64% Statements 166/170
83.33% Branches 40/48
100% Functions 5/5
97.64% Lines 166/170

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    1711x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    2x
    2x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    1x
    1x
    15x
    15x
    15x
    15x
    2x
    2x
    15x
    15x
    15x
    15x
    1x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    1x
    1x
    26x
    26x
    26x
    26x
    27x
    27x
    27x
    12x
    12x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    1x
    1x
    1x
    1x
    1x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    1x
    1x
    1x
    1x
    13x
    13x
    13x
    13x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x

    14x
    14x
    14x
    14x
    2x
    2x
    14x
    14x
    14x
    1x
    13x
    13x
    27x
    13x
    13x
    1x
    1x
    1x
    40x
    40x
    40x
    40x
    40x
    40x
    105x
    105x
    27x
    27x
    105x
    40x
    40x
    40x
     import { IInsertCodeOptions } from '../contracts';
    import { filterDoubleBlankLines, findEscapeSequence, splitContent } from './line-ending.helper';
    import { isAbsolute, resolve, dirname, join } from 'path';
    import { promisify } from 'util';
    import { readFile, writeFile } from 'fs';
    import chalk from 'chalk';

    const asyncReadFile = promisify(readFile);
    const asyncWriteFile = promisify(writeFile);

    export type FileDetails = {
        filePath: string;
        fileContent: string;
    };

    /**
     * Loads content from other files and inserts it into the target file
     * @param input - if a string is provided the target file is loaded from that path AND saved to that path once content has been inserted. If a `FileDetails` object is provided the content is not saved when done.
     * @param partialOptions - optional. changes the default tokens
     */
    export async function insertCode(
        input: FileDetails | string,
        partialOptions?: Partial<IInsertCodeOptions>,
    ): Promise<string> {
        const options: IInsertCodeOptions = { removeDoubleBlankLines: false, ...partialOptions };

        let fileDetails: FileDetails;

        if (typeof input === 'string') {
            const filePath = resolve(input);
            console.log(`Loading existing file from '${chalk.blue(filePath)}'`);
            fileDetails = { filePath, fileContent: (await asyncReadFile(filePath)).toString() };
        } else {
            fileDetails = input;
        }

        const content = fileDetails.fileContent;

        const lineBreak = findEscapeSequence(content);
        let lines = splitContent(content);

        lines = await insertCodeImpl(fileDetails.filePath, lines, options, 0);

        if (options.removeDoubleBlankLines) {
            lines = lines.filter((line, index, lines) => filterDoubleBlankLines(line, index, lines));
        }

        const modifiedContent = lines.join(lineBreak);

        if (typeof input === 'string') {
            console.log(`Saving modified content to '${chalk.blue(fileDetails.filePath)}'`);
            await asyncWriteFile(fileDetails.filePath, modifiedContent);
        }

        return modifiedContent;
    }

    async function insertCodeImpl(
        filePath: string,
        lines: string[],
        options: IInsertCodeOptions,
        startLine: number,
    ): Promise<string[]> {
        const insertCodeBelow = options?.insertCodeBelow;
        const insertCodeAbove = options?.insertCodeAbove;

        if (insertCodeBelow == null) {
            return Promise.resolve(lines);
        }

        const insertCodeBelowResult =
            insertCodeBelow != null
                ? findIndex(lines, (line) => line.indexOf(insertCodeBelow) === 0, startLine)
                : undefined;

        if (insertCodeBelowResult == null) {
            return Promise.resolve(lines);
        }

        const insertCodeAboveResult =
            insertCodeAbove != null
                ? findIndex(lines, (line) => line.indexOf(insertCodeAbove) === 0, insertCodeBelowResult.lineIndex)
                : undefined;

        const linesFromFile = await loadLines(filePath, options, insertCodeBelowResult);

        const linesBefore = lines.slice(0, insertCodeBelowResult.lineIndex + 1);
        const linesAfter = insertCodeAboveResult != null ? lines.slice(insertCodeAboveResult.lineIndex) : [];

        lines = [...linesBefore, ...linesFromFile, ...linesAfter];

        return insertCodeAboveResult == null
            ? lines
            : insertCodeImpl(filePath, lines, options, insertCodeAboveResult.lineIndex);
    }

    const fileRegExp = /file="([^"]+)"/;
    const codeCommentRegExp = /codeComment(="([^"]+)")?/; //https://regex101.com/r/3MVdBO/1
    const snippetRegExp = /snippetName="([^"]+)"/;

    async function loadLines(
        targetFilePath: string,
        options: IInsertCodeOptions,
        result: FindLineResults,
    ): Promise<string[]> {
        const partialPathResult = fileRegExp.exec(result.line);

        if (partialPathResult == null) {
            throw new Error(
                `insert code token (${options.insertCodeBelow}) found in file but file path not specified (file="relativePath/from/markdown/toFile.whatever")`,
            );
        }
        const codeCommentResult = codeCommentRegExp.exec(result.line);
        const snippetResult = snippetRegExp.exec(result.line);
        const partialPath = partialPathResult[1];

        const filePath = isAbsolute(partialPath) ? partialPath : join(dirname(targetFilePath), partialPathResult[1]);
        console.log(`Inserting code from '${chalk.blue(filePath)}' into '${chalk.blue(targetFilePath)}'`);

        const fileBuffer = await asyncReadFile(filePath);

        let contentLines = splitContent(fileBuffer.toString());

        const copyBelowMarker = options.copyCodeBelow;
        const copyAboveMarker = options.copyCodeAbove;

        const copyBelowIndex =
            copyBelowMarker != null ? contentLines.findIndex(findLine(copyBelowMarker, snippetResult?.[1])) : -1;
        const copyAboveIndex =
            copyAboveMarker != null
                ? contentLines.findIndex((line, index) => line.indexOf(copyAboveMarker) === 0 && index > copyBelowIndex)
                : -1;

        if (snippetResult != null && copyBelowIndex < 0) {
            throw new Error(
                `The copyCodeBelow marker '${options.copyCodeBelow}' was not found with the requested snippet: '${snippetResult[1]}'`,
            );
        }

        contentLines = contentLines.slice(copyBelowIndex + 1, copyAboveIndex > 0 ? copyAboveIndex : undefined);

        if (codeCommentResult != null) {
            contentLines = ['```' + (codeCommentResult[2] ?? ''), ...contentLines, '```'];
        }

        return contentLines;
    }

    function findLine(copyBelowMarker: string, snippetName?: string): (line: string) => boolean {
        return (line: string): boolean => {
            return line.indexOf(copyBelowMarker) === 0 && (snippetName == null || line.indexOf(snippetName) > 0);
        };
    }

    type FindLineResults = { line: string; lineIndex: number };

    function findIndex(
        lines: string[],
        predicate: (line: string) => boolean,
        startLine: number,
    ): FindLineResults | undefined {
        for (let lineIndex = startLine; lineIndex < lines.length; lineIndex++) {
            const line = lines[lineIndex];
            if (predicate(line)) {
                return { lineIndex, line };
            }
        }

        return undefined;
    }