generators/utils.js
function rewrite(args) {
    // check if splicable is already in the body text
    const re = new RegExp(args.splicable.map(line => `\\s*${escapeRegExp(line)}`).join('\n'));

    if (re.test(args.haystack)) {
        return args.haystack;
    }

    const lines = args.haystack.split('\n');

    let otherwiseLineIndex = -1;
    lines.forEach((line, i) => {
        if (line.includes(args.needle)) {
            otherwiseLineIndex = i;
        }
    });

    let spaces = 0;
    while (lines[otherwiseLineIndex].charAt(spaces) === ' ') {
        spaces += 1;
    }

    let spaceStr = '';

    // eslint-disable-next-line no-cond-assign
    while ((spaces -= 1) >= 0) {
        spaceStr += ' ';
    }

    lines.splice(otherwiseLineIndex, 0, args.splicable.map(line => spaceStr + line).join('\n'));

    return lines.join('\n');
}
function rewrite(args) {
    // check if splicable is already in the body text
    const re = new RegExp(args.splicable.map(line => `\\s*${escapeRegExp(line)}`).join('\n'));

    if (re.test(args.haystack)) {
        return args.haystack;
    }

    const lines = args.haystack.split('\n');

    let otherwiseLineIndex = -1;
    lines.forEach((line, i) => {
        if (line.includes(args.needle)) {
            otherwiseLineIndex = i;
        }
    });

    let spaces = 0;
    while (lines[otherwiseLineIndex].charAt(spaces) === ' ') {
        spaces += 1;
    }

    let spaceStr = '';

    // eslint-disable-next-line no-cond-assign
    while ((spaces -= 1) >= 0) {
        spaceStr += ' ';
    }

    lines.splice(otherwiseLineIndex, 0, args.splicable.map(line => spaceStr + line).join('\n'));

    return lines.join('\n');
}
function rewrite ( args ) { // check if splicable is already in the body text const re = new RegExp ( args . splicable . map ( line => ` \\ ${ escapeRegExp ( line ) } ` ) . join ( '\n' ) ) ; if ( re . test ( args . haystack ) ) { return args . haystack ; } const lines = args . haystack . split ( '\n' ) ; let otherwiseLineIndex = - 1 ; lines . forEach ( ( line , i ) => { if ( line . includes ( args . needle ) ) { otherwiseLineIndex = i ; } } ) ; let spaces = 0 ; while ( lines [ otherwiseLineIndex ] . charAt ( spaces ) === ' ' ) { spaces += 1 ; } let spaceStr = '' ; // eslint-disable-next-line no-cond-assign while ( ( spaces -= 1 ) >= 0 ) { spaceStr += ' ' ; } lines . splice ( otherwiseLineIndex , 0 , args . splicable . map ( line => spaceStr + line ) . join ( '\n' ) ) ; return lines . join ( '\n' ) ; }
Rewrite using the passed argument object.

@param {object} args arguments object (containing splicable, haystack, needle properties) to be used
@returns {*} re-written file
Rewrite using the passed argument object .