All files / rusted-chromium/config config.ts

100% Statements 26/26
100% Branches 33/33
100% Functions 1/1
100% Lines 26/26

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    2x   2x 2x     2x   2x 2x         2x 24x                                                 24x   24x 24x   24x   23x 1x   23x 1x     23x 23x   23x 1x                   22x 2x                   20x 1x                       1x           19x                                                
 
/* eslint-disable-next-line import/no-namespace */
import * as program from 'commander'
 
import { ComparableVersion } from '../commons/ComparableVersion'
import { DEFAULT_CONFIG_OPTIONS } from '../commons/constants'
import type { IConfigOptions } from '../interfaces/config.interfaces'
import type { ConfigWrapper, IChromeSingleConfig } from '../interfaces/interfaces'
import { logger } from '../log/logger'
/* eslint-disable-next-line import/no-namespace */
import * as packageJson from '../package.json'
import { mapOS } from '../utils'
 
/**
 * Checks the arguments passed to the programm and returns them
 */
export function readConfig(args: string[], platform: NodeJS.Platform): ConfigWrapper {
    program
        .version(packageJson.version)
        .option('-m, --min <version>', 'The minimum version', DEFAULT_CONFIG_OPTIONS.min)
        .option('-M, --max <version>', 'The maximum version. Newest version if not specificied', DEFAULT_CONFIG_OPTIONS.max)
        .option('-r, --max-results <results>', 'The maximum amount of results to choose from', null)
        .option('-o, --os <os>', 'The operating system for what the binary should be downloaded')
        .option('-a, --arch <arch>', 'The architecture for what the binary should be downloaded. Valid values are "x86" and "x64". Only works when --os is also set')
        .option('-d, --decrease-on-fail', 'If a binary does not exist, go to the next lower version number and try again (regarding --min, --max and --max-results)', false)
        .option('-i, --increase-on-fail', 'If a binary does not exist, go to the next higher version number and try again (regarding --min, --max and --max-results), overwrites "--decrease-on-fail" if both set', false)
        .option('-z, --unzip', 'Directly unzip the downloaded zip-file and delete the .zip afterwards', DEFAULT_CONFIG_OPTIONS.unzip)
        .option('-n, --non-interactive', 'Don\'t show the selection menu. Automatically select the newest version. Only works when --decrease-on-fail is also set.', false)
        .option('-t, --no-store', 'Don\'t store negative hits in the local store file.', DEFAULT_CONFIG_OPTIONS.store)
        .option('-l, --no-download', 'Don\'t download the binary. It also continues with the next version, if --decrease-on-fail or --increase-on-fail is set. Useful to build up the negative hit store', DEFAULT_CONFIG_OPTIONS.download)
        .option('-I, --import-store <url>', 'Imports a localstore.json file either by URL (starting with "http://" or "https://" or by local file')
        .option('-E, --export-store [path]', 'Exports the localstore.json file to stdout')
        .option('-H, --hide-negative-hits', 'Hide negative hits', DEFAULT_CONFIG_OPTIONS.hideNegativeHits)
        .option('-f, --folder <folder>', 'Set the download folder', null)
        .option('-O, --only-newest-major', 'Show only the newest major version in user selection', DEFAULT_CONFIG_OPTIONS.onlyNewestMajor)
        .option('-v, --inverse', 'Sort the selectable versions ascending', DEFAULT_CONFIG_OPTIONS.inverse)
        .option('-s, --single <version>', 'Download a specific version in non-interactive mode, even if the file is listed in the localstore.json. Several other flags have no effect.')
        .option('--list', 'List versions matching the criteria, doing nothing more', DEFAULT_CONFIG_OPTIONS.list)
        .option('-q, --quiet', 'Suppress any logging output', DEFAULT_CONFIG_OPTIONS.quiet)
        .option('--debug', 'Activates the debug mode (extended logging)', DEFAULT_CONFIG_OPTIONS.debug)
        .parse(args)
 
    const options = program.opts() as IConfigOptions
 
    const minIsSet = +options.min > 0
    const maxResultsIsSet = !!options.maxResults
 
    const os = mapOS(options.os || platform)
 
    if (!options.os && options.arch) {
        logger.warn('Setting "--arch" has no effect, when "--os" is not set!')
    }
    if (options.nonInteractive && !options.decreaseOnFail) {
        logger.warn('Setting "--non-interactive" has no effect, when "--decrease-on-fail" is not set!')
    }
 
    const is64Bit = (options.os && options.arch) ? options.arch === 'x64' : true
    const arch = is64Bit ? 'x64' : 'x86'
 
    if (options.importStore) {
        return {
            action: 'importStore',
            config: {
                url: options.importStore,
                quiet: options.quiet,
                debug: options.debug,
            },
        }
    }
 
    if (options.exportStore !== undefined) {
        return {
            action: 'exportStore',
            config: {
                path: typeof options.exportStore === 'string' ? options.exportStore : undefined,
                quiet: options.quiet,
                debug: options.debug,
            }
        }
    }
 
    if (options.single) {
        const config: IChromeSingleConfig = {
            arch,
            os,
            autoUnzip: options.unzip,
            store: options.store,
            download: options.download,
            downloadFolder: options.folder || null,
            single: new ComparableVersion(options.single),
            quiet: options.quiet,
            debug: options.debug,
        }
 
        return {
            action: 'loadChrome',
            config,
        }
    }
 
    return {
        action: 'loadChrome',
        config: {
            arch,
            autoUnzip: options.unzip,
            debug: options.debug,
            download: options.download,
            downloadFolder: options.folder || null,
            hideNegativeHits: options.hideNegativeHits,
            interactive: !options.nonInteractive,
            inverse: options.inverse,
            list: options.list,
            max: new ComparableVersion(options.max),
            min: new ComparableVersion(options.min),
            onFail: options.increaseOnFail ? 'increase' : options.decreaseOnFail ? 'decrease' : 'nothing',
            onlyNewestMajor: options.onlyNewestMajor,
            os,
            quiet: options.quiet,
            results: minIsSet && !maxResultsIsSet ? Infinity : (parseInt(options.maxResults as string, 10) || 10),
            single: null,
            store: options.store,
        },
    }
}