diff options
| -rw-r--r-- | index.js | 71 | ||||
| -rw-r--r-- | npm.js | 76 |
2 files changed, 77 insertions, 70 deletions
@@ -1,8 +1,6 @@ require('dotenv').config(); const path = require('path'); const chalk = require('chalk'); -const npm = require('npm'); -const intercept = require('intercept-stdout'); if (!process.env.MONGO_URL) { throw new Error('No MONGO_URL set in .env!'); @@ -14,6 +12,7 @@ const express = require('express'); const app = express(); const requireFromString = require('require-from-string'); const models = require('./models')(db); +const {installPackagesFromDatabase} = require('./npm')(models); app.get('/', (req, res) => { res.sendFile(path.join(__dirname, '/frontpage.html')); @@ -56,74 +55,6 @@ app.all('/:path*', async (req, res) => { } }); -const npmLoad = () => new Promise((resolve, reject) => { - npm.load(function(err) { - if (err) { - reject(err); - } - - npm.on('log', function(message) { - // log installation progress - //console.log(message); - }); - resolve(); - }); -}); - -const npmList = () => new Promise((resolve, reject) => { - const unhook_intercept = intercept(txt => ""); - npm.config.set('depth', 0); - npm.commands.list([], function(err, data) { - unhook_intercept(); - if (err) { - reject(err); - } - resolve(Object.keys(data._dependencies)); - }); -}); - -const npmInstall = (packages) => new Promise((resolve, reject) => { - const unhook_intercept = intercept(txt => ''); - npm.config.set('progress', 'false'); - npm.config.set('silent', 'true'); - npm.commands.install(packages, function(err, data) { - unhook_intercept(); - if (err) { - reject(err); - } - resolve(data); - }); -}); - -async function installPackagesFromDatabase(showInfo = false) { - try { - await npmLoad(); - // handle errors - const packagesToInstall = await models.packages.find({}); - const packages = packagesToInstall.map(obj => obj.name.trim()); - - const installedPackages = await npmList(); - - packages.forEach(async pkg => { - if (installedPackages.some(installedPkg => installedPkg.includes(pkg))) { - if (showInfo) { - console.info(`"${pkg}" Already installed, skipping...`); - } - return; - } - console.info(`Installing package "${pkg}"...`); - // install module - await npmInstall([pkg]); - }); - if (showInfo) { - console.info(`Successfully installed ${packages.length} NPM packages programmatically.`); - } - } catch (err) { - console.error(`Failed to install NPM packages programmatically.`); - console.error(err); - } -} - const packagePollInterval = process.env.PACKAGE_POLL_INTERVAL || 60000; // 1 minute installPackagesFromDatabase(true); setInterval(installPackagesFromDatabase, packagePollInterval); @@ -0,0 +1,76 @@ +const npm = require('npm'); +const intercept = require('intercept-stdout'); + +module.exports = models => { + const npmLoad = () => new Promise((resolve, reject) => { + npm.load(err => { + if (err) { + reject(err); + } + resolve(); + }); + }); + + const npmList = () => new Promise((resolve, reject) => { + const unhookIntercept = intercept(() => ''); + npm.config.set('depth', 0); + npm.commands.list([], (err, data) => { + unhookIntercept(); + if (err) { + reject(err); + } + resolve(Object.keys(data._dependencies)); + }); + }); + + const npmInstall = packages => new Promise((resolve, reject) => { + const unhookIntercept = intercept(() => ''); + npm.config.set('progress', 'false'); + npm.config.set('silent', 'true'); + npm.config.set('save', 'false'); + npm.config.set('no-save', 'true'); + npm.commands.install(packages, (err, data) => { + unhookIntercept(); + if (err) { + reject(err); + } + resolve(data); + }); + }); + + async function installPackagesFromDatabase(showInfo = false) { + try { + await npmLoad(); + // Handle errors + const packagesToInstall = await models.packages.find({}); + const packages = packagesToInstall.map(obj => obj.name.trim()); + + const installedPackages = await npmList(); + + packages.forEach(async pkg => { + if (installedPackages.some(installedPkg => installedPkg.includes(pkg))) { + if (showInfo) { + console.info(`"${pkg}" Already installed, skipping...`); + } + return; + } + console.info(`Installing package "${pkg}"...`); + // Install module + await npmInstall([pkg]); + }); + if (showInfo) { + console.info(`Successfully installed ${packages.length} NPM packages programmatically.`); + } + } catch (err) { + console.error(`Failed to install NPM packages programmatically.`); + console.error(err); + } + } + + return { + npmInstall, + npmList, + npmLoad, + installPackagesFromDatabase + }; +}; |
