diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-02-22 17:18:48 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-02-22 21:12:24 -0400 |
commit | e0bf4a7ae3a6ec2f2278f0a62efac45466397803 (patch) | |
tree | 04f6c10f16be8e39eb90d0d32df375a8cd44a220 /src | |
parent | 6820bb9b53d022394fc350c4f6307aa141954290 (diff) |
data, write: nice sorting rule messaging, etc
Diffstat (limited to 'src')
-rw-r--r-- | src/data/things/sorting-rule.js | 49 | ||||
-rw-r--r-- | src/write/build-modes/sort.js | 38 |
2 files changed, 75 insertions, 12 deletions
diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js index 890a798b..d21781e2 100644 --- a/src/data/things/sorting-rule.js +++ b/src/data/things/sorting-rule.js @@ -4,6 +4,7 @@ import {readFile, writeFile} from 'node:fs/promises'; import * as path from 'node:path'; import {input} from '#composite'; +import {compareArrays} from '#sugar'; import Thing from '#thing'; import {isStringNonEmpty, strictArrayOf} from '#validators'; @@ -30,10 +31,16 @@ export class SortingRule extends Thing { // Update & expose active: flag(true), + + message: { + flags: {update: true, expose: true}, + update: {validate: isStringNonEmpty}, + }, }); static [Thing.yamlDocumentSpec] = { fields: { + 'Message': {property: 'message'}, 'Active': {property: 'active'}, }, }; @@ -127,8 +134,18 @@ export class DocumentSortingRule extends ThingSortingRule { // TODO: glob :plead: filename: { flags: {update: true, expose: true}, - update: { - validate: isStringNonEmpty, + update: {validate: isStringNonEmpty}, + }, + + message: { + flags: {update: true, expose: true}, + update: {validate: isStringNonEmpty}, + + expose: { + dependencies: ['filename'], + transform: (value, {filename}) => + value ?? + `Sort ${filename}`, }, }, }); @@ -139,24 +156,34 @@ export class DocumentSortingRule extends ThingSortingRule { }, }); - async apply({wikiData, dataPath}) { - let layout = getThingLayoutForFilename(this.filename, wikiData); - if (!layout) return; + check({wikiData}) { + const oldLayout = getThingLayoutForFilename(this.filename, wikiData); + if (!oldLayout) return; + + const newLayout = this.#processLayout(oldLayout); - layout = this.#processLayout(layout); + const oldOrder = flattenThingLayoutToDocumentOrder(oldLayout); + const newOrder = flattenThingLayoutToDocumentOrder(newLayout); - const order = flattenThingLayoutToDocumentOrder(layout); + return compareArrays(oldOrder, newOrder); + } + + async apply({wikiData, dataPath}) { + const oldLayout = getThingLayoutForFilename(this.filename, wikiData); + if (!oldLayout) return; + + const newLayout = this.#processLayout(oldLayout); + const newOrder = flattenThingLayoutToDocumentOrder(newLayout); const realPath = path.join( dataPath, this.filename.split(path.posix.sep).join(path.sep)); - let sourceText = await readFile(realPath, 'utf8'); - - sourceText = reorderDocumentsInYAMLSourceText(sourceText, order); + const oldSourceText = await readFile(realPath, 'utf8'); + const newSourceText = reorderDocumentsInYAMLSourceText(oldSourceText, newOrder); - await writeFile(realPath, sourceText); + await writeFile(realPath, newSourceText); } #processLayout(layout) { diff --git a/src/write/build-modes/sort.js b/src/write/build-modes/sort.js index c444d295..0b759c45 100644 --- a/src/write/build-modes/sort.js +++ b/src/write/build-modes/sort.js @@ -1,5 +1,8 @@ export const description = `Update data files in-place to satisfy custom sorting rules`; +import {logInfo} from '#cli'; +import {empty} from '#sugar'; + export const config = { fileSizes: { applicable: false, @@ -31,7 +34,40 @@ export function getCLIOptions() { } export async function go({wikiData, dataPath}) { + if (empty(wikiData.sortingRules)) { + logInfo`There aren't any sorting rules in for this wiki.`; + return true; + } + + let numUpdated = 0; + let numActive = 0; + for (const sortingRule of wikiData.sortingRules) { - await sortingRule.apply({wikiData, dataPath}); + if (!sortingRule.active) continue; + + numActive++; + + const niceMessage = `"${sortingRule.message}"`; + + if (sortingRule.check({wikiData})) { + logInfo`Already good: ${niceMessage}`; + } else { + logInfo`Updating to satisfy ${niceMessage}.`; + await sortingRule.apply({wikiData, dataPath}); + + numUpdated++; + } } + + if (numUpdated > 1) { + logInfo`Updated data files to satisfy ${numUpdated} sorting rules.`; + } else if (numUpdated === 1) { + logInfo`Updated data files to satisfy ${1} sorting rule.` + } else if (numActive >= 1) { + logInfo`All sorting rules were already satisfied. Good to go!`; + } else { + logInfo`No sorting rules are currently active.`; + } + + return true; } |