Front end technology: command module and its execution method

I. create a command module 1,package.json

{
  "name": "@uad/nat-cli",
  "version": "0.0.2",
  "description": "Demo",
  "main": "index.js",
  "bin": {
    "artisan": "./src/artisan.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git"
  },
  "keywords": [
    "CLI"
  ],
  "author": "chunrong.liu",
  "license": "ISC",
  "dependencies": {
    "shelljs": "^0.8.3",
    "yargs": "^13.2.4"
  }
}

2,src/artisan.js

#!/usr/bin/env node
require('shelljs/global');
var argv = require('yargs')
.option('n', {
alias : 'name',
demand: true,
default: 'tom',
describe: 'your name',
type: 'string'
})
.usage('Usage: hello [options]')
.example('hello -n tom', 'say hello to Tom')
.help('h')
.alias('h', 'help')
.epilog('Copyright 2019')
.command("morning", "good morning", function (yargs) {
echo("Good Morning");
var argv = yargs.reset()
.option("m", {
alias: "message",
description: "provide any sentence"
})
.help("h")
.alias("h", "help")
.argv;
echo(argv.m);
})
.argv;
console.log('hello ', argv.n);
console.log(argv._);

II. Usage 1. After the command module is registered globally through npm link, the command can be directly used in the command line window 2. Reference the command module in package.json of other modules, and add scripts

"scripts": {
  "artisan": "artisan"
},
"dependencies": {
  ......
  "@uad/nat-cli": "^0.0.2",
  ......
}

After increasing the dependence on the command module, npm install will generate a shortcut to the command in the node [modules /. Bin directory, which can be used in scripts. The command is executed as follows:

npm run artisan -- -h

or

npx artisan -h

Usage: hello [options]

Order:

  artisan morning  good morning

Options: --Version display version number [Boolean] -N, -- name your name [string] [required] [default: "tom"] -h, --help display help information [Boolean]

Example:

hello -n tom  say hello to Tom

Copyright 2019.

Tags: Front-end npm JSON git

Posted on Mon, 04 Nov 2019 06:31:23 -0800 by jockey_jockey