Building Command Line Interfaces with Node.js

Explore methods for developing command-line applications using Node.js, enhancing automation and scripting capabilities.

0 likes
5 views

Rule Content

{
  "title": "Building Command Line Interfaces with Node.js",
  "description": "Explore methods for developing command-line applications using Node.js, enhancing automation and scripting capabilities.",
  "category": "Node.js Cursor Rules",
  "rules": [
    {
      "name": "Use a Command-Line Argument Parsing Library",
      "description": "Utilize libraries like 'yargs' or 'commander' to handle command-line arguments, ensuring POSIX-compliant syntax and improved user experience.",
      "examples": [
        {
          "before": "const args = process.argv.slice(2);",
          "after": "const yargs = require('yargs/yargs');\nconst { argv } = yargs(process.argv.slice(2)).option('verbose', { alias: 'v', type: 'boolean', description: 'Run with verbose logging' });"
        }
      ],
      "references": [
        "https://dev.to/boudydegeer/mastering-nodejs-cli-best-practices-and-tips-7j5"
      ]
    },
    {
      "name": "Implement Consistent Naming Conventions",
      "description": "Adopt consistent naming conventions for variables, functions, and files to enhance code readability and maintainability.",
      "examples": [
        {
          "before": "let user_name = 'John';\nfunction GetUserData() {}",
          "after": "let userName = 'John';\nfunction getUserData() {}"
        }
      ],
      "references": [
        "https://medium.com/deno-the-complete-reference/five-clean-coding-best-practices-in-node-js-1651d5fd3ffc"
      ]
    },
    {
      "name": "Ensure Cross-Platform Compatibility",
      "description": "Design CLI applications to function seamlessly across different operating systems by handling file paths and command syntax appropriately.",
      "examples": [
        {
          "before": "const myPath = `${__dirname}/../bin/myBin.js`;",
          "after": "const path = require('path');\nconst myPath = path.join(__dirname, '..', 'bin', 'myBin.js');"
        }
      ],
      "references": [
        "https://github.com/IvanDrop9/nodejs-cli-apps-best-practices"
      ]
    },
    {
      "name": "Accept Input via Standard Input (STDIN)",
      "description": "Enable the CLI to accept input through STDIN to facilitate integration with other command-line tools and support piping.",
      "examples": [
        {
          "before": "const input = process.argv[2];",
          "after": "const readline = require('readline');\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\nrl.question('Enter input: ', (answer) => { /* process answer */ rl.close(); });"
        }
      ],
      "references": [
        "https://github.com/IvanDrop9/nodejs-cli-apps-best-practices"
      ]
    },
    {
      "name": "Provide Structured Output",
      "description": "Offer options for structured output formats like JSON to facilitate parsing and integration with other tools.",
      "examples": [
        {
          "before": "console.log('Result: ' + result);",
          "after": "console.log(JSON.stringify({ result }));"
        }
      ],
      "references": [
        "https://github.com/IvanDrop9/nodejs-cli-apps-best-practices"
      ]
    },
    {
      "name": "Handle POSIX Signals Gracefully",
      "description": "Implement handlers for POSIX signals like SIGINT to ensure the CLI terminates gracefully and cleans up resources.",
      "examples": [
        {
          "before": "// No signal handling",
          "after": "process.on('SIGINT', () => { console.log('Process interrupted. Exiting gracefully.'); process.exit(0); });"
        }
      ],
      "references": [
        "https://dev.to/boudydegeer/mastering-nodejs-cli-best-practices-and-tips-7j5"
      ]
    },
    {
      "name": "Use ESLint for Code Consistency",
      "description": "Integrate ESLint into the project to enforce code style and catch potential errors early.",
      "examples": [
        {
          "before": "// No linting setup",
          "after": "npx eslint --init"
        }
      ],
      "references": [
        "https://developers.redhat.com/articles/2021/05/17/introduction-nodejs-reference-architecture-part-3-code-consistency"
      ]
    },
    {
      "name": "Modularize Code for Maintainability",
      "description": "Organize code into modules based on functionality to improve readability and facilitate testing.",
      "examples": [
        {
          "before": "// All logic in a single file",
          "after": "commands/\n  user.js\nservices/\n  api.js\nutils/\n  format.js\nindex.js"
        }
      ],
      "references": [
        "https://expertbeacon.com/writing-command-line-interfaces-in-node-js-like-an-expert/"
      ]
    }
  ]
}