diff options
| author | (quasar) nebula <qznebula@protonmail.com> | 2026-04-12 12:14:23 -0300 |
|---|---|---|
| committer | (quasar) nebula <qznebula@protonmail.com> | 2026-04-12 12:15:04 -0300 |
| commit | 0d2481ed8abdb084f5c10984181c2a7355d5208a (patch) | |
| tree | f039a8ef4def20bb8ac9202bdc85a94dfaec3dbb /src/write/tidy-modes/sort.js | |
| parent | 5fcd8ce38402c6623b57a5dc846c9786a24644f1 (diff) | |
upd8, etc: curated url validation, tidying modes, --format-urls
Diffstat (limited to 'src/write/tidy-modes/sort.js')
| -rw-r--r-- | src/write/tidy-modes/sort.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/write/tidy-modes/sort.js b/src/write/tidy-modes/sort.js new file mode 100644 index 00000000..967a5be1 --- /dev/null +++ b/src/write/tidy-modes/sort.js @@ -0,0 +1,61 @@ +export const description = `Update data files in-place to satisfy custom sorting rules`; + +import {logInfo} from '#cli'; +import {empty} from '#sugar'; +import thingConstructors from '#things'; + +export async function go({ + wikiData, + dataPath, + tidyingOnly, +}) { + if (empty(wikiData.sortingRules)) { + if (tidyingOnly) { + logInfo`There aren't any sorting rules in for this wiki.`; + } + + return 'clean'; + } + + const {SortingRule} = thingConstructors; + + if (!tidyingOnly) { + const results = + await Array.fromAsync(SortingRule.go({dataPath, wikiData})); + + if (results.some(result => result.changed)) { + logInfo`Updated data files to satisfy sorting.`; + return 'updated'; + } else { + logInfo`All sorting rules are satisfied - nice!`; + return 'clean'; + } + } + + let numUpdated = 0; + let numActive = 0; + + for await (const result of SortingRule.go({wikiData, dataPath})) { + numActive++; + + const niceMessage = `"${result.rule.message}"`; + + if (result.changed) { + numUpdated++; + logInfo`Updated to satisfy ${niceMessage}.`; + } else { + logInfo`Already good: ${niceMessage}`; + } + } + + if (numUpdated > 1) { + logInfo`Updated data files to satisfy ${numUpdated} sorting rules.`; + return 'updated'; + } else if (numUpdated === 1) { + logInfo`Updated data files to satisfy ${1} sorting rule.` + return 'updated'; + } else { + logInfo`All sorting rules were already satisfied. Good to go!`; + return 'clean'; + } +} |