mirror of
https://github.com/discourse/discourse.git
synced 2025-02-12 02:11:44 +08:00
![David Taylor](/assets/img/avatar_default.png)
Our ember-cli config now follows the same behavior as Discourse core. LOAD_PLUGINS=0 will prevent any plugin assets from being compiled/served.
97 lines
2.5 KiB
JavaScript
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 { shouldLoadPlugins } = require("discourse-plugins");
|
|
|
|
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 (shouldLoadPlugins()) {
|
|
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;
|