import chokidar from 'chokidar'; import fse from 'fs-extra'; import esbuild from 'esbuild'; // ==================================================================== // | // ==================================================================== function log(message) { const options = { encoding: 'utf-8', flag: 'a+', }; const datetime = new Date().toISOString(); message = `[${datetime}] ${message}\n`; fse.outputFileSync( '/logs/builder/javascript.log', message, options ); console.log(message); } // ==================================================================== // | // ==================================================================== async function buildJavascript(name) { const entryPoint = `/app/js/${name}/index.js`; const outfile = `/www/js/${name}.js`; log(`Building ${name}`); log(`Entry: ${entryPoint}`); log(`Output: ${outfile}`); await esbuild.build({ entryPoints: [entryPoint], bundle: true, outfile, format: 'esm', sourcemap: true, target: ['es2022'], logLevel: 'info' }); log(`Finished ${name}`); } // ==================================================================== // | // ==================================================================== async function compileAll() { await buildJavascript('component'); await buildJavascript('app'); log('Done'); } // ==================================================================== // | // ==================================================================== function startWatcher(watchFolders) { const options = { awaitWriteFinish: true, ignoreInitial: true, usePolling: true, interval: 100 }; const watcher = chokidar.watch( watchFolders, options ); watcher.on('all', async (event, filePath) => { log(`${event}: ${filePath}`); try { if (filePath.includes('/app/js/component/')) { await buildJavascript('component'); } if (filePath.includes('/app/js/app/')) { await buildJavascript('app'); } } catch (error) { log(`Build failed: ${error.message}`); } }); watcher.on( 'ready', () => log('********** Watch is running **********') ); } // ==================================================================== // | MAIN // ==================================================================== async function go() { const watchFolders = [ '/app/js' ]; if (process.argv[2] === 'compile-and-watch') { await compileAll(); startWatcher(watchFolders); } else if (process.argv[2] === 'compile-only') { await compileAll(); } else { startWatcher(watchFolders); } } go();