From bf8003891c404365a34108e22004cea3be54268e Mon Sep 17 00:00:00 2001 From: Michael C Date: Fri, 7 Oct 2022 17:12:16 -0400 Subject: [PATCH] add configDiffPlugin --- webpack/configDiffPlugin.js | 66 +++++++++++++++++++++++++++++++++++++ webpack/webpack.common.js | 4 ++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 webpack/configDiffPlugin.js diff --git a/webpack/configDiffPlugin.js b/webpack/configDiffPlugin.js new file mode 100644 index 00000000..43456e87 --- /dev/null +++ b/webpack/configDiffPlugin.js @@ -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; \ No newline at end of file diff --git a/webpack/webpack.common.js b/webpack/webpack.common.js index d05dea03..0be6c63c 100644 --- a/webpack/webpack.common.js +++ b/webpack/webpack.common.js @@ -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() ] });