diff options
Diffstat (limited to 'package_abridge.js')
| -rw-r--r-- | package_abridge.js | 196 |
1 files changed, 99 insertions, 97 deletions
diff --git a/package_abridge.js b/package_abridge.js index 197e10b..5e54be5 100644 --- a/package_abridge.js +++ b/package_abridge.js @@ -3,7 +3,7 @@ const path = require("path"); const TOML = require('fast-toml'); const UglifyJS = require('uglify-js'); const jsonminify = require("jsonminify"); -const util = require("util"); +const util = require("util"); const { exec } = require("child_process"); const { exit } = require('process'); const execPromise = util.promisify(exec); @@ -33,7 +33,7 @@ const pwa_BASE_CACHE_FILES = data.extra.pwa_BASE_CACHE_FILES; // This is used to pass arguments to zola via npm, for example: // npm run abridge -- "--base-url https://abridge.pages.dev" -var args = process.argv[2] ? ' '+process.argv[2] : ''; +var args = process.argv[2] ? ' ' + process.argv[2] : ''; // check if abridge is used directly or as a theme. bpath = ''; @@ -41,9 +41,9 @@ if (fs.existsSync('./themes')) { bpath = 'themes/abridge/'; } // cleanup pagefind files from old builds. -_rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/); -_rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/); -_rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry.*json$/); +_rmRegex(path.join(bpath, "static/js/"), /^wasm.*pagefind$/); +_rmRegex(path.join(bpath, "static/js/"), /^pagefind.*pf_meta$/); +_rmRegex(path.join(bpath, "static/js/"), /^pagefind-entry.*json$/); _rmRecursive(path.join(bpath, "static/js/index")); _rmRecursive(path.join(bpath, "static/js/fragment")); @@ -53,7 +53,7 @@ async function execWrapper(cmd) { console.log(stdout); } if (stderr) { - console.log('ERROR: '+stderr); + console.log('ERROR: ' + stderr); } } @@ -62,81 +62,81 @@ async function abridge() { const { replaceInFileSync } = await import('replace-in-file'); // set index_format for chosen search_library accordingly. if (search_library === 'offline') { - replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\""}); - args = args + " -u \""+__dirname+"\/public\""//set base_url to the path on disk for offline site. + replaceInFileSync({ files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\"" }); + args = args + " -u \"" + __dirname + "\/public\""//set base_url to the path on disk for offline site. } else if (search_library === 'elasticlunrjava') { - replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\""}); + replaceInFileSync({ files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_javascript\"" }); } else if (search_library === 'elasticlunr') { - replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_json\""}); + replaceInFileSync({ files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"elasticlunr_json\"" }); } else if (search_library === 'pagefind') { - replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"fuse_json\""}); + replaceInFileSync({ files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"fuse_json\"" }); } else if (search_library === 'tinysearch') { - replaceInFileSync({files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"fuse_json\""}); + replaceInFileSync({ files: 'config.toml', from: /index_format.*=.*/g, to: "index_format = \"fuse_json\"" }); } console.log('Zola Build to generate files for minification:'); - await execWrapper('zola build'+args); + await execWrapper('zola build' + args); //check that static/js exists, do this after zola build, it will handle creating static if missing. var jsdir = 'static/js'; try { fs.mkdirSync(jsdir); - } catch(e) { + } catch (e) { if (e.code != 'EEXIST') throw e; } base_url = data.base_url; if (base_url.slice(-1) == "/") { - base_url = base_url.slice(0, -1); + base_url = base_url.slice(0, -1); } if (search_library === 'pagefind') { // Generate pagefind index at start, otherwise it happens too late asyncronously. await createPagefindIndex(); // makes program wait for pagefind build execution - _rmRegex(path.join(bpath, "static/js/"),/^pagefind\.js$/);//pagefind temporary intermediate files - _rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.js$/);//pagefind temporary intermediate files - _rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.css$/);//pagefind temporary intermediate files + _rmRegex(path.join(bpath, "static/js/"), /^pagefind\.js$/);//pagefind temporary intermediate files + _rmRegex(path.join(bpath, "static/js/"), /^pagefind-.*\.js$/);//pagefind temporary intermediate files + _rmRegex(path.join(bpath, "static/js/"), /^pagefind-.*\.css$/);//pagefind temporary intermediate files // This line in pagefind is causing a problem for the PWA: // var e = await (await fetch(this.basePath + "pagefind-entry.json?ts=" + Date.now())).json(); // instead generate an epoch timestamp at build and add it to the filename. var hash = Math.floor(new Date().getTime() / 1000); - fs.renameSync(path.join(bpath, "static/js/pagefind-entry.json"), path.join(bpath, "static/js/pagefind-entry-"+hash+".json")); + fs.renameSync(path.join(__dirname, "static/js/pagefind-entry.json"), path.join(__dirname, "static/js/pagefind-entry-" + hash + ".json")); // original: var e=await(await fetch(this.basePath+"pagefind-entry.json?ts="+Date.now())).json(); // new: var e=await(await fetch(this.basePath+"pagefind-entry-1723268715.json")).json(); // Tricky regex, so I split it into two replaceInFileSync() calls, pull requests welcome if you can improve this. - replaceInFileSync({files: path.join(bpath, "static/js/pagefind_search.js"), from: /pagefind-entry\.json\?ts=/g, to: "pagefind-entry-"+hash+"\.json"}); - replaceInFileSync({files: path.join(bpath, "static/js/pagefind_search.js"), from: /Date.now\(\)/g, to: "\"\""}); + replaceInFileSync({ files: path.join(__dirname, "static/js/pagefind_search.js"), from: /pagefind-entry\.json\?ts=/g, to: "pagefind-entry-" + hash + "\.json" }); + replaceInFileSync({ files: path.join(__dirname, "static/js/pagefind_search.js"), from: /Date.now\(\)/g, to: "\"\"" }); //copy to public so the files are included in the PWA cache list if necessary. - fs.copyFileSync(path.join(bpath, "static/js/pagefind-entry-"+hash+".json"), path.join(bpath, "public/js/pagefind-entry-"+hash+".json")) - _cpRegex(path.join(bpath, "static/js/"),path.join(bpath, "public/js/"),/^pagefind-entry\.json$/); - _cpRegex(path.join(bpath, "static/js/"),path.join(bpath, "public/js/"),/^pagefind.*pf_meta$/); - _cpRegex(path.join(bpath, "static/js/"),path.join(bpath, "public/js/"),/^wasm.*pagefind$/); - _cpRecursive(path.join(bpath, "static/js/index"),path.join(bpath, "public/js/index")); - _cpRecursive(path.join(bpath, "static/js/fragment"),path.join(bpath, "public/js/fragment")); + fs.copyFileSync(path.join(__dirname, "static/js/pagefind-entry-" + hash + ".json"), path.join(__dirname, "public/js/pagefind-entry-" + hash + ".json")) + _cpRegex(path.join(__dirname, "static/js/"), path.join(__dirname, "public/js/"), /^pagefind-entry\.json$/); + _cpRegex(path.join(__dirname, "static/js/"), path.join(__dirname, "public/js/"), /^pagefind.*pf_meta$/); + _cpRegex(path.join(__dirname, "static/js/"), path.join(__dirname, "public/js/"), /^wasm.*pagefind$/); + _cpRecursive(path.join(__dirname, "static/js/index"), path.join(__dirname, "public/js/index")); + _cpRecursive(path.join(__dirname, "static/js/fragment"), path.join(__dirname, "public/js/fragment")); } if (pwa) {// Update pwa settings, file list, and hashes. if (typeof pwa_VER !== 'undefined' && typeof pwa_NORM_TTL !== 'undefined' && typeof pwa_LONG_TTL !== 'undefined' && typeof pwa_TTL_NORM !== 'undefined' && typeof pwa_TTL_LONG !== 'undefined' && typeof pwa_TTL_EXEMPT !== 'undefined') { // update from abridge theme. - fs.copyFileSync(bpath+'static/sw.js', 'static/sw.js'); - fs.copyFileSync(bpath+'static/js/sw_load.js', 'static/js/sw_load.js'); + fs.copyFileSync(bpath + 'static/sw.js', 'static/sw.js'); + fs.copyFileSync(bpath + 'static/js/sw_load.js', 'static/js/sw_load.js'); // Update settings in PWA javascript file, using options parsed from config.toml. sw.min.js?v=3.10.0", "++" if (fs.existsSync('static/js/sw_load.js')) { sw_load_min = '.js?v='; if (js_bundle) { sw_load_min = '.min.js?v='; } - replaceInFileSync({files: 'static/js/sw_load.js', from: /sw.*v=.*/g, to: "sw"+sw_load_min+pwa_VER+"\","}); + replaceInFileSync({ files: 'static/js/sw_load.js', from: /sw.*v=.*/g, to: "sw" + sw_load_min + pwa_VER + "\"," }); } if (fs.existsSync('static/sw.js')) { - replaceInFileSync({files: 'static/sw.js', from: /NORM_TTL.*=.*/g, to: "NORM_TTL = "+pwa_NORM_TTL+";"}); - replaceInFileSync({files: 'static/sw.js', from: /LONG_TTL.*=.*/g, to: "LONG_TTL = "+pwa_LONG_TTL+";"}); - replaceInFileSync({files: 'static/sw.js', from: /TTL_NORM.*=.*/g, to: "TTL_NORM = ["+pwa_TTL_NORM+"];"}); - replaceInFileSync({files: 'static/sw.js', from: /TTL_LONG.*=.*/g, to: "TTL_LONG = ["+pwa_TTL_LONG+"];"}); - replaceInFileSync({files: 'static/sw.js', from: /TTL_EXEMPT.*=.*/g, to: "TTL_EXEMPT = ["+pwa_TTL_EXEMPT+"];"}); + replaceInFileSync({ files: 'static/sw.js', from: /NORM_TTL.*=.*/g, to: "NORM_TTL = " + pwa_NORM_TTL + ";" }); + replaceInFileSync({ files: 'static/sw.js', from: /LONG_TTL.*=.*/g, to: "LONG_TTL = " + pwa_LONG_TTL + ";" }); + replaceInFileSync({ files: 'static/sw.js', from: /TTL_NORM.*=.*/g, to: "TTL_NORM = [" + pwa_TTL_NORM + "];" }); + replaceInFileSync({ files: 'static/sw.js', from: /TTL_LONG.*=.*/g, to: "TTL_LONG = [" + pwa_TTL_LONG + "];" }); + replaceInFileSync({ files: 'static/sw.js', from: /TTL_EXEMPT.*=.*/g, to: "TTL_EXEMPT = [" + pwa_TTL_EXEMPT + "];" }); } if (pwa_cache_all === true) { @@ -146,36 +146,36 @@ async function abridge() { var dir = 'public'; try { fs.mkdirSync(dir); - } catch(e) { + } catch (e) { if (e.code != 'EEXIST') throw e; } const path = './public/'; cache = ''; files = fs.readdirSync(path, { recursive: true, withFileTypes: false }) - .forEach( - (file) => { - // check if is directory, if not then add the path/file - if (!fs.lstatSync(path+file).isDirectory()) { - // format output - item = "/"+file.replace(/index\.html$/i,'');// strip index.html from path - item = item.replace(/\\/g,'/');// replace backslash with forward slash for Windows - item = item.replace(/^\/sw(\.min)?\.js/i,'');// dont cache service worker - item = item.replace(/^\/_headers/i,'');// dont cache the cloudflare _headers file + .forEach( + (file) => { + // check if is directory, if not then add the path/file + if (!fs.lstatSync(path + file).isDirectory()) { + // format output + item = "/" + file.replace(/index\.html$/i, '');// strip index.html from path + item = item.replace(/\\/g, '/');// replace backslash with forward slash for Windows + item = item.replace(/^\/sw(\.min)?\.js/i, '');// dont cache service worker + item = item.replace(/^\/_headers/i, '');// dont cache the cloudflare _headers file - // if formatted output is not empty line then append it to cache var - if (item != '') {// skip empty lines - cache = cache+"'"+item+"',"; + // if formatted output is not empty line then append it to cache var + if (item != '') {// skip empty lines + cache = cache + "'" + item + "',"; + } } } - } - ); + ); cache = cache.slice(0, -1)// remove the last comma } else if (pwa_BASE_CACHE_FILES) { cache = pwa_BASE_CACHE_FILES; } cache = cache.split(",").sort().join(",")//sort the cache list, this should help keep the commit history cleaner. - cache = 'this.BASE_CACHE_FILES = ['+cache+'];'; + cache = 'this.BASE_CACHE_FILES = [' + cache + '];'; // update the BASE_CACHE_FILES variable in the sw.js service worker file results = replaceInFileSync({ files: 'static/sw.js', @@ -195,10 +195,10 @@ async function abridge() { minify(['static/js/theme_light.js']); // Something went wrong with minifying katexbundle, so commenting this out for now // minify(['static/js/katex.min.js','static/js/mathtex-script-type.min.js','static/js/katex-auto-render.min.js','static/js/katexoptions.js'],'static/js/katexbundle.min.js'); - minify(['static/js/elasticlunr.min.js','static/js/search.js'],'static/js/search_elasticlunr.min.js'); - minify(['static/js/tinysearch.js'],'static/js/search_tinysearch.min.js'); - minify(['static/js/prestyle.js','static/js/theme_button.js','static/js/email.js','static/js/codecopy.js','static/js/sw_load.js'],'static/js/abridge_nosearch.min.js'); - minify(['static/js/prestyle.js','static/js/theme_button.js','static/js/email.js','static/js/codecopy.js'],'static/js/abridge_nosearch_nopwa.min.js'); + minify(['static/js/elasticlunr.min.js', 'static/js/search.js'], 'static/js/search_elasticlunr.min.js'); + minify(['static/js/tinysearch.js'], 'static/js/search_tinysearch.min.js'); + minify(['static/js/prestyle.js', 'static/js/theme_button.js', 'static/js/email.js', 'static/js/codecopy.js', 'static/js/sw_load.js'], 'static/js/abridge_nosearch.min.js'); + minify(['static/js/prestyle.js', 'static/js/theme_button.js', 'static/js/email.js', 'static/js/codecopy.js'], 'static/js/abridge_nosearch_nopwa.min.js'); minify(['static/js/sw_load.js']); minify(['static/sw.js']); } else if (pwa) { @@ -210,24 +210,24 @@ async function abridge() { if (fs.existsSync('static/manifest.json')) { let out; try { - out = JSON.minify(fs.readFileSync('static/manifest.json', {encoding:"utf-8"})); - } catch(err) { + out = JSON.minify(fs.readFileSync('static/manifest.json', { encoding: "utf-8" })); + } catch (err) { console.log(err); } fs.writeFileSync('static/manifest.min.json', out); } - abridge_bundle = bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,false); - minify(abridge_bundle,'static/js/abridge_nopwa.min.js'); + abridge_bundle = bundle(bpath, js_prestyle, js_switcher, js_email_encode, js_copycode, search_library, index_format, uglyurls, false); + minify(abridge_bundle, 'static/js/abridge_nopwa.min.js'); - abridge_bundle = bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa); - minify(abridge_bundle,'static/js/abridge.min.js'); + abridge_bundle = bundle(bpath, js_prestyle, js_switcher, js_email_encode, js_copycode, search_library, index_format, uglyurls, pwa); + minify(abridge_bundle, 'static/js/abridge.min.js'); // cleanup - _rmRegex(path.join(bpath, "static/js/"),/^pagefind_search\.js$/);//pagefind intermediate file that is now in bundle. + _rmRegex(path.join(bpath, "static/js/"), /^pagefind_search\.js$/);//pagefind intermediate file that is now in bundle. console.log('Zola Build to generate new integrity hashes for the previously minified files:'); - await execWrapper('zola build'+args); + await execWrapper('zola build' + args); } async function _headersWASM() { @@ -236,11 +236,11 @@ async function _headersWASM() { // This function adds wasm-unsafe-eval to the pagefind and tinysearch demos without adding it to the elasticlunr demo. const { replaceInFileSync } = await import('replace-in-file'); if (search_library === 'pagefind') { - replaceInFileSync({files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'"}); + replaceInFileSync({ files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'" }); } else if (search_library === 'tinysearch') { - replaceInFileSync({files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'"}); + replaceInFileSync({ files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'" }); } else { - replaceInFileSync({files: 'static/_headers', from: /script-src 'wasm-unsafe-eval' 'self'/g, to: "script-src 'self'"}); + replaceInFileSync({ files: 'static/_headers', from: /script-src 'wasm-unsafe-eval' 'self'/g, to: "script-src 'self'" }); } } @@ -254,7 +254,7 @@ function _rmRecursive(targetFiles) { } } -function _cpRecursive(source,dest) { +function _cpRecursive(source, dest) { try { fs.cpSync(source, dest, { recursive: true }); } catch (error) { @@ -262,7 +262,7 @@ function _cpRecursive(source,dest) { } } -function _rmRegex(path,regex) { +function _rmRegex(path, regex) { try { fs.readdirSync(path).filter(f => regex.test(f)).forEach(f => fs.unlinkSync(path + f)); } catch (error) { @@ -272,7 +272,7 @@ function _rmRegex(path,regex) { } } -function _cpRegex(source,dest,regex) { +function _cpRegex(source, dest, regex) { try { fs.readdirSync(source).filter(f => regex.test(f)).forEach(f => fs.copyFileSync(source + f, dest + f)); } catch (error) { @@ -280,38 +280,38 @@ function _cpRegex(source,dest,regex) { } } -function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa) { +function bundle(bpath, js_prestyle, js_switcher, js_email_encode, js_copycode, search_library, index_format, uglyurls, pwa) { minify_files = []; if (js_prestyle) { - minify_files.push(bpath+'static/js/prestyle.js'); + minify_files.push(bpath + 'static/js/prestyle.js'); } if (js_switcher) { - minify_files.push(bpath+'static/js/theme_button.js'); + minify_files.push(bpath + 'static/js/theme_button.js'); } if (js_email_encode) { - minify_files.push(bpath+'static/js/email.js'); + minify_files.push(bpath + 'static/js/email.js'); } if (js_copycode) { - minify_files.push(bpath+'static/js/codecopy.js'); + minify_files.push(bpath + 'static/js/codecopy.js'); } if (search_library) { - if ((search_library === 'offline' || (search_library === 'elasticlunrjava' && uglyurls === true))) { - minify_files.push('public/search_index.en.js'); - minify_files.push(bpath+'static/js/elasticlunr.min.js'); - minify_files.push(bpath+'static/js/searchjavaugly.js'); - } else if (search_library === 'elasticlunrjava') { - minify_files.push('public/search_index.en.js'); - minify_files.push(bpath+'static/js/elasticlunr.min.js'); - minify_files.push(bpath+'static/js/searchjava.js'); - } else if (search_library === 'elasticlunr') {//abridge default - minify_files.push(bpath+'static/js/elasticlunr.min.js'); - minify_files.push(bpath+'static/js/search.js'); - } else if (search_library === 'pagefind') { - minify_files.push(bpath+'static/js/pagefind_search.js'); - } else if (search_library === 'tinysearch') { - minify_files.push(bpath+'static/js/tinysearch.js'); - } + if ((search_library === 'offline' || (search_library === 'elasticlunrjava' && uglyurls === true))) { + minify_files.push('public/search_index.en.js'); + minify_files.push(bpath + 'static/js/elasticlunr.min.js'); + minify_files.push(bpath + 'static/js/searchjavaugly.js'); + } else if (search_library === 'elasticlunrjava') { + minify_files.push('public/search_index.en.js'); + minify_files.push(bpath + 'static/js/elasticlunr.min.js'); + minify_files.push(bpath + 'static/js/searchjava.js'); + } else if (search_library === 'elasticlunr') {//abridge default + minify_files.push(bpath + 'static/js/elasticlunr.min.js'); + minify_files.push(bpath + 'static/js/search.js'); + } else if (search_library === 'pagefind') { + minify_files.push(path.join(__dirname, 'static/js/pagefind_search.js')); + } else if (search_library === 'tinysearch') { + minify_files.push(bpath + 'static/js/tinysearch.js'); + } } if (pwa) { minify_files.push('static/js/sw_load.js'); @@ -319,7 +319,7 @@ function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search return minify_files; } -function minify(fileA,outfile) { +function minify(fileA, outfile) { const options = { mangle: true, compress: { @@ -339,10 +339,10 @@ function minify(fileA,outfile) { } } if (!outfile) {// outfile parameter omitted, infer based on input - outfile = fileA[0].slice(0,-2)+'min.js'; + outfile = fileA[0].slice(0, -2) + 'min.js'; } var filesContents = fileA.map(function (file) {// array input to support multiple files - return fs.readFileSync(file, 'utf8'); + return fs.readFileSync(file, 'utf8'); }); result = UglifyJS.minify(filesContents, options); @@ -352,7 +352,7 @@ function minify(fileA,outfile) { async function searchChange(searchOption) { const { replaceInFileSync } = await import('replace-in-file'); - replaceInFileSync({files: 'config.toml', from: /search_library.*=.*/g, to: 'search_library = \"'+searchOption+'\"'}); + replaceInFileSync({ files: 'config.toml', from: /search_library.*=.*/g, to: 'search_library = \"' + searchOption + '\"' }); } if (args === ' offline') { @@ -388,7 +388,7 @@ async function createPagefindIndex() { // Convert each lang in langArray to a promise that performs the desired operations const promises = langArray.map((lang) => (async () => { - const filePath = path.join(__dirname,"public/search_index." + lang + ".json"); + const filePath = path.join(__dirname, "public/search_index." + lang + ".json"); // Read the file content synchronously (consider using async readFile for better performance) const fileContent = fs.readFileSync(filePath); @@ -414,7 +414,7 @@ async function createPagefindIndex() { .then(async () => { // Write the index files to disk const { errors } = await index.writeFiles({ - outputPath: "./static/js/", + outputPath: path.join(__dirname, "./static/js/"), }); if (errors.length > 0) { console.log("Errors: ", errors); @@ -434,7 +434,7 @@ async function createPagefindIndex() { fs.writeFileSync(pagefindPath, pagefindContent); // now insert the CJS into the anonymous function within pagefind.search.js - const pagefind_searchPath = path.join(__dirname, "static/js/pagefind.search.js");//file to insert into + const pagefind_searchPath = path.join(bpath, "static/js/pagefind.search.js");//file to insert into const search_pagefindPath = path.join(__dirname, "static/js/pagefind_search.js");//output let pagefind_searchContent = fs.readFileSync(pagefind_searchPath, "utf8"); // Now insert into pagefind.search.js at this location: //insertHere @@ -463,6 +463,8 @@ async function sync() { const staticFolder = path.join(__dirname, "static/js"); const submoduleFolder = path.join(__dirname, "themes/abridge/static/js"); + fs.mkdirSync(staticFolder, { recursive: true }); + const files = fs.readdirSync(staticFolder); files.forEach((file) => { |
