All files copy.helper.ts

89.19% Statements 33/37
80% Branches 8/10
100% Functions 3/3
89.19% Lines 33/37

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 381x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 15x 5x 5x 5x 3x 3x 3x     3x 1x 8x 8x 8x 8x 1x     1x 1x 8x  
import { readFileSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
 
export function copyPartial(
    sourcePath: string,
    targetPaths: string[],
    keys: string[],
    jsonIndent: number,
    gitAdd: boolean,
): void {
    const source = loadJson(sourcePath, true);
 
    targetPaths.forEach((path) => {
        const targetFile = loadJson(path);
 
        keys.forEach((key) => {
            targetFile[key] = source[key];
        });
 
        writeFileSync(path, JSON.stringify(targetFile, undefined, jsonIndent));
    });
 
    if (gitAdd) {
        execSync(`git add ${targetPaths.join(' ')}`);
    }
}
 
function loadJson(path: string, throwError = false) {
    try {
        return JSON.parse(readFileSync(path).toString());
    } catch (e) {
        if (throwError) {
            throw e;
        }
        return {};
    }
}