Clean up module install code

Having a global reference for InstallModuleChildProcess isn't actually required since this is javascript. Take the substring that doesn't include the version tag, so "require" can find the module Fixes #631

TotallyNotElite committed Nov 6, 2019 at 19:41 UTC 25fbc29cd0c79e27d319c47b80d71fd9a574c253
2 files changed +5 -6
meshcentral.js
+4 -5
@@ -1932,8 +1932,10 @@ function InstallModules(modules, func) {
1932 var missingModules = [];
1933 if (modules.length > 0) {
1934 for (var i in modules) {
1935 + // Modules may contain a version tag (foobar@1.0.0), remove it so the module can be found using require
1936 + var moduleName = modules[i].split("@", 1)[0];
1937 try {
1936 - var xxmodule = require(modules[i]);
1938 + require(moduleName);
1939 } catch (e) {
1940 if (previouslyInstalledModules[modules[i]] !== true) { missingModules.push(modules[i]); }
1941 }
@@ -1943,7 +1945,6 @@ function InstallModules(modules, func) {
1945 }
1946
1947 // Check if a module is present and install it if missing
1946 -var InstallModuleChildProcess = null;
1948 function InstallModule(modulename, func, tag1, tag2) {
1949 console.log('Installing ' + modulename + '...');
1950 var child_process = require('child_process');
@@ -1952,9 +1953,7 @@ function InstallModule(modulename, func, tag1, tag2) {
1953 // Get the working directory
1954 if ((__dirname.endsWith('/node_modules/meshcentral')) || (__dirname.endsWith('\\node_modules\\meshcentral')) || (__dirname.endsWith('/node_modules/meshcentral/')) || (__dirname.endsWith('\\node_modules\\meshcentral\\'))) { parentpath = require('path').join(__dirname, '../..'); }
1955
1955 - // Looks like we need to keep a global reference to the child process object for this to work correctly.
1956 - InstallModuleChildProcess = child_process.exec('npm install --no-optional --save ' + modulename, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) {
1957 - InstallModuleChildProcess = null;
1956 + child_process.exec(`npm install --no-optional ${modulename}`, { maxBuffer: 512000, timeout: 120000, cwd: parentpath }, function (error, stdout, stderr) {
1957 if ((error != null) && (error != '')) {
1958 console.log('ERROR: Unable to install required module "' + modulename + '". MeshCentral may not have access to npm, or npm may not have suffisent rights to load the new module. Try "npm install ' + modulename + '" to manualy install this module.\r\n');
1959 process.exit();
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.4.3-v",
3 + "version": "0.4.3-w",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",