'use strict';

// LOCAL
const tools = require('../tools');
const configUtils = require('../../csdr/config/config-utils');

const publishUtils = require('./publish-utils');


module.exports.publish = () => {
  tools.logTitle('Publishing all local packages');

  return Promise.resolve()
    // iterate over all packages and publish them individually
    .then(() => {
      const packages = configUtils.packages.getPackages().filter((p) => { return !p.child });

      if (packages.length > 0) {
        tools.logInfo(`Local packages found : (total: ${packages.length})\n[${packages.map(p => p.name)}]`);

        return packages.reduce((promise, pkg) => {
          return promise
            .then(() => publishUtils.publish(pkg))
            .catch((e) => {
              throw e;
            });
        }, Promise.resolve());
      }
    })

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