From bbc7a1c911e811a1d41826348e312d8b27e17f08 Mon Sep 17 00:00:00 2001 From: Brandon Kim Date: Tue, 26 Jan 2021 18:25:31 +0900 Subject: [PATCH 1/3] plugin install fix --- server/plugins.js | 78 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/server/plugins.js b/server/plugins.js index 140fa1797e..9bc31e97bf 100644 --- a/server/plugins.js +++ b/server/plugins.js @@ -12,35 +12,67 @@ const morgan = require('morgan'); const { logEntryRequest, stream, loggerPlugin } = require('./config/logger'); const { domainMiddleware, helmetMiddleware } = require('./config/middleware'); const morganType = process.env.NODE_ENV === 'development' ? 'dev' : 'combined'; -const npmi = require('npmi'); const multer = require('multer'); const moment = require('moment'); const { checkStatus } = require('./init'); const UglifyJS = require('uglify-es'); const cors = require('cors'); +const mathjs = require('mathjs'); +const bluebird = require('bluebird'); +const rp = require('request-promise'); +const uuid = require('uuid/v4'); +const fs = require('fs'); +const path = require('path'); +const latestVersion = require('latest-version'); +const { resolve } = bluebird; +const npm = require('npm-programmatic'); + +const getInstalledLibrary = async (name, version) => { + const jsonFilePath = path.resolve(__dirname, './node_modules', name, 'package.json'); + + let fileData = fs.readFileSync(jsonFilePath); + fileData = JSON.parse(fileData); + + loggerPlugin.verbose(`${name} library found`); + if (version === 'latest') { + const v = await latestVersion(name); + if (fileData.version === v) { + loggerPlugin.verbose(`${name} version ${version} found`); + const lib = require(name); + return resolve(lib); + } else { + throw new Error('Version does not match'); + } + } else { + if (fileData.version === version) { + loggerPlugin.verbose(`${name} version ${version} found`); + const lib = require(name); + return resolve(lib); + } else { + throw new Error('Version does not match'); + } + } +}; const installLibrary = (library) => { - return new Promise((resolve, reject) => { - npmi({ - name: library, - npmLoad: { - save: false, - forceInstall: false, - loglevel: 'silent' - } - }, (err, result) => { - if (err) { - reject(err); - } else { - try { - const lib = require(library); - resolve(lib); - } catch (err) { - reject(err); - } - } + const [name, version = 'latest'] = library.split('@'); + return getInstalledLibrary(name, version) + .then((data) => { + return data; + }) + .catch((err) => { + loggerPlugin.verbose(`${name} version ${version} installing`); + return npm.install([`${name}@${version}`], { + cwd: path.resolve(__dirname, './'), + save: true, + output: true + }); + }) + .then(() => { + loggerPlugin.verbose(`${name} version ${version} installed`); + const lib = require(name); + return lib; }); - }); }; app.use(morgan(morganType, { stream })); @@ -1008,6 +1040,10 @@ checkStatus() loggerPlugin, multer, moment, + mathjs, + bluebird, + rp, + uuid, meta: plugin.meta, installedLibraries: {} }; From 6d2b7017008e8ddbb7ebf9d8988d90473e2f3ac2 Mon Sep 17 00:00:00 2001 From: Brandon Kim Date: Tue, 26 Jan 2021 18:25:40 +0900 Subject: [PATCH 2/3] pm2 igonore package.json --- server/ecosystem.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/ecosystem.config.js b/server/ecosystem.config.js index 22983e8853..43434af664 100644 --- a/server/ecosystem.config.js +++ b/server/ecosystem.config.js @@ -1,5 +1,5 @@ const watch = process.env.NODE_ENV === 'production' ? false : true; -const ignore_watch = ['logs', 'node_modules', 'tools', 'db/functions', 'db/triggers', 'storage']; +const ignore_watch = ['logs', 'node_modules', 'tools', 'db/functions', 'db/triggers', 'storage', 'package.json', 'package.json.*', 'package-lock.json', 'package-lock.json.*']; const max_memory_restart = '4000M'; const node_args = ['--max_old_space_size=4096']; const mode = process.env.DEPLOYMENT_MODE || 'all'; From 59d11f91a80bf0c66c206c847b2fa6b3e38ad94f Mon Sep 17 00:00:00 2001 From: Brandon Kim Date: Tue, 26 Jan 2021 18:26:39 +0900 Subject: [PATCH 3/3] package.json update --- server/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/package.json b/server/package.json index 243921f8ab..b73feca3c9 100644 --- a/server/package.json +++ b/server/package.json @@ -34,6 +34,7 @@ "install": "0.10.4", "json2csv": "4.5.4", "jsonwebtoken": "7.4.3", + "latest-version": "5.1.0", "lodash": "4.17.5", "mathjs": "3.20.2", "moment": "2.21.0", @@ -43,7 +44,7 @@ "node-cron": "2.0.3", "nodemailer": "6.4.6", "npm": "5.7.1", - "npmi": "4.0.0", + "npm-programmatic": "0.0.12", "otp": "0.1.3", "pg": "6.4.2", "pg-hstore": "2.3.2", @@ -56,7 +57,7 @@ "request-promise": "4.2.2", "sequelize": "4.37.7", "swagger-express-mw": "0.1.0", - "uglify-es": "^3.3.9", + "uglify-es": "3.3.9", "uuid": "3.2.1", "validator": "9.4.1", "winston": "3.2.1",