diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2022-01-18 19:45:09 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2022-01-18 19:45:09 -0400 |
commit | 859b8fb20525b44a94ab5072405c6c9d6df4da5b (patch) | |
tree | b2e56fb20931d6f8702157e7a4cb113e39faab3c /src | |
parent | b10d00e4f4cf191ed9cb914052422db4363de349 (diff) |
initial working changes for big data restructure
Diffstat (limited to 'src')
-rw-r--r-- | src/misc-templates.js | 30 | ||||
-rw-r--r-- | src/static/site.css | 5 | ||||
-rw-r--r-- | src/strings-default.json | 2 | ||||
-rw-r--r-- | src/thing/album.js | 255 | ||||
-rw-r--r-- | src/thing/cacheable-object.js | 269 | ||||
-rw-r--r-- | src/thing/structures.js | 31 | ||||
-rw-r--r-- | src/thing/thing.js | 42 | ||||
-rw-r--r-- | src/thing/validators.js | 208 | ||||
-rwxr-xr-x | src/upd8.js | 500 | ||||
-rw-r--r-- | src/util/cli.js | 37 | ||||
-rw-r--r-- | src/util/sugar.js | 146 |
11 files changed, 1276 insertions, 249 deletions
diff --git a/src/misc-templates.js b/src/misc-templates.js index 578c4e59..090c437d 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -375,3 +375,33 @@ export function generatePreviousNextLinks(current, { }) ].filter(Boolean).join(', '); } + +// Footer stuff + +export function getFooterLocalizationLinks(pathname, { + languages, + paths, + strings, + to +}) { + const { toPath } = paths; + const keySuffix = toPath[0].replace(/^localized\./, '.'); + const toArgs = toPath.slice(1); + + const links = Object.entries(languages) + .filter(([ code ]) => code !== 'default') + .map(([ code, strings ]) => strings) + .sort(( + { json: { 'meta.languageName': a } }, + { json: { 'meta.languageName': b } } + ) => a < b ? -1 : a > b ? 1 : 0) + .map(strings => html.tag('span', html.tag('a', { + href: (strings.code === languages.default.code + ? to('localizedDefaultLanguage' + keySuffix, ...toArgs) + : to('localizedWithBaseDirectory' + keySuffix, strings.code, ...toArgs)) + }, strings.json['meta.languageName']))); + + return html.tag('div', + {class: 'footer-localization-links'}, + strings('misc.uiLanguage', {languages: links.join('\n')})); +} diff --git a/src/static/site.css b/src/static/site.css index 65d4d343..c88343e5 100644 --- a/src/static/site.css +++ b/src/static/site.css @@ -173,6 +173,11 @@ footer > :last-child { margin-bottom: 0; } +.footer-localization-links > span:not(:last-child)::after { + content: " \00b7 "; + font-weight: 800; +} + .nowrap { white-space: nowrap; } diff --git a/src/strings-default.json b/src/strings-default.json index b80c99f6..86fb73fc 100644 --- a/src/strings-default.json +++ b/src/strings-default.json @@ -1,5 +1,6 @@ { "meta.languageCode": "en", + "meta.languageName": "English", "count.tracks": "{TRACKS}", "count.tracks.withUnit.zero": "", "count.tracks.withUnit.one": "{TRACKS} track", @@ -148,6 +149,7 @@ "misc.contentWarnings": "cw: {WARNINGS}", "misc.contentWarnings.reveal": "click to show", "misc.albumGridDetails": "({TRACKS}, {TIME})", + "misc.uiLanguage": "UI Language: {LANGUAGES}", "homepage.title": "{TITLE}", "homepage.news.title": "News", "homepage.news.entry.viewRest": "(View rest of entry!)", diff --git a/src/thing/album.js b/src/thing/album.js index e99cfc36..7be092e0 100644 --- a/src/thing/album.js +++ b/src/thing/album.js @@ -1,28 +1,267 @@ import Thing from './thing.js'; import { - validateDirectory, - validateReference -} from './structures.js'; + isBoolean, + isColor, + isCommentary, + isContributionList, + isDate, + isDimensions, + isDirectory, + isName, + isURL, + isString, + validateArrayItems, + validateReference, + validateReferenceList, +} from './validators.js'; import { + aggregateThrows, showAggregate, withAggregate } from '../util/sugar.js'; export default class Album extends Thing { + /* + #name = 'Unnamed Album'; + + #color = null; #directory = null; + #urls = []; + + #artists = []; + #coverArtists = []; + #trackCoverArtists = []; + + #wallpaperArtists = []; + #wallpaperStyle = ''; + #wallpaperFileExtension = 'jpg'; + + #bannerArtists = []; + #bannerStyle = ''; + #bannerFileExtension = 'jpg'; + #bannerDimensions = [0, 0]; + + #date = null; + #trackArtDate = null; + #coverArtDate = null; + #dateAddedToWiki = null; + + #hasTrackArt = true; + #isMajorRelease = false; + #isListedOnHomepage = true; + + #aka = ''; + #groups = []; + #artTags = []; + #commentary = ''; + #tracks = []; - static updateError = { + static propertyError = { + name: Thing.extendPropertyError('name'), directory: Thing.extendPropertyError('directory'), tracks: Thing.extendPropertyError('tracks') }; + */ + + static propertyDescriptors = { + // Update & expose + + name: { + flags: {update: true, expose: true}, + + update: { + default: 'Unnamed Album', + validate: isName + } + }, + + color: { + flags: {update: true, expose: true}, + update: {validate: isColor} + }, + + directory: { + flags: {update: true, expose: true}, + update: {validate: isDirectory} + }, + + urls: { + flags: {update: true, expose: true}, + + update: { + validate: validateArrayItems(isURL) + } + }, + + date: { + flags: {update: true, expose: true}, + update: {validate: isDate} + }, + + coverArtDate: { + flags: {update: true, expose: true}, + update: {validate: isDate} + }, + + trackArtDate: { + flags: {update: true, expose: true}, + update: {validate: isDate} + }, + + dateAddedToWiki: { + flags: {update: true, expose: true}, + + update: {validate: isDate} + }, + + artistContribsByRef: { + flags: {update: true, expose: true}, + update: {validate: isContributionList} + }, + + coverArtistContribsByRef: { + flags: {update: true, expose: true}, + update: {validate: isContributionList} + }, + + trackCoverArtistContribsByRef: { + flags: {update: true, expose: true}, + update: {validate: isContributionList} + }, + + wallpaperArtistContribsByRef: { + flags: {update: true, expose: true}, + update: {validate: isContributionList} + }, + + bannerArtistContribsByRef: { + flags: {update: true, expose: true}, + update: {validate: isContributionList} + }, + + groupsByRef: { + flags: {update: true, expose: true}, + + update: { + validate: validateReferenceList('group') + } + }, + + artTagsByRef: { + flags: {update: true, expose: true}, + + update: { + validate: validateReferenceList('tag') + } + }, + tracksByRef: { + flags: {update: true, expose: true}, + + update: { + validate: validateReferenceList('track') + } + }, + + wallpaperStyle: { + flags: {update: true, expose: true}, + update: {validate: isString} + }, + + wallpaperFileExtension: { + flags: {update: true, expose: true}, + update: {validate: isString} + }, + + bannerStyle: { + flags: {update: true, expose: true}, + update: {validate: isString} + }, + + bannerFileExtension: { + flags: {update: true, expose: true}, + update: {validate: isString} + }, + + bannerDimensions: { + flags: {update: true, expose: true}, + update: {validate: isDimensions} + }, + + hasTrackArt: { + flags: {update: true, expose: true}, + + update: { + default: true, + validate: isBoolean + } + }, + + isMajorRelease: { + flags: {update: true, expose: true}, + + update: { + default: false, + validate: isBoolean + } + }, + + isListedOnHomepage: { + flags: {update: true, expose: true}, + + update: { + default: true, + validate: isBoolean + } + }, + + commentary: { + flags: {update: true, expose: true}, + update: {validate: isCommentary} + }, + + // Expose only + + tracks: { + flags: {expose: true}, + + expose: { + dependencies: ['trackReferences', 'wikiData'], + compute: ({trackReferences, wikiData}) => ( + trackReferences.map(ref => find.track(ref, {wikiData}))) + } + }, + + // Update only + + wikiData: { + flags: {update: true} + } + }; + + /* update(source) { - const err = this.constructor.updateError; + const err = this.constructor.propertyError; + + withAggregate(aggregateThrows(Thing.UpdateError), ({ nest, filter, throws }) => { + if (source.name) { + nest(throws(err.name), ({ call }) => { + if (call(validateName, source.name)) { + this.#name = source.name; + } + }); + } - withAggregate(({ nest, filter, throws }) => { + if (source.color) { + nest(throws(err.color), ({ call }) => { + if (call(validateColor, source.color)) { + this.#color = source.color; + } + }); + } if (source.directory) { nest(throws(err.directory), ({ call }) => { @@ -37,10 +276,13 @@ export default class Album extends Thing { }); } + get name() { return this.#name; } get directory() { return this.#directory; } get tracks() { return this.#tracks; } + */ } +/* const album = new Album(); console.log('tracks (before):', album.tracks); @@ -60,3 +302,4 @@ try { } console.log('tracks (after):', album.tracks); +*/ diff --git a/src/thing/cacheable-object.js b/src/thing/cacheable-object.js new file mode 100644 index 00000000..f478fd23 --- /dev/null +++ b/src/thing/cacheable-object.js @@ -0,0 +1,269 @@ +// Generally extendable class for caching properties and handling dependencies, +// with a few key properties: +// +// 1) The behavior of every property is defined by its descriptor, which is a +// static value stored on the subclass (all instances share the same property +// descriptors). +// +// 1a) Additional properties may not be added past the time of object +// construction, and attempts to do so (including externally setting a +// property name which has no corresponding descriptor) will throw a +// TypeError. (This is done via an Object.seal(this) call after a newly +// created instance defines its own properties according to the descriptor +// on its constructor class.) +// +// 2) Properties may have two flags set: update and expose. Properties which +// update are provided values from the external. Properties which expose +// provide values to the external, generally dependent on other update +// properties (within the same object). +// +// 2a) Properties may be flagged as both updating and exposing. This is so +// that the same name may be used for both "output" and "input". +// +// 3) Exposed properties have values which are computations dependent on other +// properties, as described by a `compute` function on the descriptor. +// Depended-upon properties are explicitly listed on the descriptor next to +// this function, and are only provided as arguments to the function once +// listed. +// +// 3a) An exposed property may depend only upon updating properties, not other +// exposed properties (within the same object). This is to force the +// general complexity of a single object to be fairly simple: inputs +// directly determine outputs, with the only in-between step being the +// `compute` function, no multiple-layer dependencies. Note that this is +// only true within a given object - externally, values provided to one +// object's `update` may be (and regularly are) the exposed values of +// another object. +// +// 3b) If a property both updates and exposes, it is automatically regarded as +// a dependancy. (That is, its exposed value will depend on the value it is +// updated with.) Rather than a required `compute` function, these have an +// optional `transform` function, which takes the update value as its first +// argument and then the usual key-value dependencies as its second. If no +// `transform` function is provided, the expose value is the same as the +// update value. +// +// 4) Exposed properties are cached; that is, if no depended-upon properties are +// updated, the value of an exposed property is not recomputed. +// +// 4a) The cache for an exposed property is invalidated as soon as any of its +// dependencies are updated, but the cache itself is lazy: the exposed +// value will not be recomputed until it is again accessed. (Likewise, an +// exposed value won't be computed for the first time until it is first +// accessed.) +// +// 5) Updating a property may optionally apply validation checks before passing, +// declared by a `validate` function on the `update` block. This function +// should either throw an error (e.g. TypeError) or return false if the value +// is invalid. +// +// 6) Objects do not expect all updating properties to be provided at once. +// Incomplete objects are deliberately supported and enabled. +// +// 6a) The default value for every updating property is null; undefined is not +// accepted as a property value under any circumstances (it always errors). +// However, this default may be overridden by specifying a `default` value +// on a property's `update` block. (This value will be checked against +// the property's validate function.) Note that a property may always be +// updated to null, even if the default is non-null. (Null always bypasses +// the validate check.) +// +// 6b) It's required by the external consumer of an object to determine whether +// or not the object is ready for use (within the larger program). This is +// convenienced by the static CacheableObject.listAccessibleProperties() +// function, which provides a mapping of exposed property names to whether +// or not their dependencies are yet met. + +import { color, ENABLE_COLOR } from '../util/cli.js'; + +import { inspect as nodeInspect } from 'util'; + +function inspect(value) { + return nodeInspect(value, {colors: ENABLE_COLOR}); +} + +export default class CacheableObject { + #propertyUpdateValues = Object.create(null); + #propertyUpdateCacheInvalidators = Object.create(null); + + /* + // Note the constructor doesn't take an initial data source. Due to a quirk + // of JavaScript, private members can't be accessed before the superclass's + // constructor is finished processing - so if we call the overridden + // update() function from inside this constructor, it will error when + // writing to private members. Pretty bad! + // + // That means initial data must be provided by following up with update() + // after constructing the new instance of the Thing (sub)class. + */ + + constructor() { + this.#defineProperties(); + this.#initializeUpdatingPropertyValues(); + } + + #initializeUpdatingPropertyValues() { + for (const [ property, descriptor ] of Object.entries(this.constructor.propertyDescriptors)) { + const { flags, update } = descriptor; + + if (!flags.update) { + continue; + } + + if (update?.default) { + this[property] = update?.default; + } else { + this[property] = null; + } + } + } + + #defineProperties() { + for (const [ property, descriptor ] of Object.entries(this.constructor.propertyDescriptors)) { + const { flags } = descriptor; + + const definition = { + configurable: false, + enumerable: true + }; + + if (flags.update) { + definition.set = this.#getUpdateObjectDefinitionSetterFunction(property); + } + + if (flags.expose) { + definition.get = this.#getExposeObjectDefinitionGetterFunction(property); + } + + Object.defineProperty(this, property, definition); + } + + Object.seal(this); + } + + #getUpdateObjectDefinitionSetterFunction(property) { + const { update } = this.#getPropertyDescriptor(property); + const validate = update?.validate; + const allowNull = update?.allowNull; + + return (newValue) => { + const oldValue = this.#propertyUpdateValues[property]; + + if (newValue === undefined) { + throw new ValueError(`Properties cannot be set to undefined`); + } + + if (newValue === oldValue) { + return; + } + + if (newValue !== null && validate) { + try { + const result = validate(newValue); + if (result === undefined) { + throw new TypeError(`Validate function returned undefined`); + } else if (result !== true) { + throw new TypeError(`Validation failed for value ${newValue}`); + } + } catch (error) { + error.message = `Property ${color.green(property)} (${inspect(this[property])} -> ${inspect(newValue)}): ${error.message}`; + throw error; + } + } + + this.#propertyUpdateValues[property] = newValue; + this.#invalidateCachesDependentUpon(property); + }; + } + + #getUpdatePropertyValidateFunction(property) { + const descriptor = this.#getPropertyDescriptor(property); + } + + #getPropertyDescriptor(property) { + return this.constructor.propertyDescriptors[property]; + } + + #invalidateCachesDependentUpon(property) { + for (const invalidate of this.#propertyUpdateCacheInvalidators[property] || []) { + invalidate(); + } + } + + #getExposeObjectDefinitionGetterFunction(property) { + const { flags } = this.#getPropertyDescriptor(property); + const compute = this.#getExposeComputeFunction(property); + + if (compute) { + let cachedValue; + const checkCacheValid = this.#getExposeCheckCacheValidFunction(property); + return () => { + if (checkCacheValid()) { + return cachedValue; + } else { + return (cachedValue = compute()); + } + }; + } else if (!flags.update && !compute) { + throw new Error(`Exposed property ${property} does not update and is missing compute function`); + } else { + return () => this.#propertyUpdateValues[property]; + } + } + + #getExposeComputeFunction(property) { + const { flags, expose } = this.#getPropertyDescriptor(property); + + const compute = (!flags.update && expose?.compute); + const transform = (flags.update && expose?.transform); + + if (flags.update && !transform) { + return null; + } else if (!flags.update && !compute) { + throw new Error(`Exposed property ${property} does not update and is missing compute function`); + } + + const dependencyKeys = expose.dependencies || []; + const dependencyGetters = dependencyKeys.map(key => () => [key, this.#propertyUpdateValues[key]]); + const getAllDependencies = () => Object.fromEntries(dependencyGetters.map(f => f())); + + if (flags.update) { + return () => transform(this.#propertyUpdateValues[property], getAllDependencies()); + } else { + return () => compute(getAllDependencies()); + } + } + + #getExposeCheckCacheValidFunction(property) { + const { flags, expose } = this.#getPropertyDescriptor(property); + + let valid = false; + + const invalidate = () => { + valid = false; + }; + + const dependencyKeys = new Set(expose?.dependencies); + + if (flags.update) { + dependencyKeys.add(property); + } + + for (const key of dependencyKeys) { + if (this.#propertyUpdateCacheInvalidators[key]) { + this.#propertyUpdateCacheInvalidators[key].push(invalidate); + } else { + this.#propertyUpdateCacheInvalidators[key] = [invalidate]; + } + } + + return () => { + if (!valid) { + valid = true; + return false; + } else { + return true; + } + }; + } +} diff --git a/src/thing/structures.js b/src/thing/structures.js index 89c9bd39..364ba149 100644 --- a/src/thing/structures.js +++ b/src/thing/structures.js @@ -1,32 +1 @@ // Generic structure utilities common across various Thing types. - -export function validateDirectory(directory) { - if (typeof directory !== 'string') - throw new TypeError(`Expected a string, got ${directory}`); - - if (directory.length === 0) - throw new TypeError(`Expected directory to be non-zero length`); - - if (directory.match(/[^a-zA-Z0-9\-]/)) - throw new TypeError(`Expected only letters, numbers, and dash, got "${directory}"`); - - return true; -} - -export function validateReference(type = '') { - return ref => { - if (typeof ref !== 'string') - throw new TypeError(`Expected a string, got ${ref}`); - - if (type) { - if (!ref.includes(':')) - throw new TypeError(`Expected ref to begin with "${type}:", but no type specified (ref: ${ref})`); - - const typePart = ref.split(':')[0]; - if (typePart !== type) - throw new TypeError(`Expected ref to begin with "${type}:", got "${typePart}:" (ref: ${ref})`); - } - - return true; - }; -} diff --git a/src/thing/thing.js b/src/thing/thing.js index c2465e32..dd3126c1 100644 --- a/src/thing/thing.js +++ b/src/thing/thing.js @@ -6,46 +6,10 @@ // together as an AggregateError. See util/sugar.js for utility functions to // make writing code around this easier! -export default class Thing { - constructor(source, { - wikiData - } = {}) { - if (source) { - this.update(source); - } +import CacheableObject from './cacheable-object.js'; - if (wikiData && this.checkComplete()) { - this.postprocess({wikiData}); - } - } - - static PropertyError = class extends AggregateError { - #key = this.constructor.key; - get key() { return this.#key; } - - constructor(errors) { - super(errors, ''); - this.message = `${errors.length} error(s) in property "${this.#key}"`; - } - }; - - static extendPropertyError(key) { - const cls = class extends this.PropertyError { - static #key = key; - static get key() { return this.#key; } - }; - - Object.defineProperty(cls, 'name', {value: `PropertyError:${key}`}); - return cls; - } - - // Called when instantiating a thing, and when its data is updated for any - // reason. (Which currently includes no reasons, but hey, future-proofing!) - // - // Don't expect source to be a complete object, even on the first call - the - // method checkComplete() will prevent incomplete resources from being mixed - // with the rest. - update(source) {} +export default class Thing extends CacheableObject { + static propertyDescriptors = Symbol('Thing property descriptors'); // Called when collecting the full list of available things of that type // for wiki data; this method determine whether or not to include it. diff --git a/src/thing/validators.js b/src/thing/validators.js new file mode 100644 index 00000000..05736914 --- /dev/null +++ b/src/thing/validators.js @@ -0,0 +1,208 @@ +import { withAggregate } from '../util/sugar.js'; + +// Basic types (primitives) + +function a(noun) { + return (/[aeiou]/.test(noun[0]) ? `an ${noun}` : `a ${noun}`); +} + +function isType(value, type) { + if (typeof value !== type) + throw new TypeError(`Expected ${a(type)}, got ${typeof value}`); + + return true; +} + +export function isBoolean(value) { + return isType(value, 'boolean'); +} + +export function isNumber(value) { + return isType(value, 'number'); +} + +export function isPositive(number) { + isNumber(number); + + if (number <= 0) + throw new TypeError(`Expected positive number`); + + return true; +} + +export function isNegative(number) { + isNumber(number); + + if (number >= 0) + throw new TypeError(`Expected negative number`); + + return true; +} + +export function isInteger(number) { + isNumber(number); + + if (number % 1 !== 0) + throw new TypeError(`Expected integer`); + + return true; +} + +export function isString(value) { + return isType(value, 'string'); +} + +export function isStringNonEmpty(value) { + isString(value); + + if (value.trim().length === 0) + throw new TypeError(`Expected non-empty string`); + + return true; +} + +// Complex types (non-primitives) + +function isInstance(value, constructor) { + isObject(value); + + if (!(value instanceof constructor)) + throw new TypeError(`Expected ${constructor.name}, got ${value.constructor.name}`); + + return true; +} + +export function isDate(value) { + return isInstance(value, Date); +} + +export function isObject(value) { + isType(value, 'object'); + + // Note: Please remember that null is always a valid value for properties + // held by a CacheableObject. This assertion is exclusively for use in other + // contexts. + if (value === null) + throw new TypeError(`Expected an object, got null`); + + return true; +} + +export function isArray(value) { + isObject(value); + + if (!Array.isArray(value)) + throw new TypeError(`Expected an array, got ${value}`); + + return true; +} + +export function validateArrayItems(itemValidator) { + return array => { + isArray(array); + + withAggregate({message: 'Errors validating array items'}, ({ wrap }) => { + array.forEach(wrap(itemValidator)); + }); + + return true; + }; +} + +// Wiki data (primitives & non-primitives) + +export function isColor(color) { + isStringNonEmpty(color); + + if (color.startsWith('#')) { + if (![1 + 3, 1 + 4, 1 + 6, 1 + 8].includes(color.length)) + throw new TypeError(`Expected #rgb, #rgba, #rrggbb, or #rrggbbaa, got length ${color.length}`); + + if (/[^0-9a-fA-F]/.test(color.slice(1))) + throw new TypeError(`Expected hexadecimal digits`); + + return true; + } + + throw new TypeError(`Unknown color format`); +} + +export function isCommentary(commentary) { + return isString(commentary); +} + +const isArtistRef = validateReference('artist'); + +export function isContribution(contrib) { + // TODO: Use better object validation for this (supporting aggregates etc) + + isObject(contrib); + + isArtistRef(contrib.who); + + if (contrib.what !== null) { + isStringNonEmpty(contrib.what); + } + + return true; +} + +export const isContributionList = validateArrayItems(isContribution); + +export function isDimensions(dimensions) { + isArray(dimensions); + + if (dimensions.length !== 2) + throw new TypeError(`Expected 2 item array`); + + isPositive(dimensions[0]); + isInteger(dimensions[0]); + isPositive(dimensions[1]); + isInteger(dimensions[1]); + + return true; +} + +export function isDirectory(directory) { + isStringNonEmpty(directory); + + if (directory.match(/[^a-zA-Z0-9\-]/)) + throw new TypeError(`Expected only letters, numbers, and dash, got "${directory}"`); + + return true; +} + +export function isName(name) { + return isString(name); +} + +export function isURL(string) { + isStringNonEmpty(string); + + new URL(string); + + return true; +} + +export function validateReference(type = 'track') { + return ref => { + isStringNonEmpty(ref); + + const hasTwoParts = ref.includes(':'); + const [ typePart, directoryPart ] = ref.split(':'); + + if (hasTwoParts && typePart !== type) + throw new TypeError(`Expected ref to begin with "${type}:", got "${typePart}:" (ref: ${ref})`); + + if (hasTwoParts) + isDirectory(directoryPart); + + isName(ref); + + return true; + }; +} + +export function validateReferenceList(type = '') { + return validateArrayItems(validateReference(type)); +} diff --git a/src/upd8.js b/src/upd8.js index 6f538d18..fd5a21ca 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -17,6 +17,9 @@ // going to 8e in. May8e JSON, 8ut more likely some weird custom format // which will 8e a lot easier to edit. // +// Like three years later oh god: SURPISE! We went with the latter, but +// they're YAML now. Probably. Assuming that hasn't changed, yet. +// // 3. Generate the page files! They're just static index.html files, and are // what gh-pages (or wherever this is hosted) will show to clients. // Hopefully pretty minimalistic HTML, 8ut like, shrug. They'll reference @@ -28,40 +31,6 @@ // Oh yeah, like. Just run this through some relatively recent version of // node.js and you'll 8e fine. ...Within the project root. O8viously. -// HEY FUTURE ME!!!!!!!! Don't forget to implement artist pages! Those are, -// like, the coolest idea you've had yet, so DO NOT FORGET. (Remem8er, link -// from track listings, etc!) --- Thanks, past me. To futurerer me: an al8um -// listing page (a list of all the al8ums)! Make sure to sort these 8y date - -// we'll need a new field for al8ums. - -// ^^^^^^^^ DID THAT! 8ut also, artist images. Pro8a8ly stolen from the fandom -// wiki (I found half those images anywayz). - -// TRACK ART CREDITS. This is a must. - -// 2020-08-23 -// ATTENTION ALL 8*TCHES AND OTHER GENDER TRUCKERS: AS IT TURNS OUT, THIS CODE -// ****SUCKS****. I DON'T THINK ANYTHING WILL EVER REDEEM IT, 8UT THAT DOESN'T -// MEAN WE CAN'T TAKE SOME ACTION TO MAKE WRITING IT A LITTLE LESS TERRI8LE. -// We're gonna start defining STRUCTURES to make things suck less!!!!!!!! -// No classes 8ecause those are a huge pain and like, pro8a8ly 8ad performance -// or whatever -- just some standard structures that should 8e followed -// wherever reasona8le. Only one I need today is the contri8 one 8ut let's put -// any new general-purpose structures here too, ok? -// -// Contri8ution: {who, what, date, thing}. D8 and thing are the new fields. -// -// Use these wisely, which is to say all the time and instead of whatever -// terri8le new pseudo structure you're trying to invent!!!!!!!! -// -// Upd8 2021-01-03: Soooooooo we didn't actually really end up using these, -// lol? Well there's still only one anyway. Kinda ended up doing a 8ig refactor -// of all the o8ject structures today. It's not *especially* relevant 8ut feels -// worth mentioning? I'd get rid of this comment 8lock 8ut I like it too much! -// Even though I haven't actually reread it, lol. 8ut yeah, hopefully in the -// spirit of this "make things more consistent" attitude I 8rought up 8ack in -// August, stuff's lookin' 8etter than ever now. W00t! - import * as path from 'path'; import { promisify } from 'util'; import { fileURLToPath } from 'url'; @@ -75,6 +44,8 @@ import fixWS from 'fix-whitespace'; // It stands for "HTML Entities", apparently. Cursed. import he from 'he'; +import yaml from 'js-yaml'; + 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 @@ -118,6 +89,8 @@ import find from './util/find.js'; import * as html from './util/html.js'; import unbound_link, {getLinkThemeString} from './util/link.js'; +import Album from './thing/album.js'; + import { fancifyFlashURL, fancifyURL, @@ -129,6 +102,7 @@ import { getAlbumStylesheet, getArtistString, getFlashGridHTML, + getFooterLocalizationLinks, getGridHTML, getRevealStringFromTags, getRevealStringFromWarnings, @@ -137,6 +111,7 @@ import { } from './misc-templates.js'; import { + color, decorateTime, logWarn, logInfo, @@ -185,10 +160,15 @@ import { import { bindOpts, call, + filterAggregateAsync, filterEmptyLines, + mapAggregateAsync, + openAggregate, queue, + showAggregate, splitArray, unique, + withAggregate, withEntries } from './util/sugar.js'; @@ -208,6 +188,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); const CACHEBUST = 7; +// MAKE THESE END IN YAML const WIKI_INFO_FILE = 'wiki-info.txt'; const HOMEPAGE_INFO_FILE = 'homepage.txt'; const ARTIST_DATA_FILE = 'artists.txt'; @@ -274,6 +255,11 @@ function splitLines(text) { return text.split(/\r\n|\r|\n/); } +// REFERENCE CODE! +// REFERENCE CODE! +// REFERENCE CODE! +// REFERENCE CODE! + function* getSections(lines) { // ::::) const isSeparatorLine = line => /^-{8,}/.test(line); @@ -285,9 +271,11 @@ function getBasicField(lines, name) { return line && line.slice(name.length + 1).trim(); } -function getDimensionsField(lines, name) { - const string = getBasicField(lines, name); - if (!string) return string; +function parseDimensions(string) { + if (!string) { + return null; + } + const parts = string.split(/[x,* ]+/g); if (parts.length !== 2) throw new Error(`Invalid dimensions: ${string} (expected width & height)`); const nums = parts.map(part => Number(part.trim())); @@ -338,9 +326,7 @@ function getListField(lines, name) { return listLines.map(line => line.slice(2)); }; -function getContributionField(section, name) { - let contributors = getListField(section, name); - +function parseContributors(contributors) { if (!contributors) { return null; } @@ -737,10 +723,61 @@ function transformLyrics(text, { return outLines.join('\n'); } -function getCommentaryField(lines) { - const text = getMultilineField(lines, 'Commentary'); +// Use parseErrorFactory to declare different "types" of errors. By storing the +// factory itself in an accessible location, the type of error may be detected +// by comparing against its own factory property. +function parseErrorFactory(annotation) { + return function factory(data = null) { + return { + error: true, + annotation, + data, + factory + }; + }; +} + +function parseField(object, key, steps) { + let value = object[key]; + + for (const step of steps) { + try { + value = step(value); + } catch (error) { + throw parseField.stepError({ + stepName: step.name, + stepError: error + }); + } + } + + return value; +} + +parseField.stepError = parseErrorFactory('step failed'); + +function assertFieldPresent(value) { + if (value === undefined || value === null) { + throw assertFieldPresent.missingField(); + } else { + return value; + } +} + +assertFieldPresent.missingField = parseErrorFactory('missing field'); + +function assertValidDate(dateString, {optional = false} = {}) { + if (dateString && isNaN(Date.parse(dateString))) { + throw assertValidDate.invalidDate(); + } + return value; +} + +assertValidDate.invalidDate = parseErrorFactory('invalid date'); + +function parseCommentary(text) { if (text) { - const lines = text.split('\n'); + const lines = String(text).split('\n'); if (!lines[0].replace(/<\/b>/g, '').includes(':</i>')) { return {error: `An entry is missing commentary citation: "${lines[0].slice(0, 40)}..."`}; } @@ -748,8 +785,141 @@ function getCommentaryField(lines) { } else { return null; } +} + +// General function for inputting a single document (usually loaded from YAML) +// and outputting an instance of a provided Thing subclass. +// +// makeParseDocument is a factory function: the returned function will take a +// document and apply the configuration passed to makeParseDocument in order to +// construct a Thing subclass. +function makeParseDocument(thingClass, { + // Optional early step for transforming field values before providing them + // to the Thing's update() method. This is useful when the input format + // (i.e. values in the document) differ from the format the actual Thing + // expects. + // + // Each key and value are a field name (not an update() property) and a + // function which takes the value for that field and returns the value which + // will be passed on to update(). + fieldTransformations = {}, + + // Mapping of Thing.update() source properties to field names. + // + // Note this is property -> field, not field -> property. This is a + // shorthand convenience because properties are generally typical + // camel-cased JS properties, while fields may contain whitespace and be + // more easily represented as quoted strings. + propertyFieldMapping +}) { + const knownFields = Object.values(propertyFieldMapping); + + // Invert the property-field mapping, since it'll come in handy for + // assigning update() source values later. + const fieldPropertyMapping = Object.fromEntries( + (Object.entries(propertyFieldMapping) + .map(([ property, field ]) => [field, property]))); + + return function(document, {file = null}) { + const unknownFields = Object.keys(document) + .filter(field => !knownFields.includes(field)); + + if (unknownFields.length) { + throw new makeParseDocument.UnknownFieldsError(unknownFields); + } + + const fieldValues = {}; + + for (const [ field, value ] of Object.entries(document)) { + if (Object.hasOwn(fieldTransformations, field)) { + fieldValues[field] = fieldTransformations[field](value); + } else { + fieldValues[field] = value; + } + } + + const sourceProperties = {}; + + for (const [ field, value ] of Object.entries(fieldValues)) { + const property = fieldPropertyMapping[field]; + sourceProperties[property] = value; + } + + const thing = Reflect.construct(thingClass, []); + + const C = color; + const filePart = file ? `(file: ${C.bright(C.blue(path.relative(dataPath, file)))})` : ''; + withAggregate({message: `Errors applying ${C.green(thingClass.name)} properties ${filePart}`}, ({ call }) => { + for (const [ property, value ] of Object.entries(sourceProperties)) { + call(() => { + thing[property] = value; + }); + } + }); + + return thing; + }; +} + +makeParseDocument.UnknownFieldsError = class UnknownFieldsError extends Error { + constructor(fields) { + super(`Unknown fields present: ${fields.join(', ')}`); + this.fields = fields; + } }; +processAlbumDataFile.parseDocument = makeParseDocument(Album, { + fieldTransformations: { + 'Artists': parseContributors, + 'Cover Artists': parseContributors, + 'Default Track Cover Artists': parseContributors, + 'Wallpaper Artists': parseContributors, + 'Banner Artists': parseContributors, + + 'Date': value => new Date(value), + 'Date Added': value => new Date(value), + 'Cover Art Date': value => new Date(value), + 'Default Track Cover Art Date': value => new Date(value), + + 'Banner Dimensions': parseDimensions, + }, + + propertyFieldMapping: { + name: 'Album', + + color: 'Color', + directory: 'Directory', + urls: 'URLs', + + artistContribsByRef: 'Artists', + coverArtistContribsByRef: 'Cover Artists', + trackCoverArtistContribsByRef: 'Default Track Cover Artists', + + wallpaperArtistContribsByRef: 'Wallpaper Artists', + wallpaperStyle: 'Wallpaper Style', + wallpaperFileExtension: 'Wallpaper File Extension', + + bannerArtistContribsByRef: 'Banner Artists', + bannerStyle: 'Banner Style', + bannerFileExtension: 'Banner File Extension', + bannerDimensions: 'Banner Dimensions', + + date: 'Date', + trackArtDate: 'Default Track Cover Art Date', + coverArtDate: 'Cover Art Date', + dateAddedToWiki: 'Date Added', + + hasTrackArt: 'Has Track Art', + isMajorRelease: 'Major Release', + isListedOnHomepage: 'Listed on Homepage', + + aka: 'Also Released As', + groupsByRef: 'Groups', + artTagsByRef: 'Art Tags', + commentary: 'Commentary', + } +}); + async function processAlbumDataFile(file) { let contents; try { @@ -771,43 +941,54 @@ async function processAlbumDataFile(file) { // We'll just return more specific errors if it's missing necessary data // fields. - const contentLines = contents.split(/\r\n|\r|\n/); + const documents = yaml.loadAll(contents); - // In this line of code I defeat the purpose of using a generator in the - // first place. Sorry!!!!!!!! - const sections = Array.from(getSections(contentLines)); + const albumDoc = documents[0]; - const albumSection = sections[0]; - const album = {}; + return processAlbumDataFile.parseDocument(albumDoc, {file}); - album.name = getBasicField(albumSection, 'Album'); + // -------------------------------------------------------------- + + // const album = {}; + + album.name = parseField(albumDoc, 'Album', [ + assertFieldPresent + ]); if (!album.name) { return {error: `The file "${path.relative(dataPath, file)}" is missing the "Album" field - maybe this is a misplaced file instead of album data?`}; } - album.artists = getContributionField(albumSection, 'Artists') || getContributionField(albumSection, 'Artist'); - album.wallpaperArtists = getContributionField(albumSection, 'Wallpaper Art'); - album.wallpaperStyle = getMultilineField(albumSection, 'Wallpaper Style'); - album.wallpaperFileExtension = getBasicField(albumSection, 'Wallpaper File Extension') || 'jpg'; - album.bannerArtists = getContributionField(albumSection, 'Banner Art'); - album.bannerStyle = getMultilineField(albumSection, 'Banner Style'); - album.bannerFileExtension = getBasicField(albumSection, 'Banner File Extension') || 'jpg'; - album.bannerDimensions = getDimensionsField(albumSection, 'Banner Dimensions'); - album.date = getBasicField(albumSection, 'Date'); - album.trackArtDate = getBasicField(albumSection, 'Track Art Date') || album.date; - album.coverArtDate = getBasicField(albumSection, 'Cover Art Date') || album.date; - album.dateAdded = getBasicField(albumSection, 'Date Added'); - album.coverArtists = getContributionField(albumSection, 'Cover Art'); - album.hasTrackArt = getBooleanField(albumSection, 'Has Track Art') ?? true; - album.trackCoverArtists = getContributionField(albumSection, 'Track Art'); - album.artTags = getListField(albumSection, 'Art Tags') || []; - album.commentary = getCommentaryField(albumSection); - album.urls = getListField(albumSection, 'URLs') || []; - album.groups = getListField(albumSection, 'Groups') || []; - album.directory = getBasicField(albumSection, 'Directory'); - album.isMajorRelease = getBooleanField(albumSection, 'Major Release') ?? false; - album.isListedOnHomepage = getBooleanField(albumSection, 'Listed on Homepage') ?? true; + // album.directory = albumDoc['Directory']; + // album.urls = albumDoc['URLs'] || []; + + // album.artists = parseContributors(albumDoc['Artists']); + + // album.date = albumDoc['Date']; + // album.trackArtDate = albumDoc['Track Art Date'] || album.date; + // album.coverArtDate = albumDoc['Cover Art Date'] || album.date; + // album.dateAdded = albumDoc['Date Added']; + + // album.coverArtists = parseContributors(albumDoc['Cover Artists']); + // album.trackCoverArtists = parseContributors(albumDoc['Default Track Cover Artists']); + // album.hasTrackArt = albumDoc['Has Track Art'] ?? true; + + // album.wallpaperArtists = parseContributors(albumDoc['Wallpaper Artists']); + // album.wallpaperStyle = albumDoc['Wallpaper Style']; + // album.wallpaperFileExtension = albumDoc['Wallpaper File Extension'] || 'jpg'; + + // album.bannerArtists = albumDoc['Banner Artists']; + // album.bannerStyle = albumDoc['Banner Style']; + // album.bannerFileExtension = albumDoc['Banner File Extension'] || 'jpg'; + // album.bannerDimensions = parseDimensions(albumDoc['Banner Dimensions']); + + // album.groups = albumDoc['Groups'] || []; + // album.artTags = albumDoc['Art Tags'] || []; + + // album.commentary = parseCommentary(albumDoc['Commentary']); + + // album.isMajorRelease = albumDoc['Major Release'] ?? false; + // album.isListedOnHomepage = albumDoc['Listed on Homepage'] ?? true; if (album.artists && album.artists.error) { return {error: `${album.artists.error} (in ${album.name})`}; @@ -829,10 +1010,7 @@ async function processAlbumDataFile(file) { return {error: `The album "${album.name}" is missing the "Cover Art" field.`}; } - album.color = ( - getBasicField(albumSection, 'Color') || - getBasicField(albumSection, 'FG') - ); + // album.color = albumDoc['Color']; if (!album.name) { return {error: `Expected "Album" (name) field!`}; @@ -879,24 +1057,20 @@ async function processAlbumDataFile(file) { let group = null; let trackIndex = 0; - for (const section of sections.slice(1)) { + for (const doc of documents.slice(1)) { // Just skip empty sections. Sometimes I paste a 8unch of dividers, // and this lets the empty sections doing that creates (temporarily) // exist without raising an error. - if (!section.filter(Boolean).length) { + if (!doc) { continue; } - const groupName = getBasicField(section, 'Group'); + const groupName = doc['Group']; if (groupName) { group = { name: groupName, - color: ( - getBasicField(section, 'Color') || - getBasicField(section, 'FG') || - album.color - ), - originalDate: getBasicField(section, 'Original Date'), + color: doc['Color'] || album.color, + originalDate: doc['Original Date'], startIndex: trackIndex, tracks: [] }; @@ -921,27 +1095,46 @@ async function processAlbumDataFile(file) { const track = {}; - track.name = getBasicField(section, 'Track'); - track.commentary = getCommentaryField(section); - track.lyrics = getMultilineField(section, 'Lyrics'); - track.originalDate = getBasicField(section, 'Original Date'); - track.coverArtDate = getBasicField(section, 'Cover Art Date') || track.originalDate || album.trackArtDate; - track.references = getListField(section, 'References') || []; - track.artists = getContributionField(section, 'Artists') || getContributionField(section, 'Artist'); - track.coverArtists = getContributionField(section, 'Track Art'); - track.artTags = getListField(section, 'Art Tags') || []; - track.contributors = getContributionField(section, 'Contributors') || []; - track.directory = getBasicField(section, 'Directory'); - track.aka = getBasicField(section, 'AKA'); + track.name = String(doc['Track']); + + track.commentary = parseCommentary(doc['Commentary']); + track.lyrics = String(doc['Lyrics']); + + track.originalDate = doc['Date First Released']; + track.coverArtDate = doc['Cover Art Date'] || track.originalDate || album.trackArtDate; + + isNaN(Date.parse(track.originalDate)) + + if (track.originalDate) { + if (isNaN(Date.parse(track.originalDate))) { + return {error: `The track "${track.name}"'s has an invalid "Date First Released" field: "${track.originalDate}"`}; + } + track.originalDate = new Date(track.originalDate); + track.date = new Date(track.originalDate); + } else if (group && group.originalDate) { + track.originalDate = group.originalDate; + track.date = group.originalDate; + } else { + track.date = album.date; + } + + track.coverArtDate = new Date(track.coverArtDate); + + track.references = doc['References'] || []; + track.artists = parseContributors(doc['Artists']); + track.coverArtists = parseContributors(doc['Cover Artists']); + track.artTags = doc['Art Tags'] || []; + track.contributors = parseContributors(doc['Contributors']); + track.directory = doc['Directory']; + track.aka = doc['AKA']; if (!track.name) { - return {error: `A track section is missing the "Track" (name) field (in ${album.name}, previous: ${album.tracks[album.tracks.length - 1]?.name}).`}; + return {error: `A track document is missing the "Track" (name) field (in ${album.name}, previous: ${album.tracks[album.tracks.length - 1]?.name}).`}; } - let durationString = getBasicField(section, 'Duration') || '0:00'; - track.duration = getDurationInSeconds(durationString); + track.duration = getDurationInSeconds(doc['Duration'] || '0:00'); - if (track.contributors.error) { + if (track.contributors?.error) { return {error: `${track.contributors.error} (in ${track.name}, ${album.name})`}; } @@ -960,42 +1153,28 @@ async function processAlbumDataFile(file) { } } - if (!track.coverArtists) { - if (getBasicField(section, 'Track Art') !== 'none' && album.hasTrackArt) { - if (album.trackCoverArtists) { - track.coverArtists = album.trackCoverArtists; - } else { - return {error: `The track "${track.name}" is missing the "Track Art" field (in ${album.name}).`}; - } - } - } - - if (track.coverArtists && track.coverArtists.length && track.coverArtists[0] === 'none') { + if (doc['Has Cover Art'] === false) { track.coverArtists = null; + } else if (album.hasTrackArt && !track.coverArtists) { + if (album.trackCoverArtists) { + track.coverArtists = album.trackCoverArtists; + } else { + return {error: `The track "${track.name}" is missing the "Cover Artists" field (in ${album.name}).`}; + } } if (!track.directory) { - track.directory = getKebabCase(track.name); - } - - if (track.originalDate) { - if (isNaN(Date.parse(track.originalDate))) { - return {error: `The track "${track.name}"'s has an invalid "Original Date" field: "${track.originalDate}"`}; + try { + track.directory = getKebabCase(track.name); + } catch (error) { + console.log('error:', track.name); + process.exit(); } - track.originalDate = new Date(track.originalDate); - track.date = new Date(track.originalDate); - } else if (group && group.originalDate) { - track.originalDate = group.originalDate; - track.date = group.originalDate; - } else { - track.date = album.date; } - track.coverArtDate = new Date(track.coverArtDate); - - const hasURLs = getBooleanField(section, 'Has URLs') ?? true; + const hasURLs = doc['Has URLs'] ?? true; - track.urls = hasURLs && (getListField(section, 'URLs') || []).filter(Boolean); + track.urls = hasURLs && doc['URLs'] || []; if (hasURLs && !track.urls.length) { return {error: `The track "${track.name}" should have at least one URL specified.`}; @@ -1726,15 +1905,35 @@ writePage.to = ({ }) => (targetFullKey, ...args) => { const [ groupKey, subKey ] = targetFullKey.split('.'); let path = paths.subdirectoryPrefix; + + let from; + let to; + // When linking to *outside* the localized area of the site, we need to // make sure the result is correctly relative to the 8ase directory. - if (groupKey !== 'localized' && baseDirectory) { - path += urls.from('localizedWithBaseDirectory.' + pageSubKey).to(targetFullKey, ...args); + if (groupKey !== 'localized' && groupKey !== 'localizedDefaultLanguage' && baseDirectory) { + from = 'localizedWithBaseDirectory.' + pageSubKey; + to = targetFullKey; + } else if (groupKey === 'localizedDefaultLanguage' && baseDirectory) { + // Special case for specifically linking *from* a page with base + // directory *to* a page without! Used for the language switcher and + // hopefully nothing else oh god. + from = 'localizedWithBaseDirectory.' + pageSubKey; + to = 'localized.' + subKey; + } else if (groupKey === 'localizedDefaultLanguage') { + // Linking to the default, except surprise, we're already IN the default + // (no baseDirectory set). + from = 'localized.' + pageSubKey; + to = 'localized.' + subKey; } else { // If we're linking inside the localized area (or there just is no // 8ase directory), the 8ase directory doesn't matter. - path += urls.from('localized.' + pageSubKey).to(targetFullKey, ...args); + from = 'localized.' + pageSubKey; + to = targetFullKey; } + + path += urls.from(from).to(to, ...args); + return path; }; @@ -1792,8 +1991,12 @@ writePage.html = (pageFn, { footer.classes ??= []; footer.content ??= (wikiInfo.footer ? transformMultiline(wikiInfo.footer) : ''); + footer.content += '\n' + getFooterLocalizationLinks(paths.pathname, { + languages, paths, strings, to + }); + const canonical = (wikiInfo.canonicalBase - ? wikiInfo.canonicalBase + (paths.pathname === '/' ? '' : paths.pathanme) + ? wikiInfo.canonicalBase + (paths.pathname === '/' ? '' : paths.pathname) : ''); const collapseSidebars = (sidebarLeft.collapse !== false) && (sidebarRight.collapse !== false); @@ -2035,6 +2238,7 @@ writePage.paths = (baseDirectory, fullKey, directory = '', { const outputFile = path.join(outputDirectory, file); return { + toPath: [fullKey, directory], pathname, subdirectoryPrefix, outputDirectory, outputFile @@ -2419,12 +2623,32 @@ 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, DATA_ALBUM_DIRECTORY)); + const albumDataFiles = await findFiles(path.join(dataPath, DATA_ALBUM_DIRECTORY), f => path.extname(f) === '.yaml'); + + class LoadDataFileError extends AggregateError {} + + // WD.albumData = await progressPromiseAll(`Reading & processing album files.`, albumDataFiles.map(aggregate.wrapAsync(processAlbumDataFile))); + + const processDataAggregate = openAggregate({message: `Errors processing data files`}); + + await processDataAggregate.callAsync(async () => { + const { aggregate, result } = await mapAggregateAsync(albumDataFiles, processAlbumDataFile, { + message: `Errors processing album files`, + promiseAll: array => progressPromiseAll(`Reading & processing album files.`, array) + }); + + WD.albumData = result; - // 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 - // with it. - WD.albumData = await progressPromiseAll(`Reading & processing album files.`, albumDataFiles.map(processAlbumDataFile)); + aggregate.close(); + }); + + try { + processDataAggregate.close(); + } catch (error) { + showAggregate(error); + } + + process.exit(); { const errors = WD.albumData.filter(obj => obj.error); diff --git a/src/util/cli.js b/src/util/cli.js index 7f84be7c..b6335726 100644 --- a/src/util/cli.js +++ b/src/util/cli.js @@ -3,18 +3,47 @@ // A 8unch of these depend on process.stdout 8eing availa8le, so they won't // work within the 8rowser. +const { process } = globalThis; + +export const ENABLE_COLOR = process && ( + (process.env.CLICOLOR_FORCE && process.env.CLICOLOR_FORCE === '1') + ?? (process.env.CLICOLOR && process.env.CLICOLOR === '1' && process.stdout.hasColors && process.stdout.hasColors()) + ?? (process.stdout.hasColors ? process.stdout.hasColors() : true)); + +const C = n => (ENABLE_COLOR + ? text => `\x1b[${n}m${text}\x1b[0m` + : text => text); + +export const color = { + bright: C('1'), + dim: C('2'), + black: C('30'), + red: C('31'), + green: C('32'), + yellow: C('33'), + blue: C('34'), + magenta: C('35'), + cyan: C('36'), + white: C('37') +}; + const logColor = color => (literals, ...values) => { const w = s => process.stdout.write(s); - w(`\x1b[${color}m`); + const wc = text => { + if (ENABLE_COLOR) w(text); + }; + + wc(`\x1b[${color}m`); for (let i = 0; i < literals.length; i++) { w(literals[i]); if (values[i] !== undefined) { - w(`\x1b[1m`); + wc(`\x1b[1m`); w(String(values[i])); - w(`\x1b[0;${color}m`); + wc(`\x1b[0;${color}m`); } } - w(`\x1b[0m\n`); + wc(`\x1b[0m`); + w('\n'); }; export const logInfo = logColor(2); diff --git a/src/util/sugar.js b/src/util/sugar.js index 38c8047f..64291f36 100644 --- a/src/util/sugar.js +++ b/src/util/sugar.js @@ -6,6 +6,8 @@ // It will likely only do exactly what I want it to, and only in the cases I // decided were relevant enough to 8other handling. +import { color } from './cli.js'; + // 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 @@ -133,10 +135,25 @@ export function openAggregate({ } }; + aggregate.wrapAsync = fn => (...args) => { + return fn(...args).then( + value => value, + error => { + errors.push(error); + return (typeof returnOnFail === 'function' + ? returnOnFail(...args) + : returnOnFail); + }); + }; + aggregate.call = (fn, ...args) => { return aggregate.wrap(fn)(...args); }; + aggregate.callAsync = (fn, ...args) => { + return aggregate.wrapAsync(fn)(...args); + }; + aggregate.nest = (...args) => { return aggregate.call(() => withAggregate(...args)); }; @@ -183,6 +200,19 @@ export function aggregateThrows(errorClass) { // use aggregate.close() to throw the error. (This aggregate may be passed to a // parent aggregate: `parent.call(aggregate.close)`!) export function mapAggregate(array, fn, aggregateOpts) { + return _mapAggregate('sync', null, array, fn, aggregateOpts); +} + +export function mapAggregateAsync(array, fn, { + promiseAll = Promise.all, + ...aggregateOpts +} = {}) { + return _mapAggregate('async', promiseAll, array, fn, aggregateOpts); +} + +// Helper function for mapAggregate which holds code common between sync and +// async versions. +export function _mapAggregate(mode, promiseAll, array, fn, aggregateOpts) { const failureSymbol = Symbol(); const aggregate = openAggregate({ @@ -190,10 +220,16 @@ export function mapAggregate(array, fn, aggregateOpts) { ...aggregateOpts }); - const result = array.map(aggregate.wrap(fn)) - .filter(value => value !== failureSymbol); - - return {result, aggregate}; + if (mode === 'sync') { + const result = array.map(aggregate.wrap(fn)) + .filter(value => value !== failureSymbol); + return {result, aggregate}; + } else { + return promiseAll(array.map(aggregate.wrapAsync(fn))).then(values => { + const result = values.filter(value => value !== failureSymbol); + return {result, aggregate}; + }); + } } // Performs an ordinary array filter with the given function, collating into a @@ -204,6 +240,19 @@ export function mapAggregate(array, fn, aggregateOpts) { // // As with mapAggregate, the returned aggregate property is not yet closed. export function filterAggregate(array, fn, aggregateOpts) { + return _filterAggregate('sync', null, array, fn, aggregateOpts); +} + +export async function filterAggregateAsync(array, fn, { + promiseAll = Promise.all, + ...aggregateOpts +} = {}) { + return _filterAggregate('async', promiseAll, array, fn, aggregateOpts); +} + +// Helper function for filterAggregate which holds code common between sync and +// async versions. +function _filterAggregate(mode, promiseAll, array, fn, aggregateOpts) { const failureSymbol = Symbol(); const aggregate = openAggregate({ @@ -211,32 +260,57 @@ export function filterAggregate(array, fn, aggregateOpts) { ...aggregateOpts }); - const result = array.map(aggregate.wrap((x, ...rest) => ({ - input: x, - output: fn(x, ...rest) - }))) - .filter(value => { - // Filter out results which match the failureSymbol, i.e. errored - // inputs. - if (value === failureSymbol) return false; - - // Always keep results which match the overridden returnOnFail - // value, if provided. - if (value === aggregateOpts.returnOnFail) return true; - - // Otherwise, filter according to the returned value of the wrapped - // function. - return value.output; - }) - .map(value => { - // Then turn the results back into their corresponding input, or, if - // provided, the overridden returnOnFail value. - return (value === aggregateOpts.returnOnFail - ? value - : value.input); - }); + function filterFunction(value) { + // Filter out results which match the failureSymbol, i.e. errored + // inputs. + if (value === failureSymbol) return false; - return {result, aggregate}; + // Always keep results which match the overridden returnOnFail + // value, if provided. + if (value === aggregateOpts.returnOnFail) return true; + + // Otherwise, filter according to the returned value of the wrapped + // function. + return value.output; + } + + function mapFunction(value) { + // Then turn the results back into their corresponding input, or, if + // provided, the overridden returnOnFail value. + return (value === aggregateOpts.returnOnFail + ? value + : value.input); + } + + function wrapperFunction(x, ...rest) { + return { + input: x, + output: fn(x, ...rest) + }; + } + + if (mode === 'sync') { + const result = array + .map(aggregate.wrap((input, index, array) => { + const output = fn(input, index, array); + return {input, output}; + })) + .filter(filterFunction) + .map(mapFunction); + + return {result, aggregate}; + } else { + return promiseAll(array.map(aggregate.wrapAsync(async (input, index, array) => { + const output = await fn(input, index, array); + return {input, output}; + }))).then(values => { + const result = values + .filter(filterFunction) + .map(mapFunction); + + return {result, aggregate}; + }); + } } // Totally sugar function for opening an aggregate, running the provided @@ -256,7 +330,17 @@ export function withAggregate(aggregateOpts, fn) { export function showAggregate(topError) { const recursive = error => { - const header = `[${error.constructor.name || 'unnamed'}] ${error.message || '(no message)'}`; + const stackLines = error.stack?.split('\n'); + const stackLine = stackLines?.find(line => + line.trim().startsWith('at') + && !line.includes('sugar') + && !line.includes('node:internal')); + const tracePart = (stackLine + ? '- ' + stackLine.trim() + : '(no stack trace)'); + + const header = `[${error.constructor.name || 'unnamed'}] ${error.message || '(no message)'} ${color.dim(tracePart)}`; + if (error instanceof AggregateError) { return header + '\n' + (error.errors .map(recursive) @@ -268,5 +352,5 @@ export function showAggregate(topError) { } }; - console.log(recursive(topError)); + console.error(recursive(topError)); } |