UNPKG

monex

Version:

Execute one or multiple scripts, interactively or in daemon mode, and restart them whenever they crash or a watched file changes.

62 lines (42 loc) 1.68 kB
/* IMPORT */ import ControllerMultiple from '~/interactive/controller_multiple'; import ControllerSingle from '~/interactive/controller_single'; import type {IController, OptionsMultiple, OptionsSingle} from '~/types'; /* MAIN */ const execute = ( options: Partial<OptionsMultiple | OptionsSingle> ): IController => { 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;