All files parseArgs.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 3/3
100% Lines 13/13

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 802x 2x           2x                       9x                         9x         9x       9x       9x         9x       9x         9x         9x                       9x      
import { ArgumentParser } from 'argparse';
import { version } from '../package.json';
 
/**
 * This class defines all arguments which can be passed to the Node Package Builder.
 * It also parses the cli input.
 */
export class ParseArgs {
    /**
     * Is the main ArgumentParser object.
     *
     * @private
     */
    private readonly parser: ArgumentParser;
 
    /**
     * The constructor (instantiates the ArgumentParser)
     */
    constructor() {
        this.parser = new ArgumentParser({
            description:
                'A node package helper which assists you in organizing your package.json for multiple environment.',
            add_help: true,
        });
    }
 
    /**
     * Here the whole arguments are build together.
     *
     * @returns The ParsArgs object
     */
    public build() {
        this.parser.add_argument('environment', {
            help: 'The name of the environment to apply to package.json',
            default: '__reset__',
            nargs: '?',
        });
        this.parser.add_argument('-e', '--env-dir', {
            help: 'path to environment files (default == envs)',
            default: 'envs',
        });
        this.parser.add_argument('-d', '--dry-run', {
            help: 'Shows only the result. No modification of package.json done (default == false)',
            action: 'store_true',
        });
        this.parser.add_argument('-b', '--backup-name', {
            help:
                'Name of the package.json backup file. Restored when calling without any environment. (default == .package.json)',
            default: '.package.json',
        });
        this.parser.add_argument('-i', '--include-environment', {
            help: 'Inserts a field into the modified package.json which contains the used environment',
            action: 'store_true',
        });
        this.parser.add_argument('-r', '--replace', {
            help:
                'Replaces the package.json instead of a merge. For this, the environment package.json has to be complete.',
            action: 'store_true',
        });
        this.parser.add_argument('-v', '--verbose', {
            help: 'Select level of verbosity (max: 2)',
            action: 'count',
            default: 0,
        });
        this.parser.add_argument('--version', {
            action: 'version',
            version,
        });
    }
 
    /**
     * Parses the cli inputs
     *
     * @returns An object with the parsed parameters
     */
    public parseArgs(): any {
        return this.parser.parse_args();
    }
}