const fs = require('fs'); const path = require('path'); const { exec }= require("child_process"); const directoryPath = path.resolve(__dirname, 'build'); function postProcess() { fs.readdir(directoryPath, (err, files) => { if (err) { console.error("Error reading directory: ", err); return; } processHtmlFiles(files); // // wanted to take it out of the readdir callback but it messes up print orders // appendToCxcore(); console.log("---Finished post-processing---"); }); } function processHtmlFiles(files) { console.log(); console.log(`---Starting post-processing---`); console.log(); const htmlFiles = files.filter(file => file.endsWith('html')); htmlFiles.forEach(htmlFile => { console.log( '-' + htmlFile); const filePath = path.join(directoryPath, htmlFile); const fileContent = fs.readFileSync(filePath, 'utf-8'); const scriptContent = extractScript(fileContent); if (scriptContent === '') { if (htmlFile === 'login.html') { console.log(`\tAll good! No expected script for ${filePath}, skipped`); return; } console.warn(`\tWarning: no script content for ${filePath}`); return; } const scriptName = createJsFile(htmlFile, scriptContent); if (scriptName === '') { console.warn(`\tWarning: no scriptName for ${filePath}`); return; } replaceScript(fileContent, filePath, scriptName); console.log(`\tSuccessfully extracted scripts from [${htmlFile}] into separate [${scriptName}].`); }); console.log(); console.log("---Finished post-processing HTML files---"); } function appendToCxcore() { const cxcoreFile = path.resolve(directoryPath, 'cxcore.js'); const content = 'cxCoreInit.promise.then(function(){cxCoreInit();}).catch(function(e){postMessage({type:", m, ",value:e.toString()});})'; try { fs.appendFileSync(cxcoreFile, content); console.log(`\nappended cxCoreInit() to build/cxcore.js`); } catch (err) { console.error("Failed to append: ", err); } } function extractScript(fileContent) { const scriptStart = fileContent.indexOf('__sveltekit_'); if (scriptStart === -1) { console.log('\tCould not find __sveltekit_ script start'); return ''; } var scriptEnd = fileContent.lastIndexOf(''); scriptEnd -= 6; // getting rid of extra } closing bracket. Will find a better solution than this. if (scriptEnd === -1) { console.log('\tCould not match the closing `); fs.writeFileSync(filePath, updatedContent, 'utf-8', (err) =>{ if (err) { console.log("Failed to write replacedContent instead of inlined script: ", err); } }); } postProcess();