Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin.js support #10763

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/plugins/data.js
Expand Up @@ -37,15 +37,25 @@ Data.getPluginPaths = async function () {
};

Data.loadPluginInfo = async function (pluginPath) {
const pluginJsonPath = path.join(pluginPath, 'plugin.json');
const pluginJsPath = path.join(pluginPath, 'plugin.js');

const [hasPluginJson, hasPluginJs] = await Promise.all([pluginJsonPath, pluginJsPath].map(file.exists));

const [packageJson, pluginJson] = await Promise.all([
fs.promises.readFile(path.join(pluginPath, 'package.json'), 'utf8'),
fs.promises.readFile(path.join(pluginPath, 'plugin.json'), 'utf8'),
hasPluginJson ? fs.promises.readFile(pluginJsonPath, 'utf8') : Promise.resolve(null),
]);

let pluginData;
let packageData;
try {
pluginData = JSON.parse(pluginJson);
if (hasPluginJson && pluginJson && typeof pluginJson === 'string') {
pluginData = JSON.parse(pluginJson);
} else if (hasPluginJs) {
pluginData = require(pluginJsPath);
}

packageData = JSON.parse(packageJson);

pluginData.license = parseLicense(packageData);
Expand All @@ -59,8 +69,7 @@ Data.loadPluginInfo = async function (pluginPath) {
pluginData.path = pluginPath;
} catch (err) {
const pluginDir = path.basename(pluginPath);

winston.error(`[plugins/${pluginDir}] Error in plugin.json or package.json!${err.stack}`);
winston.error(`[plugins/${pluginDir}] Error in plugin.${hasPluginJson ? 'json' : 'js'} or package.json!${err.stack}`);
throw new Error('[[error:parse-error]]');
}
return pluginData;
Expand Down Expand Up @@ -221,6 +230,7 @@ Data.getModules = async function getModules(pluginData) {
}

const modules = {};

async function processModule(key) {
const modulePath = await resolveModulePath(pluginData.path, pluginModules[key]);
if (modulePath) {
Expand Down
17 changes: 14 additions & 3 deletions src/upgrade.js
Expand Up @@ -62,8 +62,19 @@ Upgrade.getAll = async function () {
Upgrade.appendPluginScripts = async function (files) {
// Find all active plugins
const activePlugins = await plugins.getActive();
activePlugins.forEach((plugin) => {
const configPath = path.join(paths.nodeModules, plugin, 'plugin.json');
for (const plugin of activePlugins) {
let configPath = path.join(paths.nodeModules, plugin, 'plugin.json');
// eslint-disable-next-line no-await-in-loop
if (!await file.exists(configPath)) {
configPath = path.join(paths.nodeModules, plugin, 'plugin.js');
}
// eslint-disable-next-line no-await-in-loop
if (!await file.exists(configPath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if block should move into the above if block, otherwise if there is a plugin.json we are doing 2x file.exists(configPath) checks with the same configPath value.

winston.error(`Missing plugin scripts for ${plugin} -- ignoring`);
// eslint-disable-next-line no-continue
continue;
}

try {
const pluginConfig = require(configPath);
if (pluginConfig.hasOwnProperty('upgrades') && Array.isArray(pluginConfig.upgrades)) {
Expand All @@ -76,7 +87,7 @@ Upgrade.appendPluginScripts = async function (files) {
winston.error(e.stack);
}
}
});
}
return files;
};

Expand Down