@coat/cli
Version:
TODO: See #3
60 lines (56 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mergeScripts = mergeScripts;
var _flatten = _interopRequireDefault(require("lodash/flatten"));
var _groupBy = _interopRequireDefault(require("lodash/groupBy"));
var _uniqBy = _interopRequireDefault(require("lodash/uniqBy"));
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) {
// Use the latest script entry for scripts that share the same id
const uniqueScripts = (0, _uniqBy.default)((0, _flatten.default)(scripts).reverse(), "id");
// Group scripts by scriptName
const groupedScripts = (0, _groupBy.default)(uniqueScripts, "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: []
});
}