feat: #7743, meta/dependencies

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 96ebe7b5f7
commit 1c2540d3e1

@ -1,59 +1,51 @@
'use strict'; 'use strict';
var path = require('path'); const path = require('path');
var fs = require('fs'); const fs = require('fs');
var async = require('async'); const util = require('util');
var semver = require('semver'); const readFileAsync = util.promisify(fs.readFile);
var winston = require('winston');
const semver = require('semver');
const winston = require('winston');
require('colors'); require('colors');
var pkg = require('../../package.json'); const pkg = require('../../package.json');
var Dependencies = module.exports; const Dependencies = module.exports;
var depsMissing = false; let depsMissing = false;
var depsOutdated = false; let depsOutdated = false;
Dependencies.check = function (callback) { Dependencies.check = async function () {
var modules = Object.keys(pkg.dependencies); const modules = Object.keys(pkg.dependencies);
winston.verbose('Checking dependencies for outdated modules'); winston.verbose('Checking dependencies for outdated modules');
async.each(modules, Dependencies.checkModule, function (err) { await Promise.all(modules.map(module => Dependencies.checkModule(module)));
if (err) {
return callback(err);
}
if (depsMissing) { if (depsMissing) {
callback(new Error('dependencies-missing')); throw new Error('dependencies-missing');
} else if (depsOutdated) { } else if (depsOutdated && global.env !== 'development') {
callback(global.env !== 'development' ? new Error('dependencies-out-of-date') : null); throw new Error('dependencies-out-of-date');
} else { }
callback(null);
}
});
}; };
var pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; const pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/;
Dependencies.checkModule = function (moduleName, callback) {
fs.readFile(path.join(__dirname, '../../node_modules/', moduleName, 'package.json'), {
encoding: 'utf-8',
}, function (err, pkgData) {
if (err) {
// If a bundled plugin/theme is not present, skip the dep check (#3384)
if (err.code === 'ENOENT' && pluginNamePattern.test(moduleName)) {
winston.warn('[meta/dependencies] Bundled plugin ' + moduleName + ' not found, skipping dependency check.');
return callback(null, true);
}
return callback(err);
}
Dependencies.checkModule = async function (moduleName) {
try {
let pkgData = await readFileAsync(path.join(__dirname, '../../node_modules/', moduleName, 'package.json'), 'utf8');
pkgData = Dependencies.parseModuleData(moduleName, pkgData); pkgData = Dependencies.parseModuleData(moduleName, pkgData);
var satisfies = Dependencies.doesSatisfy(pkgData, pkg.dependencies[moduleName]); const satisfies = Dependencies.doesSatisfy(pkgData, pkg.dependencies[moduleName]);
callback(null, satisfies); return satisfies;
}); } catch (err) {
if (err.code === 'ENOENT' && pluginNamePattern.test(moduleName)) {
winston.warn('[meta/dependencies] Bundled plugin ' + moduleName + ' not found, skipping dependency check.');
return true;
}
throw err;
}
}; };
Dependencies.parseModuleData = function (moduleName, pkgData) { Dependencies.parseModuleData = function (moduleName, pkgData) {
@ -71,9 +63,9 @@ Dependencies.doesSatisfy = function (moduleData, packageJSONVersion) {
if (!moduleData) { if (!moduleData) {
return false; return false;
} }
var versionOk = !semver.validRange(packageJSONVersion) || semver.satisfies(moduleData.version, packageJSONVersion); const versionOk = !semver.validRange(packageJSONVersion) || semver.satisfies(moduleData.version, packageJSONVersion);
var githubRepo = moduleData._resolved && moduleData._resolved.includes('//github.com'); const githubRepo = moduleData._resolved && moduleData._resolved.includes('//github.com');
var satisfies = versionOk || githubRepo; const satisfies = versionOk || githubRepo;
if (!satisfies) { if (!satisfies) {
winston.warn('[' + 'outdated'.yellow + '] ' + moduleData.name.bold + ' installed v' + moduleData.version + ', package.json requires ' + packageJSONVersion + '\n'); winston.warn('[' + 'outdated'.yellow + '] ' + moduleData.name.bold + ' installed v' + moduleData.version + ', package.json requires ' + packageJSONVersion + '\n');
depsOutdated = true; depsOutdated = true;

Loading…
Cancel
Save