mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-06 12:31:33 +01:00
refactor: shared constants (#8707)
define plugin name and theme name regexs in one location for consistency define various shared paths in one place for consistency
This commit is contained in:
@@ -6,6 +6,7 @@ const winston = require('winston');
|
||||
|
||||
const db = require('../database');
|
||||
const file = require('../file');
|
||||
const { paths } = require('../constants');
|
||||
|
||||
const Data = module.exports;
|
||||
|
||||
@@ -14,7 +15,7 @@ const basePath = path.join(__dirname, '../../');
|
||||
Data.getPluginPaths = async function () {
|
||||
let plugins = await db.getSortedSetRange('plugins:active', 0, -1);
|
||||
plugins = plugins.filter(plugin => plugin && typeof plugin === 'string')
|
||||
.map(plugin => path.join(__dirname, '../../node_modules/', plugin));
|
||||
.map(plugin => path.join(paths.nodeModules, plugin));
|
||||
|
||||
const exists = await Promise.all(plugins.map(p => file.exists(p)));
|
||||
return plugins.filter((p, i) => exists[i]);
|
||||
@@ -221,13 +222,13 @@ Data.getLanguageData = async function getLanguageData(pluginData) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
|
||||
const paths = await file.walk(pathToFolder);
|
||||
const pathToFolder = path.join(paths.nodeModules, pluginData.id, pluginData.languages);
|
||||
const filepaths = await file.walk(pathToFolder);
|
||||
|
||||
const namespaces = [];
|
||||
const languages = [];
|
||||
|
||||
paths.forEach(function (p) {
|
||||
filepaths.forEach(function (p) {
|
||||
const rel = path.relative(pathToFolder, p).split(/[/\\]/);
|
||||
const language = rel.shift().replace('_', '-').replace('@', '-x-');
|
||||
const namespace = rel.join('/').replace(/\.json$/, '');
|
||||
@@ -241,7 +242,7 @@ Data.getLanguageData = async function getLanguageData(pluginData) {
|
||||
});
|
||||
|
||||
return {
|
||||
languages: languages,
|
||||
namespaces: namespaces,
|
||||
languages,
|
||||
namespaces,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@ const request = require('request-promise-native');
|
||||
|
||||
const user = require('../user');
|
||||
const posts = require('../posts');
|
||||
const { pluginNamePattern, themeNamePattern, paths } = require('../constants');
|
||||
|
||||
var app;
|
||||
var middleware;
|
||||
@@ -176,7 +177,7 @@ Plugins.list = async function (matching) {
|
||||
if (matching === undefined) {
|
||||
matching = true;
|
||||
}
|
||||
const version = require(path.join(nconf.get('base_dir'), 'package.json')).version;
|
||||
const version = require(paths.currentPackage).version;
|
||||
const url = (nconf.get('registry') || 'https://packages.nodebb.org') + '/api/v1/plugins' + (matching !== false ? '?version=' + version : '');
|
||||
try {
|
||||
const body = await request(url, {
|
||||
@@ -197,9 +198,8 @@ Plugins.listTrending = async () => {
|
||||
};
|
||||
|
||||
Plugins.normalise = async function (apiReturn) {
|
||||
const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/;
|
||||
const pluginMap = {};
|
||||
const dependencies = require(path.join(nconf.get('base_dir'), 'package.json')).dependencies;
|
||||
const dependencies = require(paths.currentPackage).dependencies;
|
||||
apiReturn = Array.isArray(apiReturn) ? apiReturn : [];
|
||||
apiReturn.forEach(function (packageData) {
|
||||
packageData.id = packageData.name;
|
||||
@@ -263,7 +263,7 @@ Plugins.normalise = async function (apiReturn) {
|
||||
return pluginArray;
|
||||
};
|
||||
|
||||
Plugins.nodeModulesPath = path.join(__dirname, '../../node_modules');
|
||||
Plugins.nodeModulesPath = paths.nodeModules;
|
||||
|
||||
Plugins.showInstalled = async function () {
|
||||
const dirs = await fs.promises.readdir(Plugins.nodeModulesPath);
|
||||
@@ -290,7 +290,6 @@ Plugins.showInstalled = async function () {
|
||||
};
|
||||
|
||||
async function findNodeBBModules(dirs) {
|
||||
const pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/;
|
||||
const pluginPaths = [];
|
||||
await async.each(dirs, function (dirname, next) {
|
||||
var dirPath = path.join(Plugins.nodeModulesPath, dirname);
|
||||
|
||||
@@ -12,6 +12,7 @@ const request = require('request-promise-native');
|
||||
const db = require('../database');
|
||||
const meta = require('../meta');
|
||||
const pubsub = require('../pubsub');
|
||||
const { paths } = require('../constants');
|
||||
|
||||
const supportedPackageManagerList = require('../cli/package-install').supportedPackageManager; // load config from src/cli/package-install.js
|
||||
const packageManager = supportedPackageManagerList.indexOf(nconf.get('package_manager')) >= 0 ? nconf.get('package_manager') : 'npm';
|
||||
@@ -132,7 +133,7 @@ module.exports = function (Plugins) {
|
||||
}
|
||||
|
||||
Plugins.isInstalled = async function (id) {
|
||||
const pluginDir = path.join(__dirname, '../../node_modules', id);
|
||||
const pluginDir = path.join(paths.nodeModules, id);
|
||||
try {
|
||||
const stats = await fs.promises.stat(pluginDir);
|
||||
return stats.isDirectory();
|
||||
|
||||
@@ -7,6 +7,7 @@ const nconf = require('nconf');
|
||||
const _ = require('lodash');
|
||||
|
||||
const meta = require('../meta');
|
||||
const { themeNamePattern } = require('../constants');
|
||||
|
||||
module.exports = function (Plugins) {
|
||||
async function registerPluginAssets(pluginData, fields) {
|
||||
@@ -102,8 +103,6 @@ module.exports = function (Plugins) {
|
||||
await Promise.all(plugins.map(p => registerPluginAssets(p, fields)));
|
||||
};
|
||||
|
||||
const themeNamePattern = /(@.*?\/)?nodebb-theme-.*$/;
|
||||
|
||||
Plugins.loadPlugin = async function (pluginPath) {
|
||||
let pluginData;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user