4

This program will not terminate in the console, I have to use Ctrl-C. Documentation does not give any clues. Tried various things such as Return but just can't get it to terminate, it just hangs in the console. The last thing in the console is 'now we are here'.

var fs = require('fs');
var path = require('path');
var co = require('co');
var prompt = require('co-prompt');
var program = require('commander');

program
.arguments('<file>')
.option('-i, --Object name <objectName>', 'DP device IP address')
.action(function(file) {
    co(function *() {
        var objectName = yield prompt('Object Name: ');
        console.log('\nObject Name: %s file: %s',objectName, file);
        notmain(file, objectName);
        console.info('now we are here');
    });
})
.parse(process.argv);

function notmain(file, objectName) {
    try {
        var normalPath = path.normalize(__dirname + '/' + file);
        console.info('\nfile path  ' + normalPath);
        certstring = fs.readFileSync(normalPath).toString();
        console.info('\nstring of cert file is  \n' + certstring);
        clientcert = fs.readFileSync(normalPath).toString('base64');
        console.info('\nbase64 string of cert file is  \n' + clientcert);
        var newJson = {};
        newJson.name = objectName;
        newJson.content = clientcert;
        var newfile = {"file": newJson};
        console.info('\nnew json for cert object  ' +     JSON.stringify(newfile));
        console.info('\nclient certificate read from directory  ');
    } catch (err) {
        console.info('file path  ' + normalPath);
        console.info('client certificate file not found');
        return;
    }
}
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
Nepomuk
  • 55
  • 1
  • 5
  • The only thing that works in process.exit(0) but other answers say that is a last resort. The program is obviously waiting for something, I need to tell it that its finished. – Nepomuk Nov 22 '16 at 13:35
  • `co` is not defined, and (whatever it is) looks like the thing that needs telling to stop. – OrangeDog Nov 22 '16 at 13:41
  • co is a wrapper for asynch code using generators and promises. It's defined at the top. – Nepomuk Nov 22 '16 at 15:16
  • Seems odd that you're using that, but then you use `readFileSync` and lose all your async advantage. – OrangeDog Nov 22 '16 at 15:35

2 Answers2

0

The console is waiting on more input. Try adding this after the 'now we are here' line.

process.stdin.pause();

like this

co(function *() {
    var objectName = yield prompt('Object Name: ');
    console.log('\nObject Name: %s file: %s',objectName, file);
    notmain(file, objectName);
    console.info('now we are here');
    process.stdin.pause();
});
curtwphillips
  • 5,441
  • 1
  • 20
  • 19
0
co(function *() {
        var objectName = yield prompt('Object Name: ');
        console.log('\n1 Object Name: %s file: %s',objectName, file);
        var normalPath = path.normalize(__dirname + '/' + file);
        console.info('\n2 file path  ' + normalPath);
        fs.readFile(normalPath, function(err, data) {
            if (err) {
                throw err;
            }
            console.info('\n4 string of cert file is  \n' + data);
            if (!(data.includes('BEGIN CERTIFICATE'))) {
                throw 'invalid file type';
            }
            let b64 = data.toString('base64');
            notmain(b64, objectName, results);
        })
        process.stdin.pause();
    })
Nepomuk
  • 55
  • 1
  • 5