Skip to content

Utility Functions

David Waring edited this page Feb 13, 2018 · 4 revisions

The following modules located in the /src/utils/ directory may be useful in the action function when executing the command.

When creating an external plugin command, these utility functions can be imported via the rtm-cli module.

Example:

const rtm = require('rtm-cli');

// Start a log spinner
rtm.log.spinner.start("Getting User...");

// Get RTM User info
rtm.config.user(function(user) {
  
  // Stop the spinner
  rtm.log.spinner.stop();

  // Print the RTM User
  console.log(user);

});

Available Utility Functions

  • config - get the user configuration (the default configuration properties or those overridden by the user configuration file or a configuration file provided as a CLI option)

    • config.client - The RTM client information
    • config.config - The entire configuration
    • config.get() - get the entire configuration
    • config.user(callback) - get the logged in user, where callback is a function accepting an RTMUser instance as an argument
    • config.reset(file) - reset the RTM configuration with the specified user config file, where file is the file path
    • config.read(file) - read a configuration file to merge with the existing configuration, where file is the file path
    • config.saveUser(user) - save the RTM User to the user configuration file, where user is an RTMUser instance
    • config.removeUser() - remove the saved RTM User information from the user configuration file
  • filter

    • filter(filter) - add additional RTM filters to the preconfigured filter options, where filter is the additional filter text
  • finish

    • finish() - use this function to finish the execution of a command. It will exit the process if not in interactive mode or return to the interactive prompt if started in interactive mode.
  • log - console printing functions

    • log(text, newline=true) - log the specified text to the console and optionally disable a newline at the end of the text
    • log.info(text, newline=true) - log the specified text styled as an info block and optionally disable a newline at the end of the text
    • log.error(text) - log an error message using the spinner fail function
    • log.style(text, style=reset, newline=false) - log the specified text using the specified chalk style attributes (ie bgYellow.black). You must specify when to end the line by setting newline=true.
    • log.spinner.start(text) - start the spinner with the specified text
    • log.spinner.stop() - stop the spinner
    • log.spinner.success(text) - Set the spinner as a success with the specified text
    • log.spinner.error(text) - Set the spinner as a failure with the specified text
  • prompt - console prompt function

    • prompt(prompt..., callback) - Prompt the user for a set of answers, where prompt is one or more questions to prompt the user with and callback is a callback function accepting a two-dimensional array of answers. The prompt will keep looping through the questions until a blank line is entered by the user.

An example for the prompt function:

const prompt = require('../utils/prompt.js');
prompt("Question 1:", "Question 2:", function(answers) {
  console.log(answers);
});

will have the following output:

[Enter a blank line when finished]
Question 1: Answer 1A
Question 2: Answer 2A
Question 1: Answer 1B
Question 2: Answer 2B
Question 1:
[ [ 'Answer 1A', 'Answer 2A' ], [ 'Answer 1B', 'Answer 2B' ] ]
Clone this wiki locally