thelounge/src/plugins/packages/index.js

235 lines
5.7 KiB
JavaScript
Raw Normal View History

2018-01-05 18:40:34 +01:00
"use strict";
const _ = require("lodash");
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");
2020-08-21 13:26:35 +02:00
const semver = require("semver");
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 = [];
2019-10-02 11:28:08 +02:00
const files = [];
2018-01-05 18:40:34 +01:00
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
const cache = {
outdated: undefined,
};
let experimentalWarningPrinted = false;
2018-01-05 18:40:34 +01:00
module.exports = {
2019-10-02 11:28:08 +02:00
getFiles,
2018-01-05 18:40:34 +01:00
getStylesheets,
getPackage,
loadPackages,
outdated,
2018-01-05 18:40:34 +01:00
};
const packageApis = function (packageInfo) {
2018-01-05 18:40:34 +01:00
return {
Stylesheets: {
addFile: addStylesheet.bind(this, packageInfo.packageName),
2018-01-05 18:40:34 +01:00
},
2019-10-02 11:28:08 +02:00
PublicFiles: {
add: addFile.bind(this, packageInfo.packageName),
2019-10-02 11:28:08 +02:00
},
2019-07-02 18:02:02 +02:00
Commands: {
add: inputs.addPluginCommand.bind(this, packageInfo),
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,
getPersistentStorageDir: getPersistentStorageDir.bind(this, packageInfo.packageName),
2018-03-12 10:46:28 +01:00
},
Logger: {
error: (...args) => log.error(`[${packageInfo.packageName}]`, ...args),
warn: (...args) => log.warn(`[${packageInfo.packageName}]`, ...args),
info: (...args) => log.info(`[${packageInfo.packageName}]`, ...args),
debug: (...args) => log.debug(`[${packageInfo.packageName}]`, ...args),
},
2018-01-05 18:40:34 +01:00
};
};
function addStylesheet(packageName, filename) {
stylesheets.push(packageName + "/" + filename);
}
function getStylesheets() {
return stylesheets;
}
2019-10-02 11:28:08 +02:00
function addFile(packageName, filename) {
files.push(packageName + "/" + filename);
}
function getFiles() {
return files.concat(stylesheets);
}
2018-01-05 18:40:34 +01:00
function getPackage(name) {
return packageMap.get(name);
}
function getEnabledPackages(packageJson) {
try {
const json = JSON.parse(fs.readFileSync(packageJson, "utf-8"));
return Object.keys(json.dependencies);
} catch (e) {
log.error(`Failed to read packages/package.json: ${colors.red(e)}`);
}
2018-02-05 07:30:57 +01:00
return [];
}
2018-02-05 07:30:57 +01:00
function getPersistentStorageDir(packageName) {
const dir = path.join(Helper.getPackagesPath(), packageName);
fs.mkdirSync(dir, {recursive: true}); // we don't care if it already exists or not
return dir;
}
function loadPackage(packageName) {
let packageInfo;
let packageFile;
2018-02-05 07:30:57 +01:00
try {
const packagePath = Helper.getPackageModulePath(packageName);
packageInfo = JSON.parse(fs.readFileSync(path.join(packagePath, "package.json"), "utf-8"));
if (!packageInfo.thelounge) {
throw "'thelounge' is not present in package.json";
}
2018-02-05 07:30:57 +01:00
2020-08-21 13:26:35 +02:00
if (
packageInfo.thelounge.supports &&
!semver.satisfies(Helper.getVersionNumber(), packageInfo.thelounge.supports)
) {
throw `v${packageInfo.version} does not support this version of The Lounge. Supports: ${packageInfo.thelounge.supports}`;
}
packageFile = require(packagePath);
} catch (e) {
log.error(`Package ${colors.bold(packageName)} could not be loaded: ${colors.red(e)}`);
log.debug(e.stack);
return;
}
const version = packageInfo.version;
packageInfo = packageInfo.thelounge;
packageInfo.packageName = packageName;
packageInfo.version = version;
2018-01-05 18:40:34 +01:00
packageMap.set(packageName, packageFile);
2019-10-02 11:28:08 +02:00
if (packageInfo.type === "theme") {
themes.addTheme(packageName, packageInfo);
2018-02-05 07:30:57 +01:00
if (packageInfo.files) {
packageInfo.files.forEach((file) => addFile(packageName, file));
}
}
if (packageFile.onServerStart) {
packageFile.onServerStart(packageApis(packageInfo));
}
log.info(`Package ${colors.bold(packageName)} ${colors.green("v" + version)} loaded`);
2018-02-05 07:30:57 +01:00
if (packageInfo.type !== "theme" && !experimentalWarningPrinted) {
experimentalWarningPrinted = true;
2019-07-05 09:26:22 +02:00
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-17 11:33:59 +02:00
);
2019-07-05 09:26:22 +02:00
}
2018-01-05 18:40:34 +01:00
}
function loadPackages() {
const packageJson = path.join(Helper.getPackagesPath(), "package.json");
const packages = getEnabledPackages(packageJson);
packages.forEach(loadPackage);
watchPackages(packageJson);
}
function watchPackages(packageJson) {
fs.watch(
packageJson,
{
persistent: false,
},
_.debounce(
() => {
const updated = getEnabledPackages(packageJson);
for (const packageName of updated) {
if (packageMap.has(packageName)) {
continue;
}
loadPackage(packageName);
}
},
1000,
{maxWait: 10000}
)
);
}
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");
2020-01-01 00:11:04 +01:00
const packagesList = JSON.parse(fs.readFileSync(packagesConfig, "utf-8")).dependencies;
const argsList = [
"outdated",
"--latest",
"--json",
"--production",
"--ignore-scripts",
"--non-interactive",
"--cwd",
packagesPath,
];
// Check if the configuration file exists
if (!Object.entries(packagesList).length) {
// CLI calls outdated with zero TTL, so we can print the warning there
if (!cacheTimeout) {
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;
}