初识commander&自定义脚本执行命令

前言

嗯…. 今天没有前言

目标

模仿http-server 启动服务并打印服务相关信息在控制台

image.png

准备

npm依赖包

npm init -y //初始化项目
npm install http-server -g //安装依赖 
npm install commander //开发自定义的命令
npm install chalk //自定义控制台 打印信息颜色

复制代码

本地项目和本地npm模块之间建立连接

  • 文件结构

image.png

  • package.json 文件里 配置命令&要执行的文件
 "bin": {
    "fs": "./bin/www.js",
    "file-server": "./bin/www.js"
  },
复制代码
#! /usr/local/bin/node
//指定这个脚本的解释程序是node
console.log('npm link 测试ok')
复制代码
  • npm link

链接成功的样子

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN http-server@1.0.0 No description
npm WARN http-server@1.0.0 No repository field.

up to date in 0.255s
/usr/local/bin/fs -> /usr/local/lib/node_modules/http-server/bin/www.js
/usr/local/bin/file-server -> /usr/local/lib/node_modules/http-server/bin/www.js
/usr/local/lib/node_modules/http-server -> /Users/用户名/Documents/all_demo/demos/NODE-CORE/网络http系列/http-server
复制代码

潜在问题:环境path指向错误,一开始我用的是
#! /user/bin/env node
执行npm link 链接成功 但是 fs 执行命令 zsh: command not found: fs 错误

解决which node 命令来找到你本地的node安装路径,将/usr/bin/env改为你查找到的node路径即可。

  • 自定义命令 fs 和file-server 测试

    都成功将www.js 文件里的console打印了出来

image.png

开始

config.js 配置

const option = {
  port: {
    option: "-p, --port <n>", //根据commander的option(‘’)
    default: 8080, //默认端口
    usage: "fs --port 3000", //用法
    description: "set fs port", //描述
  }
};
module.exports = option;

复制代码

www.js

commander 对应的具体使用 和配置


const program = require("commander");
const option = require("./config");
const defaultMapping = {};

Object.entries(option).forEach(([key, value]) => {
  defaultMapping[key] = value.default;
});
// 打印用户输入的  配置项
// 例如 用户手动执行 fs --port 3000 ,那么打印的端口号就是永恒输入的
let userArgs = program.opts();
// 合并用户配置 和默认的 一同打印,查看启动参数
let serverOptions = Object.assign(defaultMapping, userArgs);

// 开始 启动一个服务 做测试
// 自定义的Server ?
const Server = require("../src/index");
let server = new Server(serverOptions);
复制代码

Server 类

保留了我写代码过程的console

const http = require("http");
const os = require("os");
//给控制台的打印 加点颜色
const chalk = require("chalk"); 

//os.networkInterfaces() 方法返回一个对象,其中包含有关每个网络接口的信息。返回的对象将包含网络接口数组
let interfaces = os.networkInterfaces();
// console.log(interfaces);//可以打印看 具体是什么
//这里interfaces 是数组 里有对象 对象值是一个数组,我们需要将这些对象合成一个简单数组
interfaces = Object.values(interfaces).reduce((memo, current) => {
  return memo.concat(current);
}, []);
let ip = interfaces.find((item) => {
  // 为什么是item.family==='IPv4'&&item.cidr.startWith('192')
  // ifconfig 查看我的ip=== 对应en0  的inet 对应值 192开头的ip
  //interfaces打印有很多ip  192开头 对应的是IPv4
  return item.family === "IPv4" && item.cidr.startsWith("192");
});
class Server {
  // 1:初始化默认参数
  constructor(serverOptions) {
    this.port = serverOptions.port;
  }
  start() {
    // http.createServer 创建一个本地服务
    const server = http.createServer((req, res) => {});
    //监听端口
    server.listen(this.port, (err) => {
      console.log(chalk.yellow("Starting up http-server, serving ./\n"));
      console.log("Available on:\n");
      console.log(`http://` + ip.address + `:${chalk.green(this.port)}\n`);
      console.log(`http://127.0.0.1:${chalk.green(this.port)}\n`);
    });

    // fs --port 3000 启动的服务的时候 设置端口号为3000,其他 均为config里的默认参数
  }
}
module.exports = Server;

复制代码

测试

样式上 不太好看 凑活看吧
image.png

最后如果觉得本文有帮助 记得点赞三连哦 十分感谢

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享