aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@eficode.com>2018-07-13 11:11:43 +0300
committerJan Tuomi <jan.tuomi@eficode.com>2018-07-13 11:11:43 +0300
commitbd993ccdf8aa2cadc3722d2d169386a7b24661c7 (patch)
tree050a27fa6e3d3788c2c9c67a200ec80d2c0eee0b
parenta79413824414d1cda8341900becec1ea2b9d8617 (diff)
Fix tests
-rw-r--r--index.js71
-rw-r--r--npm.js76
2 files changed, 77 insertions, 70 deletions
diff --git a/index.js b/index.js
index 5a3ad9e..2d8f5ba 100644
--- a/index.js
+++ b/index.js
@@ -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);
diff --git a/npm.js b/npm.js
new file mode 100644
index 0000000..5242eeb
--- /dev/null
+++ b/npm.js
@@ -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
+ };
+};