import watch from 'node-watch' import { PlataCluster } from '..' import { PlataBuild } from './build' export namespace HotReload { export function canReload(filename: string): boolean { if (filename.includes('__BUILD__')) return false ; if (filename.includes('__RELEASE__')) return false ; if (filename.includes('node_modules')) return false ; if (Plata.ProjectJson['ignore-hot']) for (let i = 0; i < Plata.ProjectJson['ignore-hot'].length; i++) { const ignore = Plata.ProjectJson['ignore-hot'][i]; if(filename.includes(ignore)) return false ; } ; return /(ts|env|json)$/.test(filename) } export async function realod() { await PlataBuild.compile({ basePath: Plata.ProjectDir, mapFiles: true, packageJsonSetup: false, otimizar: false, }) const cluster = new PlataCluster.PlataClusterManager() cluster._sendMsgToAllWorkers({ action: 'RELOAD', msg: true }) } export async function setupHotReloadPrimary() { console.log('🔥 Hot Realod ativo') watch(Plata.ProjectDir, { recursive: true, delay: 500, filter: HotReload.canReload }, HotReload.realod) } export async function setupHotReloadWorker() { process.on('message', (msg: Cluster.Actions) => { if (msg.action === 'RELOAD') process.exit(0) ; }) } }