const fs = require('fs/promises'); const path = require('path'); const copyright_disclaimer = ` /** * This file is part of the jps-like-websites lib * URL: https://git.labos.goip.de/chris/jpc-like-websites * @copyright by its creator */ `; /** * Kept for future reference, might be that we will return to the dependency resolvement approach. * @param {OAO} oao * @returns {OAO} */ function resolveDependencyOrder(oao) { let roots = oao.keys.filter(k => oao.objects[k].dependencies.length === 0); let folderList = [...new Set(oao.keys.map(k => oao.objects[k].folder))]; console.log("Folders", folderList); /** * * @param {OAO} oao * @param {Array>} dealt */ function recursiveResolvement(oao, dealtLvls, dealt) { const arrsEqualSize = (arr1, arr2) => arr1.length === arr2.length; const arrHasAll = (arr1, arr2) => arr2.every(k => arr1.includes(k)) if (oao.keys.length === dealt.length) { return [...dealt, ...current]; } else { console.log(`Received ${dealt.length} dealt of ${oao.keys.length}`); } let remaining = oao.keys .filter(k => !dealt.includes(k)); if (remaining.length < 2) { return [...dealt, ...remaining]; } let current = remaining .filter(k => oao.objects[k].dependencies.every(sk => dealt.includes(sk))); if (current.length === 0) { console.log("Couldn't resolve", remaining); return remaining; } dealtLvls.push(current); if (arrsEqualSize(remaining, current)) { return [...dealt, ...current]; } return recursiveResolvement(oao, dealtLvls, [...dealt, ...current]); } let recursiveResolved = recursiveResolvement(oao, [roots], roots); recursiveResolved .forEach((k, i) => { oao.objects[k].orderNr = i; }); oao.isOrdered = recursiveResolved.slice(-1).length <= 2; if (oao.isOrdered) { oao.keys = recursiveResolved; } return oao; } /** * * @param {string} srcFile path to src file * @param {string} targetFile path of target file that will contain all scripts */ async function appendContent(srcFile, targetFile, contentModifications = [], lineModifications = []) { try { let content = await fs.readFile(srcFile, 'utf8'); console.log(`Processing '${srcFile}'`); console.log(` READ: successfully!`); console.log(` Adding Copyright disclaimer!`); console.log(` Attepting to append to default script variant`); /* Apply modifications */ content = contentModifications .reduce((a, c) => c(a), content.split('\r\n')) .map(line => lineModifications.reduce((a, c) => c(a), line)) .join('\r\n'); await fs.appendFile(targetFile, content); console.log(` Append/Write: successfully!`); } catch (error) { console.error(`Error reading/writing files: ${error.message}`); } } /** * * @param {string} srcFile path to src file * @param {string} targetFile path of target file that will contain all scripts */ async function bundleWithSpecifiedMode(targetFile, fileList, modulize = true) { let contentModifications = [ function (content) { return [ ...copyright_disclaimer.split('\r\n'), ...content ]; } ]; let lineModifications = [ function (line) { let starters = ["function", "const", "let", "class"]; return (starters.some(s => line.startsWith(s)) ? "export " : "") + line; } ]; fileList .reduce((prevPromise, filePath) => prevPromise .then( () => appendContent(filePath, targetFile, contentModifications) ), Promise.resolve() ) if (modulize) { let targetModule = path.basename(targetFile).split('.', 1)[0] + '.mjs'; fileList .reduce((prevPromise, filePath) => prevPromise .then( () => appendContent(filePath, targetModule, contentModifications, lineModifications) ), Promise.resolve() ) } } /** * @deprecated Currently this function takles the task of successfully bundling the jpc-framework. * A custom bundler will replace this function/file. * Some logic might be copied/used from here. * * Takes the given order from the jpclw-bundle.json and bundles the scripts accordingly. * * @param {string|path} targetFile path to the targetfile (without file extension) * @param {boolean} modulize flag to decide to generate a mjs module as well * @param {string|path} bundleFile path to the bundleFile if there is some * - preparation for jpclw-bundler */ function bundle(targetFile = "./jpc-like-websites.js", modulize = true, bundleFile = './jpclw-bundle.json') { let fileOrder = require(bundleFile); console.log("(Re-) Creating target file: '" + targetFile + "'"); /* EMPTY (create?) TARGET FILE */ fs.writeFile(targetFile, "", err => { }); if (modulize) { let targetModule = path.basename(targetFile).split('.', 1)[0] + 'mjs'; fs.writeFile(targetModule, "", err => { }); } let fileList = Object.keys(fileOrder.orderedGroups) .flatMap(groupName => fileOrder.orderedGroups[groupName]) .map(fileName => { let f = fileOrder.objects[fileName]; /* The Path build by name and folder */ return `${f.folder}/${f.name}`; }); bundleWithSpecifiedMode(targetFile, fileList, modulize); } let args = process.argv.slice(2); bundle(args[0], args[1], args[2]);