thelounge/src/plugins/packages/index.js

154 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-01-05 18:40:34 +01:00
"use strict";
2018-06-15 22:31:06 +02:00
const log = require("../../log");
2018-03-02 19:28:54 +01:00
const colors = require("chalk");
const path = require("path");
2018-01-05 18:40:34 +01:00
const Helper = require("../../helper");
const themes = require("./themes");
const packageMap = new Map();
2019-07-02 18:02:02 +02:00
const inputs = require("../inputs");
const fs = require("fs");
const Utils = require("../../command-line/utils");
2018-01-05 18:40:34 +01:00
const stylesheets = [];
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
const cache = {
outdated: undefined,
};
2018-01-05 18:40:34 +01:00
module.exports = {
getStylesheets,
getPackage,
loadPackages,
outdated,
2018-01-05 18:40:34 +01:00
};
const packageApis = function(packageName) {
2018-01-05 18:40:34 +01:00
return {
Stylesheets: {
addFile: addStylesheet.bind(this, packageName),
},
2019-07-02 18:02:02 +02:00
Commands: {
add: inputs.addPluginCommand,
2019-07-17 11:33:59 +02:00
runAsUser: (command, targetId, client) =>
client.inputLine({target: targetId, text: command}),
2019-07-02 18:02:02 +02:00
},
2018-03-12 10:46:28 +01:00
Config: {
getConfig: () => Helper.config,
},
2018-01-05 18:40:34 +01:00
};
};
function addStylesheet(packageName, filename) {
stylesheets.push(packageName + "/" + filename);
}
function getStylesheets() {
return stylesheets;
}
function getPackage(name) {
return packageMap.get(name);
}
function loadPackages() {
const packageJson = path.join(Helper.getPackagesPath(), "package.json");
let packages;
2019-07-05 09:26:22 +02:00
let anyPlugins = false;
2018-02-05 07:30:57 +01:00
try {
packages = Object.keys(require(packageJson).dependencies);
} catch (e) {
packages = [];
}
2018-02-05 07:30:57 +01:00
packages.forEach((packageName) => {
const errorMsg = `Package ${colors.bold(packageName)} could not be loaded`;
let packageInfo;
let packageFile;
2018-02-05 07:30:57 +01:00
try {
2019-07-17 11:33:59 +02:00
packageInfo = require(path.join(
Helper.getPackageModulePath(packageName),
"package.json"
));
packageFile = require(Helper.getPackageModulePath(packageName));
} catch (e) {
log.warn(errorMsg);
return;
}
2018-02-05 07:30:57 +01:00
if (!packageInfo.thelounge) {
log.warn(errorMsg);
return;
}
2018-02-05 07:30:57 +01:00
packageInfo = packageInfo.thelounge;
packageMap.set(packageName, packageFile);
2018-01-05 18:40:34 +01:00
if (packageInfo.type === "theme") {
themes.addTheme(packageName, packageInfo);
2019-07-05 09:26:22 +02:00
} else {
anyPlugins = true;
}
2018-02-05 07:30:57 +01:00
if (packageFile.onServerStart) {
packageFile.onServerStart(packageApis(packageName));
}
2018-02-05 07:30:57 +01:00
log.info(`Package ${colors.bold(packageName)} loaded`);
});
2019-07-05 09:26:22 +02:00
if (anyPlugins) {
2019-07-17 11:33:59 +02:00
log.info(
"There are packages using the experimental plugin API. Be aware that this API is not yet stable and may change in future The Lounge releases."
);
2019-07-05 09:26:22 +02:00
}
2018-01-05 18:40:34 +01:00
}
async function outdated(cacheTimeout = TIME_TO_LIVE) {
if (cache.outdated !== undefined) {
return cache.outdated;
}
// Get paths to the location of packages directory
const packagesPath = Helper.getPackagesPath();
const packagesConfig = path.join(packagesPath, "package.json");
const argsList = [
"outdated",
"--latest",
"--json",
"--production",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
];
// Check if the configuration file exists
if (!fs.existsSync(packagesConfig)) {
log.warn("There are no packages installed.");
return false;
}
// If we get an error from calling outdated and the code isn't 0, then there are no outdated packages
await Utils.executeYarnCommand(...argsList)
.then(() => updateOutdated(false))
.catch((code) => updateOutdated(code !== 0));
if (cacheTimeout > 0) {
setTimeout(() => {
delete cache.outdated;
}, cacheTimeout);
}
return cache.outdated;
}
function updateOutdated(outdatedPackages) {
cache.outdated = outdatedPackages;
}