'use strict';

const tools = require('../../scripts/utils/tools');
const configUtils = require('../../scripts/csdr/config/config-utils');
const depGraph = configUtils.packages.getDepGraph();

let pkg, deps;

Promise.resolve()
  .then(() => {
    pkg = configUtils.packages.getPackage();
    deps = depGraph.dependenciesOf(pkg.name);
  })

  .then(() => {
    if (deps.length > 0) {
      tools.logInfo(`Building package dependencies of package ${pkg.name} (total: ${deps.length})\n[${deps}]`);

      return deps.reduce((promise, dep) => {
        const item = depGraph.getNodeData(dep);
        console.log(item);

        return promise
          .then(() => {
            return tools.runScript(`eui-scripts clean-package ${dep}`);
          })
          .then(() => {
            return tools.runScript(`eui-scripts build-package ${dep} --skipTest`);
          })
          .catch((e) => {
            console.log(e);
            process.exit(1);
          });
      }, Promise.resolve());
    }
  })

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

