0

I created this simple console application to consume data from an API, I am looking for a way in which the previous contents on a user console will be cleared any time the program is run. I thought clear() should do that but it doesn't seem to be working. Any help on how to do this

'use strict';
var request = require('request'),
    clear   = require('clear'),
    figlet  = require('figlet'),
    inquirer    = require('inquirer'),
    chalk       = require('chalk'),
    Spinner     = require('clui').Spinner;
clear();
console.log(
  chalk.cyan(
    figlet.textSync('Welcome!', { horizontalLayout: 'left' })
  )
);

function getIpadd () {

    var status = new Spinner('Getting your IP, please wait...');

    status.start();
    //Request the IP address
    request('https://api.ipify.org?format=json', function (err, res, data){
         if (err) {
            console.log(err)
            return;
        }
        data = JSON.parse(data);
        status.stop();
        console.log('\nYour IP is: ' + data.ip + '\n');
        exitApp();
    });
}


function startApp() {
    inquirer.prompt([ {
    type: 'list',
    name: 'option',
    message: 'Press enter to Check Ip address',
    choices: [
      'Check my Ip address',
    ]
  } ]).then(function (answer) { 
        if(answer.option === 'Check my Ip address'){
            getIpadd();
        };
    });
}

//Function that Exits the app

function exitApp() {
    inquirer.prompt([ {
    type: 'list',
    name: 'option',
    message: 'Exit?',
    choices: [
      'Yes',
      'No',
    ]

  } ]).then(function (answer) {     
        if(answer.option === 'No') return startApp();

        if(answer.option === 'Yes') return console.log(chalk.yellow('=============Thanks for using this app!============='));
    });
}

startApp();
Kayode Adeola
  • 45
  • 1
  • 8
  • http://stackoverflow.com/questions/3011600/clear-javascript-console-in-google-chrome maybe one of the answers can help you – Thiago Custodio Apr 12 '17 at 20:54
  • The command line is cleared using `cls`. You can try running it using `gulp-shell`, or `gulp-exec`. Not submitting as an answer as I have no idea if this will work as I expect. –  Apr 12 '17 at 20:59
  • this will work on most terminals not sure on windows `console.log("\033[2J")` – adrianj98 Apr 12 '17 at 21:18
  • Finally got it to work on windows command, thanks all. While the `gulp-exec` as suggested by @Amy worked when I did a callback on `clear()`, problem is it also cleared the app display content. `console.log("\033[2J")` didn't work either. Funny thing is `clear()` worked after installing node module for `gulp-exec` – Kayode Adeola Apr 12 '17 at 21:50

1 Answers1

0

Using the cli-clear package instead of clear finally worked

Kayode Adeola
  • 45
  • 1
  • 8