All files parseArgs.ts

94.73% Statements 18/19
75% Branches 3/4
100% Functions 4/4
94.73% Lines 18/19

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 1082x 2x 2x             9x 9x 9x 9x                                             2x                       9x                         9x         9x       9x       9x         9x       9x         9x         9x                       9x      
import { ArgumentParser } from 'argparse';
import fs from 'fs';
import path from 'path';
 
/**
 * Reads the version from the nearest package.json at runtime.
 * This works both in the source tree and after compilation to dist/.
 */
function getVersion(): string {
    try {
        const pkgPath = path.resolve(__dirname, '..', 'package.json');
        const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
        return pkg.version ?? 'unknown';
    } catch {
        return 'unknown';
    }
}
 
/**
 * Interface for the parsed CLI arguments.
 */
export interface ParsedArgs {
    environment: string;
    env_dir: string;
    dry_run: boolean;
    backup_name: string;
    include_environment: boolean;
    replace: boolean;
    verbose: number;
}
 
/**
 * 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: getVersion(),
        });
    }
 
    /**
     * Parses the cli inputs
     *
     * @returns An object with the parsed parameters
     */
    public parseArgs(): ParsedArgs {
        return this.parser.parse_args();
    }
}