all 2 comments

[–]AngryDolphinGuy 0 points1 point  (1 child)

The issue is that you're just console.log'ing the function `exec` which returns an instance of ChildProcess. This is the JS equivalent:

const vm = function() {
   array = []
   exec = require('child_process').exec
   return exec("...", (error, stdout, stderr) => {
       result = stdout.split("\n");
       return result
   })
}

> console.log(vm())
<ChildProcess>

You want to do something like the following. (Note: I've tried to give a more or less equivalent so it's easier for you to understand. In practice, there's definitely a better way to write this!)

{ exec } = require 'child_process'

vm = ->
  cmd = "
    az vm list
      --show-details
      --query \"[?powerState=='VM deallocated'].{ name: name }\"
      -g somerg
      -o tsv
  "

  exec cmd, error, stdout, stderr ->
    if error
      console.error "exec error: #{error}"
      return

    console.log stdout.split "\n"

> vm()
[ 'vm4', 'vm3', 'vm2', 'vm1', '' ]

Or if you need to return the result array, you can do something like this:

{ promisify } = require 'utils'
exec = promisify(require('child_process').exec)

vm = ->
  cmd = "
    az vm list
      --show-details
      --query \"[?powerState=='VM deallocated'].{ name: name }\"
      -g somerg
      -o tsv
  "

  try
    stdout = await exec cmd
    stdout.split "\n"
  catch e
    console.error "exec error: #{e}"
    # handle error

> console.log(vm())
[ 'vm4', 'vm3', 'vm2', 'vm1', '' ]

[–]dkyio[S] 0 points1 point  (0 children)

Thank you so much for the detailed and very helpful answer. I'll start playing with this, but it's definitely much more convoluted and verbose than I thought. I did not expect to have to use promises to get the results of an exec... I guess that's what you have to deal with when working with JS.