first time post here and I'm hoping someone can help it'd be greatly appreciated.
So I'm writing a program in ES6 which I've already written in python, everything works perfectly in python but I want to migrate to ES6 in the hopes of making a desktop app with Electron.
I have a class that creates a Drive object, and to populate the variables in the Drive, I have a function which executes a shell command. I would like to be able to assign a Drive variable e.g 'this.mdts' to the output of the command I'm running but it only returns undefined. I can log the output to console when it's created but it doesn't return that same value to the variable.
Here are some snippets of the code:
This is drive.js
var sys = require('util')
const exec = require('child_process').exec
export default class Drive {
constructor(node,sn) {
this.node = node
this.sn = sn
this.mdts = this.getIdCtrl("mdts")
}
getIdCtrl(specifier) {
exec('nvme id-ctrl /dev/' + this.node,(e,stdout,stderr) => {
if (e instanceof Error) {
console.error(e);
throw e;
}
let line = stdout.split('\n')
for(let i = 0; i<line.length;i++) {
if (line[i].includes(specifier)) {
console.log(line[i]) // prints exactly what I want
return line[i]
}
}
})
}
}
Here is test.js where I create the Drive object
import Drive from './drive';
var sys = require('util')
var exec = require('child-process-promise').exec;
var child;
let fs = require('fs')
let listOfDrives = []
function runTest() {
exec("nvme list").then(function (result) {
let stdout = result.stdout;
let stderr = result.stderr;
let error = result.error;
var line = stdout.split('\n');
for(let i = 0; i<line.length;i++) {
if (line[i].includes("/dev/")) {
if (listOfDrives.length == 0) {
let x = new Drive(line[i].slice(5,13).trim(),line[i].slice(17,37).trim())
listOfDrives.push(x)
}
else {
for (let x in listOfDrives) {
if (x.sn == line[i].slice(17,37).trim()) {
break
}
}
let x = new Drive(line[i].slice(5,13).trim(),line[i].slice(17,37).trim())
listOfDrives.push(x)
}
}
}
if (error) {
console.log('exec error: ' + error);
}
console.log(listOfDrives[0])
for(let x of listOfDrives) {
console.log(x.sn);
}
})
}
runTest()
When it's after running I return the Drive object and I get this output:
{node: 'nvme0n1', sn: 'xxxx10', mdts: undefined }
I've tried child-process-promise
in Drive.js just like test.js but to no avail.
Thanks very much for taking a look
EDIT:
Here's the updated version of drive.js
var sys = require('util')
const exec = require('child-process-promise').exec
export default class Drive {
constructor(node,sn) {
this.node = node
this.sn = sn
this.mdts = this.getIdCtrl("mdts")
}
getIdCtrl(specifier) {
return exec('nvme id-ctrl /dev/' + this.node).then(function (result) {
let stdout = result.stdout;
let stderr = result.stderr;
let error = result.error;
if (error instanceof Error) {
console.error(e);
throw e;
}
let line = stdout.split('\n')
for(let i = 0; i<line.length;i++) {
if (line[i].includes(specifier)) {
console.log(line[i]) // prints exactly what I want
return line[i]
}
}
})
}
}
EDIT:
Here is what the Drive is returning:
Drive {
node: 'nvme0n1',
sn: '66GP6035PCTU',
mdts:
ChildProcessPromise {
<pending>,
_cpResolve: [Function: y],
_cpReject: [Function: z],
childProcess:
ChildProcess {
domain: null,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
_closesNeeded: 3,
_closesGot: 0,
connected: false,
signalCode: null,
exitCode: null,
killed: false,
spawnfile: '/bin/sh',
_handle: [Object],
spawnargs: [Object],
pid: 7858,
stdin: [Object],
stdout: [Object],
stderr: [Object],
stdio: [Object] } } }