hardlounge/src/plugins/packages/index.js

214 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-01-05 17:40:34 +00:00
"use strict";
const _ = require("lodash");
2018-06-15 20:31:06 +00:00
const log = require("../../log");
2018-03-02 18:28:54 +00:00
const colors = require("chalk");
const path = require("path");
2018-01-05 17:40:34 +00:00
const Helper = require("../../helper");
const themes = require("./themes");
const packageMap = new Map();
2019-07-02 16:02:02 +00:00
const inputs = require("../inputs");
const fs = require("fs");
const Utils = require("../../command-line/utils");
2018-01-05 17:40:34 +00:00
const stylesheets = [];
2019-10-02 09:28:08 +00:00
const files = [];
2018-01-05 17:40:34 +00:00
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
const cache = {
outdated: undefined,
};
let experimentalWarningPrinted = false;
2018-01-05 17:40:34 +00:00
module.exports = {
2019-10-02 09:28:08 +00:00
getFiles,
2018-01-05 17:40:34 +00:00
getStylesheets,
getPackage,
loadPackages,
outdated,
2018-01-05 17:40:34 +00:00
};
const packageApis = function(packageInfo) {
2018-01-05 17:40:34 +00:00
return {
Stylesheets: {
addFile: addStylesheet.bind(this, packageInfo.packageName),
2018-01-05 17:40:34 +00:00
},
2019-10-02 09:28:08 +00:00
PublicFiles: {
add: addFile.bind(this, packageInfo.packageName),
2019-10-02 09:28:08 +00:00
},
2019-07-02 16:02:02 +00:00
Commands: {
add: inputs.addPluginCommand.bind(this, packageInfo),
2019-07-17 09:33:59 +00:00
runAsUser: (command, targetId, client) =>
client.inputLine({target: targetId, text: command}),
2019-07-02 16:02:02 +00:00
},
2018-03-12 09:46:28 +00:00
Config: {
getConfig: () => Helper.config,
},
2018-01-05 17:40:34 +00:00
};
};
function addStylesheet(packageName, filename) {
stylesheets.push(packageName + "/" + filename);
}
function getStylesheets() {
return stylesheets;
}
2019-10-02 09:28:08 +00:00
function addFile(packageName, filename) {
files.push(packageName + "/" + filename);
}
function getFiles() {
return files.concat(stylesheets);
}
2018-01-05 17:40:34 +00: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 06:30:57 +00:00
return [];
}
2018-02-05 06:30:57 +00:00
function loadPackage(packageName) {
let packageInfo;
let packageFile;
2018-02-05 06:30:57 +00: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 06:30:57 +00:00
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 17:40:34 +00:00
packageMap.set(packageName, packageFile);
2019-10-02 09:28:08 +00:00
if (packageInfo.type === "theme") {
themes.addTheme(packageName, packageInfo);
2018-02-05 06:30:57 +00: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 06:30:57 +00:00
if (packageInfo.type !== "theme" && !experimentalWarningPrinted) {
experimentalWarningPrinted = true;
2019-07-05 07:26:22 +00:00
2019-07-17 09:33:59 +00: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 09:33:59 +00:00
);
2019-07-05 07:26:22 +00:00
}
2018-01-05 17:40:34 +00: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");
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;
}