diff options
| author | Jan Tuomi <jan.tuomi@eficode.com> | 2018-07-13 10:44:19 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@eficode.com> | 2018-07-13 10:44:19 +0300 |
| commit | 9615718012ee798f58834572cfe69c468a05027f (patch) | |
| tree | 55d8d8f0f1ef42d1e7f8096e7f3e44078d96ba8b /index.js | |
| parent | 8b07446f67a5e68d00725fef37061bc87643beb6 (diff) | |
Use NPM programmatically to install packages periodically
Diffstat (limited to 'index.js')
| -rw-r--r-- | index.js | 74 |
1 files changed, 64 insertions, 10 deletions
@@ -1,7 +1,8 @@ require('dotenv').config(); const path = require('path'); const chalk = require('chalk'); -const npm = require('npm-programmatic'); +const npm = require('npm'); +const intercept = require('intercept-stdout'); if (!process.env.MONGO_URL) { throw new Error('No MONGO_URL set in .env!'); @@ -51,28 +52,81 @@ app.all('/:path*', async (req, res) => { res.status(500); console.error(chalk.red(`Error in endpoint ${endpoint.name}! Details below.`)); console.error(err); - res.send('Internal server error.'); + res.send(err); } }); -(async function () { +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); + 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}"...`); - await npm.install([pkg], { - cwd: __dirname, - save: false - }); + // install module + await npmInstall([pkg]); }); - console.info(`Successfully installed ${packages.length} NPM packages programmatically.`); + 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); const port = process.env.PORT || 3000; app.listen(port, () => console.log(`${chalk.blue('faas')} listening on port ${port}! URL: ${chalk.yellow(`http://localhost:${port}`)}`)); |
