diff options
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | common/common.js | 146 | ||||
-rw-r--r-- | package-lock.json | 34 | ||||
-rw-r--r-- | package.json | 6 | ||||
-rw-r--r-- | src/gen-thumbs.js (renamed from upd8/gen-thumbs.js) | 79 | ||||
-rw-r--r-- | src/static/client.js (renamed from static/client.js) | 8 | ||||
-rw-r--r-- | src/static/icons.svg (renamed from static/icons.svg) | 0 | ||||
-rw-r--r-- | src/static/lazy-loading.js (renamed from static/lazy-loading.js) | 0 | ||||
-rw-r--r-- | src/static/site-basic.css (renamed from static/site-basic.css) | 0 | ||||
-rw-r--r-- | src/static/site.css (renamed from static/site.css) | 0 | ||||
-rw-r--r-- | src/strings-default.json (renamed from upd8/strings-default.json) | 0 | ||||
-rwxr-xr-x | src/upd8.js (renamed from upd8/main.js) | 588 | ||||
-rw-r--r-- | src/util/cli.js | 210 | ||||
-rw-r--r-- | src/util/colors.js | 47 | ||||
-rw-r--r-- | src/util/html.js | 92 | ||||
-rw-r--r-- | src/util/link.js | 67 | ||||
-rw-r--r-- | src/util/node-utils.js | 27 | ||||
-rw-r--r-- | src/util/sugar.js | 70 | ||||
-rw-r--r-- | src/util/urls.js | 102 | ||||
-rw-r--r-- | src/util/wiki-data.js | 126 | ||||
-rw-r--r-- | upd8/util.js | 423 |
21 files changed, 978 insertions, 1053 deletions
diff --git a/README.md b/README.md index f0e2493b..a463f101 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ HSMusic, short for the *Homestuck Music Wiki*, is a revitalization and reimagini **Disclaimer:** most of the code here *sucks*. It's been shambled together over the course of over a year, and while we're fairly confident it's all at minimum functional, we can't guarantee the same about its understandability! Still, for the official release of [hsmusic.wiki][hsmusic], we've done our best to put together a codebase which is *somewhat* navigable. The description below summarizes it: -* `upd8`: "Build" code for the site. Everything specific to generating the structure and HTML content of the website is conatined in this folder. As expected, it's pretty massive, and is currently undergoing some much-belated restructuring. -* `static`: Static code and supporting files. Everything here is wholly client-side and referenced by the generated HTML files. -* `common`: Code which is depended upon by both client- and server-side code. For the most part, this is constants such as directory paths, though there are a few handy algorithms here too. +* `src/upd8.js`: "Build" code for the site. Everything specific to generating the structure and HTML content of the website is conatined in this file. As expected, it's pretty massive, and is currently undergoing some much-belated restructuring. +* `src/static`: Static code and supporting files. Everything here is wholly client-side and referenced by the generated HTML files. +* `src/common`: Code which is depended upon by both client- and server-side code. For the most part, this is constants such as directory paths, though there are a few handy algorithms here too. * In the not quite so far past, we used to have `data` and `media` folders too. Today, for portability and convenience in project structure, those are saved in separate repositories, and you can pass hsmusic paths to them through the `--data` and `--media` options, or the `HSMUSIC_DATA` and `HSMUSIC_MEDIA` environment variables. * Data directory: The majority of data files belonging to the wiki are here. If you were to, say, create a fork of hsmusic for some other music archival project, you'd want to change the files here. Data files are all a custom text format designed to be easy to edit, process, and maintain; they should be self-descriptive. * Media directory: Images and other static files referenced by generated and static content across the site. Many of the files here are cover art, and their names match the automatically generated "kebab case" identifiers for tracks and albums (or a manually overridden one). diff --git a/common/common.js b/common/common.js deleted file mode 100644 index 165e0f6d..00000000 --- a/common/common.js +++ /dev/null @@ -1,146 +0,0 @@ -// This file's shared 8y 8oth the client and the static file 8uilder (i.e, -// upd8.js). It's got common constants and a few utility functions! - -const C = { - // Code that's common 8etween the 8uild code (i.e. upd8.js) and gener8ted - // site code should 8e put here. Which, uh, only really means this one - // file. 8ut rather than hard code it, anything in this directory can 8e - // shared across 8oth ends of the code8ase. - // (This gets symlinked into the --data directory.) - COMMON_DIRECTORY: 'common', - - // Code that's used only in the static site! CSS, cilent JS, etc. - // (This gets symlinked into the --data directory.) - STATIC_DIRECTORY: 'static', - - // Su8directory under DATA_DIRECTORY for al8um files. - DATA_ALBUM_DIRECTORY: 'album', - - // Media files! This is symlinked into the --data directory from the - // also user-provided --media directory. - MEDIA_DIRECTORY: 'media', - - // Contains a folder for each al8um, within which is the al8um cover art - // as well as any track art. Structure itself looks somethin' like this: - // * album-art/<album.directory>/cover.jpg - // * album-art/<album.directory>/<track1.directory>.jpg - // * album-art/<album.directory>/<track2.directory>.jpg - MEDIA_ALBUM_ART_DIRECTORY: 'album-art', - - // Just one folder, with a single image for each flash, matching its output - // directory like al8um and track art. (Just keep in mind the directory of - // a flash is just its page num8er most of the time.) - MEDIA_FLASH_ART_DIRECTORY: 'flash-art', - - // Again, a single folder, with one image for each artist, matching their - // output directory (which is usually their name in ke8a8-case). Although, - // unlike other art directories, you don't to specify an image for *every* - // artist - and present files will 8e automatically added! - MEDIA_ARTIST_AVATAR_DIRECTORY: 'artist-avatar', - - // Miscellaneous stuff! This is pretty much only referenced in commentary - // fields. - MEDIA_MISC_DIRECOTRY: 'misc', - - // The folder you stick your random downloads in is called "Downloads", - // yeah? (Unless you sort all your downloads into manual, organized - // locations. Good for you.) It might just 8e me, 8ut I've always said "the - // downloads folder." And yet here I say "the al8um directory!" It's like - // we've gotten "Downloads" as a name so ingrained into our heads that we - // use it like an adjective too, even though it doesn't make any - // grammatical sense to do so. Anyway, also for contrast, note that this - // folder is called "album" and not "albums". To 8e clear, that IS against - // how I normally name folders - 8ut here, I'm doing it to match 8andcamp's - // URL schema: "/album/genesis-frog" instead of "/albums/genesis-frog." - // That seems to kind of 8e a standard for a lot of sites? 8ut only KIND OF. - // Twitter has the weird schema of "/<user>/status/<id>" (not "statuses")... - // 8ut it also has "/<user>/likes", so I really have no idea how people - // decide to make their URL schemas consistent. Luckily I don't have to - // worry a8out any of that, 8ecause I'm just stealing 8andcamp. - // - // Upd8 03/11/2020: Oh my god this was a pain to re-align (copying from - // udp8.js over to shared.js). - // - // Upd8 03/10/2021 (wow, almost exactly a year later): This code comment - // from literally the first day of wiki development is finally no longer - // necessary! It was commenting constnats like "ALBUM_DIRECTORY" 8efore. - // 8ut we don't have those constants anymore, 'cuz urlSpec in upd8.js - // covers all that! - - UNRELEASED_TRACKS_DIRECTORY: 'unreleased-tracks', - OFFICIAL_GROUP_DIRECTORY: 'official', - FANDOM_GROUP_DIRECTORY: 'fandom', - - // This function was originally made to sort just al8um data, 8ut its exact - // code works fine for sorting tracks too, so I made the varia8les and names - // more general. - sortByDate: data => { - // Just to 8e clear: sort is a mutating function! I only return the array - // 8ecause then you don't have to define it as a separate varia8le 8efore - // passing it into this function. - return data.sort((a, b) => a.date - b.date); - }, - - // Same details as the sortByDate, 8ut for covers~ - sortByArtDate: data => { - return data.sort((a, b) => (a.coverArtDate || a.date) - (b.coverArtDate || b.date)); - }, - - // This gets all the track o8jects defined in every al8um, and sorts them 8y - // date released. Generally, albumData will pro8a8ly already 8e sorted 8efore - // you pass it to this function, 8ut individual tracks can have their own - // original release d8, distinct from the al8um's d8. I allowed that 8ecause - // in Homestuck, the first four Vol.'s were com8ined into one al8um really - // early in the history of the 8andcamp, and I still want to use that as the - // al8um listing (not the original four al8um listings), 8ut if I only did - // that, all the tracks would 8e sorted as though they were released at the - // same time as the compilation al8um - i.e, after some other al8ums (including - // Vol.'s 5 and 6!) were released. That would mess with chronological listings - // including tracks from multiple al8ums, like artist pages. So, to fix that, - // I gave tracks an Original Date field, defaulting to the release date of the - // al8um if not specified. Pretty reasona8le, I think! Oh, and this feature can - // 8e used for other projects too, like if you wanted to have an al8um listing - // compiling a 8unch of songs with radically different & interspersed release - // d8s, 8ut still keep the al8um listing in a specific order, since that isn't - // sorted 8y date. - getAllTracks: albumData => C.sortByDate(albumData.reduce((acc, album) => acc.concat(album.tracks), [])), - - getKebabCase: name => name.split(' ').join('-').replace(/&/g, 'and').replace(/[^a-zA-Z0-9\-]/g, '').replace(/-{2,}/g, '-').replace(/^-+|-+$/g, '').toLowerCase(), - - // Terri8le hack: since artists aren't really o8jects and don't have proper - // "directories", we just reformat the artist's name. - getArtistDirectory: artistName => C.getKebabCase(artistName), - - getArtistNumContributions: artist => ( - artist.tracks.asAny.length + - artist.albums.asCoverArtist.length + - (artist.flashes ? artist.flashes.asContributor.length : 0) - ), - - getArtistCommentary: (artist, {justEverythingMan}) => justEverythingMan.filter(thing => thing.commentary && thing.commentary.replace(/<\/?b>/g, '').includes('<i>' + artist.name + ':</i>')), - - // Graciously stolen from https://stackoverflow.com/a/54071699! ::::) - // in: r,g,b in [0,1], out: h in [0,360) and s,l in [0,1] - rgb2hsl: (r,g,b) => { - let a=Math.max(r,g,b), n=a-Math.min(r,g,b), f=(1-Math.abs(a+a-n-1)); - let h= n && ((a==r) ? (g-b)/n : ((a==g) ? 2+(b-r)/n : 4+(r-g)/n)); - return [60*(h<0?h+6:h), f ? n/f : 0, (a+a-n)/2]; - }, - - getColors: primary => { - const [ r, g, b ] = primary.slice(1) - .match(/[0-9a-fA-F]{2,2}/g) - .slice(0, 3) - .map(val => parseInt(val, 16) / 255); - const [ h, s, l ] = C.rgb2hsl(r, g, b); - const dim = `hsl(${Math.round(h)}deg, ${Math.round(s * 50)}%, ${Math.round(l * 80)}%)`; - - return {primary, dim}; - } -}; - -if (typeof module === 'object') { - module.exports = C; -} else if (typeof window === 'object') { - window.C = C; -} diff --git a/package-lock.json b/package-lock.json index 76f76678..155caebd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,11 +10,10 @@ "license": "GPL-3.0", "dependencies": { "fix-whitespace": "^1.0.4", - "he": "^1.2.0", - "mkdirp": "^0.5.5" + "he": "^1.2.0" }, "bin": { - "hsmusic": "upd8.js" + "hsmusic": "upd8/main.js" } }, "node_modules/fix-whitespace": { @@ -29,22 +28,6 @@ "bin": { "he": "bin/he" } - }, - "node_modules/minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" - }, - "node_modules/mkdirp": { - "version": "0.5.5", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", - "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", - "dependencies": { - "minimist": "^1.2.5" - }, - "bin": { - "mkdirp": "bin/cmd.js" - } } }, "dependencies": { @@ -57,19 +40,6 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==" - }, - "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" - }, - "mkdirp": { - "version": "0.5.5", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", - "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", - "requires": { - "minimist": "^1.2.5" - } } } } diff --git a/package.json b/package.json index 80ee2c52..1017d4aa 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "hsmusic-wiki", "version": "0.1.0", "description": "static wiki software cataloguing collaborative creation", + "type": "module", "main": "upd8.js", "bin": { - "hsmusic": "./upd8/main.js" + "hsmusic": "./src/upd8.js" }, "dependencies": { "fix-whitespace": "^1.0.4", - "he": "^1.2.0", - "mkdirp": "^0.5.5" + "he": "^1.2.0" }, "license": "GPL-3.0" } diff --git a/upd8/gen-thumbs.js b/src/gen-thumbs.js index bec01d1d..d636d2fd 100644 --- a/upd8/gen-thumbs.js +++ b/src/gen-thumbs.js @@ -77,41 +77,55 @@ const CACHE_FILE = 'thumbnail-cache.json'; const WARNING_DELAY_TIME = 10000; -const { spawn } = require('child_process'); -const crypto = require('crypto'); -const fsp = require('fs/promises'); // Whatcha know! Nice. -const fs = require('fs'); // Still gotta include 8oth tho, for createReadStream. -const path = require('path'); +import { spawn } from 'child_process'; +import { createHash } from 'crypto'; +import * as path from 'path'; -const { - delay, +import { + readdir, + readFile, + writeFile +} from 'fs/promises'; // Whatcha know! Nice. + +import { + createReadStream +} from 'fs'; // Still gotta import from 8oth tho, for createReadStream. + +import { logError, logInfo, logWarn, parseOptions, - progressPromiseAll, + progressPromiseAll +} from './util/cli.js'; + +import { promisifyProcess, +} from './util/node-utils.js'; + +import { + delay, queue, -} = require('./util'); +} from './util/sugar.js'; function traverse(startDirPath, { filterFile = () => true, filterDir = () => true } = {}) { const recursive = (names, subDirPath) => Promise - .all(names.map(name => fsp.readdir(path.join(startDirPath, subDirPath, name)).then( + .all(names.map(name => readdir(path.join(startDirPath, subDirPath, name)).then( names => filterDir(name) ? recursive(names, path.join(subDirPath, name)) : [], err => filterFile(name) ? [path.join(subDirPath, name)] : []))) .then(pathArrays => pathArrays.flatMap(x => x)); - return fsp.readdir(startDirPath) + return readdir(startDirPath) .then(names => recursive(names, '')); } function readFileMD5(filePath) { return new Promise((resolve, reject) => { - const md5 = crypto.createHash('md5'); - const stream = fs.createReadStream(filePath); + const md5 = createHash('md5'); + const stream = createReadStream(filePath); stream.on('data', data => md5.update(data)); stream.on('end', data => resolve(md5.digest('hex'))); stream.on('error', err => reject(err)); @@ -147,7 +161,7 @@ function generateImageThumbnails(filePath) { }); } -async function genThumbs(mediaPath, { +export default async function genThumbs(mediaPath, { queueSize = 0, quiet = false } = {}) { @@ -179,7 +193,7 @@ async function genThumbs(mediaPath, { let cache, firstRun = false, failedReadingCache = false; try { - cache = JSON.parse(await fsp.readFile(path.join(mediaPath, CACHE_FILE))); + cache = JSON.parse(await readFile(path.join(mediaPath, CACHE_FILE))); quietInfo`Cache file successfully read.`; } catch (error) { cache = {}; @@ -195,7 +209,7 @@ async function genThumbs(mediaPath, { } try { - await fsp.writeFile(path.join(mediaPath, CACHE_FILE), JSON.stringify(cache)); + await writeFile(path.join(mediaPath, CACHE_FILE), JSON.stringify(cache)); quietInfo`Writing to cache file appears to be working.`; } catch (error) { logWarn`Test of cache file writing failed: ${error}`; @@ -280,7 +294,7 @@ async function genThumbs(mediaPath, { } try { - await fsp.writeFile(path.join(mediaPath, CACHE_FILE), JSON.stringify(updatedCache)); + await writeFile(path.join(mediaPath, CACHE_FILE), JSON.stringify(updatedCache)); quietInfo`Updated cache file successfully written!`; } catch (error) { logWarn`Failed to write updated cache file: ${error}`; @@ -289,35 +303,4 @@ async function genThumbs(mediaPath, { } return true; -}; - -module.exports = genThumbs; - -if (require.main === module) { - (async () => { - const miscOptions = await parseOptions(process.argv.slice(2), { - 'media': { - type: 'value' - }, - - 'queue-size': { - type: 'value', - validate(size) { - if (parseInt(size) !== parseFloat(size)) return 'an integer'; - if (parseInt(size) < 0) return 'a counting number or zero'; - return true; - } - }, - queue: {alias: 'queue-size'} - }); - - const mediaPath = miscOptions.media || process.env.HSMUSIC_MEDIA; - if (!mediaPath) { - logError`Expected --media option or HSMUSIC_MEDIA to be set`; - } - - const queueSize = +(miscOptions['queue-size'] ?? 0); - - await genThumbs(mediaPath, {queueSize}); - })().catch(err => console.error(err)); } diff --git a/static/client.js b/src/static/client.js index 649ca312..c12ff355 100644 --- a/static/client.js +++ b/src/static/client.js @@ -5,7 +5,9 @@ // // Upd8: As of 04/02/2021, it's now used for info cards too! Nice. -'use strict'; +import { + getColors +} from '../util/colors.js'; let albumData, artistData, flashData; let officialAlbumData, fandomAlbumData, artistNames; @@ -47,7 +49,7 @@ if ( // Miscellaneous helpers ---------------------------------- function rebase(href, rebaseKey = 'rebaseLocalized') { - const relative = document.documentElement.dataset[rebaseKey] + '/'; + const relative = (document.documentElement.dataset[rebaseKey] || '.') + '/'; if (relative) { return relative + href; } else { @@ -211,7 +213,7 @@ let endFastHoverTimeout = null; function colorLink(a, color) { if (color) { - const { primary, dim } = C.getColors(color); + const { primary, dim } = getColors(color); a.style.setProperty('--primary-color', primary); a.style.setProperty('--dim-color', dim); } diff --git a/static/icons.svg b/src/static/icons.svg index 1e4351bf..1e4351bf 100644 --- a/static/icons.svg +++ b/src/static/icons.svg diff --git a/static/lazy-loading.js b/src/static/lazy-loading.js index a403d7ca..a403d7ca 100644 --- a/static/lazy-loading.js +++ b/src/static/lazy-loading.js diff --git a/static/site-basic.css b/src/static/site-basic.css index d26584ae..d26584ae 100644 --- a/static/site-basic.css +++ b/src/static/site-basic.css diff --git a/static/site.css b/src/static/site.css index ae41f88d..ae41f88d 100644 --- a/static/site.css +++ b/src/static/site.css diff --git a/upd8/strings-default.json b/src/strings-default.json index 7a948d64..7a948d64 100644 --- a/upd8/strings-default.json +++ b/src/strings-default.json diff --git a/upd8/main.js b/src/upd8.js index 84bab6c1..c0bea083 100755 --- a/upd8/main.js +++ b/src/upd8.js @@ -62,76 +62,99 @@ // spirit of this "make things more consistent" attitude I 8rought up 8ack in // August, stuff's lookin' 8etter than ever now. W00t! -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const util = require('util'); +import * as path from 'path'; +import { promisify } from 'util'; +import { fileURLToPath } from 'url'; // I made this dependency myself! A long, long time ago. It is pro8a8ly my // most useful li8rary ever. I'm not sure 8esides me actually uses it, though. -const fixWS = require('fix-whitespace'); +import fixWS from 'fix-whitespace'; // Wait nevermind, I forgot a8out why-do-kids-love-the-taste-of-cinnamon-toast- // crunch. THAT is my 8est li8rary. -// The require function just returns whatever the module exports, so there's -// no reason you can't wrap it in some decorator right out of the 8ox. Which is -// exactly what we do here. -const mkdirp = util.promisify(require('mkdirp')); - // It stands for "HTML Entities", apparently. Cursed. -const he = require('he'); - -// This is the dum8est name for a function possi8le. Like, SURE, fine, may8e -// the UNIX people had some valid reason to go with the weird truncated -// lowercased convention they did. 8ut Node didn't have to ALSO use that -// convention! Would it have 8een so hard to just name the function something -// like fs.readDirectory???????? No, it wouldn't have 8een. -const readdir = util.promisify(fs.readdir); -// 8ut okay, like, look at me. DOING THE SAME THING. See, *I* could have named -// my promisified function differently, and yet I did not. I literally cannot -// explain why. We are all used to following in the 8ad decisions of our -// ancestors, and never never never never never never never consider that hey, -// may8e we don't need to make the exact same decisions they did. Even when -// we're perfectly aware th8t's exactly what we're doing! Programmers, -// including me, are all pretty stupid. - -// 8ut I mean, come on. Look. Node decided to use readFile, instead of like, -// what, cat? Why couldn't they rename readdir too???????? As Johannes Kepler -// once so elegantly put it: "Shrug." -const readFile = util.promisify(fs.readFile); -const writeFile = util.promisify(fs.writeFile); -const access = util.promisify(fs.access); -const symlink = util.promisify(fs.symlink); -const unlink = util.promisify(fs.unlink); - -const { - cacheOneArg, - call, - chunkByConditions, - chunkByProperties, - curry, +import he from 'he'; + +import { + // This is the dum8est name for a function possi8le. Like, SURE, fine, may8e + // the UNIX people had some valid reason to go with the weird truncated + // lowercased convention they did. 8ut Node didn't have to ALSO use that + // convention! Would it have 8een so hard to just name the function + // something like fs.readDirectory???????? No, it wouldn't have 8een. + readdir, + // ~~ 8ut okay, like, look at me. DOING THE SAME THING. See, *I* could have + // named my promisified function differently, and yet I did not. I literally + // cannot explain why. We are all used to following in the 8ad decisions of + // our ancestors, and never never never never never never never consider + // that hey, may8e we don't need to make the exact same decisions they did. + // Even when we're perfectly aware th8t's exactly what we're doing! ~~ + // + // 2021 ADDENDUM: Ok, a year and a half later the a8ove is still true, + // except for the part a8out promisifying, since fs/promises + // already does that for us. 8ut I could STILL import it + // using my own name (`readdir as readDirectory`), and yet + // here I am, defin8tely not doing that. + // SOME THINGS NEVER CHANGE. + // + // Programmers, including me, are all pretty stupid. + + // 8ut I mean, come on. Look. Node decided to use readFile, instead of like, + // what, cat? Why couldn't they rename readdir too???????? As Johannes + // Kepler once so elegantly put it: "Shrug." + readFile, + writeFile, + access, + mkdir, + symlink, + unlink +} from 'fs/promises'; + +import genThumbs from './gen-thumbs.js'; +import * as html from './util/html.js'; +import link from './util/link.js'; + +import { decorateTime, - filterEmptyLines, - joinNoOxford, - mapInPlace, logWarn, logInfo, logError, parseOptions, - progressPromiseAll, + progressPromiseAll +} from './util/cli.js'; + +import { + getLinkThemeString, + getThemeString +} from './util/colors.js'; + +import { + chunkByConditions, + chunkByProperties, + getAllTracks, + getArtistCommentary, + getArtistNumContributions, + getKebabCase, + sortByArtDate, + sortByDate, + sortByName +} from './util/wiki-data.js'; + +import { + call, + filterEmptyLines, + mapInPlace, queue, - s, - sortByName, splitArray, - th, unique, withEntries -} = require('./util'); +} from './util/sugar.js'; -const genThumbs = require('./gen-thumbs'); +import { + generateURLs, + thumb +} from './util/urls.js'; -const C = require('../common/common'); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const CACHEBUST = 5; @@ -145,7 +168,26 @@ const GROUP_DATA_FILE = 'groups.txt'; const STATIC_PAGE_DATA_FILE = 'static-pages.txt'; const DEFAULT_STRINGS_FILE = 'strings-default.json'; -const CSS_FILE = 'site.css'; +const UNRELEASED_TRACKS_DIRECTORY = 'unreleased-tracks'; +const OFFICIAL_GROUP_DIRECTORY = 'official'; +const FANDOM_GROUP_DIRECTORY = 'fandom'; + +// Code that's common 8etween the 8uild code (i.e. upd8.js) and gener8ted +// site code should 8e put here. Which, uh, ~~only really means this one +// file~~ is now a variety of useful utilities! +// +// Rather than hard code it, anything in this directory can 8e shared across +// 8oth ends of the code8ase. +// (This gets symlinked into the --data directory.) +const UTILITY_DIRECTORY = 'util'; + +// Code that's used only in the static site! CSS, cilent JS, etc. +// (This gets symlinked into the --data directory.) +const STATIC_DIRECTORY = 'static'; + +// Su8directory under provided --data directory for al8um files, which are +// read from and processed to compose the majority of album and track data. +const DATA_ALBUM_DIRECTORY = 'album'; // Shared varia8les! These are more efficient to access than a shared varia8le // (or at least I h8pe so), and are easier to pass across functions than a @@ -184,99 +226,6 @@ let queueSize; let languages; -const html = { - // Non-comprehensive. ::::P - selfClosingTags: ['br', 'img'], - - // Pass to tag() as an attri8utes key to make tag() return a 8lank string - // if the provided content is empty. Useful for when you'll only 8e showing - // an element according to the presence of content that would 8elong there. - onlyIfContent: Symbol(), - - tag(tagName, ...args) { - const selfClosing = html.selfClosingTags.includes(tagName); - - let openTag; - let content; - let attrs; - - if (typeof args[0] === 'object' && !Array.isArray(args[0])) { - attrs = args[0]; - content = args[1]; - } else { - content = args[0]; - } - - if (selfClosing && content) { - throw new Error(`Tag <${tagName}> is self-closing but got content!`); - } - - if (attrs?.[html.onlyIfContent] && !content) { - return ''; - } - - if (attrs) { - const attrString = html.attributes(args[0]); - if (attrString) { - openTag = `${tagName} ${attrString}`; - } - } - - if (!openTag) { - openTag = tagName; - } - - if (Array.isArray(content)) { - content = content.filter(Boolean).join('\n'); - } - - if (content) { - if (content.includes('\n')) { - return fixWS` - <${openTag}> - ${content} - </${tagName}> - `; - } else { - return `<${openTag}>${content}</${tagName}>`; - } - } else { - if (selfClosing) { - return `<${openTag}>`; - } else { - return `<${openTag}></${tagName}>`; - } - } - }, - - escapeAttributeValue(value) { - return value - .replaceAll('"', '"') - .replaceAll("'", '''); - }, - - attributes(attribs) { - return Object.entries(attribs) - .map(([ key, val ]) => { - if (!val) - return [key, val]; - else if (typeof val === 'string' || typeof val === 'boolean') - return [key, val]; - else if (typeof val === 'number') - return [key, val.toString()]; - else if (Array.isArray(val)) - return [key, val.join(' ')]; - else - throw new Error(`Attribute value for ${key} should be primitive or array, got ${typeof val}`); - }) - .filter(([ key, val ]) => val) - .map(([ key, val ]) => (typeof val === 'boolean' - ? `${key}` - : `${key}="${html.escapeAttributeValue(val)}"`)) - .join(' '); - } -}; - const urlSpec = { data: { prefix: 'data/', @@ -332,7 +281,10 @@ const urlSpec = { root: '', path: '<>', - commonFile: 'common/<>', + utilityRoot: 'util', + staticRoot: 'static', + + utilityFile: 'util/<>', staticFile: 'static/<>' } }, @@ -363,149 +315,7 @@ urlSpec.localizedWithBaseDirectory = { ) }; -const linkHelper = (hrefFn, {color = true, attr = null} = {}) => - (thing, { - strings, to, - text = '', - class: className = '', - hash = '' - }) => ( - html.tag('a', { - ...attr ? attr(thing) : {}, - href: hrefFn(thing, {to}) + (hash ? (hash.startsWith('#') ? '' : '#') + hash : ''), - style: color ? getLinkThemeString(thing) : '', - class: className - }, text || thing.name) - ); - -const linkDirectory = (key, {expose = null, attr = null, ...conf} = {}) => - linkHelper((thing, {to}) => to('localized.' + key, thing.directory), { - attr: thing => ({ - ...attr ? attr(thing) : {}, - ...expose ? {[expose]: thing.directory} : {} - }), - ...conf - }); - -const linkPathname = (key, conf) => linkHelper(({directory: pathname}, {to}) => to(key, pathname), conf); -const linkIndex = (key, conf) => linkHelper((_, {to}) => to('localized.' + key), conf); - -const link = { - album: linkDirectory('album'), - albumCommentary: linkDirectory('albumCommentary'), - artist: linkDirectory('artist', {color: false}), - artistGallery: linkDirectory('artistGallery', {color: false}), - commentaryIndex: linkIndex('commentaryIndex', {color: false}), - flashIndex: linkIndex('flashIndex', {color: false}), - flash: linkDirectory('flash'), - groupInfo: linkDirectory('groupInfo'), - groupGallery: linkDirectory('groupGallery'), - home: linkIndex('home', {color: false}), - listingIndex: linkIndex('listingIndex'), - listing: linkDirectory('listing'), - newsIndex: linkIndex('newsIndex', {color: false}), - newsEntry: linkDirectory('newsEntry', {color: false}), - staticPage: linkDirectory('staticPage', {color: false}), - tag: linkDirectory('tag'), - track: linkDirectory('track', {expose: 'data-track'}), - - media: linkPathname('media.path', {color: false}), - root: linkPathname('shared.path', {color: false}), - data: linkPathname('data.path', {color: false}), - site: linkPathname('localized.path', {color: false}) -}; - -const thumbnailHelper = name => file => - file.replace(/\.(jpg|png)$/, name + '.jpg'); - -const thumb = { - medium: thumbnailHelper('.medium'), - small: thumbnailHelper('.small') -}; - -function generateURLs(fromPath) { - const getValueForFullKey = (obj, fullKey, prop = null) => { - const [ groupKey, subKey ] = fullKey.split('.'); - if (!groupKey || !subKey) { - throw new Error(`Expected group key and subkey (got ${fullKey})`); - } - - if (!obj.hasOwnProperty(groupKey)) { - throw new Error(`Expected valid group key (got ${groupKey})`); - } - - const group = obj[groupKey]; - - if (!group.hasOwnProperty(subKey)) { - throw new Error(`Expected valid subkey (got ${subKey} for group ${groupKey})`); - } - - return { - value: group[subKey], - group - }; - }; - - const generateTo = (fromPath, fromGroup) => { - const rebasePrefix = '../'.repeat((fromGroup.prefix || '').split('/').filter(Boolean).length); - - const pathHelper = (toPath, toGroup) => { - let target = toPath; - - let argIndex = 0; - target = target.replaceAll('<>', () => `<${argIndex++}>`); - - if (toGroup.prefix !== fromGroup.prefix) { - // TODO: Handle differing domains in prefixes. - target = rebasePrefix + (toGroup.prefix || '') + target; - } - - return (path.relative(fromPath, target) - + (toPath.endsWith('/') ? '/' : '')); - }; - - const groupSymbol = Symbol(); - - const groupHelper = urlGroup => ({ - [groupSymbol]: urlGroup, - ...withEntries(urlGroup.paths, entries => entries - .map(([key, path]) => [key, pathHelper(path, urlGroup)])) - }); - - const relative = withEntries(urlSpec, entries => entries - .map(([key, urlGroup]) => [key, groupHelper(urlGroup)])); - - const to = (key, ...args) => { - const { value: template, group: {[groupSymbol]: toGroup} } = getValueForFullKey(relative, key) - let result = template.replaceAll(/<([0-9]+)>/g, (match, n) => args[n]); - - // Kinda hacky lol, 8ut it works. - const missing = result.match(/<([0-9]+)>/g); - if (missing) { - throw new Error(`Expected ${missing[missing.length - 1]} arguments, got ${args.length}`); - } - - return result; - }; - - return {to, relative}; - }; - - const generateFrom = () => { - const map = withEntries(urlSpec, entries => entries - .map(([key, group]) => [key, withEntries(group.paths, entries => entries - .map(([key, path]) => [key, generateTo(path, group)]) - )])); - - const from = key => getValueForFullKey(map, key).value; - - return {from, map}; - }; - - return generateFrom(); -} - -const urls = generateURLs(); +const urls = generateURLs(urlSpec); const searchHelper = (keys, dataFn, findFn) => ref => { if (!ref) return null; @@ -1460,7 +1270,7 @@ async function processAlbumDataFile(file) { album.dateAdded = new Date(album.dateAdded); if (!album.directory) { - album.directory = C.getKebabCase(album.name); + album.directory = getKebabCase(album.name); } album.tracks = []; @@ -1557,7 +1367,7 @@ async function processAlbumDataFile(file) { } if (!track.directory) { - track.directory = C.getKebabCase(track.name); + track.directory = getKebabCase(track.name); } if (track.originalDate) { @@ -1620,7 +1430,7 @@ async function processArtistDataFile(file) { } if (!directory) { - directory = C.getArtistDirectory(name); + directory = getKebabCase(name); } if (alias) { @@ -1776,7 +1586,7 @@ async function processTagDataFile(file) { } } - const directory = C.getKebabCase(name); + const directory = getKebabCase(name); return { name, @@ -1817,7 +1627,7 @@ async function processGroupDataFile(file) { let directory = getBasicField(section, 'Directory'); if (!directory) { - directory = C.getKebabCase(name); + directory = getKebabCase(name); } let description = getMultilineField(section, 'Description'); @@ -2390,7 +2200,7 @@ writePage.html = (pageFn, {paths, strings, to}) => { body.style ??= ''; - theme = theme || getThemeString(wikiInfo); + theme = theme || getThemeString(wikiInfo.color); banner ||= {}; banner.classes ??= []; @@ -2612,15 +2422,14 @@ writePage.html = (pageFn, {paths, strings, to}) => { ${layoutHTML} </div> ${infoCardHTML} - <script src="${to('shared.commonFile', `common.js?${CACHEBUST}`)}"></script> - <script src="${to('shared.staticFile', `client.js?${CACHEBUST}`)}"></script> + <script type="module" src="${to('shared.staticFile', `client.js?${CACHEBUST}`)}"></script> </body> </html> `); }; writePage.write = async (content, {paths}) => { - await mkdirp(paths.outputDirectory); + await mkdir(paths.outputDirectory, {recursive: true}); await writeFile(paths.outputFile, content); }; @@ -2658,7 +2467,7 @@ function getGridHTML({ lazy = true }) { return entries.map(({ large, item }, i) => fixWS` - <a ${classes('grid-item', 'box', large && 'large-grid-item')} href="${hrefFn(item)}" style="${getLinkThemeString(item)}"> + <a ${classes('grid-item', 'box', large && 'large-grid-item')} href="${hrefFn(item)}" style="${getLinkThemeString(item.color)}"> ${img({ src: srcFn(item), alt: altFn(item), @@ -2807,13 +2616,14 @@ function getNewAdditions(numAlbums) { function writeSymlinks() { return progressPromiseAll('Writing site symlinks.', [ - link(path.join(__dirname, C.COMMON_DIRECTORY), C.COMMON_DIRECTORY), - link(path.join(__dirname, C.STATIC_DIRECTORY), C.STATIC_DIRECTORY), - link(mediaPath, C.MEDIA_DIRECTORY) + link(path.join(__dirname, UTILITY_DIRECTORY), 'shared.utilityRoot'), + link(path.join(__dirname, STATIC_DIRECTORY), 'shared.staticRoot'), + link(mediaPath, 'media.root') ]); - async function link(directory, target) { - const file = path.join(outputPath, target); + async function link(directory, urlKey) { + const pathname = urls.from('shared.root').to(urlKey); + const file = path.join(outputPath, pathname); try { await unlink(file); } catch (error) { @@ -2829,7 +2639,7 @@ function writeSharedFilesAndPages({strings}) { const redirect = async (title, from, urlKey, directory) => { const target = path.relative(from, urls.from('shared.root').to(urlKey, directory)); const content = generateRedirectPage(title, target, {strings}); - await mkdirp(path.join(outputPath, from)); + await mkdir(path.join(outputPath, from), {recursive: true}); await writeFile(path.join(outputPath, from, 'index.html'), content); }; @@ -2866,7 +2676,7 @@ function writeHomepage() { content: fixWS` <h1>${wikiInfo.name}</h1> ${homepageInfo.rows.map((row, i) => fixWS` - <section class="row" style="${getLinkThemeString(row)}"> + <section class="row" style="${getLinkThemeString(row.color)}"> <h2>${row.name}</h2> ${row.type === 'albums' && fixWS` <div class="grid-listing"> @@ -3120,7 +2930,7 @@ function writeAlbumPage(album) { duration: strings.count.duration(track.duration), track: strings.link.track(track, {to}) }; - return `<li style="${getLinkThemeString(track)}">${ + return `<li style="${getLinkThemeString(track.color)}">${ (track.artists === album.artists ? strings('trackList.item.withDuration', itemOpts) : strings('trackList.item.withDuration.withArtists', { @@ -3174,7 +2984,7 @@ function writeAlbumPage(album) { const page = {type: 'page', path: ['album', album.directory], page: ({strings, to}) => ({ title: strings('albumPage.title', {album: album.name}), stylesheet: getAlbumStylesheet(album, {to}), - theme: getThemeString(album, [ + theme: getThemeString(album.color, [ `--album-directory: ${album.directory}` ]), @@ -3334,11 +3144,11 @@ function writeTrackPage(track) { const { album } = track; const tracksThatReference = track.referencedBy; - const useDividedReferences = groupData.some(group => group.directory === C.OFFICIAL_GROUP_DIRECTORY); + const useDividedReferences = groupData.some(group => group.directory === OFFICIAL_GROUP_DIRECTORY); const ttrFanon = (useDividedReferences && - tracksThatReference.filter(t => t.album.groups.every(group => group.directory !== C.OFFICIAL_GROUP_DIRECTORY))); + tracksThatReference.filter(t => t.album.groups.every(group => group.directory !== OFFICIAL_GROUP_DIRECTORY))); const ttrOfficial = (useDividedReferences && - tracksThatReference.filter(t => t.album.groups.some(group => group.directory === C.OFFICIAL_GROUP_DIRECTORY))); + tracksThatReference.filter(t => t.album.groups.some(group => group.directory === OFFICIAL_GROUP_DIRECTORY))); const tracksReferenced = track.references; const otherReleases = track.otherReleases; @@ -3346,7 +3156,7 @@ function writeTrackPage(track) { let flashesThatFeature; if (wikiInfo.features.flashesAndGames) { - flashesThatFeature = C.sortByDate([track, ...otherReleases] + flashesThatFeature = sortByDate([track, ...otherReleases] .flatMap(track => track.flashes.map(flash => ({flash, as: track})))); } @@ -3412,7 +3222,7 @@ function writeTrackPage(track) { const page = {type: 'page', path: ['track', track.directory], page: ({strings, to}) => ({ title: strings('trackPage.title', {track: track.name}), stylesheet: getAlbumStylesheet(album, {to}), - theme: getThemeString(track, [ + theme: getThemeString(track.color, [ `--album-directory: ${album.directory}`, `--track-directory: ${track.directory}` ]), @@ -3452,7 +3262,7 @@ function writeTrackPage(track) { showIcons: true }) }), - album.directory !== C.UNRELEASED_TRACKS_DIRECTORY && strings('releaseInfo.released', { + album.directory !== UNRELEASED_TRACKS_DIRECTORY && strings('releaseInfo.released', { date: strings.count.date(track.date) }), +track.coverArtDate !== +track.date && strings('releaseInfo.artReleased', { @@ -3604,9 +3414,9 @@ function writeArtistPage(artist) { note = '' } = artist; - const artThingsAll = C.sortByDate(unique([...artist.albums.asCoverArtist, ...artist.albums.asWallpaperArtist, ...artist.albums.asBannerArtist, ...artist.tracks.asCoverArtist])); - const artThingsGallery = C.sortByDate([...artist.albums.asCoverArtist, ...artist.tracks.asCoverArtist]); - const commentaryThings = C.sortByDate([...artist.albums.asCommentator, ...artist.tracks.asCommentator]); + const artThingsAll = sortByDate(unique([...artist.albums.asCoverArtist, ...artist.albums.asWallpaperArtist, ...artist.albums.asBannerArtist, ...artist.tracks.asCoverArtist])); + const artThingsGallery = sortByDate([...artist.albums.asCoverArtist, ...artist.tracks.asCoverArtist]); + const commentaryThings = sortByDate([...artist.albums.asCommentator, ...artist.tracks.asCommentator]); const hasGallery = artThingsGallery.length > 0; @@ -3634,9 +3444,9 @@ function writeArtistPage(artist) { track: thing.album ? thing : null })), ['album']); - const allTracks = C.sortByDate(unique([...artist.tracks.asArtist, ...artist.tracks.asContributor])); - const unreleasedTracks = allTracks.filter(track => track.album.directory === C.UNRELEASED_TRACKS_DIRECTORY); - const releasedTracks = allTracks.filter(track => track.album.directory !== C.UNRELEASED_TRACKS_DIRECTORY); + const allTracks = sortByDate(unique([...artist.tracks.asArtist, ...artist.tracks.asContributor])); + const unreleasedTracks = allTracks.filter(track => track.album.directory === UNRELEASED_TRACKS_DIRECTORY); + const releasedTracks = allTracks.filter(track => track.album.directory !== UNRELEASED_TRACKS_DIRECTORY); const chunkTracks = tracks => ( chunkByProperties(tracks.map(track => ({ @@ -3681,7 +3491,7 @@ function writeArtistPage(artist) { let flashes, flashListChunks; if (wikiInfo.features.flashesAndGames) { - flashes = C.sortByDate(artist.flashes.asContributor.slice()); + flashes = sortByDate(artist.flashes.asContributor.slice()); flashListChunks = ( chunkByProperties(flashes.map(flash => ({ act: flash.act, @@ -4069,12 +3879,12 @@ function writeFlashIndex() { <p class="quick-info">${strings('misc.jumpTo')}</p> <ul class="quick-info"> ${flashActData.filter(act => act.jump).map(({ anchor, jump, jumpColor }) => fixWS` - <li><a href="#${anchor}" style="${getLinkThemeString({color: jumpColor})}">${jump}</a></li> + <li><a href="#${anchor}" style="${getLinkThemeString(jumpColor)}">${jump}</a></li> `).join('\n')} </ul> </div> ${flashActData.map((act, i) => fixWS` - <h2 id="${act.anchor}" style="${getLinkThemeString(act)}"><a href="${to('localized.flash', act.flashes[0].directory)}">${act.name}</a></h2> + <h2 id="${act.anchor}" style="${getLinkThemeString(act.color)}"><a href="${to('localized.flash', act.flashes[0].directory)}">${act.name}</a></h2> <div class="grid-listing"> ${getFlashGridHTML({ strings, to, @@ -4093,7 +3903,7 @@ function writeFlashIndex() { function writeFlashPage(flash) { return ({strings, writePage}) => writePage('flash', flash.directory, ({to}) => ({ title: strings('flashPage.title', {flash: flash.name}), - theme: getThemeString(flash, [ + theme: getThemeString(flash.color, [ `--flash-directory: ${flash.directory}` ]), @@ -4232,7 +4042,7 @@ function generateSidebarForFlash(flash, {strings, to}) { index < outsideCanon ? side === 2 : true ))() - && `<dt ${classes(act === currentAct && 'current')}><a href="${to('localized.flash', act.flashes[0].directory)}" style="${getLinkThemeString(act)}">${act.name}</a></dt>`, + && `<dt ${classes(act === currentAct && 'current')}><a href="${to('localized.flash', act.flashes[0].directory)}" style="${getLinkThemeString(act.color)}">${act.name}</a></dt>`, act === currentAct && fixWS` <dd><ul> ${act.flashes.map(f => fixWS` @@ -4304,8 +4114,8 @@ const listingSpec = [ title: ({strings}) => strings('listingPage.listAlbums.byDate.title'), data() { - return C.sortByDate(albumData - .filter(album => album.directory !== C.UNRELEASED_TRACKS_DIRECTORY)); + return sortByDate(albumData + .filter(album => album.directory !== UNRELEASED_TRACKS_DIRECTORY)); }, row(album, {strings, to}) { @@ -4355,7 +4165,7 @@ const listingSpec = [ data() { return artistData.slice() .sort(sortByName) - .map(artist => ({artist, contributions: C.getArtistNumContributions(artist)})); + .map(artist => ({artist, contributions: getArtistNumContributions(artist)})); }, row({artist, contributions}, {strings, to}) { @@ -4462,7 +4272,7 @@ const listingSpec = [ data() { return artistData .map(artist => ({artist, duration: getTotalDuration( - [...artist.tracks.asArtist, ...artist.tracks.asContributor].filter(track => track.album.directory !== C.UNRELEASED_TRACKS_DIRECTORY)) + [...artist.tracks.asArtist, ...artist.tracks.asContributor].filter(track => track.album.directory !== UNRELEASED_TRACKS_DIRECTORY)) })) .filter(({ duration }) => duration > 0) .sort((a, b) => b.duration - a.duration); @@ -4485,23 +4295,23 @@ const listingSpec = [ const reversedArtThings = justEverythingSortedByArtDateMan.slice().reverse(); return { - toTracks: C.sortByDate(artistData + toTracks: sortByDate(artistData .filter(artist => !artist.alias) .map(artist => ({ artist, date: reversedTracks.find(({ album, artists, contributors }) => ( - album.directory !== C.UNRELEASED_TRACKS_DIRECTORY && + album.directory !== UNRELEASED_TRACKS_DIRECTORY && [...artists, ...contributors].some(({ who }) => who === artist) ))?.date })) .filter(({ date }) => date) .sort((a, b) => a.name < b.name ? 1 : a.name > b.name ? -1 : 0)).reverse(), - toArtAndFlashes: C.sortByDate(artistData + toArtAndFlashes: sortByDate(artistData .filter(artist => !artist.alias) .map(artist => { const thing = reversedArtThings.find(({ album, coverArtists, contributors }) => ( - album?.directory !== C.UNRELEASED_TRACKS_DIRECTORY && + album?.directory !== UNRELEASED_TRACKS_DIRECTORY && [...coverArtists || [], ...!album && contributors || []].some(({ who }) => who === artist) )); return thing && { @@ -4665,7 +4475,7 @@ const listingSpec = [ condition: () => wikiInfo.features.groupUI, data() { - return C.sortByDate(groupData + return sortByDate(groupData .map(group => ({group, date: group.albums[group.albums.length - 1].date})) // So this is kinda tough to explain, 8ut 8asically, when we reverse the list after sorting it 8y d8te // (so that the latest d8tes come first), it also flips the order of groups which share the same d8te. @@ -4731,7 +4541,7 @@ const listingSpec = [ data() { return chunkByProperties( - C.sortByDate(trackData.filter(track => track.album.directory !== C.UNRELEASED_TRACKS_DIRECTORY)), + sortByDate(trackData.filter(track => track.album.directory !== UNRELEASED_TRACKS_DIRECTORY)), ['album', 'date'] ); }, @@ -4767,7 +4577,7 @@ const listingSpec = [ data() { return trackData - .filter(track => track.album.directory !== C.UNRELEASED_TRACKS_DIRECTORY) + .filter(track => track.album.directory !== UNRELEASED_TRACKS_DIRECTORY) .map(track => ({track, duration: track.duration})) .filter(({ duration }) => duration > 0) .sort((a, b) => b.duration - a.duration); @@ -4840,7 +4650,7 @@ const listingSpec = [ data() { return chunkByProperties(trackData.filter(t => t.flashes.length > 0), ['album']) - .filter(({ album }) => album.directory !== C.UNRELEASED_TRACKS_DIRECTORY); + .filter(({ album }) => album.directory !== UNRELEASED_TRACKS_DIRECTORY); }, html(chunks, {strings, to}) { @@ -4874,7 +4684,7 @@ const listingSpec = [ html({strings, to}) { return fixWS` <dl> - ${C.sortByDate(flashData.slice()).map(flash => fixWS` + ${sortByDate(flashData.slice()).map(flash => fixWS` <dt>${strings('listingPage.listTracks.inFlashes.byFlash.flash', { flash: strings.link.flash(flash, {to}), date: strings.count.date(flash.date) @@ -4987,7 +4797,7 @@ const listingSpec = [ ].map(category => fixWS` <dt>${category.name}: (<a href="#" data-random="album-in-${category.code}">Random Album</a>, <a href="#" data-random="track-in-${category.code}">Random Track</a>)</dt> <dd><ul>${category.albumData.map(album => fixWS` - <li><a style="${getLinkThemeString(album)}; --album-directory: ${album.directory}" href="#" data-random="track-in-album">${album.name}</a></li> + <li><a style="${getLinkThemeString(album.color)}; --album-directory: ${album.directory}" href="#" data-random="track-in-album">${album.name}</a></li> `).join('\n')}</ul></dd> `).join('\n')} </dl> @@ -5007,8 +4817,8 @@ function writeListingPages() { } function writeListingIndex() { - const releasedTracks = trackData.filter(track => track.album.directory !== C.UNRELEASED_TRACKS_DIRECTORY); - const releasedAlbums = albumData.filter(album => album.directory !== C.UNRELEASED_TRACKS_DIRECTORY); + const releasedTracks = trackData.filter(track => track.album.directory !== UNRELEASED_TRACKS_DIRECTORY); + const releasedAlbums = albumData.filter(album => album.directory !== UNRELEASED_TRACKS_DIRECTORY); const duration = getTotalDuration(releasedTracks); return ({strings, writePage}) => writePage('listingIndex', '', ({to}) => ({ @@ -5178,7 +4988,7 @@ function writeAlbumCommentaryPage(album) { return ({strings, writePage}) => writePage('albumCommentary', album.directory, ({to}) => ({ title: strings('albumCommentaryPage.title', {album: album.name}), stylesheet: getAlbumStylesheet(album, {to}), - theme: getThemeString(album), + theme: getThemeString(album.color), main: { content: fixWS` @@ -5200,7 +5010,7 @@ function writeAlbumCommentaryPage(album) { <h3 id="${track.directory}">${strings('albumCommentaryPage.entry.title.trackCommentary', { track: strings.link.track(track, {to}) })}</h3> - <blockquote style="${getLinkThemeString(track)}"> + <blockquote style="${getLinkThemeString(track.color)}"> ${transformMultiline(track.commentary, {strings, to})} </blockquote> `).join('\n')} @@ -5241,7 +5051,7 @@ function writeTagPage(tag) { return ({strings, writePage}) => writePage('tag', tag.directory, ({to}) => ({ title: strings('tagPage.title', {tag: tag.name}), - theme: getThemeString(tag), + theme: getThemeString(tag.color), main: { classes: ['top-index'], @@ -5299,29 +5109,6 @@ function getArtistString(artists, {strings, to, showIcons = false, showContrib = })); } -function getLinkThemeString(thing) { - const { primary, dim } = C.getColors(thing.color || wikiInfo.color); - return `--primary-color: ${primary}; --dim-color: ${dim}`; -} - -function getThemeString(thing, additionalVariables = []) { - const { primary, dim } = C.getColors(thing.color || wikiInfo.color); - - const variables = [ - `--primary-color: ${primary}`, - `--dim-color: ${dim}`, - ...additionalVariables - ].filter(Boolean); - - return fixWS` - ${variables.length && fixWS` - :root { - ${variables.map(line => line + ';').join('\n')} - } - `} - `; -} - function getFlashDirectory(flash) { // const kebab = getKebabCase(flash.name.replace('[S] ', '')); // return flash.page + (kebab ? '-' + kebab : ''); @@ -5330,11 +5117,11 @@ function getFlashDirectory(flash) { } function getTagDirectory({name}) { - return C.getKebabCase(name); + return getKebabCase(name); } function getAlbumListTag(album) { - if (album.directory === C.UNRELEASED_TRACKS_DIRECTORY) { + if (album.directory === UNRELEASED_TRACKS_DIRECTORY) { return 'ul'; } else { return 'ol'; @@ -5417,10 +5204,10 @@ function chronologyLinks(currentThing, { } return contributions.map(({ who: artist }) => { - const things = C.sortByDate(unique(getThings(artist))); + const things = sortByDate(unique(getThings(artist))); const releasedThings = things.filter(thing => { const album = albumData.includes(thing) ? thing : thing.album; - return !(album && album.directory === C.UNRELEASED_TRACKS_DIRECTORY); + return !(album && album.directory === UNRELEASED_TRACKS_DIRECTORY); }); const index = releasedThings.indexOf(currentThing); @@ -5543,12 +5330,12 @@ function generateSidebarForAlbum(album, currentTrack, {strings, to}) { ${!currentTrack && fixWS` ${next && `<p class="group-chronology-link">${ strings('albumSidebar.groupBox.next', { - album: `<a href="${to('localized.album', next.directory)}" style="${getLinkThemeString(next)}">${next.name}</a>` + album: `<a href="${to('localized.album', next.directory)}" style="${getLinkThemeString(next.color)}">${next.name}</a>` }) }</p>`} ${previous && `<p class="group-chronology-link">${ strings('albumSidebar.groupBox.previous', { - album: `<a href="${to('localized.album', previous.directory)}" style="${getLinkThemeString(previous)}">${previous.name}</a>` + album: `<a href="${to('localized.album', previous.directory)}" style="${getLinkThemeString(previous.color)}">${previous.name}</a>` }) }</p>`} `} @@ -5593,12 +5380,12 @@ function generateSidebarForGroup(currentGroup, {strings, to, isGallery}) { fixWS` <dt ${classes(category === currentGroup.category && 'current')}>${ strings('groupSidebar.groupList.category', { - category: `<a href="${to(urlKey, category.groups[0].directory)}" style="${getLinkThemeString(category)}">${category.name}</a>` + category: `<a href="${to(urlKey, category.groups[0].directory)}" style="${getLinkThemeString(category.color)}">${category.name}</a>` }) }</dt> <dd><ul> ${category.groups.map(group => fixWS` - <li ${classes(group === currentGroup && 'current')} style="${getLinkThemeString(group)}">${ + <li ${classes(group === currentGroup && 'current')} style="${getLinkThemeString(group.color)}">${ strings('groupSidebar.groupList.item', { group: `<a href="${to(urlKey, group.directory)}">${group.name}</a>` }) @@ -5680,14 +5467,14 @@ function writeGroupPages() { } function writeGroupPage(group) { - const releasedAlbums = group.albums.filter(album => album.directory !== C.UNRELEASED_TRACKS_DIRECTORY); + const releasedAlbums = group.albums.filter(album => album.directory !== UNRELEASED_TRACKS_DIRECTORY); const releasedTracks = releasedAlbums.flatMap(album => album.tracks); const totalDuration = getTotalDuration(releasedTracks); return async ({strings, writePage}) => { await writePage('groupInfo', group.directory, ({to}) => ({ title: strings('groupInfoPage.title', {group: group.name}), - theme: getThemeString(group), + theme: getThemeString(group.color), main: { content: fixWS` @@ -5713,7 +5500,7 @@ function writeGroupPage(group) { <li>${ strings('groupInfoPage.albumList.item', { year: album.date.getFullYear(), - album: `<a href="${to('localized.album', album.directory)}" style="${getLinkThemeString(album)}">${album.name}</a>` + album: `<a href="${to('localized.album', album.directory)}" style="${getLinkThemeString(album.color)}">${album.name}</a>` }) }</li> `).join('\n')} @@ -5727,7 +5514,7 @@ function writeGroupPage(group) { await writePage('groupGallery', group.directory, ({to}) => ({ title: strings('groupGalleryPage.title', {group: group.name}), - theme: getThemeString(group), + theme: getThemeString(group.color), main: { classes: ['top-index'], @@ -5744,7 +5531,7 @@ function writeGroupPage(group) { <div class="grid-listing"> ${getAlbumGridHTML({ strings, to, - entries: C.sortByDate(group.albums.map(item => ({item}))).reverse(), + entries: sortByDate(group.albums.map(item => ({item}))).reverse(), details: true })} </div> @@ -5848,8 +5635,8 @@ async function main() { }, // Static media will 8e referenced in the site here! The contents are - // categorized; check out MEDIA_DIRECTORY and rel8ted constants in - // common/common.js. (This gets symlinked into the --data directory.) + // categorized; check out MEDIA_ALBUM_ART_DIRECTORY and other constants + // near the top of this file (upd8.js). 'media-path': { type: 'value' }, @@ -5885,6 +5672,12 @@ async function main() { type: 'flag' }, + // Or, if you *only* want to gener8te newly upd8ted thum8nails, you can + // pass this flag! It exits 8efore 8uilding the rest of the site. + 'thumbs-only': { + type: 'flag' + }, + // Only want 8uild one language during testing? This can chop down // 8uild times a pretty 8ig chunk! Just pass a single language code. 'lang': { @@ -5928,6 +5721,12 @@ async function main() { } const skipThumbs = miscOptions['skip-thumbs'] ?? false; + const thumbsOnly = miscOptions['thumbs-only'] ?? false; + + if (skipThumbs && thumbsOnly) { + logInfo`Well, you've put yourself rather between a roc and a hard place, hmmmm?`; + return; + } if (skipThumbs) { logInfo`Skipping thumbnail generation.`; @@ -5935,9 +5734,8 @@ async function main() { logInfo`Begin thumbnail generation... -----+`; const result = await genThumbs(mediaPath, {queueSize, quiet: true}); logInfo`Done thumbnail generation! --------+`; - if (!result) { - return; - } + if (!result) return; + if (thumbsOnly) return; } const defaultStrings = await processLanguageFile(path.join(__dirname, DEFAULT_STRINGS_FILE)); @@ -6046,7 +5844,7 @@ async function main() { // avoiding that in our code 8ecause, again, we want to avoid assuming the // format of the returned paths here - they're only meant to 8e used for // reading as-is. - const albumDataFiles = await findFiles(path.join(dataPath, C.DATA_ALBUM_DIRECTORY)); + const albumDataFiles = await findFiles(path.join(dataPath, DATA_ALBUM_DIRECTORY)); // Technically, we could do the data file reading and output writing at the // same time, 8ut that kinda makes the code messy, so I'm not 8othering @@ -6063,7 +5861,7 @@ async function main() { } } - C.sortByDate(albumData); + sortByDate(albumData); artistData = await processArtistDataFile(path.join(dataPath, ARTIST_DATA_FILE)); if (artistData.error) { @@ -6084,7 +5882,7 @@ async function main() { artistAliasData = artistData.filter(x => x.alias); artistData = artistData.filter(x => !x.alias); - trackData = C.getAllTracks(albumData); + trackData = getAllTracks(albumData); if (wikiInfo.features.flashesAndGames) { flashData = await processFlashDataFile(path.join(dataPath, FLASH_DATA_FILE)); @@ -6190,7 +5988,7 @@ async function main() { return; } - C.sortByDate(newsData); + sortByDate(newsData); newsData.reverse(); } @@ -6214,8 +6012,8 @@ async function main() { artistNames.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : a.toLowerCase() > b.toLowerCase() ? 1 : 0); - justEverythingMan = C.sortByDate([...albumData, ...trackData, ...(flashData || [])]); - justEverythingSortedByArtDateMan = C.sortByArtDate(justEverythingMan.slice()); + justEverythingMan = sortByDate([...albumData, ...trackData, ...(flashData || [])]); + justEverythingSortedByArtDateMan = sortByArtDate(justEverythingMan.slice()); // console.log(JSON.stringify(justEverythingSortedByArtDateMan.map(toAnythingMan), null, 2)); { @@ -6370,7 +6168,7 @@ async function main() { trackData.forEach(track1 => track1.referencedBy = trackData.filter(track2 => track2.references.includes(track1))); groupData.forEach(group => group.albums = albumData.filter(album => album.groups.includes(group))); - tagData.forEach(tag => tag.things = C.sortByArtDate([...albumData, ...trackData]).filter(thing => thing.artTags.includes(tag))); + tagData.forEach(tag => tag.things = sortByArtDate([...albumData, ...trackData]).filter(thing => thing.artTags.includes(tag))); groupData.forEach(group => group.category = groupCategoryData.find(x => x.name === group.category)); groupCategoryData.forEach(category => category.groups = groupData.filter(x => x.category === category)); @@ -6416,8 +6214,8 @@ async function main() { } }); - officialAlbumData = albumData.filter(album => album.groups.some(group => group.directory === C.OFFICIAL_GROUP_DIRECTORY)); - fandomAlbumData = albumData.filter(album => album.groups.every(group => group.directory !== C.OFFICIAL_GROUP_DIRECTORY)); + officialAlbumData = albumData.filter(album => album.groups.some(group => group.directory === OFFICIAL_GROUP_DIRECTORY)); + fandomAlbumData = albumData.filter(album => album.groups.every(group => group.directory !== OFFICIAL_GROUP_DIRECTORY)); // Makes writing a little nicer on CPU theoretically, 8ut also costs in // performance right now 'cuz it'll w8 for file writes to 8e completed diff --git a/src/util/cli.js b/src/util/cli.js new file mode 100644 index 00000000..77711566 --- /dev/null +++ b/src/util/cli.js @@ -0,0 +1,210 @@ +// Utility functions for CLI- and de8ugging-rel8ted stuff. +// +// A 8unch of these depend on process.stdout 8eing availa8le, so they won't +// work within the 8rowser. + +const logColor = color => (literals, ...values) => { + const w = s => process.stdout.write(s); + w(`\x1b[${color}m`); + for (let i = 0; i < literals.length; i++) { + w(literals[i]); + if (values[i] !== undefined) { + w(`\x1b[1m`); + w(String(values[i])); + w(`\x1b[0;${color}m`); + } + } + w(`\x1b[0m\n`); +}; + +export const logInfo = logColor(2); +export const logWarn = logColor(33); +export const logError = logColor(31); + +// Stolen as #@CK from mtui! +export async function parseOptions(options, optionDescriptorMap) { + // This function is sorely lacking in comments, but the basic usage is + // as such: + // + // options is the array of options you want to process; + // optionDescriptorMap is a mapping of option names to objects that describe + // the expected value for their corresponding options. + // Returned is a mapping of any specified option names to their values, or + // a process.exit(1) and error message if there were any issues. + // + // Here are examples of optionDescriptorMap to cover all the things you can + // do with it: + // + // optionDescriptorMap: { + // 'telnet-server': {type: 'flag'}, + // 't': {alias: 'telnet-server'} + // } + // + // options: ['t'] -> result: {'telnet-server': true} + // + // optionDescriptorMap: { + // 'directory': { + // type: 'value', + // validate(name) { + // // const whitelistedDirectories = ['apple', 'banana'] + // if (whitelistedDirectories.includes(name)) { + // return true + // } else { + // return 'a whitelisted directory' + // } + // } + // }, + // 'files': {type: 'series'} + // } + // + // ['--directory', 'apple'] -> {'directory': 'apple'} + // ['--directory', 'artichoke'] -> (error) + // ['--files', 'a', 'b', 'c', ';'] -> {'files': ['a', 'b', 'c']} + // + // TODO: Be able to validate the values in a series option. + + const handleDashless = optionDescriptorMap[parseOptions.handleDashless]; + const handleUnknown = optionDescriptorMap[parseOptions.handleUnknown]; + const result = Object.create(null); + for (let i = 0; i < options.length; i++) { + const option = options[i]; + if (option.startsWith('--')) { + // --x can be a flag or expect a value or series of values + let name = option.slice(2).split('=')[0]; // '--x'.split('=') = ['--x'] + let descriptor = optionDescriptorMap[name]; + if (!descriptor) { + if (handleUnknown) { + handleUnknown(option); + } else { + console.error(`Unknown option name: ${name}`); + process.exit(1); + } + continue; + } + if (descriptor.alias) { + name = descriptor.alias; + descriptor = optionDescriptorMap[name]; + } + if (descriptor.type === 'flag') { + result[name] = true; + } else if (descriptor.type === 'value') { + let value = option.slice(2).split('=')[1]; + if (!value) { + value = options[++i]; + if (!value || value.startsWith('-')) { + value = null; + } + } + if (!value) { + console.error(`Expected a value for --${name}`); + process.exit(1); + } + result[name] = value; + } else if (descriptor.type === 'series') { + if (!options.slice(i).includes(';')) { + console.error(`Expected a series of values concluding with ; (\\;) for --${name}`); + process.exit(1); + } + const endIndex = i + options.slice(i).indexOf(';'); + result[name] = options.slice(i + 1, endIndex); + i = endIndex; + } + if (descriptor.validate) { + const validation = await descriptor.validate(result[name]); + if (validation !== true) { + console.error(`Expected ${validation} for --${name}`); + process.exit(1); + } + } + } else if (option.startsWith('-')) { + // mtui doesn't use any -x=y or -x y format optionuments + // -x will always just be a flag + let name = option.slice(1); + let descriptor = optionDescriptorMap[name]; + if (!descriptor) { + if (handleUnknown) { + handleUnknown(option); + } else { + console.error(`Unknown option name: ${name}`); + process.exit(1); + } + continue; + } + if (descriptor.alias) { + name = descriptor.alias; + descriptor = optionDescriptorMap[name]; + } + if (descriptor.type === 'flag') { + result[name] = true; + } else { + console.error(`Use --${name} (value) to specify ${name}`); + process.exit(1); + } + } else if (handleDashless) { + handleDashless(option); + } + } + return result; +} + +export const handleDashless = Symbol(); +export const handleUnknown = Symbol(); + +export function decorateTime(functionToBeWrapped) { + const fn = function(...args) { + const start = Date.now(); + const ret = functionToBeWrapped(...args); + const end = Date.now(); + fn.timeSpent += end - start; + fn.timesCalled++; + return ret; + }; + + fn.wrappedName = functionToBeWrapped.name; + fn.timeSpent = 0; + fn.timesCalled = 0; + fn.displayTime = function() { + const averageTime = fn.timeSpent / fn.timesCalled; + console.log(`\x1b[1m${fn.wrappedName}(...):\x1b[0m ${fn.timeSpent} ms / ${fn.timesCalled} calls \x1b[2m(avg: ${averageTime} ms)\x1b[0m`); + }; + + decorateTime.decoratedFunctions.push(fn); + + return fn; +} + +decorateTime.decoratedFunctions = []; +decorateTime.displayTime = function() { + if (decorateTime.decoratedFunctions.length) { + console.log(`\x1b[1mdecorateTime results: ` + '-'.repeat(40) + '\x1b[0m'); + for (const fn of decorateTime.decoratedFunctions) { + fn.displayTime(); + } + } +}; + +export function progressPromiseAll(msgOrMsgFn, array) { + if (!array.length) { + return Promise.resolve([]); + } + + const msgFn = (typeof msgOrMsgFn === 'function' + ? msgOrMsgFn + : () => msgOrMsgFn); + + let done = 0, total = array.length; + process.stdout.write(`\r${msgFn()} [0/${total}]`); + const start = Date.now(); + return Promise.all(array.map(promise => promise.then(val => { + done++; + // const pc = `${done}/${total}`; + const pc = (Math.round(done / total * 1000) / 10 + '%').padEnd('99.9%'.length, ' '); + if (done === total) { + const time = Date.now() - start; + process.stdout.write(`\r\x1b[2m${msgFn()} [${pc}] \x1b[0;32mDone! \x1b[0;2m(${time} ms) \x1b[0m\n`) + } else { + process.stdout.write(`\r${msgFn()} [${pc}] `); + } + return val; + }))); +} diff --git a/src/util/colors.js b/src/util/colors.js new file mode 100644 index 00000000..1df591bf --- /dev/null +++ b/src/util/colors.js @@ -0,0 +1,47 @@ +// Color and theming utility functions! Handy. + +// Graciously stolen from https://stackoverflow.com/a/54071699! ::::) +// in: r,g,b in [0,1], out: h in [0,360) and s,l in [0,1] +export function rgb2hsl(r, g, b) { + let a=Math.max(r,g,b), n=a-Math.min(r,g,b), f=(1-Math.abs(a+a-n-1)); + let h= n && ((a==r) ? (g-b)/n : ((a==g) ? 2+(b-r)/n : 4+(r-g)/n)); + return [60*(h<0?h+6:h), f ? n/f : 0, (a+a-n)/2]; +} + +export function getColors(primary) { + const [ r, g, b ] = primary.slice(1) + .match(/[0-9a-fA-F]{2,2}/g) + .slice(0, 3) + .map(val => parseInt(val, 16) / 255); + const [ h, s, l ] = rgb2hsl(r, g, b); + const dim = `hsl(${Math.round(h)}deg, ${Math.round(s * 50)}%, ${Math.round(l * 80)}%)`; + + return {primary, dim}; +} + +export function getLinkThemeString(color) { + if (!color) return ''; + + const { primary, dim } = getColors(color); + return `--primary-color: ${primary}; --dim-color: ${dim}`; +} + +export function getThemeString(color, additionalVariables = []) { + if (!color) return ''; + + const { primary, dim } = getColors(color); + + const variables = [ + `--primary-color: ${primary}`, + `--dim-color: ${dim}`, + ...additionalVariables + ].filter(Boolean); + + if (!variables.length) return ''; + + return ( + `:root {\n` + + variables.map(line => ` ` + line + ';\n').join('') + + `}` + ); +} diff --git a/src/util/html.js b/src/util/html.js new file mode 100644 index 00000000..4895301b --- /dev/null +++ b/src/util/html.js @@ -0,0 +1,92 @@ +// Some really simple functions for formatting HTML content. + +// Non-comprehensive. ::::P +export const selfClosingTags = ['br', 'img']; + +// Pass to tag() as an attri8utes key to make tag() return a 8lank string +// if the provided content is empty. Useful for when you'll only 8e showing +// an element according to the presence of content that would 8elong there. +export const onlyIfContent = Symbol(); + +export function tag(tagName, ...args) { + const selfClosing = selfClosingTags.includes(tagName); + + let openTag; + let content; + let attrs; + + if (typeof args[0] === 'object' && !Array.isArray(args[0])) { + attrs = args[0]; + content = args[1]; + } else { + content = args[0]; + } + + if (selfClosing && content) { + throw new Error(`Tag <${tagName}> is self-closing but got content!`); + } + + if (attrs?.[onlyIfContent] && !content) { + return ''; + } + + if (attrs) { + const attrString = attributes(args[0]); + if (attrString) { + openTag = `${tagName} ${attrString}`; + } + } + + if (!openTag) { + openTag = tagName; + } + + if (Array.isArray(content)) { + content = content.filter(Boolean).join('\n'); + } + + if (content) { + if (content.includes('\n')) { + return ( + `<${openTag}>\n` + + content.split('\n').map(line => ' ' + line + '\n').join('') + + `</${tagName}>` + ); + } else { + return `<${openTag}>${content}</${tagName}>`; + } + } else { + if (selfClosing) { + return `<${openTag}>`; + } else { + return `<${openTag}></${tagName}>`; + } + } +} + +export function escapeAttributeValue(value) { + return value + .replaceAll('"', '"') + .replaceAll("'", '''); +} + +export function attributes(attribs) { + return Object.entries(attribs) + .map(([ key, val ]) => { + if (!val) + return [key, val]; + else if (typeof val === 'string' || typeof val === 'boolean') + return [key, val]; + else if (typeof val === 'number') + return [key, val.toString()]; + else if (Array.isArray(val)) + return [key, val.join(' ')]; + else + throw new Error(`Attribute value for ${key} should be primitive or array, got ${typeof val}`); + }) + .filter(([ key, val ]) => val) + .map(([ key, val ]) => (typeof val === 'boolean' + ? `${key}` + : `${key}="${escapeAttributeValue(val)}"`)) + .join(' '); +} diff --git a/src/util/link.js b/src/util/link.js new file mode 100644 index 00000000..e5c3c596 --- /dev/null +++ b/src/util/link.js @@ -0,0 +1,67 @@ +// This file is essentially one level of a8straction a8ove urls.js (and the +// urlSpec it gets its paths from). It's a 8unch of utility functions which +// take certain types of wiki data o8jects (colloquially known as "things") +// and return actual <a href> HTML link tags. +// +// The functions we're cre8ting here (all factory-style) take a "to" argument, +// which is roughly a function which takes a urlSpec key and spits out a path +// to 8e stuck in an href or src or suchever. There are also a few other +// options availa8le in all the functions, making a common interface for +// gener8ting just a8out any link on the site. + +import * as html from './html.js' +import { getLinkThemeString } from './colors.js' + +const linkHelper = (hrefFn, {color = true, attr = null} = {}) => + (thing, { + strings, to, + text = '', + class: className = '', + hash = '' + }) => ( + html.tag('a', { + ...attr ? attr(thing) : {}, + href: hrefFn(thing, {to}) + (hash ? (hash.startsWith('#') ? '' : '#') + hash : ''), + style: color ? getLinkThemeString(thing.color) : '', + class: className + }, text || thing.name) + ); + +const linkDirectory = (key, {expose = null, attr = null, ...conf} = {}) => + linkHelper((thing, {to}) => to('localized.' + key, thing.directory), { + attr: thing => ({ + ...attr ? attr(thing) : {}, + ...expose ? {[expose]: thing.directory} : {} + }), + ...conf + }); + +const linkPathname = (key, conf) => linkHelper(({directory: pathname}, {to}) => to(key, pathname), conf); +const linkIndex = (key, conf) => linkHelper((_, {to}) => to('localized.' + key), conf); + +const link = { + album: linkDirectory('album'), + albumCommentary: linkDirectory('albumCommentary'), + artist: linkDirectory('artist', {color: false}), + artistGallery: linkDirectory('artistGallery', {color: false}), + commentaryIndex: linkIndex('commentaryIndex', {color: false}), + flashIndex: linkIndex('flashIndex', {color: false}), + flash: linkDirectory('flash'), + groupInfo: linkDirectory('groupInfo'), + groupGallery: linkDirectory('groupGallery'), + home: linkIndex('home', {color: false}), + listingIndex: linkIndex('listingIndex'), + listing: linkDirectory('listing'), + newsIndex: linkIndex('newsIndex', {color: false}), + newsEntry: linkDirectory('newsEntry', {color: false}), + staticPage: linkDirectory('staticPage', {color: false}), + tag: linkDirectory('tag'), + track: linkDirectory('track', {expose: 'data-track'}), + + media: linkPathname('media.path', {color: false}), + root: linkPathname('shared.path', {color: false}), + data: linkPathname('data.path', {color: false}), + site: linkPathname('localized.path', {color: false}) +}; + +export default link; diff --git a/src/util/node-utils.js b/src/util/node-utils.js new file mode 100644 index 00000000..d660612e --- /dev/null +++ b/src/util/node-utils.js @@ -0,0 +1,27 @@ +// Utility functions which are only relevant to particular Node.js constructs. + +// Very cool function origin8ting in... http-music pro8a8ly! +// Sorry if we happen to 8e violating past-us's copyright, lmao. +export function promisifyProcess(proc, showLogging = true) { + // Takes a process (from the child_process module) and returns a promise + // that resolves when the process exits (or rejects, if the exit code is + // non-zero). + // + // Ayy look, no alpha8etical second letter! Couldn't tell this was written + // like three years ago 8efore I was me. 8888) + + return new Promise((resolve, reject) => { + if (showLogging) { + proc.stdout.pipe(process.stdout); + proc.stderr.pipe(process.stderr); + } + + proc.on('exit', code => { + if (code === 0) { + resolve(); + } else { + reject(code); + } + }) + }) +} diff --git a/src/util/sugar.js b/src/util/sugar.js new file mode 100644 index 00000000..9970bad1 --- /dev/null +++ b/src/util/sugar.js @@ -0,0 +1,70 @@ +// Syntactic sugar! (Mostly.) +// Generic functions - these are useful just a8out everywhere. +// +// Friendly(!) disclaimer: these utility functions haven't 8een tested all that +// much. Do not assume it will do exactly what you want it to do in all cases. +// It will likely only do exactly what I want it to, and only in the cases I +// decided were relevant enough to 8other handling. + +// Apparently JavaScript doesn't come with a function to split an array into +// chunks! Weird. Anyway, this is an awesome place to use a generator, even +// though we don't really make use of the 8enefits of generators any time we +// actually use this. 8ut it's still awesome, 8ecause I say so. +export function* splitArray(array, fn) { + let lastIndex = 0; + while (lastIndex < array.length) { + let nextIndex = array.findIndex((item, index) => index >= lastIndex && fn(item)); + if (nextIndex === -1) { + nextIndex = array.length; + } + yield array.slice(lastIndex, nextIndex); + // Plus one because we don't want to include the dividing line in the + // next array we yield. + lastIndex = nextIndex + 1; + } +}; + +export const mapInPlace = (array, fn) => array.splice(0, array.length, ...array.map(fn)); + +export const filterEmptyLines = string => string.split('\n').filter(line => line.trim()).join('\n'); + +export const unique = arr => Array.from(new Set(arr)); + +// Stolen from jq! Which pro8a8ly stole the concept from other places. Nice. +export const withEntries = (obj, fn) => Object.fromEntries(fn(Object.entries(obj))); + +// Nothin' more to it than what it says. Runs a function in-place. Provides an +// altern8tive syntax to the usual IIFEs (e.g. (() => {})()) when you want to +// open a scope and run some statements while inside an existing expression. +export const call = fn => fn(); + +export function queue(array, max = 50) { + if (max === 0) { + return array.map(fn => fn()); + } + + const begin = []; + let current = 0; + const ret = array.map(fn => new Promise((resolve, reject) => { + begin.push(() => { + current++; + Promise.resolve(fn()).then(value => { + current--; + if (current < max && begin.length) { + begin.shift()(); + } + resolve(value); + }, reject); + }); + })); + + for (let i = 0; i < max && begin.length; i++) { + begin.shift()(); + } + + return ret; +} + +export function delay(ms) { + return new Promise(res => setTimeout(res, ms)); +} diff --git a/src/util/urls.js b/src/util/urls.js new file mode 100644 index 00000000..f0f9cdb1 --- /dev/null +++ b/src/util/urls.js @@ -0,0 +1,102 @@ +// Code that deals with URLs (really the pathnames that get referenced all +// throughout the gener8ted HTML). Most nota8ly here is generateURLs, which +// is in charge of pre-gener8ting a complete network of template strings +// which can really quickly take su8stitute parameters to link from any one +// place to another; 8ut there are also a few other utilities, too. +// +// Nota8ly, everything here is string-8ased, for gener8ting and transforming +// actual path strings. More a8stract operations using wiki data o8jects is +// the domain of link.js. + +import * as path from 'path'; +import { withEntries } from './sugar.js'; + +export function generateURLs(urlSpec) { + const getValueForFullKey = (obj, fullKey, prop = null) => { + const [ groupKey, subKey ] = fullKey.split('.'); + if (!groupKey || !subKey) { + throw new Error(`Expected group key and subkey (got ${fullKey})`); + } + + if (!obj.hasOwnProperty(groupKey)) { + throw new Error(`Expected valid group key (got ${groupKey})`); + } + + const group = obj[groupKey]; + + if (!group.hasOwnProperty(subKey)) { + throw new Error(`Expected valid subkey (got ${subKey} for group ${groupKey})`); + } + + return { + value: group[subKey], + group + }; + }; + + const generateTo = (fromPath, fromGroup) => { + const rebasePrefix = '../'.repeat((fromGroup.prefix || '').split('/').filter(Boolean).length); + + const pathHelper = (toPath, toGroup) => { + let target = toPath; + + let argIndex = 0; + target = target.replaceAll('<>', () => `<${argIndex++}>`); + + if (toGroup.prefix !== fromGroup.prefix) { + // TODO: Handle differing domains in prefixes. + target = rebasePrefix + (toGroup.prefix || '') + target; + } + + return (path.relative(fromPath, target) + + (toPath.endsWith('/') ? '/' : '')); + }; + + const groupSymbol = Symbol(); + + const groupHelper = urlGroup => ({ + [groupSymbol]: urlGroup, + ...withEntries(urlGroup.paths, entries => entries + .map(([key, path]) => [key, pathHelper(path, urlGroup)])) + }); + + const relative = withEntries(urlSpec, entries => entries + .map(([key, urlGroup]) => [key, groupHelper(urlGroup)])); + + const to = (key, ...args) => { + const { value: template, group: {[groupSymbol]: toGroup} } = getValueForFullKey(relative, key) + let result = template.replaceAll(/<([0-9]+)>/g, (match, n) => args[n]); + + // Kinda hacky lol, 8ut it works. + const missing = result.match(/<([0-9]+)>/g); + if (missing) { + throw new Error(`Expected ${missing[missing.length - 1]} arguments, got ${args.length}`); + } + + return result; + }; + + return {to, relative}; + }; + + const generateFrom = () => { + const map = withEntries(urlSpec, entries => entries + .map(([key, group]) => [key, withEntries(group.paths, entries => entries + .map(([key, path]) => [key, generateTo(path, group)]) + )])); + + const from = key => getValueForFullKey(map, key).value; + + return {from, map}; + }; + + return generateFrom(); +} + +const thumbnailHelper = name => file => + file.replace(/\.(jpg|png)$/, name + '.jpg'); + +export const thumb = { + medium: thumbnailHelper('.medium'), + small: thumbnailHelper('.small') +}; diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js new file mode 100644 index 00000000..e76722bd --- /dev/null +++ b/src/util/wiki-data.js @@ -0,0 +1,126 @@ +// Utility functions for interacting with wiki data. + +// Generic value operations + +export function getKebabCase(name) { + return name + .split(' ') + .join('-') + .replace(/&/g, 'and') + .replace(/[^a-zA-Z0-9\-]/g, '') + .replace(/-{2,}/g, '-') + .replace(/^-+|-+$/g, '') + .toLowerCase(); +} + +export function chunkByConditions(array, conditions) { + if (array.length === 0) { + return []; + } else if (conditions.length === 0) { + return [array]; + } + + const out = []; + let cur = [array[0]]; + for (let i = 1; i < array.length; i++) { + const item = array[i]; + const prev = array[i - 1]; + let chunk = false; + for (const condition of conditions) { + if (condition(item, prev)) { + chunk = true; + break; + } + } + if (chunk) { + out.push(cur); + cur = [item]; + } else { + cur.push(item); + } + } + out.push(cur); + return out; +} + +export function chunkByProperties(array, properties) { + return chunkByConditions(array, properties.map(p => (a, b) => { + if (a[p] instanceof Date && b[p] instanceof Date) + return +a[p] !== +b[p]; + + if (a[p] !== b[p]) return true; + + // Not sure if this line is still necessary with the specific check for + // d8tes a8ove, 8ut, uh, keeping it anyway, just in case....? + if (a[p] != b[p]) return true; + + return false; + })) + .map(chunk => ({ + ...Object.fromEntries(properties.map(p => [p, chunk[0][p]])), + chunk + })); +} + +// Sorting functions + +export function sortByName(a, b) { + let an = a.name.toLowerCase(); + let bn = b.name.toLowerCase(); + if (an.startsWith('the ')) an = an.slice(4); + if (bn.startsWith('the ')) bn = bn.slice(4); + return an < bn ? -1 : an > bn ? 1 : 0; +} + +// This function was originally made to sort just al8um data, 8ut its exact +// code works fine for sorting tracks too, so I made the varia8les and names +// more general. +export function sortByDate(data) { + // Just to 8e clear: sort is a mutating function! I only return the array + // 8ecause then you don't have to define it as a separate varia8le 8efore + // passing it into this function. + return data.sort((a, b) => a.date - b.date); +} + +// Same details as the sortByDate, 8ut for covers~ +export function sortByArtDate(data) { + return data.sort((a, b) => (a.coverArtDate || a.date) - (b.coverArtDate || b.date)); +} + +// Specific data utilities + +// This gets all the track o8jects defined in every al8um, and sorts them 8y +// date released. Generally, albumData will pro8a8ly already 8e sorted 8efore +// you pass it to this function, 8ut individual tracks can have their own +// original release d8, distinct from the al8um's d8. I allowed that 8ecause +// in Homestuck, the first four Vol.'s were com8ined into one al8um really +// early in the history of the 8andcamp, and I still want to use that as the +// al8um listing (not the original four al8um listings), 8ut if I only did +// that, all the tracks would 8e sorted as though they were released at the +// same time as the compilation al8um - i.e, after some other al8ums (including +// Vol.'s 5 and 6!) were released. That would mess with chronological listings +// including tracks from multiple al8ums, like artist pages. So, to fix that, +// I gave tracks an Original Date field, defaulting to the release date of the +// al8um if not specified. Pretty reasona8le, I think! Oh, and this feature can +// 8e used for other projects too, like if you wanted to have an al8um listing +// compiling a 8unch of songs with radically different & interspersed release +// d8s, 8ut still keep the al8um listing in a specific order, since that isn't +// sorted 8y date. +export function getAllTracks(albumData) { + return sortByDate(albumData.flatMap(album => album.tracks)); +} + +export function getArtistNumContributions(artist) { + return ( + artist.tracks.asAny.length + + artist.albums.asCoverArtist.length + + (artist.flashes ? artist.flashes.asContributor.length : 0) + ); +} + +export function getArtistCommentary(artist, {justEverythingMan}) { + return justEverythingMan.filter(thing => + (thing?.commentary + .replace(/<\/?b>/g, '') + .includes('<i>' + artist.name + ':</i>'))); +} diff --git a/upd8/util.js b/upd8/util.js deleted file mode 100644 index 4c4186f7..00000000 --- a/upd8/util.js +++ /dev/null @@ -1,423 +0,0 @@ -// This is used by upd8.js! It's part of the 8ackend. Read the notes there if -// you're curious. -// -// Friendly(!) disclaimer: these utility functions haven't 8een tested all that -// much. Do not assume it will do exactly what you want it to do in all cases. -// It will likely only do exactly what I want it to, and only in the cases I -// decided were relevant enough to 8other handling. - -'use strict'; - -// Apparently JavaScript doesn't come with a function to split an array into -// chunks! Weird. Anyway, this is an awesome place to use a generator, even -// though we don't really make use of the 8enefits of generators any time we -// actually use this. 8ut it's still awesome, 8ecause I say so. -module.exports.splitArray = function*(array, fn) { - let lastIndex = 0; - while (lastIndex < array.length) { - let nextIndex = array.findIndex((item, index) => index >= lastIndex && fn(item)); - if (nextIndex === -1) { - nextIndex = array.length; - } - yield array.slice(lastIndex, nextIndex); - // Plus one because we don't want to include the dividing line in the - // next array we yield. - lastIndex = nextIndex + 1; - } -}; - -// This function's name is a joke. Jokes! Hahahahahahahaha. Funny. -module.exports.joinNoOxford = function(array, plural = 'and') { - array = array.filter(Boolean); - - if (array.length === 0) { - // ???????? - return ''; - } - - if (array.length === 1) { - return array[0]; - } - - if (array.length === 2) { - return `${array[0]} ${plural} ${array[1]}`; - } - - return `${array.slice(0, -1).join(', ')} ${plural} ${array[array.length - 1]}`; -}; - -module.exports.progressPromiseAll = function (msgOrMsgFn, array) { - if (!array.length) { - return Promise.resolve([]); - } - - const msgFn = (typeof msgOrMsgFn === 'function' - ? msgOrMsgFn - : () => msgOrMsgFn); - - let done = 0, total = array.length; - process.stdout.write(`\r${msgFn()} [0/${total}]`); - const start = Date.now(); - return Promise.all(array.map(promise => promise.then(val => { - done++; - // const pc = `${done}/${total}`; - const pc = (Math.round(done / total * 1000) / 10 + '%').padEnd('99.9%'.length, ' '); - if (done === total) { - const time = Date.now() - start; - process.stdout.write(`\r\x1b[2m${msgFn()} [${pc}] \x1b[0;32mDone! \x1b[0;2m(${time} ms) \x1b[0m\n`) - } else { - process.stdout.write(`\r${msgFn()} [${pc}] `); - } - return val; - }))); -}; - -module.exports.queue = function (array, max = 50) { - if (max === 0) { - return array.map(fn => fn()); - } - - const begin = []; - let current = 0; - const ret = array.map(fn => new Promise((resolve, reject) => { - begin.push(() => { - current++; - Promise.resolve(fn()).then(value => { - current--; - if (current < max && begin.length) { - begin.shift()(); - } - resolve(value); - }, reject); - }); - })); - - for (let i = 0; i < max && begin.length; i++) { - begin.shift()(); - } - - return ret; -}; - -module.exports.delay = ms => new Promise(res => setTimeout(res, ms)); - -module.exports.th = function (n) { - if (n % 10 === 1 && n !== 11) { - return n + 'st'; - } else if (n % 10 === 2 && n !== 12) { - return n + 'nd'; - } else if (n % 10 === 3 && n !== 13) { - return n + 'rd'; - } else { - return n + 'th'; - } -}; - -// My function names just keep getting 8etter. -module.exports.s = function (n, word) { - return `${n} ${word}` + (n === 1 ? '' : 's'); -}; - -// Hey, did you know I apparently put a space 8efore the parameters in function -// names? 8ut only in function expressions, not declar8tions? I mean, I guess -// you did. You're pro8a8ly more familiar with my code than I am 8y this -// point. I haven't messed with any of this code in ages. Yay!!!!!!!! -// -// This function only does anything on o8jects you're going to 8e reusing. -// Argua8ly I could use a WeakMap here, 8ut since the o8ject needs to 8e -// reused to 8e useful anyway, I just store the result with a symbol. -// Sorry if it's 8een frozen I guess?? -module.exports.cacheOneArg = function (fn) { - const symbol = Symbol('Cache'); - return arg => { - if (!arg[symbol]) { - arg[symbol] = fn(arg); - } - return arg[symbol]; - }; -}; - -const decorateTime = function (functionToBeWrapped) { - const fn = function(...args) { - const start = Date.now(); - const ret = functionToBeWrapped(...args); - const end = Date.now(); - fn.timeSpent += end - start; - fn.timesCalled++; - return ret; - }; - - fn.wrappedName = functionToBeWrapped.name; - fn.timeSpent = 0; - fn.timesCalled = 0; - fn.displayTime = function() { - const averageTime = fn.timeSpent / fn.timesCalled; - console.log(`\x1b[1m${fn.wrappedName}(...):\x1b[0m ${fn.timeSpent} ms / ${fn.timesCalled} calls \x1b[2m(avg: ${averageTime} ms)\x1b[0m`); - }; - - decorateTime.decoratedFunctions.push(fn); - - return fn; -}; - -decorateTime.decoratedFunctions = []; -decorateTime.displayTime = function() { - if (decorateTime.decoratedFunctions.length) { - console.log(`\x1b[1mdecorateTime results: ` + '-'.repeat(40) + '\x1b[0m'); - for (const fn of decorateTime.decoratedFunctions) { - fn.displayTime(); - } - } -}; - -module.exports.decorateTime = decorateTime; - -// Stolen as #@CK from mtui! -const parseOptions = async function(options, optionDescriptorMap) { - // This function is sorely lacking in comments, but the basic usage is - // as such: - // - // options is the array of options you want to process; - // optionDescriptorMap is a mapping of option names to objects that describe - // the expected value for their corresponding options. - // Returned is a mapping of any specified option names to their values, or - // a process.exit(1) and error message if there were any issues. - // - // Here are examples of optionDescriptorMap to cover all the things you can - // do with it: - // - // optionDescriptorMap: { - // 'telnet-server': {type: 'flag'}, - // 't': {alias: 'telnet-server'} - // } - // - // options: ['t'] -> result: {'telnet-server': true} - // - // optionDescriptorMap: { - // 'directory': { - // type: 'value', - // validate(name) { - // // const whitelistedDirectories = ['apple', 'banana'] - // if (whitelistedDirectories.includes(name)) { - // return true - // } else { - // return 'a whitelisted directory' - // } - // } - // }, - // 'files': {type: 'series'} - // } - // - // ['--directory', 'apple'] -> {'directory': 'apple'} - // ['--directory', 'artichoke'] -> (error) - // ['--files', 'a', 'b', 'c', ';'] -> {'files': ['a', 'b', 'c']} - // - // TODO: Be able to validate the values in a series option. - - const handleDashless = optionDescriptorMap[parseOptions.handleDashless]; - const handleUnknown = optionDescriptorMap[parseOptions.handleUnknown]; - const result = Object.create(null); - for (let i = 0; i < options.length; i++) { - const option = options[i]; - if (option.startsWith('--')) { - // --x can be a flag or expect a value or series of values - let name = option.slice(2).split('=')[0]; // '--x'.split('=') = ['--x'] - let descriptor = optionDescriptorMap[name]; - if (!descriptor) { - if (handleUnknown) { - handleUnknown(option); - } else { - console.error(`Unknown option name: ${name}`); - process.exit(1); - } - continue; - } - if (descriptor.alias) { - name = descriptor.alias; - descriptor = optionDescriptorMap[name]; - } - if (descriptor.type === 'flag') { - result[name] = true; - } else if (descriptor.type === 'value') { - let value = option.slice(2).split('=')[1]; - if (!value) { - value = options[++i]; - if (!value || value.startsWith('-')) { - value = null; - } - } - if (!value) { - console.error(`Expected a value for --${name}`); - process.exit(1); - } - result[name] = value; - } else if (descriptor.type === 'series') { - if (!options.slice(i).includes(';')) { - console.error(`Expected a series of values concluding with ; (\\;) for --${name}`); - process.exit(1); - } - const endIndex = i + options.slice(i).indexOf(';'); - result[name] = options.slice(i + 1, endIndex); - i = endIndex; - } - if (descriptor.validate) { - const validation = await descriptor.validate(result[name]); - if (validation !== true) { - console.error(`Expected ${validation} for --${name}`); - process.exit(1); - } - } - } else if (option.startsWith('-')) { - // mtui doesn't use any -x=y or -x y format optionuments - // -x will always just be a flag - let name = option.slice(1); - let descriptor = optionDescriptorMap[name]; - if (!descriptor) { - if (handleUnknown) { - handleUnknown(option); - } else { - console.error(`Unknown option name: ${name}`); - process.exit(1); - } - continue; - } - if (descriptor.alias) { - name = descriptor.alias; - descriptor = optionDescriptorMap[name]; - } - if (descriptor.type === 'flag') { - result[name] = true; - } else { - console.error(`Use --${name} (value) to specify ${name}`); - process.exit(1); - } - } else if (handleDashless) { - handleDashless(option); - } - } - return result; -} - -parseOptions.handleDashless = Symbol(); -parseOptions.handleUnknown = Symbol(); - -module.exports.parseOptions = parseOptions; - -// Cheap FP for a cheap dyke! -// I have no idea if this is what curry actually means. -module.exports.curry = f => x => (...args) => f(x, ...args); - -module.exports.mapInPlace = (array, fn) => array.splice(0, array.length, ...array.map(fn)); - -module.exports.filterEmptyLines = string => string.split('\n').filter(line => line.trim()).join('\n'); - -module.exports.unique = arr => Array.from(new Set(arr)); - -const logColor = color => (literals, ...values) => { - const w = s => process.stdout.write(s); - w(`\x1b[${color}m`); - for (let i = 0; i < literals.length; i++) { - w(literals[i]); - if (values[i] !== undefined) { - w(`\x1b[1m`); - w(String(values[i])); - w(`\x1b[0;${color}m`); - } - } - w(`\x1b[0m\n`); -}; - -module.exports.logInfo = logColor(2); -module.exports.logWarn = logColor(33); -module.exports.logError = logColor(31); - -module.exports.sortByName = (a, b) => { - let an = a.name.toLowerCase(); - let bn = b.name.toLowerCase(); - if (an.startsWith('the ')) an = an.slice(4); - if (bn.startsWith('the ')) bn = bn.slice(4); - return an < bn ? -1 : an > bn ? 1 : 0; -}; - -module.exports.chunkByConditions = function(array, conditions) { - if (array.length === 0) { - return []; - } else if (conditions.length === 0) { - return [array]; - } - - const out = []; - let cur = [array[0]]; - for (let i = 1; i < array.length; i++) { - const item = array[i]; - const prev = array[i - 1]; - let chunk = false; - for (const condition of conditions) { - if (condition(item, prev)) { - chunk = true; - break; - } - } - if (chunk) { - out.push(cur); - cur = [item]; - } else { - cur.push(item); - } - } - out.push(cur); - return out; -}; - -module.exports.chunkByProperties = function(array, properties) { - return module.exports.chunkByConditions(array, properties.map(p => (a, b) => { - if (a[p] instanceof Date && b[p] instanceof Date) - return +a[p] !== +b[p]; - - if (a[p] !== b[p]) return true; - - // Not sure if this line is still necessary with the specific check for - // d8tes a8ove, 8ut, uh, keeping it anyway, just in case....? - if (a[p] != b[p]) return true; - - return false; - })) - .map(chunk => ({ - ...Object.fromEntries(properties.map(p => [p, chunk[0][p]])), - chunk - })); -}; - -// Very cool function origin8ting in... http-music pro8a8ly! -// Sorry if we happen to 8e violating past-us's copyright, lmao. -module.exports.promisifyProcess = function(proc, showLogging = true) { - // Takes a process (from the child_process module) and returns a promise - // that resolves when the process exits (or rejects, if the exit code is - // non-zero). - // - // Ayy look, no alpha8etical second letter! Couldn't tell this was written - // like three years ago 8efore I was me. 8888) - - return new Promise((resolve, reject) => { - if (showLogging) { - proc.stdout.pipe(process.stdout); - proc.stderr.pipe(process.stderr); - } - - proc.on('exit', code => { - if (code === 0) { - resolve(); - } else { - reject(code); - } - }) - }) -}; - -// Stolen from jq! Which pro8a8ly stole the concept from other places. Nice. -module.exports.withEntries = (obj, fn) => Object.fromEntries(fn(Object.entries(obj))); - -// Nothin' more to it than what it says. Runs a function in-place. Provides an -// altern8tive syntax to the usual IIFEs (e.g. (() => {})()) when you want to -// open a scope and run some statements while inside an existing expression. -module.exports.call = fn => fn(); |