aboutsummaryrefslogtreecommitdiffhomepage
path: root/webpack/configDiffPlugin.js
blob: ce5e5c5a4ffc8ee5780515c5cddb325bdad4b0ee (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
/* eslint-disable @typescript-eslint/no-var-requires */
const { readFile } = require("fs/promises")
let logger;

const readFileContents = (name) => readFile(name)
  .then(data => JSON.parse(data))

// partialDeepEquals from ajayyy/SponsorBlockServer
function partialDeepEquals (actual, expected, logger) {
  // loop over key, value of expected
  let failed = false;
  for (const [ key, value ] of Object.entries(expected)) {
    if (key === "serverAddress" || key === "testingServerAddress" || key === "serverAddressComment" || key === "freeChapterAccess") continue
    // if value is object, recurse
    const actualValue = actual?.[key]
    if (typeof value !== "string" && Array.isArray(value)) {
      if (!arrayPartialDeepEquals(actualValue, value)) {
        printActualExpected(key, actualValue, value, logger)
        failed = true
      }
    } else if (typeof value === "object") {
      if (partialDeepEquals(actualValue, value, logger)) {
        console.log("obj failed")
        printActualExpected(key, actualValue, value, logger)
        failed = true
      }
    } else if (actualValue !== value) {
      printActualExpected(key, actualValue, value, logger)
      failed = true
    }
  }
  return failed
}

const arrayPartialDeepEquals = (actual, expected) =>
  expected.every(a => actual?.includes(a))

function printActualExpected(key, actual, expected, logger) {
  logger.error(`Differing value for: ${key}`)
  logger.error(`Actual: ${JSON.stringify(actual)}`)
  logger.error(`Expected: ${JSON.stringify(expected)}`)
}

class configDiffPlugin {
  apply(compiler) {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    compiler.hooks.done.tapAsync("configDiffPlugin", async (stats, callback) => {
      logger = compiler.getInfrastructureLogger('configDiffPlugin')
      logger.log('Checking for config.json diff...')
      
      // check example
      const exampleConfig = await readFileContents("./config.json.example")
      const currentConfig = await readFileContents("./config.json")

      const difference = partialDeepEquals(currentConfig, exampleConfig, logger)
      if (difference) {
        logger.warn("config.json is missing values from config.json.example")
      } else {
        logger.info("config.json is not missing any values from config.json.example")
      }
      callback()
    })
  }
}

module.exports = configDiffPlugin;