Monday, 22 May 2017

Node: 2D array not filling up

Hello im trying to make a function that returns version numbers of a installed program on the clients machine. so far i got the version numbers but now im trying to store them into a 2d Array to use them later.

My function:

const EventEmitter = require('events');
const spawn = require('child_process').spawn;
const inst = {};

inst.check = (cmd, callback) => {
    let command;
    let res = [
        ['ffmpeg',''],
        ['fpcalc',''],
        ['eyed3','']
    ];
    let pos;
    let count = 0;
    // Spwan all the comands
    for (let i = 0; i < cmd.length; i++) {
        pos = i;
        switch (cmd[i]) {
            case 'ffmpeg':
                command = spawn(cmd[i], ['-version']);
                break;

            case 'fpcalc':
                command = spawn(cmd[i], ['-v']);
                break;

            case 'eyed3':
                command = spawn(cmd[i], ['--version']);
                break;
            default:
        }
        // Add the result to the res array
        command.stdout.on('data', (data) => {
            res[pos][1] = data.toString().slice(15, 20);
            console.log(res[pos][1]);
        });
        command.stderr.on('data', (data) => {
            res[pos][1] = data.toString();
            console.log(res[pos][1]);
        });
        command.on('error', (err) => {
            res[pos][1] = 'error';
            throw err;
        });
    }
    return callback(res);
};

module.exports = inst;

i call the function with this:

const cmd = ['ffmpeg', 'fpcalc', 'eyed3'];
isInstalled.check(cmd, (res) => {
    console.log(res);
});

but i get following result :

[ [ 'ffmpeg', '' ], [ 'fpcalc', '' ], [ 'eyed3', '' ] ]

can someone tell me what im doing wrong ?



via Nicolai

No comments:

Post a Comment