Only allow commands to be run if a setup one has been completed in Commander.js

234 Views Asked by At

Is it possible in commander.js to check that a setup command has been run and completed before allowing any other actions to be run.

If a command is attempted to be run it will show a message to run the $ command auth first

1

There are 1 best solutions below

0
On

There is no way to easily do that in commander.js. You need to do that on each commands .action, or you could do it when you setup the commander.

program
  .command('setup')
  .action(function () {
    console.log('setup done');
    storeSetup();
  });

if (setupHasRun()) {
  program
    .command('ls [dirs...]')
    .action(function (dirs) {
        console.log('ls %s', dirs);
    });

  program 
    .command('rmdir <dir> [otherDirs...]')
    .action(function (dir, otherDirs) {
      console.log('rmdir %s', dir);
      if (otherDirs) {
        otherDirs.forEach(function (oDir) {
          console.log('rmdir %s', oDir);
        });
      }
    });  
}