發布你的第一個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就可以了。