前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >发布你的第一个node-cli

发布你的第一个node-cli

作者头像
theanarkh
发布2020-03-12 20:28:16
3740
发布2020-03-12 20:28:16
举报
文章被收录于专栏:原创分享

cli可以方便我们的日常工作,类似shell脚本一样。而且可以实现一次编写,到处运行。下面我们来看一下怎么编写一各node-cli。 首先新建一个目录scan-file,然后在目录下执行,执行npm init。初始化一下package.json。大概如下。

代码语言:javascript
复制
{
  "name": "scan-file",
  "version": "1.0.4",
  "description": "scan file and you can handle the content of file",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

接下来我们加一个配置。

代码语言:javascript
复制
{
  "name": "scan-file",
  "version": "1.0.4",
  "description": "scan file and you can handle the content of file",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "scan": "./index.js"
  },
  "author": "",
  "license": "ISC"
}

我们加了一个配置bin。他告诉npm执行我们的命令的时候,会执行哪个js。然后在scan-file目录下新建一个index.js。代码根据你cli的逻辑。我写了一个scan-file。遍历文件的工具。

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

const fs = require('fs');
const path = require('path');
if (!process.argv.slice(2).length) {
  console.error('please config the configPath: scan configPath=xxx');
  process.exit();
}
const params = {};
// parse the params
process.argv.slice(2).forEach((item) => {
  const [key, vlaue] = item.split(/\s*=\s*/);
  params[key] = vlaue;
});
// only one param: the configPath
const {
  configPath
} = params;
// parse the absolute path of configPath 
const configFilePath = path.resolve(configPath);
let config;
try {
  config = require(configFilePath);
} catch(e) {
  console.error(`${configFilePath} is not found`);
  process.exit();
}
// support config
let {
  root,
  exclude,
  output,
  hooks = []
} = config;
// support mutiple root and resolve them
root = [].concat(root).map((item) => {
  return path.resolve(item);
});

// dir queue
const dirQueue = root;
// file queue
const fileQueue = [];
let dir;

// collect all files path
while(dir = dirQueue.shift()) {
    // read all file of dir include subdir
    const files = fs.readdirSync(dir);
    files.forEach((filename) => {
        const currentFile = path.resolve(dir + '/' + filename);
        const stat = fs.statSync(currentFile);
        // you can exclude the file by return true
        if (typeof exclude === 'function' && exclude(currentFile, filename) === true) {
          return;
        }
        if (stat.isFile()) {
          fileQueue.push(currentFile);
        } else if (stat.isDirectory()) {
          dirQueue.push(currentFile);
        }
    })
}

let result = [];
fileQueue.forEach(function(filename) {
    var fileContent = fs.readFileSync(filename, 'utf-8');
    // you can handle the fileConent by mutiple hooks and the return of last hook is final result
    hooks.forEach(function(fn) {
        fileContent = fn(fileContent, filename);
    });
    result = result.concat(fileContent);
})

if (config.output) {
    if (typeof config.output === 'function') {
      config.output(result);
    } else {
      fs.writeFileSync(config.output, Array.from(new Set(result)).join('\n'), 'utf-8');
    }
} else {
  console.log(Array.from(new Set(result)).join('\n'));
}

脚本的第一行要写上#! /usr/bin/env node。他告诉npm生成对应的脚本。并且一定要写在第一行,因为操作系统的exec系统调用只会读取执行文件的第一行,从而判断需要加载的解释器。ok,我们执行npm login登录自己的账号,然后执行npm publish发布我们的cli。 我们开始试用一下cli。执行npm install scan-file -g安装。随便在一个cmd下执行scan configPath=xxx就可以了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程杂技 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档