@softeq/utils

0.1.0 • Public • Published

Javascript Utils

Overview

Implementation of helper tools.
Development documentation

Install

npm install @softeq/utils

Usage

Package helper tools are responsible on helpers to simplify interaction with server commands. Node.js has child_process module that provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3)

Default child_process.exec callback has a bit non standard arguments order to have more stable common handlers callback approach. Default handler callback:

/**
 * @param {Error} error - Instance of Error object. Can be thrown when runtime errors occur.
 * @param {string | Buffer} stdout - `stdout` unix output string
 * @param {string | Buffer} stderr - `stderr` unix output string
 */
function callback(error, stdout, stderr) {
  // ...
}

exec('ps', (error, stdout, stderr) => {
  // ...
})

Helper function execAction provides more standard arguments order and provides internal errors handlers according to conditional configuration exitConditions.

/**
 * @param {string | Buffer} response - `stdout` unix output string
 * @param {string | Buffer} stderr - `stderr` unix output string
 * @param {Error} error - Instance of Error object. Can be thrown when runtime errors occur.
 */
function callback(response, stderr, error) {
  // ...
}

exec('ps', execAction((response, stderr, error) => {
  // ...
}, { stderr: true, error: false }));

Helper function stop stops any running process and displays message according to the Logger.stack method.

exec('ps', execAction(() => {
  stop([`${ANSI_FG_RED}%s${ANSI_FG_NC}`, `The machine will not be reboot according to the \`stop\` command.`]);
  exec('sudo shutdown -r now', execAction(() => {
    Logger.stack([
      [`${ANSI_FG_RED}%s${ANSI_FG_NC}`, `The machine has been reboot.`],
    ]);
  }))
}));

Pay attention to the argument of stop function:

Colors:

  • red
  • yellow
  • green
  • no color

Note: try to consider why do we need no color ANSI escape code.

Logger

Node.js interaction with server requires terminal notifications often. Logger class provides the common way to compose notifications with next static methods.

static Logger.stack

Displays the stack of messages. Message is an array of console.log arguments.

Logger.stack([
  [`%s`, `${ANSI_FG_GREEN}Response:${ANSI_FG_NC}`],
  [response],
]);
static Logger.error

Displays an error according to unix output standard with Node.js improvement according to conditional configuration exitConditions.

Logger.error(error, stderr, { stderr: true, error: false });
Common
  • Some terminal messages can contain EMPTY_LINE as a new line or EMPTY_STRING.
  • Examples are focused on Unix environment.

Readme

Keywords

Package Sidebar

Install

npm i @softeq/utils

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

19.3 kB

Total Files

8

Last publish

Collaborators

  • softeq