All files / src/bin cli.ts

0% Statements 0/8
0% Branches 0/2
0% Functions 0/2
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                                                                                             
/**
 * @namespace cli
 * @description This script demonstrates a basic command-line interface (CLI) that counts down from 60 seconds and then exits.
 * @summary A simple CLI countdown timer that serves as a minimal example of how to create a CLI application using TypeScript.
 *
 * All files in the ./src/bin folder will have `#!/usr/bin/env node` included at the beginning of the file
 *
 *
 * @example
 * // Run the script
 * node cli.js
 *
 * @mermaid
 * sequenceDiagram
 *   participant User
 *   participant CLI
 *   participant Timer
 *   User->>CLI: Run script
 *   CLI->>User: Display initial message
 *   CLI->>Timer: Start countdown
 *   loop Every second
 *     Timer->>CLI: Decrement counter
 *     CLI->>User: Display current count
 *   end
 *   Timer->>CLI: Counter reaches 0
 *   CLI->>User: Exit process
 *
 * @memberOf module:ts-workspace
 * @see {@link https://nodejs.org/api/process.html#process_process_exit_code|Node.js process.exit()}
 * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/setTimeout|MDN setTimeout()}
 */
 
/**
 * @const counter
 * @name counter
 * @description The countdown timer, initialized to 60 seconds.
 * @summary Used to track the remaining time in the countdown.
 * @type {number}
 * @memberOf module:ts-workspace.cli
 */
let counter = 60;
console.log(`This is a poor example of a cli. will stop in ${60} seconds`);
 
/**
 * @function iterator
 * @description A recursive function that manages the countdown timer.
 * @summary It uses setTimeout to create a delay of 1 second between each count.
 * The function decrements the counter, logs the current count, and calls itself
 * until the counter reaches 0. When the counter reaches 0, the process exits.
 *
 * @return {void}
 * @memberOf module:ts-workspace.cli
 */
function iterator() {
  setTimeout(() => {
    if (!--counter) process.exit(1);
    console.log(counter);
    iterator();
  }, 1000);
}
 
iterator();