diff --git a/src/cli/index.js b/src/cli/index.js index 90142fa00add..8d53f069f780 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -1,16 +1,16 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); +const fs = require('fs'); +const path = require('path'); require('../../require-main'); -var packageInstall = require('./package-install'); -var dirname = require('./paths').baseDir; +const packageInstall = require('./package-install'); +const { paths } = require('../constants'); // check to make sure dependencies are installed try { - fs.accessSync(path.join(dirname, 'package.json'), fs.constants.R_OK); + fs.accessSync(paths.currentPackage, fs.constants.R_OK); } catch (e) { if (e.code === 'ENOENT') { console.warn('package.json not found.'); @@ -20,7 +20,7 @@ try { packageInstall.preserveExtraneousPlugins(); try { - fs.accessSync(path.join(dirname, 'node_modules/colors/package.json'), fs.constants.R_OK); + fs.accessSync(path.join(paths.nodeModules, 'colors/package.json'), fs.constants.R_OK); require('colors'); console.log('OK'.green); @@ -33,13 +33,13 @@ try { } try { - fs.accessSync(path.join(dirname, 'node_modules/semver/package.json'), fs.constants.R_OK); + fs.accessSync(path.join(paths.nodeModules, 'semver/package.json'), fs.constants.R_OK); var semver = require('semver'); var defaultPackage = require('../../install/package.json'); var checkVersion = function (packageName) { - var version = JSON.parse(fs.readFileSync(path.join(dirname, 'node_modules', packageName, 'package.json'), 'utf8')).version; + var version = JSON.parse(fs.readFileSync(path.join(paths.nodeModules, packageName, 'package.json'), 'utf8')).version; if (!semver.satisfies(version, defaultPackage.dependencies[packageName])) { var e = new TypeError('Incorrect dependency version: ' + packageName); e.code = 'DEP_WRONG_VERSION'; @@ -97,7 +97,7 @@ global.env = env; prestart.setupWinston(); // Alternate configuration file support -var configFile = path.resolve(dirname, nconf.get('config') || 'config.json'); +var configFile = path.resolve(paths.baseDir, nconf.get('config') || 'config.json'); var configExists = file.existsSync(configFile) || (nconf.get('url') && nconf.get('secret') && nconf.get('database')); prestart.loadConfig(configFile); diff --git a/src/cli/manage.js b/src/cli/manage.js index de250ce4aa06..69d2902ad72d 100644 --- a/src/cli/manage.js +++ b/src/cli/manage.js @@ -1,17 +1,18 @@ 'use strict'; -var async = require('async'); -var winston = require('winston'); -var childProcess = require('child_process'); -var _ = require('lodash'); -var CliGraph = require('cli-graph'); - -var build = require('../meta/build'); -var db = require('../database'); -var plugins = require('../plugins'); -var events = require('../events'); -var analytics = require('../analytics'); -var reset = require('./reset'); +const async = require('async'); +const winston = require('winston'); +const childProcess = require('child_process'); +const _ = require('lodash'); +const CliGraph = require('cli-graph'); + +const build = require('../meta/build'); +const db = require('../database'); +const plugins = require('../plugins'); +const events = require('../events'); +const analytics = require('../analytics'); +const reset = require('./reset'); +const { pluginNamePattern, themeNamePattern } = require('../constants'); function buildTargets() { var aliases = build.aliases; @@ -34,9 +35,6 @@ function buildTargets() { ); } -var themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; -var pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; - function activate(plugin) { if (themeNamePattern.test(plugin)) { reset.reset({ diff --git a/src/cli/package-install.js b/src/cli/package-install.js index 07e93a79278c..8f60f656b167 100644 --- a/src/cli/package-install.js +++ b/src/cli/package-install.js @@ -4,28 +4,24 @@ const path = require('path'); const fs = require('fs'); const cproc = require('child_process'); -const packageFilePath = path.join(__dirname, '../../package.json'); -const packageDefaultFilePath = path.join(__dirname, '../../install/package.json'); -const modulesPath = path.join(__dirname, '../../node_modules'); - -const isPackage = /^(@\w+\/)?nodebb-(plugin|theme|widget|reward)-\w+/; +const { paths, pluginNamePattern } = require('../constants'); function updatePackageFile() { let oldPackageContents = {}; try { - oldPackageContents = JSON.parse(fs.readFileSync(packageFilePath, 'utf8')); + oldPackageContents = JSON.parse(fs.readFileSync(paths.currentPackage, 'utf8')); } catch (e) { if (e.code !== 'ENOENT') { throw e; } } - const defaultPackageContents = JSON.parse(fs.readFileSync(packageDefaultFilePath, 'utf8')); + const defaultPackageContents = JSON.parse(fs.readFileSync(paths.installPackage, 'utf8')); let dependencies = {}; Object.entries(oldPackageContents.dependencies || {}).forEach(([dep, version]) => { - if (isPackage.test(dep)) { + if (pluginNamePattern.test(dep)) { dependencies[dep] = version; } }); @@ -38,7 +34,7 @@ function updatePackageFile() { const packageContents = { ...oldPackageContents, ...defaultPackageContents, dependencies: dependencies }; - fs.writeFileSync(packageFilePath, JSON.stringify(packageContents, null, 2)); + fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2)); } exports.updatePackageFile = updatePackageFile; @@ -54,7 +50,7 @@ function installAll() { const prod = global.env !== 'development'; let command = 'npm install'; try { - fs.accessSync(path.join(modulesPath, 'nconf/package.json'), fs.constants.R_OK); + fs.accessSync(path.join(paths.nodeModules, 'nconf/package.json'), fs.constants.R_OK); const supportedPackageManagerList = exports.supportedPackageManager; // load config from src/cli/package-install.js const packageManager = require('nconf').get('package_manager'); if (supportedPackageManagerList.indexOf(packageManager) >= 0) { @@ -94,34 +90,34 @@ exports.installAll = installAll; function preserveExtraneousPlugins() { // Skip if `node_modules/` is not found or inaccessible try { - fs.accessSync(modulesPath, fs.constants.R_OK); + fs.accessSync(paths.nodeModules, fs.constants.R_OK); } catch (e) { return; } - const packages = fs.readdirSync(modulesPath).filter(function (pkgName) { - return isPackage.test(pkgName); + const packages = fs.readdirSync(paths.nodeModules).filter(function (pkgName) { + return pluginNamePattern.test(pkgName); }); - const packageContents = JSON.parse(fs.readFileSync(packageFilePath, 'utf8')); + const packageContents = JSON.parse(fs.readFileSync(paths.currentPackage, 'utf8')); const extraneous = packages // only extraneous plugins (ones not in package.json) which are not links .filter(function (pkgName) { const extraneous = !packageContents.dependencies.hasOwnProperty(pkgName); - const isLink = fs.lstatSync(path.join(modulesPath, pkgName)).isSymbolicLink(); + const isLink = fs.lstatSync(path.join(paths.nodeModules, pkgName)).isSymbolicLink(); return extraneous && !isLink; }) // reduce to a map of package names to package versions .reduce(function (map, pkgName) { - const pkgConfig = JSON.parse(fs.readFileSync(path.join(modulesPath, pkgName, 'package.json'), 'utf8')); + const pkgConfig = JSON.parse(fs.readFileSync(path.join(paths.nodeModules, pkgName, 'package.json'), 'utf8')); map[pkgName] = pkgConfig.version; return map; }, {}); // Add those packages to package.json Object.assign(packageContents.dependencies, extraneous); - fs.writeFileSync(packageFilePath, JSON.stringify(packageContents, null, 2)); + fs.writeFileSync(paths.currentPackage, JSON.stringify(packageContents, null, 2)); } exports.preserveExtraneousPlugins = preserveExtraneousPlugins; diff --git a/src/cli/paths.js b/src/cli/paths.js deleted file mode 100644 index 2a9bec354714..000000000000 --- a/src/cli/paths.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -var path = require('path'); - -var baseDir = path.join(__dirname, '../../'); -var loader = path.join(baseDir, 'loader.js'); -var app = path.join(baseDir, 'app.js'); -var pidfile = path.join(baseDir, 'pidfile'); -var config = path.join(baseDir, 'config.json'); - -module.exports = { - baseDir: baseDir, - loader: loader, - app: app, - pidfile: pidfile, - config: config, -}; diff --git a/src/cli/reset.js b/src/cli/reset.js index 1368962246b3..9edbfee8e705 100644 --- a/src/cli/reset.js +++ b/src/cli/reset.js @@ -11,11 +11,7 @@ const meta = require('../meta'); const plugins = require('../plugins'); const widgets = require('../widgets'); const privileges = require('../privileges'); - -const dirname = require('./paths').baseDir; - -const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; -const pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; +const { paths, pluginNamePattern, themeNamePattern } = require('../constants'); exports.reset = async function (options) { const map = { @@ -97,7 +93,7 @@ async function resetSettings() { async function resetTheme(themeId) { try { - await fs.promises.access(path.join(dirname, 'node_modules', themeId, 'package.json')); + await fs.promises.access(path.join(paths.nodeModules, themeId, 'package.json')); } catch (err) { winston.warn('[reset] Theme `%s` is not installed on this forum', themeId); throw new Error('theme-not-found'); diff --git a/src/cli/running.js b/src/cli/running.js index b637e40f354b..7a4087451246 100644 --- a/src/cli/running.js +++ b/src/cli/running.js @@ -1,12 +1,12 @@ 'use strict'; -var fs = require('fs'); -var childProcess = require('child_process'); +const fs = require('fs'); +const childProcess = require('child_process'); -var fork = require('../meta/debugFork'); -var paths = require('./paths'); +const fork = require('../meta/debugFork'); +const { paths } = require('../constants'); -var dirname = paths.baseDir; +const cwd = paths.baseDir; function getRunningPid(callback) { fs.readFile(paths.pidfile, { @@ -32,8 +32,8 @@ function start(options) { process.env.NODE_ENV = 'development'; fork(paths.loader, ['--no-daemon', '--no-silent'], { env: process.env, - cwd: dirname, stdio: 'inherit', + cwd, }); return; } @@ -56,12 +56,12 @@ function start(options) { // Spawn a new NodeBB process var child = fork(paths.loader, process.argv.slice(3), { env: process.env, - cwd: dirname, + cwd, }); if (options.log) { childProcess.spawn('tail', ['-F', './logs/output.log'], { - cwd: dirname, stdio: 'inherit', + cwd, }); } @@ -112,8 +112,8 @@ function status() { function log() { console.log('\nHit '.red + 'Ctrl-C '.bold + 'to exit\n'.red + '\n'.reset); childProcess.spawn('tail', ['-F', './logs/output.log'], { - cwd: dirname, stdio: 'inherit', + cwd, }); } diff --git a/src/cli/setup.js b/src/cli/setup.js index eb15511de6c8..be0cb55623ff 100644 --- a/src/cli/setup.js +++ b/src/cli/setup.js @@ -1,18 +1,18 @@ 'use strict'; -var winston = require('winston'); -var async = require('async'); -var path = require('path'); -var nconf = require('nconf'); +const winston = require('winston'); +const async = require('async'); +const path = require('path'); +const nconf = require('nconf'); -var install = require('../../install/web').install; +const { install } = require('../../install/web'); function setup(initConfig) { - var paths = require('./paths'); - var install = require('../install'); - var build = require('../meta/build'); - var prestart = require('../prestart'); - var pkg = require('../../package.json'); + const { paths } = require('../constants'); + const install = require('../install'); + const build = require('../meta/build'); + const prestart = require('../prestart'); + const pkg = require('../../package.json'); winston.info('NodeBB Setup Triggered via Command Line'); diff --git a/src/cli/upgrade-plugins.js b/src/cli/upgrade-plugins.js index 8f2c240eeadf..577f8140b9f9 100644 --- a/src/cli/upgrade-plugins.js +++ b/src/cli/upgrade-plugins.js @@ -9,7 +9,7 @@ const fs = require('fs'); const path = require('path'); const nconf = require('nconf'); -const paths = require('./paths'); +const { paths, pluginNamePattern } = require('../constants'); const packageManager = nconf.get('package_manager'); const supportedPackageManagerList = require('./package-install').supportedPackageManager; // load config from src/cli/package-install.js @@ -21,13 +21,11 @@ if (process.platform === 'win32') { packageManagerExecutable += '.cmd'; } -const dirname = paths.baseDir; - function getModuleVersions(modules, callback) { const versionHash = {}; async.eachLimit(modules, 50, function (module, next) { - fs.readFile(path.join(dirname, 'node_modules', module, 'package.json'), { encoding: 'utf-8' }, function (err, pkg) { + fs.readFile(path.join(paths.nodeModules, module, 'package.json'), { encoding: 'utf-8' }, function (err, pkg) { if (err) { return next(err); } @@ -47,19 +45,16 @@ function getModuleVersions(modules, callback) { function getInstalledPlugins(callback) { async.parallel({ - files: async.apply(fs.readdir, path.join(dirname, 'node_modules')), - deps: async.apply(fs.readFile, path.join(dirname, 'package.json'), { encoding: 'utf-8' }), - bundled: async.apply(fs.readFile, path.join(dirname, 'install/package.json'), { encoding: 'utf-8' }), + files: async.apply(fs.readdir, paths.nodeModules), + deps: async.apply(fs.readFile, paths.currentPackage, { encoding: 'utf-8' }), + bundled: async.apply(fs.readFile, paths.installPackage, { encoding: 'utf-8' }), }, function (err, payload) { if (err) { return callback(err); } - const isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w-]+$/; - - payload.files = payload.files.filter(function (file) { - return isNbbModule.test(file); + return pluginNamePattern.test(file); }); try { @@ -70,10 +65,10 @@ function getInstalledPlugins(callback) { } payload.bundled = payload.bundled.filter(function (pkgName) { - return isNbbModule.test(pkgName); + return pluginNamePattern.test(pkgName); }); payload.deps = payload.deps.filter(function (pkgName) { - return isNbbModule.test(pkgName); + return pluginNamePattern.test(pkgName); }); // Whittle down deps to send back only extraneously installed plugins/themes/etc @@ -84,7 +79,7 @@ function getInstalledPlugins(callback) { // Ignore git repositories try { - fs.accessSync(path.join(dirname, 'node_modules', pkgName, '.git')); + fs.accessSync(path.join(paths.nodeModules, pkgName, '.git')); return false; } catch (e) { return true; @@ -96,7 +91,7 @@ function getInstalledPlugins(callback) { } function getCurrentVersion(callback) { - fs.readFile(path.join(dirname, 'install/package.json'), { encoding: 'utf-8' }, function (err, pkg) { + fs.readFile(paths.installPackage, { encoding: 'utf-8' }, function (err, pkg) { if (err) { return callback(err); } diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 000000000000..cc563f1353da --- /dev/null +++ b/src/constants.js @@ -0,0 +1,26 @@ +'use strict'; + +const path = require('path'); + +const baseDir = path.join(__dirname, '../'); +const loader = path.join(baseDir, 'loader.js'); +const app = path.join(baseDir, 'app.js'); +const pidfile = path.join(baseDir, 'pidfile'); +const config = path.join(baseDir, 'config.json'); +const currentPackage = path.join(baseDir, 'package.json'); +const installPackage = path.join(baseDir, 'install/package.json'); +const nodeModules = path.join(baseDir, 'node_modules'); + +exports.paths = { + baseDir, + loader, + app, + pidfile, + config, + currentPackage, + installPackage, + nodeModules, +}; + +exports.pluginNamePattern = /^(@[\w-]+\/)?nodebb-(theme|plugin|widget|rewards)-[\w-]+$/; +exports.themeNamePattern = /^(@[\w-]+\/)?nodebb-theme-[\w-]+$/; diff --git a/src/controllers/admin/themes.js b/src/controllers/admin/themes.js index 5fcce4d7fb9d..db08d08f8461 100644 --- a/src/controllers/admin/themes.js +++ b/src/controllers/admin/themes.js @@ -4,13 +4,14 @@ const path = require('path'); const fs = require('fs'); const file = require('../../file'); +const { paths } = require('../../constants'); const themesController = module.exports; const defaultScreenshotPath = path.join(__dirname, '../../../public/images/themes/default.png'); themesController.get = async function (req, res, next) { - const themeDir = path.join(__dirname, '../../../node_modules', req.params.theme); + const themeDir = path.join(paths.nodeModules, req.params.theme); const themeConfigPath = path.join(themeDir, 'theme.json'); let themeConfig; diff --git a/src/meta/dependencies.js b/src/meta/dependencies.js index 14364278dd2b..ee78f54a8aee 100644 --- a/src/meta/dependencies.js +++ b/src/meta/dependencies.js @@ -8,6 +8,7 @@ const winston = require('winston'); require('colors'); const pkg = require('../../package.json'); +const { paths, pluginNamePattern } = require('../constants'); const Dependencies = module.exports; @@ -28,11 +29,9 @@ Dependencies.check = async function () { } }; -const pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; - Dependencies.checkModule = async function (moduleName) { try { - let pkgData = await fs.promises.readFile(path.join(__dirname, '../../node_modules/', moduleName, 'package.json'), 'utf8'); + let pkgData = await fs.promises.readFile(path.join(paths.nodeModules, moduleName, 'package.json'), 'utf8'); pkgData = Dependencies.parseModuleData(moduleName, pkgData); const satisfies = Dependencies.doesSatisfy(pkgData, pkg.dependencies[moduleName]); diff --git a/src/meta/languages.js b/src/meta/languages.js index 64bc7219dded..5f27486fd3b7 100644 --- a/src/meta/languages.js +++ b/src/meta/languages.js @@ -12,9 +12,10 @@ const rimrafAsync = util.promisify(rimraf); const file = require('../file'); const Plugins = require('../plugins'); +const { paths } = require('../constants'); -const buildLanguagesPath = path.join(__dirname, '../../build/public/language'); -const coreLanguagesPath = path.join(__dirname, '../../public/language'); +const buildLanguagesPath = path.join(paths.baseDir, 'build/public/language'); +const coreLanguagesPath = path.join(paths.baseDir, 'public/language'); async function getTranslationMetadata() { const paths = await file.walk(coreLanguagesPath); @@ -98,7 +99,7 @@ async function buildNamespaceLanguage(lang, namespace, plugins) { } async function addPlugin(translations, pluginData, lang, namespace) { - const pluginLanguages = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages); + const pluginLanguages = path.join(paths.nodeModules, pluginData.id, pluginData.languages); const defaultLang = pluginData.defaultLang || 'en-GB'; // for each plugin, fallback in this order: diff --git a/src/meta/templates.js b/src/meta/templates.js index ec5557ca5b34..16f7f231196d 100644 --- a/src/meta/templates.js +++ b/src/meta/templates.js @@ -15,6 +15,7 @@ const Benchpress = require('benchpressjs'); const plugins = require('../plugins'); const file = require('../file'); const db = require('../database'); +const { themeNamePattern, paths } = require('../constants'); const viewsPath = nconf.get('views_dir'); @@ -43,8 +44,6 @@ async function processImports(paths, templatePath, source) { } Templates.processImports = processImports; -const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; - async function getTemplateDirs(activePlugins) { const pluginTemplates = activePlugins.map(function (id) { if (themeNamePattern.test(id)) { @@ -53,7 +52,7 @@ async function getTemplateDirs(activePlugins) { if (!plugins.pluginsData[id]) { return ''; } - return path.join(__dirname, '../../node_modules/', id, plugins.pluginsData[id].templates || 'templates'); + return path.join(paths.nodeModules, id, plugins.pluginsData[id].templates || 'templates'); }).filter(Boolean); let themeConfig = require(nconf.get('theme_config')); diff --git a/src/meta/themes.js b/src/meta/themes.js index bfe9cb5d2f78..8c184f776d86 100644 --- a/src/meta/themes.js +++ b/src/meta/themes.js @@ -10,12 +10,11 @@ const file = require('../file'); const db = require('../database'); const Meta = require('./index'); const events = require('../events'); -const utils = require('../../public/src/utils'); +const utils = require('../utils'); +const { themeNamePattern } = require('../constants'); const Themes = module.exports; -const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; - Themes.get = async () => { const themePath = nconf.get('themes_path'); if (typeof themePath !== 'string') { diff --git a/src/plugins/data.js b/src/plugins/data.js index 3715492ec43a..4ffd02d3d294 100644 --- a/src/plugins/data.js +++ b/src/plugins/data.js @@ -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, }; }; diff --git a/src/plugins/index.js b/src/plugins/index.js index e8fe3443ac41..1d84174c126e 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -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); diff --git a/src/plugins/install.js b/src/plugins/install.js index a0061bc77fca..9394cb784a94 100644 --- a/src/plugins/install.js +++ b/src/plugins/install.js @@ -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(); diff --git a/src/plugins/load.js b/src/plugins/load.js index 78c58b8db400..2c639659b3c7 100644 --- a/src/plugins/load.js +++ b/src/plugins/load.js @@ -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 { diff --git a/src/prestart.js b/src/prestart.js index 2f83fc81031e..6d9839a96235 100644 --- a/src/prestart.js +++ b/src/prestart.js @@ -1,12 +1,12 @@ 'use strict'; -var nconf = require('nconf'); -var url = require('url'); -var winston = require('winston'); -var path = require('path'); +const nconf = require('nconf'); +const url = require('url'); +const winston = require('winston'); +const path = require('path'); -var pkg = require('../package.json'); -var dirname = require('./cli/paths').baseDir; +const pkg = require('../package.json'); +const { paths } = require('./constants'); function setupWinston() { if (!winston.format) { @@ -49,10 +49,10 @@ function loadConfig(configFile) { }); nconf.defaults({ - base_dir: dirname, - themes_path: path.join(dirname, 'node_modules'), + base_dir: paths.baseDir, + themes_path: paths.nodeModules, upload_path: 'public/uploads', - views_dir: path.join(dirname, 'build/public/templates'), + views_dir: path.join(paths.baseDir, 'build/public/templates'), version: pkg.version, isCluster: false, isPrimary: true, @@ -72,8 +72,8 @@ function loadConfig(configFile) { nconf.set('runJobs', nconf.get('isPrimary') && !nconf.get('jobsDisabled')); // Ensure themes_path is a full filepath - nconf.set('themes_path', path.resolve(dirname, nconf.get('themes_path'))); - nconf.set('core_templates_path', path.join(dirname, 'src/views')); + nconf.set('themes_path', path.resolve(paths.baseDir, nconf.get('themes_path'))); + nconf.set('core_templates_path', path.join(paths.baseDir, 'src/views')); nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates')); nconf.set('upload_path', path.resolve(nconf.get('base_dir'), nconf.get('upload_path'))); diff --git a/src/upgrade.js b/src/upgrade.js index 6f9dbe4366ef..0ab82a135f24 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -9,6 +9,7 @@ const winston = require('winston'); const db = require('./database'); const file = require('./file'); +const { paths } = require('./constants'); /* * Need to write an upgrade script for NodeBB? Cool. @@ -61,7 +62,7 @@ Upgrade.appendPluginScripts = async function (files) { // Find all active plugins const plugins = await db.getSortedSetRange('plugins:active', 0, -1); plugins.forEach((plugin) => { - const configPath = path.join(__dirname, '../node_modules', plugin, 'plugin.json'); + const configPath = path.join(paths.nodeModules, plugin, 'plugin.json'); try { const pluginConfig = require(configPath); if (pluginConfig.hasOwnProperty('upgrades') && Array.isArray(pluginConfig.upgrades)) {