monex
Version:
Execute one or multiple scripts, interactively or in daemon mode, and restart them whenever they crash or a watched file changes.
45 lines (44 loc) • 1.62 kB
JavaScript
/* IMPORT */
import ControllerMultiple from './controller_multiple.js';
import ControllerSingle from './controller_single.js';
/* MAIN */
const execute = (options) => {
const { cluster, delay, exec, ignore, name, watch } = options;
const clusters = Array.isArray(cluster) ? cluster.map(Number) : (cluster !== undefined ? [cluster].map(Number) : []);
const names = Array.isArray(name) ? name : (name ? [name] : []);
const restart = ('restart' in options) ? options.restart : undefined;
const execs = Array.isArray(exec) ? exec : (exec ? [exec] : []);
const ignores = Array.isArray(ignore) ? ignore : (ignore ? [ignore] : undefined);
const watches = Array.isArray(watch) ? watch : (watch ? [watch] : undefined);
if (!execs.length)
throw new Error('No scripts to execute provided');
if (names.length && names.length !== execs.length)
throw new Error('Mismatching number of names and scripts provided');
if (execs.length > 1) {
const controller = new ControllerMultiple({
name: names,
restart,
exec: execs,
ignore: ignores,
watch: watches,
cluster: clusters,
delay
});
controller.start();
return controller;
}
else {
const controller = new ControllerSingle({
name: names[0],
exec: execs[0],
ignore: ignores,
watch: watches,
cluster: clusters[0],
delay
});
controller.start();
return controller;
}
};
/* EXPORT */
export default execute;