UNPKG

laravel-mix-glob

Version:

Laravel mix extension that add support for globs usage with an extensive concise api. For a more natural boosted productivity and dynamic. Configure once and forget about adding the files each time.

3 lines (2 loc) 4.5 kB
"use strict";var e=require("cross-spawn"),s=require("lodash.uniq"),r=require("../../Utils/Logger/index.js"),i=require("../../Utils/helpers/index.js"),t=require("../Cache/index.js");exports.RestartHandler=class{isRestarting=!1;masterProcessId;parentProcessId;_watchingHandler;_cacheHandler;_isKillingAll=!1;constructor(){this._cacheHandler=new t.Cache,this.masterProcessId=Number(process.env.MIX_GLOB_MASTER_PROCESS_ID),this.parentProcessId=Number(process.env.MIX_GLOB_PARENT_PROCESS_ID),this._checkAndKillParentProcess(),i.isChildProcess()?(r.LOGGER.debug(`Adding new child current process (${process.pid}) to cache.`),this._cacheHandler.addChildPids([process.pid]).save()):this._cacheHandler.cleanOrphans()}_checkAndKillParentProcess(){if(r.LOGGER.debug("Checking to Kill parent process:"),r.LOGGER.debug(this.parentProcessId?`Parent pid found (${this.parentProcessId})`:"No parent"),r.LOGGER.debug(`pid: ${process.env.MIX_GLOB_PARENT_PROCESS_ID}`),this.parentProcessId&&this.parentProcessId!==this.masterProcessId){const e=i.killProcess(this.parentProcessId);this._cacheHandler.removeChildPids([this.parentProcessId]).save(),r.LOGGER.debug("After remove: ",this._cacheHandler.values),e&&r.LOGGER.debug(`Kill: Parent process (pid: ${this.parentProcessId}) is killed.`)}else r.LOGGER.debug("Parent is Master. No killing.")}setWatchingHandler(e){return this._watchingHandler=e,this}restartMix(t,c,d){if(!this._watchingHandler)throw new Error("Watching handler need to be set first using .setWatchingHandler() method");r.LOGGER.log(`WATCH: ${t}`.bgYellow),r.LOGGER.log(c);let l="";switch(t){case"add":l="File added :";break;case"unlink":l="File removed :";break;case"unlinkDir":l="Directory removed :"}if(!this.isRestarting){this.isRestarting=!0;try{r.LOGGER.log(l.bgBlue),r.LOGGER.log(c.yellow),r.LOGGER.log("Matching glob: ".bgBlue),r.LOGGER.log(d.value),r.LOGGER.debug(this._watchingHandler.watchedGlobs),r.LOGGER.log("restart...".cyan);let t=["mix","watch"];"hot"===this._watchingHandler.watchType&&t.push("--hot"),t=s([...t,"--",...process.argv.slice(2)]),r.LOGGER.debug("Command args:",t);let a={...process.env,MIX_GLOB_IS_CHILD:"true",MIX_GLOB_PARENT_PROCESS_ID:process.pid.toString()};i.isChildProcess()||(a={...a,MIX_GLOB_MASTER_PROCESS_ID:process.pid.toString(),GLOB_MIX_SESSION_ID:this._cacheHandler.sessionId});const n=e.spawn("npx",t,{detached:!0,stdio:"inherit",cwd:process.cwd(),env:a});if(n.on("error",(e=>{r.LOGGER.debug("childProcess.on(error)"),r.LOGGER.err(e),setTimeout((()=>{this.killAllAliveProcesses()}),500)})),n.unref(),!i.isChildProcess())for(r.LOGGER.debug("////////////////////////////////////////////////////////////////".bgYellow);;){r.LOGGER.debug("BLOCKING MASTER! sleep 999999999!");try{i.blockingSleep(999999999)}catch(e){return r.LOGGER.debug("////////////////////////////////////////////////////////////////".bgBlue),r.LOGGER.debug("pids alone",this._cacheHandler.flatCache.getKey("childPids")),r.LOGGER.debug("before syncing: ",this._cacheHandler.values),this._cacheHandler.syncWithCache(),r.LOGGER.debug("After syncing: ",this._cacheHandler.values),r.LOGGER.debug("Exiting process MASTER process."),r.LOGGER.debug("Exiting process. See you next time!"),void this.killAllAliveProcesses()}}r.LOGGER.debug("////////////////////////////////////////////////////////////////".bgBlue),r.LOGGER.debug(`Parent pid ==== ${process.pid}`),r.LOGGER.debug("To be killed by new Child replacement.")}catch(e){r.LOGGER.err(e)}}}killAllAliveProcesses(){if(this._isKillingAll)return;this._isKillingAll=!0,r.LOGGER.debug("killAllAliveProcesses > /////////////////////".bgRed),this._cacheHandler.syncWithCache(),r.LOGGER.debug("Child pids: ",this._cacheHandler.values.childPids),this._cacheHandler.values.childPids.forEach((e=>{r.LOGGER.debug(`pid: ${e}`),r.LOGGER.debug(`current process id: ${process.pid}`),e!==process.pid&&(r.LOGGER.debug("Different > kill"),i.killProcess(e))})),r.LOGGER.debug("Check master process:"),r.LOGGER.debug(`Master pid: ${this.masterProcessId}`),r.LOGGER.debug(`Current process id: ${process.pid}`),this.masterProcessId&&this.masterProcessId!==process.pid&&(r.LOGGER.debug("Different > kill"),i.killProcess(this.masterProcessId)),r.LOGGER.debug("Destroying cache document. And updating meta cache.");const{sessionId:e}=this._cacheHandler;this._cacheHandler.metaCache.removeSessionsByIds([e]).save(),this._cacheHandler.destroy(),r.LOGGER.debug("Exiting current process."),setTimeout((()=>{process.exit(0)}),100)}}; //# sourceMappingURL=index.js.map