Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
- Replaced the hardcoded nvm.sh path with path computed from NVM_DIR.
- Used require('paht').join to build paths, for more portability.
- Applied 'nvmVersion' input parameter.
- 'nvm use \<version\>' can be run from anywhere in the repo, since
  it only impacts the shell environment variables.
  • Loading branch information
nunoguedelha committed Sep 17, 2021
1 parent 1fc39f3 commit 79c6fca
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions iCubTelemVizServer/openMctServerHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";

// Import utilities for working with file and directory paths.
const path = require('path');

function OpenMctServerHandler(outputCallback) {
// Create a child process spawn for later setting the NVM version and running the server
this.childProcess = require('child_process');
Expand All @@ -15,31 +18,30 @@ OpenMctServerHandler.prototype.setNvmVersion = function (nvmVersion) {

// Build the string of shell commands
const homeDir = process.env.HOME;
const cmdString = [
'. /Users/nunoguedelha/.nvm/nvm.sh 1> /dev/null' // Load the NVM function script
+ '&& nvm use v14.17.0 1> /dev/null' // Set the NVM version
+ '&& echo {\\"NVM_INC\\":\\"$NVM_INC\\", \\"NVM_CD_FLAGS\\":\\"$NVM_CD_FLAGS\\", \\"NVM_BIN\\":\\"$NVM_BIN\\"}' // Return the updated NVM path (env. variables)
];
const execPath = [process.cwd() + '/../openmctStaticServer'];
const cmdString =
'. ' + path.join(process.env.NVM_DIR, 'nvm.sh') + ' 1> /dev/null ' + // Load the NVM function script
'&& nvm use ' + nvmVersion + ' 1> /dev/null ' + // Set the NVM version
'&& echo {\\"NVM_INC\\":\\"$NVM_INC\\", \\"NVM_CD_FLAGS\\":\\"$NVM_CD_FLAGS\\", \\"NVM_BIN\\":\\"$NVM_BIN\\"}'; // Return the updated NVM path (env. variables)
try {
const stdout = this.childProcess.execSync(cmdString, {shell:'bash',cwd:execPath, timeout: 3000});
const stdout = this.childProcess.execSync(cmdString, {shell: 'bash', timeout: 3000});
this.nvmVars = JSON.parse(stdout.toString());
this.nvmVersion = nvmVersion;
return {status: 'OK', message: 'Updated NVM config in spawned shell: '+stdout.toString()+'\n'};
}
catch (error) {
return {status: 'OK', message: 'Updated NVM config in spawned shell: ' + stdout.toString() + '\n'};
} catch (error) {
this.nvmVersion = 'default';
return {status: 'ERROR', message: 'command "nvm use version" failed!'};
}
}

OpenMctServerHandler.prototype.start = function () {
// Check if the server is already running
if (this.isOn()) {
return {status: 'WARNING', msg: 'OpenMCT server already running.'};
} else {
const embeddedThis = this;
// Start the process
let npmStart = this.childProcess.spawn('npm', ['start']);
const execPath = path.join(process.cwd(), '..', 'openmctStaticServer');
let npmStart = this.childProcess.spawn('npm', ['start'],{shell: 'bash', cwd: execPath);
// Set the output callbacks
npmStart.stdout.on('data', function (data) {
this.outputCallback('stdout: ' + data);
Expand Down

0 comments on commit 79c6fca

Please sign in to comment.