发布你的第一个node-cli

  • 2020 年 3 月 12 日
  • 筆記

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

{    "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"  }

接下来我们加一个配置。

{    "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。遍历文件的工具。

#! /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就可以了。