all files / bin/ Squeezer.js

77.03% Statements 57/74
38.89% Branches 7/18
100% Functions 4/4
77.03% Lines 57/74
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157    1× 1× 1× 1×   1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1×       1×             1× 1× 1× 1× 1× 1× 1× 1× 1× 1×   1×   1×                               1×       1× 1× 1×       1×   1×     1× 5×   5×                               5×     1×                   1× 1×   1×           1×             1×   1× 19× 19× 19× 35× 35×     19×           1×                             1×  
'use strict';
 
const appRoot = require('app-root-path');
const _       = require('lodash');
const colors  = require('colors');
const fs      = require('fs');
 
const CommonArchiver   = require(`${appRoot}/lib/common/archiver`);
const CommonYaml       = require(`${appRoot}/lib/common/yaml`);
const CommonCommand    = require(`${appRoot}/lib/common/command`);
const CommonCliError   = require(`${appRoot}/lib/common/cli/error`);
const CommonCliLoader  = require(`${appRoot}/lib/common/cli/loader`);
const CommonCliLog     = require(`${appRoot}/lib/common/cli/log`);
const CommonCliParams  = require(`${appRoot}/lib/common/cli/params`);
const CommonCliHelp    = require(`${appRoot}/lib/common/cli/help`);
const CommonUtils      = require(`${appRoot}/lib/common/utils`);
const CommonLifecycle  = require(`${appRoot}/lib/common/lifecycle`);
const CommonVariables  = require(`${appRoot}/lib/common/variables`);
const CommonVersion    = require(`${appRoot}/lib/common/version`);
const CommonConfig     = require(`${appRoot}/lib/common/config`);
const CommonValidate   = require(`${appRoot}/lib/common/validate`);
const CommonChecksums  = require(`${appRoot}/lib/common/checksums`);
 
class Squeezer {
  init() {
    this.cli       = {
      params : new CommonCliParams(this),
      error  : new CommonCliError(this),
      loader : new CommonCliLoader(this),
      log    : new CommonCliLog(this),
      help   : new CommonCliHelp(this)
    };
    this.utils     = new CommonUtils(this);
    this.yaml      = new CommonYaml(this);
    this.lifecycle = new CommonLifecycle(this);
    this.command   = new CommonCommand(this);
    this.variables = new CommonVariables(this);
    this.archive   = new CommonArchiver(this);
    this.version   = new CommonVersion(this);
    this.config    = new CommonConfig(this);
    this.validate  = new CommonValidate(this);
    this.checksums = new CommonChecksums(this);
 
    this.deploy = {};
 
    this.vars = {
      project       : {},
      microservices : {},
      hooks         : [],
      apiBaseUrl    : 'https://api.squeezer.io',
      stage         : 'dev',
      assets        : {
        main          : {
          previousChecksum : null,
          currentChecksum  : null
        },
        microservices : [],
        uploadPaths   : []
      }
    };
 
    Iif (process.argv[2] === 'deploy') {
      this.vars.deploy = true;
    }
 
    this.validateVersion();
    this.loadProject();
    this.loadHooks();
  }
 
  loadProject() {
    this.vars.project.isValid = false;
 
    const splitPath = process.cwd().split('/');
 
    // search the current cwd backwards for a valid Squeezer project
    splitPath.reduce((curr) => {
      const currPath = splitPath.slice(0, curr).join('/');
 
      Iif (fs.existsSync(`${currPath}/squeezer.yml`)) {
        this.vars.project.identifier = _.upperFirst(
          _.camelCase(this.yaml.parse(`${currPath}/squeezer.yml`).name)
        );
        this.vars.project            = _.assign(
          {
            identifier : this.vars.project.identifier
          },
          this.yaml.parse(`${currPath}/squeezer.yml`)
        );
 
        if (this.vars.project.framework.name === 'squeezer') {
          this.vars.project.isValid = true;
          this.vars.project.path    = currPath;
        }
      }
      return curr - 1;
    }, splitPath.length);
 
    Iif (this.vars.project.isValid === true) {
      const buildPath             = `${this.vars.project.path}/.build`;
      this.vars.project.buildPath = buildPath;
      if (!fs.existsSync(buildPath)) {
        fs.mkdirSync(buildPath);
      }
    }
  }
 
  validateVersion() {
    const minNodeVersion  = 6;
    const baseNodeVersion = parseInt(process.version.split('.')[0].replace(/\D/g, ''), 0);
 
    Iif (baseNodeVersion < minNodeVersion) {
      this.cli.log.error(
        `Squeezer framework requires at least NodeJS version ${colors.blue.bold(minNodeVersion)}`
      );
    }
 
    Iif (this.cli.params.get().options.stage) {
      this.vars.stage = this.cli.params.get().options.stage;
    }
  }
 
  loadHooks() {
    /* load frameworks hooks */
    const frameworkPlugins = this.yaml.parse(`${appRoot}/lib/plugins/plugins.yml`);
 
    _.forOwn(frameworkPlugins.plugins, (plugin) => {
      const hookFile = `${appRoot}/lib/plugins/${plugin}/hooks.yml`;
      Eif (fs.existsSync(hookFile)) {
        const data = this.yaml.parse(hookFile).map((val) => {
          val.path = `${appRoot}/lib/plugins/${plugin}/${val.path}`;
          return val;
        });
 
        this.vars.hooks = this.vars.hooks.concat(data);
      }
    });
 
 
    /* load plugins hooks */
    Iif (this.vars.project.isValid === true) {
      _.forEach(this.vars.project.plugins, (plugin) => {
        const data = this
          .yaml.parse(`${this.vars.project.path}/${plugin.path}/${plugin.name}/hooks.yml`)
          .map((val) => {
            val.path = `${this.vars.project.path}/${plugin.path}/${plugin.name}/${val.path}`;
            return val;
          });
 
        this.vars.hooks = this.vars.hooks.concat(data);
      });
    }
  }
}
 
module.exports = new Squeezer();