All files worker.ts

100% Statements 36/36
100% Branches 12/12
100% Functions 6/6
100% Lines 36/36

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 1292x 2x   2x 2x         2x                         9x           9x 9x   9x             6x 2x 1x   1x     4x 4x 4x 4x                         5x 1x   4x 4x 3x       5x                     4x                               6x 1x   5x     6x 1x     6x                     4x 1x 1x 1x 1x   3x        
import { ParseArgs } from './parseArgs';
import { getLogger } from './logger';
 
import merge from 'lodash.merge';
import { Utils } from './utils';
 
/**
 * The main class of the Node Package Builder. Handels the whole workflow.
 */
export class Worker {
    /**
     * The Utils class
     *
     * @private
     */
    private readonly utils: Utils;
 
    /**
     * The logger
     *
     * @private
     */
    private readonly logger = getLogger('worker');
 
    /**
     * The constructor parses the cli arguments and instantiate the Util class.
     */
    constructor() {
        const parsedArgs: ParseArgs = new ParseArgs();
        parsedArgs.build();
 
        this.utils = new Utils(parsedArgs.parseArgs());
    }
 
    /**
     * Main method. Starts the workflow.
     */
    public start() {
        if (this.utils.isResetEnvironment()) {
            if (this.utils.isDryRun()) {
                this.logger.warn(`Won't reset package.json because of "dry-run" enabled...`);
            } else {
                this.utils.restoreBackup();
            }
        } else {
            const packageJson = this.getPackageJson();
            const environmentJson = this.getEnvironmentJson();
            const mergedPackage = this.mergeJson(packageJson, environmentJson);
            this.writeNewPackage(mergedPackage);
        }
    }
 
    /**
     * Loads the package.json file.
     *
     * @returns The package.json as object
     *
     * @private
     */
    private getPackageJson(): any {
        let packageString;
        if (this.utils.isBackupExisting()) {
            packageString = this.utils.loadBackupJson();
        } else {
            packageString = this.utils.loadPackageJson();
            if (!this.utils.isDryRun()) {
                this.utils.backupPackageJson();
            }
        }
 
        return JSON.parse(packageString);
    }
 
    /**
     * Loads the environment file.
     *
     * @returns Environment as object
     *
     * @private
     */
    private getEnvironmentJson(): any {
        return JSON.parse(this.utils.loadEnvironmentJson());
    }
 
    /**
     * Does the merge of the two package.json parts. If "replace" is passed via parameter, only the environment file content is used. (So no merge is done)
     *
     * @param packageJson The package.json object
     * @param environmentJson The environment object
     *
     * @returns The merged/replaced package.json object
     *
     * @private
     */
    private mergeJson(packageJson: any, environmentJson: any): any {
        let merged;
 
        if (this.utils.isReplace()) {
            merged = environmentJson;
        } else {
            merged = merge({}, packageJson, environmentJson);
        }
 
        if (this.utils.isIncludeEnvironment()) {
            merged.npbEnv = [this.utils.getEnvironment()];
        }
 
        return merged;
    }
 
    /**
     * Saved the new package.json to disk or displays it to console if "dry-run" is passed via cli.
     *
     * @param mergedPackage New package.json content object
     *
     * @private
     */
    private writeNewPackage(mergedPackage: any) {
        if (this.utils.isDryRun()) {
            this.logger.info('##################################');
            this.logger.info('######### DRY-RUN output #########');
            this.logger.info(JSON.stringify(mergedPackage, null, 4));
            this.logger.info('##################################');
        } else {
            this.utils.savePackageJson(mergedPackage);
        }
    }
}