From 20816d509db3504760ece1c0b7db00e1bef01273 Mon Sep 17 00:00:00 2001
From: Alistair McKinlay
+ The Lounge is up to date, but there are out of date packages
+
+ Run thelounge upgrade
on the server to upgrade packages.
+
The Lounge is up to date! diff --git a/src/command-line/index.js b/src/command-line/index.js index c8a835fd..d91abcc2 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -60,6 +60,7 @@ if (!Helper.config.public && !Helper.config.ldap.enable) { require("./install"); require("./uninstall"); require("./upgrade"); +require("./outdated"); // `parse` expects to be passed `process.argv`, but we need to remove to give it // a version of `argv` that does not contain options already parsed by diff --git a/src/command-line/outdated.js b/src/command-line/outdated.js new file mode 100644 index 00000000..72c561d4 --- /dev/null +++ b/src/command-line/outdated.js @@ -0,0 +1,27 @@ +"use strict"; + +const program = require("commander"); +const Utils = require("./utils"); +const packageManager = require("../plugins/packages"); +const log = require("../log"); + +program + .command("outdated") + .description("Check for any outdated packages") + .on("--help", Utils.extraHelp) + .action(async () => { + log.info("Checking for outdated packages"); + + await packageManager + .outdated(0) + .then((outdated) => { + if (outdated) { + log.info("There are outdated packages"); + } else { + log.info("No outdated packages"); + } + }) + .catch(() => { + log.error("Error finding outdated packages."); + }); + }); diff --git a/src/plugins/packages/index.js b/src/plugins/packages/index.js index 1c5b4f64..01d37436 100644 --- a/src/plugins/packages/index.js +++ b/src/plugins/packages/index.js @@ -7,13 +7,22 @@ const Helper = require("../../helper"); const themes = require("./themes"); const packageMap = new Map(); const inputs = require("../inputs"); +const fs = require("fs"); +const Utils = require("../../command-line/utils"); const stylesheets = []; +const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds + +const cache = { + outdated: undefined, +}; + module.exports = { getStylesheets, getPackage, loadPackages, + outdated, }; const packageApis = function(packageName) { @@ -99,3 +108,46 @@ function loadPackages() { ); } } + +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; +} diff --git a/src/server.js b/src/server.js index 9f436c62..498bf38c 100644 --- a/src/server.js +++ b/src/server.js @@ -454,9 +454,13 @@ function initializeClient(socket, client, token, lastMessage) { } }); - socket.on("changelog", async () => { - const data = await changelog.fetch(); - socket.emit("changelog", data); + socket.on("changelog", () => { + Promise.all([changelog.fetch(), packages.outdated()]).then( + ([changelogData, packageUpdate]) => { + changelogData.packages = packageUpdate; + socket.emit("changelog", changelogData); + } + ); }); socket.on("msg:preview:toggle", (data) => {