This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/site-settings-plugin.js
Martin Brennan ac7bf98ad1
DEV: Load client site settings YML into JS tests (#18413)
Our method of loading a subset of client settings into tests via
tests/helpers/site-settings.js can be improved upon. Currently we have a
hardcoded subset of the client settings, which may get out of date and not have
the correct defaults. As well as this plugins do not get their settings into the
tests, so whenever you need a setting from a plugin, even if it has a default,
you have to do needs.setting({ ... }) which is inconvenient.

This commit introduces an ember CLI build step to take the site_settings.yml and
all the plugin settings.yml files, pull out the client settings, and dump them
into a variable in a single JS file we can load in our tests, so we have the
correct selection of settings and default values in our JS tests. It also fixes
many, many tests that were operating under incorrect assumptions or old
settings.

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2022-11-08 09:17:43 +10:00

97 lines
2.5 KiB
JavaScript

const Plugin = require("broccoli-plugin");
const Yaml = require("js-yaml");
const fs = require("fs");
const concat = require("broccoli-concat");
const mergeTrees = require("broccoli-merge-trees");
const deepmerge = require("deepmerge");
const glob = require("glob");
const { shouldLoadPluginTestJs } = require("discourse/lib/plugin-js");
let built = false;
class SiteSettingsPlugin extends Plugin {
constructor(inputNodes, inputFile, options) {
super(inputNodes, {
...options,
persistentOutput: true,
});
}
build() {
if (built) {
return;
}
let parsed = {};
this.inputPaths.forEach((path) => {
let inputFile;
if (path.includes("plugins")) {
inputFile = "settings.yml";
} else {
inputFile = "site_settings.yml";
}
const file = path + "/" + inputFile;
let yaml;
try {
yaml = fs.readFileSync(file, { encoding: "UTF-8" });
} catch (err) {
// the plugin does not have a config file, go to the next file
return;
}
const loaded = Yaml.load(yaml, { json: true });
parsed = deepmerge(parsed, loaded);
});
let clientSettings = {};
// eslint-disable-next-line no-unused-vars
for (const [category, settings] of Object.entries(parsed)) {
for (const [setting, details] of Object.entries(settings)) {
if (details.client) {
clientSettings[setting] = details.default;
}
}
}
const contents = `var CLIENT_SITE_SETTINGS_WITH_DEFAULTS = ${JSON.stringify(
clientSettings
)}`;
fs.writeFileSync(`${this.outputPath}/` + "settings_out.js", contents);
built = true;
}
}
module.exports = function siteSettingsPlugin(...params) {
return new SiteSettingsPlugin(...params);
};
module.exports.parsePluginClientSettings = function (
discourseRoot,
vendorJs,
app
) {
let settings = [discourseRoot + "/config"];
if (shouldLoadPluginTestJs()) {
const pluginInfos = app.project
.findAddonByName("discourse-plugins")
.pluginInfos();
pluginInfos.forEach(({ hasConfig, configDirectory }) => {
if (hasConfig) {
settings = settings.concat(glob.sync(configDirectory));
}
});
}
const loadedSettings = new SiteSettingsPlugin(settings, "site_settings.yml");
return concat(mergeTrees([loadedSettings]), {
inputFiles: [],
headerFiles: [],
footerFiles: [],
outputFile: `assets/test-site-settings.js`,
});
};
module.exports.SiteSettingsPlugin = SiteSettingsPlugin;