2025-01-16 05:22:33 +00:00
"use strict" ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . default = startProcess ;
const fs _1 = _ _importDefault ( require ( "fs" ) ) ;
const console _colors _1 = _ _importDefault ( require ( "./console-colors" ) ) ;
const preflight _1 = _ _importDefault ( require ( "./preflight" ) ) ;
const run _1 = _ _importDefault ( require ( "./run" ) ) ;
const kill _child _1 = _ _importDefault ( require ( "./kill-child" ) ) ;
let childProcess = null ;
2025-01-16 08:18:45 +00:00
const pTitle = "buncid" ;
2025-01-16 05:22:33 +00:00
process . title = pTitle ;
/ * *
* # Start the process
* @ param { object } param0
* @ param { string } param0 . command
* @ param { string [ ] | string } param0 . preflight
* @ param { string [ ] | string } [ param0 . postflight ]
* @ param { string } param0 . redeploy _file
* @ param { string | number | ( string | number ) [ ] } [ param0 . port ] - The port to kill on rebuild
* @ param { boolean } [ param0 . first _run ] - Whether to run the preflight on first run . Default ` false `
* /
function startProcess ( { command , preflight , postflight , redeploy _file , port , first _run , } ) {
try {
if ( first _run ) {
console . log ( "First Run ..." ) ;
const runPreflight = ( 0 , preflight _1 . default ) ( preflight ) ;
}
if ( ! preflight ) {
console . log ( ` ${ console _colors _1 . default . FgRed } Error: ${ console _colors _1 . default . Reset } No preflight included in config file. If you don't want to run any preflight command simply add an empty array. ` ) ;
process . exit ( ) ;
}
childProcess = ( 0 , run _1 . default ) ( command ) ;
if ( ! childProcess ) {
console . log ( ` ${ console _colors _1 . default . FgRed } Error: ${ console _colors _1 . default . Reset } Process couldn't start. Exiting... ` ) ;
process . exit ( ) ;
}
console . log ( "Watching" , redeploy _file ) ;
fs _1 . default . watchFile ( redeploy _file , { interval : 100 } , ( curr , prev ) => {
console . log ( ` ${ console _colors _1 . default . BgBlue } File Changed ${ console _colors _1 . default . Reset } ` ) ;
2025-01-22 11:31:32 +00:00
if ( global . REDEPLOYMENTS == 0 ) {
2025-01-16 05:22:33 +00:00
return ;
2025-01-22 11:31:32 +00:00
}
2025-01-16 05:22:33 +00:00
if ( childProcess ) {
console . log ( "******************************" ) ;
2025-01-22 11:31:32 +00:00
console . log ( ` ******** ${ console _colors _1 . default . FgBlue } Rebuilding ${ console _colors _1 . default . FgMagenta } ${ global . REDEPLOYMENTS } ${ console _colors _1 . default . Reset } ******** ` ) ;
2025-01-16 05:22:33 +00:00
console . log ( "******************************" ) ;
try {
const runPreflight = ( 0 , preflight _1 . default ) ( preflight ) ;
if ( ! runPreflight ) {
// TODO: Action to take if preflight fails
console . log ( ` ${ console _colors _1 . default . FgRed } Error: ${ console _colors _1 . default . Reset } Preflight Failed. ` ) ;
}
else {
2025-01-16 08:28:46 +00:00
( 0 , kill _child _1 . default ) ( childProcess , port ) . then ( ( kill ) => {
2025-01-16 05:22:33 +00:00
if ( kill ) {
childProcess = ( 0 , run _1 . default ) ( command ) ;
if ( postflight ) {
const runPostflight = ( 0 , preflight _1 . default ) ( postflight , true ) ;
if ( ! runPostflight ) {
// TODO: Action to take if postflight fails
console . log ( ` ${ console _colors _1 . default . FgRed } Error: ${ console _colors _1 . default . Reset } Postflight Failed. ` ) ;
}
}
}
else {
process . exit ( ) ;
}
} ) ;
}
}
catch ( error ) {
console . log ( ` ${ console _colors _1 . default . FgRed } Error: ${ console _colors _1 . default . Reset } killing child processes => ${ error . message } ` ) ;
process . exit ( ) ;
}
}
} ) ;
}
catch ( error ) {
console . log ( ` ${ console _colors _1 . default . FgRed } Error: ${ console _colors _1 . default . Reset } First run failed! => ${ error . message } ` ) ;
}
}