Hi everyone, I’m Luo Zhu 🎋, a woodworking front end 🧚🏻♀️ living in Hangzhou. If you like my article 🧚🏻, you can gather spiritulist blog for me by clicking like.

Node.js is used for a wide range of purposes, and is well known to developers for developing services, desktop applications, and so on. There is another very useful scenario for Node.js – command-line applications.

This article can refer to the source: github.com/youngjuning…

HelloWorld

Create a youngjuning directory and enter the directory using:

$ mkdir youngjuning && cd youngjuning
Copy the code

Create bin/cli.js in this directory:

#! /usr/bin/env node
console.log('Hello World')
Copy the code

The bin item using package.json is used to specify the location of the executable file for each internal command:

"bin": {
  "youngjuning": "./bin/cli.js"
}
Copy the code

From the command line, type NPM link or NPM install -g to install the current project into the global environment, so you can run the file directly using Youngjuning:

commander.js

Node.js provides process.argv to read command line arguments. We won’t cover this here.

  • command: Defines a command line directive, which can be followed by a name, separated by a space, as in.command( 'app [name]')
  • alias: Defines a shorter command line instruction
  • description: description, which will be shown in help
  • option: Defines parameters. It takes four arguments
    • In the first argument, it can enter a short name-aAnd a long name- the app, the use of|or.When used on the command line, the two are equivalent, except that the latter can be retrieved from the program via callbacks
    • The second is the description, which will be inhelpShow it in the message
    • The third argument is the callback function, which receives one argumentstringSometimes we need a command line to create multiple modules and need a callback to handle it
    • The fourth parameter is the default value
  • action: Register onecallbackFunction, note that the callback does not currently support let declaration variables
  • parse: Parses the command line

inquire.js

Interactive command input plug-in!

The basic grammar

const answers = await inquirer.prompt([
  /* Pass your questions in here */
])
console.log(answers)
Copy the code

Parameters,

  • type: Indicates the type of the question, including:input.confirm.list.rawlist.expand.checkbox.password.editor;
  • name: stores the variable that answers the current question;
  • message: description of the problem;
  • default: Default value;
  • choicesThe: list option, available under certain types, and containing a separator;
  • validate: verifies the user’s answers;
  • filter: Filters user answers and returns the processed value.
  • transformer: Processing the display effect of the user’s answer (such as changing the font or background color of the answer), but will not affect the content of the final answer;
  • when: Judge whether the current question needs to be answered based on the answers to the previous questions;
  • pageSize: Modify somethingtypeNumber of render lines under type;
  • prefix: modifymessageDefault prefix;
  • suffix: modifymessageThe default suffix.

Implementing dynamic templates

  • usingrecursive-readdirThe library recursively reads all files in the project
  • usinghandlebarsTemplate replacement
  • usingfsThe module reads and writes files
const fs = require('fs')
const handlebars = require('handlebars')
const recursive = require('recursive-readdir')

const compile = (meta, file) = > {
  const content = fs.readFileSync(file).toString()
  const result = handlebars.compile(content)(meta)
  fs.writeFileSync(file, result)
}

module.exports = (meta, src) = > {
  recursive(src, (err, files) = > {
    files.forEach(file= > {
      compile(meta, file)
    })
  })
}
Copy the code

Command line tool

  • Chalk. Js: beautify the command line module
  • ora: Elegant terminal spinner
  • NCP: asynchronous copy files, including empty folders
  • Recursive-readdir: Lists recursively all files in directories and subdirectories, excluding the directory itself.
  • shelljs: Unix shell commands for Node.js
  • Log-symbols: Special symbols for printing logs
  • download-git-repo: Download and extract a git repository (GitHub, GitLab, Bitbucket) from node
  • Global-prefix: obtains the prefix of the NPM global installation
  • Exec-sh: Executes the shell command to forward all stdio streams, which is better than the shelljs exec command
  • WML: WML listens for changes in one folder (using Watchman) and copies the changed files to another folder.
  • Cmd-open: extends the Windows command open
  • shx: shx is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts.

This article was first published on the official website of Luozhu, as well as the public account luozhu Morning Teahouse and Gold digging Column.