All files / core/use-cases setup-flags.ts

91.3% Statements 21/23
50% Branches 4/8
100% Functions 5/5
90.48% Lines 19/21

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 581x     1x             1x     1x 1x 1x   1x     9x 9x   9x 414x 288x         126x                     126x       9x   9x         9x   1x   1x  
import { Flag, PUBLIC_FLAGS } from '../entities/public-flags';
import { CLIProgram } from '../entities/cli-program';
 
export class SetupFlags {
    private static instance: SetupFlags;
 
    public program: CLIProgram;
    public programOptions;
 
    constructor() {
        this.program = require('commander');
    }
 
    public static getInstance() {
        Eif (!SetupFlags.instance) {
            SetupFlags.instance = new SetupFlags();
        }
        return SetupFlags.instance;
    }
 
    public setup(pkg): CLIProgram {
        this.program.version(pkg.version).usage('<src> [options]');
 
        PUBLIC_FLAGS.forEach((publicFlag: Flag) => {
            if (publicFlag.hasOwnProperty('defaultValue')) {
                this.program.option(
                    publicFlag.flag,
                    publicFlag.description,
                    publicFlag.defaultValue
                );
            } else Iif (publicFlag.parsingFunction) {
                const defaultValue = publicFlag.stringifyDefaultValue
                    ? JSON.stringify(publicFlag.defaultValue)
                    : publicFlag.defaultValue;
                this.program.option(
                    publicFlag.flag,
                    publicFlag.description,
                    publicFlag.parsingFunction,
                    defaultValue
                );
            } else {
                this.program.option(publicFlag.flag, publicFlag.description);
            }
        });
 
        this.program.parse(process.argv);
 
        this.programOptions = this.program.opts();
 
        /**
         * TODO : return something wrapping program and handling internal configuration
         */
        return this.programOptions;
    }
}
 
export default SetupFlags.getInstance();