add configDiffPlugin

This commit is contained in:
Michael C 2022-10-07 17:12:16 -04:00
parent bb7f069254
commit bf8003891c
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
2 changed files with 69 additions and 1 deletions

View file

@ -0,0 +1,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") 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;

View file

@ -7,6 +7,7 @@ const BuildManifest = require('./webpack.manifest');
const srcDir = '../src/';
const fs = require("fs");
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const configDiffPlugin = require('./configDiffPlugin');
const edgeLanguages = [
"de",
@ -116,6 +117,7 @@ module.exports = env => ({
browser: env.browser,
pretty: env.mode === "production",
stream: env.stream
})
}),
new configDiffPlugin()
]
});