2018-01-05 17:40:34 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
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");
|
2018-02-08 05:19:44 +00:00
|
|
|
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");
|
2019-07-04 07:41:09 +00:00
|
|
|
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
|
|
|
|
2019-07-04 07:41:09 +00:00
|
|
|
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
|
|
|
|
|
|
|
|
const cache = {
|
|
|
|
outdated: undefined,
|
|
|
|
};
|
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
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,
|
2019-07-04 07:41:09 +00:00
|
|
|
outdated,
|
2018-01-05 17:40:34 +00:00
|
|
|
};
|
|
|
|
|
2019-10-22 18:03:54 +00:00
|
|
|
const packageApis = function(packageInfo) {
|
2018-01-05 17:40:34 +00:00
|
|
|
return {
|
|
|
|
Stylesheets: {
|
2019-10-22 18:03:54 +00:00
|
|
|
addFile: addStylesheet.bind(this, packageInfo.packageName),
|
2018-01-05 17:40:34 +00:00
|
|
|
},
|
2019-10-02 09:28:08 +00:00
|
|
|
PublicFiles: {
|
2019-10-22 18:03:54 +00:00
|
|
|
add: addFile.bind(this, packageInfo.packageName),
|
2019-10-02 09:28:08 +00:00
|
|
|
},
|
2019-07-02 16:02:02 +00:00
|
|
|
Commands: {
|
2019-10-22 18:03:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
function getEnabledPackages(packageJson) {
|
2018-02-08 05:19:44 +00:00
|
|
|
try {
|
2019-12-13 18:17:14 +00:00
|
|
|
const json = JSON.parse(fs.readFileSync(packageJson, "utf-8"));
|
|
|
|
return Object.keys(json.dependencies);
|
2018-02-08 05:19:44 +00:00
|
|
|
} catch (e) {
|
2019-12-13 18:17:14 +00:00
|
|
|
//
|
2018-02-08 05:19:44 +00:00
|
|
|
}
|
2018-02-05 06:30:57 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
return [];
|
|
|
|
}
|
2018-02-05 06:30:57 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
function loadPackage(packageName) {
|
|
|
|
let packageInfo;
|
|
|
|
let packageFile;
|
2018-02-05 06:30:57 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
try {
|
|
|
|
const packagePath = Helper.getPackageModulePath(packageName);
|
2019-09-17 16:57:21 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
packageInfo = JSON.parse(fs.readFileSync(path.join(packagePath, "package.json"), "utf-8"));
|
2019-09-17 16:57:21 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
if (!packageInfo.thelounge) {
|
|
|
|
throw "'thelounge' is not present in package.json";
|
2018-02-08 05:19:44 +00:00
|
|
|
}
|
2018-02-05 06:30:57 +00:00
|
|
|
|
2019-12-13 18:17:14 +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;
|
|
|
|
}
|
2018-02-13 11:57:13 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
const version = packageInfo.version;
|
|
|
|
packageInfo = packageInfo.thelounge;
|
|
|
|
packageInfo.packageName = packageName;
|
|
|
|
packageInfo.version = version;
|
2018-01-05 17:40:34 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
packageMap.set(packageName, packageFile);
|
2019-10-02 09:28:08 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
if (packageInfo.type === "theme") {
|
|
|
|
themes.addTheme(packageName, packageInfo);
|
2018-02-05 06:30:57 +00:00
|
|
|
|
2019-12-13 18:17:14 +00:00
|
|
|
if (packageInfo.files) {
|
|
|
|
packageInfo.files.forEach((file) => addFile(packageName, file));
|
2018-02-08 05:19:44 +00:00
|
|
|
}
|
2019-12-13 18:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-12-13 18:17:14 +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(
|
2019-12-13 18:17:14 +00:00
|
|
|
"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
|
|
|
}
|
2019-07-04 07:41:09 +00:00
|
|
|
|
2019-12-13 18:17:14 +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}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-04 07:41:09 +00: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)) {
|
2019-10-16 14:07:25 +00:00
|
|
|
// CLI calls outdated with zero TTL, so we can print the warning there
|
|
|
|
if (!cacheTimeout) {
|
|
|
|
log.warn("There are no packages installed.");
|
|
|
|
}
|
|
|
|
|
2019-07-04 07:41:09 +00:00
|
|
|
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;
|
|
|
|
}
|