使用 commander 定义脚手架命令

  • 使用commander 实现基本的脚手架命令流程;
const commander = require("commander");
const pkg = require("../package.json");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

program
    .name(Object.keys(pkg.bin)[0])
    .usage("<command> [options]")
    .version(pkg.version)
    .option("-d, --debug", "是否开启调试模式", true)
    .option("-e, --envName <envName>", "获取环境变量名称")
    .parse(process.argv)
  • 使用 commander 定义单线 命令;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

const clone = program.command("clone <source> [destination]");
clone
    .description("clone a repository")
    .option("-f, --force", "是否强制克隆")
    .action((source, destination, cmdObj) => {
        console.log("do clone", source, destination);
        console.log(cmdObj);
    });
program.parse(process.argv);
  • 使用 commander 定义复合命令;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

const service = new commander.Command("service");

service
    .command("start [port]")
    .description("start service at some port")
    .action((port) => { 
        console.log("do service start", port);
    });

service
    .command("stop [port]")
    .description("stop service at some port")
    .action((port) => { 
        console.log("do service stop", port);
    });

program.addCommand(service);

program.parse(process.argv);
  • 使用 commander 实现脚手架的转嫁;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

program
    .command("install [name]", "install package", {
        executableFile: "imst-cli", // imst-cli 为其它的脚手架,当前项目脚手架的名称为 igly-test-dev
        // isDefault: true
        hidden: true
    })
    .alias("i");
  
program.parse(process.argv);
  • 使用 commander 实现命令缺失的提示;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();
program
    .arguments("<cmd> [options]")
    .description("test command", {
        cmd: "command to run",
        options: "options for command"
    })
    .action((cmd, options) => {
        console.log(cmd, options);
    });

program.parse(process.argv);
  • 使用 commander 实现高级定制:自定义 hlep信息;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

program.helpInformation = () => "";

program.on("--help", () => {
    console.log("your help information");
});

program.parse(process.argv);
  • 使用 commander 实现高级定制:实现debug模式;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

program.on("option:debug", () => {
    // console.log("debug");
    if(program.opts().debug) {
        process.env.LOG_LEVEL = "verbose";
    }
    console.log(process.env.LOG_LEVEL);
});

program.parse(process.argv);
  • 使用 commander 实现高级定制;对未知命令的监听;
const commander = require("commander");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

program.on("command:*", (obj) => {
    console.error("未知命令:" + obj[0])
    const availableCommands = program.commands.map((cmd) => cmd.name());
    console.log("可用命令:", availableCommands.join(","));
});

program.parse(process.argv);

完整代码如下:

#! /usr/bin/env node

const commander = require("commander");
const pkg = require("../package.json");

// 手动实例化一个 commander 实例;
const program = new commander.Command();

program
    .name(Object.keys(pkg.bin)[0])
    .usage("<command> [options]")
    .version(pkg.version)
    .option("-d, --debug", "是否开启调试模式", true)
    .option("-e, --envName <envName>", "获取环境变量名称");

const clone = program.command("clone <source> [destination]");
clone
    .description("clone a repository")
    .option("-f, --force", "是否强制克隆")
    .action((source, destination, cmdObj) => {
        console.log("do clone", source, destination);
        console.log(cmdObj);
    });

const service = new commander.Command("service");

service
    .command("start [port]")
    .description("start service at some port")
    .action((port) => { 
        console.log("do service start", port);
    });

service
    .command("stop [port]")
    .description("stop service at some port")
    .action((port) => { 
        console.log("do service stop", port);
    });

program.addCommand(service);

program
    .command("install [name]", "install package", {
        executableFile: "imst-cli",
        // isDefault: true
        hidden: true
    })
    .alias("i")

// program
//     .arguments("<cmd> [options]")
//     .description("test command", {
//         cmd: "command to run",
//         options: "options for command"
//     })
//     .action((cmd, options) => {
//         console.log(cmd, options);
//     });

// 高级定制1:自定义help信息
// program.helpInformation = () => "";

// program.on("--help", () => {
//     console.log("your help information");
// });

// 高级定制2:实现debug模式
program.on("option:debug", () => {
    // console.log("debug");
    if(program.opts().debug) {
        process.env.LOG_LEVEL = "verbose";
    }
    console.log(process.env.LOG_LEVEL);
});

//高级定制3:对未知命令的监听
program.on("command:*", (obj) => {
    console.error("未知命令:" + obj[0])
    const availableCommands = program.commands.map((cmd) => cmd.name());
    console.log("可用命令:", availableCommands.join(","));
})


program
    .parse(process.argv);