All files / wdclib/DevUtils BuildNumber.js

0% Statements 0/29
0% Branches 0/22
0% Functions 0/2
0% Lines 0/28
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                                                                                                                                                                   
import { version as BUILD_NUMBER } from '../package.json';
export let WDC_LIB_PREFIX = 'tableauwdc-';
 
/**
 *
 */
export class VersionNumber {
    /**
     *
     * @param {String} versionString
     */
    constructor (versionString) {
        let components = versionString.split('.');
 
        if (components.length < 3) {
            console.log();
            throw new Error(`Invalid number of components. versionString was  ${versionString} `);
        }
 
        this.major = parseInt(components[0]).toString();
        this.minor = parseInt(components[1]).toString();
        this.patch = parseInt(components[2]).toString();
    }
 
    /**
     * @returns {String}
     */
    toString () {
        return `${this.major}.${this.minor}.${this.patch}`;
    }
 
    /**
     *
     * @param {{
     *          major: (String),
     *          minor: (String),
     *          patch: (String)
     *         }}  version Input version to compare against this.version
     *
     * @returns {Number}
     */
    compare ({ major, minor, patch } = {}) {
        let majorDiff = this.major - major;
        let minorDiff = this.minor - minor;
        let patchDiff = this.patch - patch;
 
        if (majorDiff !== 0) {
            return majorDiff;
        }
 
        if (minorDiff !== 0) {
            return minorDiff;
        }
 
        if (patchDiff !== 0) {
            return patchDiff;
        }
 
        return 0;
    }
}
 
/**
 *
 * @param {Object} config
 * @returns {String}
 */
export function getBuildNumber ({ showLog = false } = {}) {
    // Single source of truth for version is package json
    // which is stored at the top as BUILD_NUMBER constant
 
    if (!BUILD_NUMBER) {
        throw new Error(`Unable to retrieve version number from package.json`);
    }
 
    if (showLog) {
        console.log(`Found versionNumber in package.json: ${BUILD_NUMBER}`);
    }
 
    return BUILD_NUMBER;
}