Ext script test into console

Dear all,

How to test the ext script into console ?

Many thanks

Laurent

Copy this script to genieacs/config/ext/ext-runner.js

Then from your genieacs dir run node config/ext/ext-runner.js. It will then list out your scripts and the exported functions, simliar to

$ node config/ext/ext-runner.js
Must provide name of script and command to run.
Usage: node config/ext/ext-runner.js SCRIPT_NAME FUNCTION
Script: Function:
cpe-config portForwards, staticReservations, resetPppoe, getConfig
events send, diagnostic
network ip

And to execute a script you would do: node config/ext/ext-runner.js cpe-config getConfig. That will run the getConfig func in the cpe-config script using the block of parameters in let args = ...

-dan

"use strict";

const process = require('process');
const path = require('path');
const fs = require('fs');

const serial = 'some-serial';
const oui = '3c9066';
const productClass = '963168_OT142C_B';

let args = {
    serial: serial,
    oui: oui,
    productClass: productClass,
    deviceId: 'some-acs-id'
};

let scriptName = process.argv[2];
let cmd = process.argv[3];
let dir = path.dirname(__filename);
let runner = path.basename(__filename);
let cmdIndex = -1;

let script = scriptName ? loadScript(scriptName) : null;
if (script) {
    cmdIndex = Object.keys(script).indexOf(cmd);
}

if (cmd === undefined || script === null || cmdIndex === -1) {
    if (cmd !== undefined) {
        console.error('Invalid option: \t' + cmd);
    } else {
        console.error('Must provide name of script and command to run.');
    }

    showHelp();
    process.exit(255);
}

console.log('Running script ' + cmd);

script[cmd]([JSON.stringify(args)], function (e, result) {
    if (e) {
        console.error(e);
    }

    if (result) {
        console.log(result);
    }
});

function loadScript(name) {
    if (name.indexOf('.js') === -1) {
        name += ".js"
    }

    let fullPath = dir + '/' + name;
    if (!fs.existsSync(fullPath)) {
        console.error(fullPath + ' was not found');
        return;
    }

    return require(fullPath);
}

function showHelp() {
    console.log('Usage: node config/ext/' + runner + ' SCRIPT_NAME FUNCTION');

    var files = fs.readdirSync(dir);

    console.log('  ' + padRight("Script:", 20) + "Function:");

    for (var i = 0; i < files.length; i++) {
        if (files[i] === runner) {
            continue;
        }

        let scriptName = files[i].replace(/\.js/, '');
        let script = require(dir + '/' + files[i]);
        console.log('  ' + padRight(scriptName, 20) + Object.keys(script).join(', '));
    }
}


function padRight(value, length) {
    return (value.toString().length < length) ? padRight(value + ' ', length) : value;
}

function padLeft(value, length) {
    return (value.toString().length < length) ? padLeft(' ' + value, length) : value;
}
2 Likes

Hello,

Perfect !!!
Many thanks Dan for your help.

Have a good day

Laurent