"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mergeScripts = mergeScripts;

var _flatten = _interopRequireDefault(require("lodash/flatten"));

var _groupBy = _interopRequireDefault(require("lodash/groupBy"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Merges all scripts of a coat project.
 *
 * Scripts are shell commands that are placed as npm scripts in
 * the package.json file of a coat project.
 *
 * If multiple scripts share the same scriptName, they will
 * be placed to run in parallel.
 *
 * @param scripts All scripts that should be merged
 */
function mergeScripts(scripts) {
  // Group scripts by scriptName
  const groupedScripts = (0, _groupBy.default)((0, _flatten.default)(scripts), "scriptName");
  return Object.entries(groupedScripts).reduce((accumulator, [scriptName, scripts]) => {
    if (scripts.length === 1) {
      accumulator.scripts[scriptName] = scripts[0].run;
    } else {
      // Create sub-scripts which will be run via coat run
      const scriptIdPrefix = `${scriptName}-`;
      const scriptNamePrefix = `${scriptName}:`; // Add scriptNamePrefix to parallelScriptPrefixes to be able to
      // later delete potentially existing scripts from
      // the current package.json file

      accumulator.parallelScriptPrefixes.push(scriptNamePrefix);
      scripts.forEach(script => {
        let scriptNameToUse = scriptNamePrefix;

        if (script.id.startsWith(scriptIdPrefix)) {
          scriptNameToUse += script.id.substr(scriptIdPrefix.length);
        } else {
          scriptNameToUse += script.id;
        }

        accumulator.scripts[scriptNameToUse] = script.run;
      }); // Merge the script by adding the coat run
      // command to run all scripts in parallel.

      accumulator.scripts[scriptName] = `coat run ${scriptName}:*`;
    }

    return accumulator;
    /* eslint-enable no-param-reassign */
  }, {
    scripts: {},
    parallelScriptPrefixes: []
  });
}