'use strict';

// GLOBAL
const path = require('path');

// UTILS
const utils = require('../../../utils');

// CSDR RELATED
const configUtils = require('../../config/config-utils');

// INNER MODULES
const innerCommon = require('./common');


module.exports.run = () => {

    // fetching package from config based on default arg
    const pkg = configUtils.packages.getPackage();

    // checking if the package can be retrieved from args
    if (!pkg.name) {
      utils.tools.logError('ERROR : "package" NOT PROVIDED as argument !!!');
      process.exit(1);
    }

    if (pkg.remote) {
      utils.tools.logWarning('Remote UI package - skipping...');
      process.exit(1);
    }

    return Promise.resolve()
      // LOGGING CURRENT eUI TOOLS VERSION
      .then(() => {
        utils.tools.logVersion();
      })

      // INSTALL dependencies by type
      .then(() => {
        return Promise.resolve()
          // FETCHING CURRENT PACKAGES & INSTALL
          .then(() => {
            // fetch related project if passed as arguments
            const prj = configUtils.projects.getProject();

            // install the dependencies for current package build
            return installUtils.buildPackage.installPackage(prj, pkg, false);
          })

          .catch((e) => {
            throw e;
          })
      })

      // BUILD PACKAGE
      .then(() => {
        return utils.buildPackage.build(pkg);
      })

      .catch((e) => {
        console.log(e);
        process.exit(1);
      })
}
