all files / lib/common/validate/ index.js

20% Statements 4/20
0% Branches 0/6
33.33% Functions 1/3
20% Lines 4/20
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    1× 1×             1×                                                                                                     1×  
'use strict';
 
const _      = require('lodash');
const colors = require('colors');
 
/**
 * Class that compiles some reuirements needed for deployment
 */
class Validate {
  constructor(sqz) {
    this.sqz = sqz;
  }
 
  /**
   * Validate stages
   *
   * @param stages
   * @returns {boolean}
   */
  stage(stages) {
    const stage = this.sqz.vars.stage;
 
    if (!_.isEmpty(stages)) {
      if (!_.includes(stages, stage)) {
        return false;
      }
    }
 
    return true;
  }
 
  /**
   * Check if Node defined project's platform it's compatible with the current runtime
   */
  platform() {
    const projectRuntime = this.sqz.vars.project.runtime.name;
    const versionCheck    = (projectVersion, runtimeVersion) => {
      if (projectVersion !== runtimeVersion) {
        this.sqz.cli.log.warn(
          `Your ${colors.blue.bold(projectRuntime)} runtime version ( ${colors.blue.bold(runtimeVersion)} ) is different from ` +
          `the one specified in the project's config ( ${colors.blue.bold(projectVersion)} )`
        );
      }
    };
 
    const types = {
      nodejs : {
        fn : () => {
          const projectVersionSplit = this.sqz.vars.project.runtime.version.toString().replace(/[^0-9.]/gi, '').split('.');
          const projectVersion = `${projectVersionSplit[0]}.${projectVersionSplit[1]}`;
          const runtimeVersionSplit = process.version.toString().replace(/[^0-9.]/gi, '').split('.');
          const runtimeVersion = `${runtimeVersionSplit[0]}.${runtimeVersionSplit[1]}`;
 
          versionCheck(projectVersion, runtimeVersion);
        }
      }
    };
    types[projectRuntime].fn();
  }
}
 
module.exports = Validate;