All files / src/helpers add-content.helper.ts

77.41% Statements 48/62
80.95% Branches 17/21
50% Functions 1/2
77.41% Lines 48/62

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 631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 11x 11x 11x 1x                              
import { IReplaceOptions } from '../contracts';
import { footerReplaceBelowMarker } from '../write-markdown.constants';
import { splitContent, findEscapeSequence, filterDoubleBlankLines } from './line-ending.helper';
 
/**
 * Adds or replaces content between 2 markers within a text string
 * @param inputString
 * @param content
 * @param options
 * @returns
 */
export function addContent(inputString: string, content: string | string[], options: IReplaceOptions): string {
    const replaceBelow = options?.replaceBelow;
    const replaceAbove = options?.replaceAbove;
    content = Array.isArray(content) ? content : [content];
 
    const lineBreak = findEscapeSequence(inputString);
    const lines = splitContent(inputString);
    const replaceBelowLine =
        replaceBelow != null ? lines.filter((line) => line.indexOf(replaceBelow) === 0)[0] : undefined;
    const replaceBelowIndex = replaceBelowLine != null ? lines.indexOf(replaceBelowLine) : -1;
    const replaceAboveLine =
        replaceAbove != null ? lines.filter((line) => line.indexOf(replaceAbove) === 0)[0] : undefined;
    const replaceAboveIndex = replaceAboveLine != null ? lines.indexOf(replaceAboveLine) : -1;
 
    if (replaceAboveIndex > -1 && replaceBelowIndex > -1 && replaceAboveIndex < replaceBelowIndex) {
        throw new Error(
            `The replaceAbove marker '${options.replaceAbove}' was found before the replaceBelow marker '${options.replaceBelow}'. The replaceBelow marked must be before the replaceAbove.`,
        );
    }
 
    const linesBefore = lines.slice(0, replaceBelowIndex + 1);
    const linesAfter = replaceAboveIndex >= 0 ? lines.slice(replaceAboveIndex) : [];
 
    const contentLines = content.reduce(
        (lines, currentContent) => [...lines, ...splitContent(currentContent)],
        new Array<string>(),
    );
 
    let allLines = [...linesBefore, ...contentLines, ...linesAfter];
 
    if (options.removeDoubleBlankLines) {
        allLines = allLines.filter((line, index, lines) => filterDoubleBlankLines(line, index, lines));
    }
 
    return allLines.join(lineBreak);
}
 
export function addCommandLineArgsFooter(fileContent: string): string {
    if (fileContent.indexOf(footerReplaceBelowMarker) < 0) {
        fileContent = `${fileContent}  

${footerReplaceBelowMarker}`;
    }

    const footerContent = `Markdown Generated by [ts-command-line-args](https://www.npmjs.com/package/ts-command-line-args)`;

    return addContent(fileContent, footerContent, {
        replaceBelow: footerReplaceBelowMarker,
        removeDoubleBlankLines: false,
    });
}