« get me outta code hell

sort.js « build-modes « write « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write/build-modes/sort.js
blob: 0b759c45141ad28b242193b9a34ec97018ae5983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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,
  },

  languageReloading: {
    applicable: false,
  },

  mediaValidation: {
    applicable: false,
  },

  search: {
    applicable: false,
  },

  thumbs: {
    applicable: false,
  },

  webRoutes: {
    applicable: false,
  },
};

export function getCLIOptions() {
  return {};
}

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) {
    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;
}