前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 Node.js 开发 CLI | moq

使用 Node.js 开发 CLI | moq

作者头像
yiyun
发布2022-04-01 16:15:43
5660
发布2022-04-01 16:15:43
举报
文章被收录于专栏:yiyun 的专栏

引言

通过 Node.js 编写一个 全局可用 CLI,用于日常生活。

功能如下:

  1. 实现执行下方语句,将用于笔记本的Hexo文章中公开文章复制到 用于博客的 Hexo 文章中:
代码语言:javascript
复制
moq hexop './' '../yiyungent.github.io'

npm 初始化 项目

新建文件夹 moq

代码语言:javascript
复制
mkdir moq

进入文件夹

代码语言:javascript
复制
cd moq

npm 初始化项目

代码语言:javascript
复制
npm init

输入项目描述

完成 package.json 的创建

自定义命令

package.json 添加 bin

代码语言:javascript
复制
"bin": {
    "moq": "index.js"
},

完整 package.json 如下:

代码语言:javascript
复制
{
  "name": "moq",
  "version": "0.1.0",
  "description": "a CLI tool for daily life.",
  "main": "index.js",
  "bin": {
    "moq": "index.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/yiyungent/moq"
  },
  "keywords": [
    "cli"
  ],
  "author": "yiyun <yiyungent@gmail.com>",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/yiyungent/moq/issues"
  },
  "homepage": "https://github.com/yiyungent/moq#readme"
}

bin 使得 moq 成为一个可执行命令,如 npm init 中的 npm,而命令所执行文件即是 ./index.js

测试

新建 index.js,内容如下:

代码语言:javascript
复制
#!/usr/bin/env node

console.log("执行成功")

!/usr/bin/env node 表明 当前文件需以 Node.js 脚本执行

完成后,即可全局安装 moq,在项目所在目录执行:

代码语言:javascript
复制
npm install -g

此时全局安装成功,下面测试命令:

代码语言:javascript
复制
moq

测试成功

交互式命令行

这里依赖两个库进行开发

代码语言:javascript
复制
npm install commander
代码语言:javascript
复制
npm install inquirer

index.js 添加

代码语言:javascript
复制
const { program } = require('commander');
const inquirer = require('inquirer');

1. moq hexop

1.1 解析 YAML

使用:https://github.com/nodeca/js-yaml

代码语言:javascript
复制
npm install js-yaml

1.2 编写 tools.js

新建 tools.js,内容如下:

代码语言:javascript
复制
const fs = require("fs"),
  stat = fs.stat,
  path = require("path");

/*
 * 复制目录中的所有文件包括子目录
 * @param{ String } 需要复制的目录
 * @param{ String } 复制到指定的目录
 */
let copy = function (src, dst) {
  // 读取目录中的所有文件/目录
  fs.readdir(src, function (err, paths) {
    if (err) {
      throw err;
    }

    paths.forEach(function (path) {
      var _src = src + "/" + path,
        _dst = dst + "/" + path,
        readable,
        writable;

      stat(_src, function (err, st) {
        if (err) {
          throw err;
        }

        // 判断是否为文件
        if (st.isFile()) {
          // 创建读取流
          readable = fs.createReadStream(_src);
          // 创建写入流
          writable = fs.createWriteStream(_dst);
          // 通过管道来传输流
          readable.pipe(writable);
        }
        // 如果是目录则递归调用自身
        else if (st.isDirectory()) {
          exists(_src, _dst, copy);
        }
      });
    });
  });
};

// 在复制目录前需要判断该目录是否存在,不存在需要先创建目录
let exists = function (src, dst, callback) {
  fs.exists(dst, function (exists) {
    // 已存在
    if (exists) {
      callback(src, dst);
    }
    // 不存在
    else {
      fs.mkdir(dst, function () {
        callback(src, dst);
      });
    }
  });
};

let deleteFile = function deleteFile(path) {
  var files = [];
  if (fs.existsSync(path)) {
    files = fs.readdirSync(path);
    files.forEach(function (file, index) {
      var curPath = path + "/" + file;
      if (fs.statSync(curPath).isDirectory()) {
        deleteFile(curPath);
      } else {
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
};

let mapDir = function mapDir(dir, callback, finish) {
  fs.readdir(dir, function (err, files) {
    if (err) {
      console.error(err);
      return;
    }
    // .md 文件数
    let fileNum = 0;
    files.forEach((filename, index) => {
      let pathname = path.join(dir, filename);
      fs.stat(pathname, (err, stats) => {
        // 读取文件信息
        if (err) {
          console.log("获取文件stats失败");
          return;
        }
        if (stats.isDirectory()) {
          // 不递归文件夹
          //mapDir(pathname, callback, finish)
        } else if (stats.isFile()) {
          if ([".md"].includes(path.extname(pathname))) {
            // 只要 .md 文件

            fs.readFile(pathname, (err, data) => {
              if (err) {
                console.error(err);
                return;
              }
              callback && callback(data, filename, pathname);
            });

            fileNum++;
            if (index === files.length - 1) {
              finish && finish(fileNum);
            }
          }
        }
      });
    });
  });
};

let getFileNameWithoutExt = function (filename) {
  let endIndex = filename.lastIndexOf(".");
  if (endIndex != -1) {
    return filename.substring(0, endIndex);
  }
  return filename;
};

module.exports = { copy, exists, deleteFile, mapDir, getFileNameWithoutExt };

1.3 编写 index.js

代码语言:javascript
复制
#!/usr/bin/env node

const { program } = require("commander");
const inquirer = require("inquirer");
const fs = require("fs");
const yaml = require("js-yaml");
const tools = require("./tools");

program
  .command("hexop <noteRoot> <blogRoot>")
  .description(
    "将 Hexo笔记中 标记为public的文章(source/_posts) 复制到 Hexo Blog 中,以供发布"
  )
  .action((noteRoot, blogRoot) => {
    // 1. 先清空 <blogRoot>/source/_posts, 注意:_posts 文件夹也会被删除
    tools.deleteFile(`${blogRoot}/source/_posts`);
    console.log(`清空 '${blogRoot}/source/_posts' 成功`);
    fs.mkdirSync(`${blogRoot}/source/_posts`);
    // 提取 markdown 中的 front-matter
    let re = /---(.*?)---/s;
    const defaultPublic = true;
    let publicNum = 0;
    let totalNum = 0;
    tools.mapDir(
      noteRoot + "/source/_posts",
      function (data, filename, pathname) {
        let s = re.exec(data)[1];
        let doc = yaml.load(s);
        if (doc.public == undefined) {
          doc.public = defaultPublic;
        }
        if (doc.public) {
          publicNum++;
          // 2. 复制公开文章文件及对应媒体文件夹 到 <blogRoot>/source/_posts
          let temp = `${blogRoot}/source/_posts/${filename}`;
          fs.copyFileSync(pathname, temp);
          const src = tools.getFileNameWithoutExt(pathname);
          const dst = tools.getFileNameWithoutExt(temp);
          if(fs.existsSync(src)) {
            tools.exists(
              src,
              dst,
              tools.copy
            );
          }
          
          console.log(`${publicNum}: ${tools.getFileNameWithoutExt(filename)}`);
          if(publicNum == totalNum) {
            console.log(`复制完毕: ${publicNum}/${totalNum} 公开/总共`);
          }
        }
      },
      function (fileNum) {
        totalNum = fileNum;
      }
    );
    
  });

// 解析来自process.argv上的数据,commander会自动帮助我们添加一个 -h 的解析
program.parse(process.argv);

1.4 测试

moq 项目下执行

代码语言:javascript
复制
npm install -g

notebook 项目下执行

代码语言:javascript
复制
moq hexop './' '../yiyungent.github.io'

1.5 创建 note-to-blog.ps1

在 用于笔记本 的 Hexo 根目录:notebook 创建 note-to-blog.ps1 文件

内容如下:

代码语言:javascript
复制
moq hexop './' '../yiyungent.github.io'
cd ../yiyungent.github.io
git add source/_posts/*
git commit -m 'feat(posts): note-to-blog'
git push
cd ../notebook

注意: yiyungent.github.io 为本人博客项目文件夹,与 notebook 处于同一级,所以才使用 ../yiyungent.github.io./ 表示当前路径 最后 cd ../notebook 又切回来,方便以后操作,当然也可以不要

发布到 npm

代码语言:javascript
复制
npm publish --registry https://registry.npmjs.org

CLI简介

举例:vue-cli: vue create app

代码语言:javascript
复制
command [subCommand] [options] [arguments]

command:命令,比如 vue subCommand:子命令,比如 vue create options:选项,配置,同一个命令不同选项会有不一样的操作结果,比如 vue -h,vue -v arguments:参数,某些命令需要使用的值,比如 vue create myApp 选项与参数的区别:选项是命令内置实现,用户进行选择,参数一般是用户决定传入的值

选项一般会有全拼与简写形式(具体看使用的命令帮助),比如 --version = -v 全拼:以 -- 开头 / 简写:以 - 开头 选项也可以接受值,值写在选项之后,通过空格分隔 多个简写的选项可以连写,开头使用一个 - 即可,需要注意的是,如果有接受值的选项需要放在最后,比如: vue create -d -r <-r的值> myApp vue create -dr <-r的值> myApp

执行 PowerShell(xxx.ps1)文件

代码语言:javascript
复制
./note-to-blog.ps1

参考

感谢帮助!

本文作者: yiyun

本文链接: https://cloud.tencent.com/developer/article/1970794

版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
  • npm 初始化 项目
  • 自定义命令
    • 测试
    • 交互式命令行
    • 1. moq hexop
      • 1.1 解析 YAML
        • 1.2 编写 tools.js
          • 1.3 编写 index.js
            • 1.4 测试
              • 1.5 创建 note-to-blog.ps1
              • 发布到 npm
                • CLI简介
                  • 执行 PowerShell(xxx.ps1)文件
                  • 参考
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档