From 8b07446f67a5e68d00725fef37061bc87643beb6 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 12 Jul 2018 16:56:26 +0300 Subject: Implement installation of NPM packages on host --- index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'index.js') diff --git a/index.js b/index.js index 7ae392d..c1d4121 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ require('dotenv').config(); const path = require('path'); const chalk = require('chalk'); +const npm = require('npm-programmatic'); if (!process.env.MONGO_URL) { throw new Error('No MONGO_URL set in .env!'); @@ -54,5 +55,24 @@ app.all('/:path*', async (req, res) => { } }); +(async function () { + try { + const packagesToInstall = await models.packages.find({}); + const packages = packagesToInstall.map(obj => obj.name); + + packages.forEach(async pkg => { + console.info(`Installing package "${pkg}"...`); + await npm.install([pkg], { + cwd: __dirname, + save: false + }); + }); + console.info(`Successfully installed ${packages.length} NPM packages programmatically.`); + } catch (err) { + console.error(`Failed to install NPM packages programmatically.`); + console.error(err); + } +})(); + const port = process.env.PORT || 3000; app.listen(port, () => console.log(`${chalk.blue('faas')} listening on port ${port}! URL: ${chalk.yellow(`http://localhost:${port}`)}`)); -- cgit v1.3