接入 commander.js
和 Inquirer.js
以後,本應該直接接上 colors.js
,畢竟咱們如今是控制檯輸出,控制檯不搞得飄飄亮亮(花裏胡哨的)。前端
可是上篇 Inquires.js
太給力了,直接上了 1.7w 字,因此相對而言,這篇就簡短點唄。node
閱讀提示:本篇文章略短,易讀,下一篇再搞大的,不接受吐槽~
colors.js
是 Marak 作的一個 4.1k star(2021-06-16)的倉庫。git
接入 colors.js
後可讓你的控制檯更爆炸更有美感。github
npm i colors
src/index.ts
import program from 'commander'; import common from './common'; import colors from 'colors'; program .version('0.0.1') .description('工具庫') program .command('jsliang') .description('jsliang 幫助指令') .action(() => { common(); }); program .command('test') .description('測試頻道') .action(() => { const text = ` _ _____ _ _ ___ __ _ _____ | | / ___/ | | | | / | | \\ | | / ___| | | | |___ | | | | / /| | | \\| | | | _ | | \\___ \\ | | | | / /—| | | |\\ | | | _ | |_| | ___| | | |___ | | / / | | | | \\ | | |_| | \\_____/ /_____/ |_____| |_| /_/ |_| |_| \\_| \\_____/ `; console.log(colors.rainbow(text)); }); program.parse(process.argv);
package.json
{ "name": "jsliang", "version": "1.0.0", "description": "Fe-util, Node 工具庫", "main": "index.js", "scripts": { "jsliang": "ts-node ./src/index.ts jsliang", "test": "ts-node ./src/index.ts test" }, "keywords": [ "jsliang", "Node 工具庫", "Node" ], "author": "jsliang", "license": "ISC", "devDependencies": { "@types/inquirer": "^7.3.1", "@types/node": "^15.12.2", "@typescript-eslint/eslint-plugin": "^4.26.1", "@typescript-eslint/parser": "^4.26.1", "eslint": "^7.28.0", "ts-node": "^10.0.0", "typescript": "^4.3.2" }, "dependencies": { "colors": "^1.4.0", "commander": "^7.2.0", "inquirer": "^8.1.0", "rxjs": "^5.5.12" } }
npm run test
發現控制檯老漂亮了:typescript
在上面代碼中,添加了 test
相關指令(後續咱們測試內容就塞到這裏,能夠不用加,可是 jsliang 會拿來作示例用)npm
至於這個好看的字體,就是經過 ASCII 藝術字轉換器轉換過來的。json
這邊隨意推薦 2 個,更多的小夥伴能夠自行挖掘。dom
工欲善其事,必先利其器。工具
在上面咱們展現了 colors.js
中的一種彩虹色 colors.rainbow
,那麼確定會有其餘顏色。測試
import colors from 'colors'; console.log(colors.rainbow('rainbow')); console.log(colors.black('black')); console.log(colors.red('red')); console.log(colors.green('green')); console.log(colors.yellow('yellow')); console.log(colors.blue('blue')); console.log(colors.magenta('magenta')); console.log(colors.cyan('cyan')); console.log(colors.white('white')); console.log(colors.gray('gray')); console.log(colors.grey('grey')); console.log(colors.bgBlack('bgBlack')); console.log(colors.bgRed('bgRed')); console.log(colors.bgGreen('bgGreen')); console.log(colors.bgYellow('bgYellow')); console.log(colors.bgBlue('bgBlue')); console.log(colors.bgMagenta('bgMagenta')); console.log(colors.bgCyan('bgCyan')); console.log(colors.bgWhite('bgWhite')); console.log(colors.bgGrey('bgGrey')); console.log(colors.reset('reset')); console.log(colors.bold('bold')); console.log(colors.dim('dim')); console.log(colors.italic('italic')); console.log(colors.underline('underline')); console.log(colors.inverse('inverse')); console.log(colors.hidden('hidden')); console.log(colors.strikethrough('strikethrough')); console.log(colors.rainbow('rainbow')); console.log(colors.zebra('zebra')); console.log(colors.america('america')); console.log(colors.trap('trap')); console.log(colors.random('random'));
將它們丟到 test
中,執行 npm run test
,獲得花裏花哨的打印:
OK,在上面咱們已經華麗呼哨了,每次打印若是都要引用一遍 colors
那就有點說不過去啦。
因此我們重寫 console.log
,這樣只要有地方用到它了咱們就有彩虹色了!
base/getType.ts
/** * @name getType * @description 獲取類型 * @param {string|object} param 傳入的變量 */ export const getType = (param: string): string => { return Object.prototype.toString.call(param).slice(8, -1); };
base/console.ts
import colors from 'colors'; import { getType } from './getType'; // 打印索引 let consoleIndex = 1; // 重寫 console.log const log = console.log; console.log = (...args: any) => { log(`\n---${consoleIndex++}---`); for (let i = 0; i < args.length; i++) { const arg = args[i]; if (['String', 'Number', 'Boolean'].includes(getType(arg))) { log(colors.rainbow(String(arg))); } else { log(arg); } } }; // 重寫 console.error const error = console.error; console.error = (...args: any) => { log(`\n---${consoleIndex++}---`); for (let i = 0; i < args.length; i++) { const arg = args[i]; if (['String', 'Number', 'Boolean'].includes(getType(arg))) { error(colors.red(String(arg))); } else { error(arg); } } };
而後在 src/index.ts
中引用這個重寫的 console.ts
,這樣全局就可使用到了:
src/index.ts
import program from 'commander'; import common from './common'; import './base/console'; program .version('0.0.1') .description('工具庫') program .command('jsliang') .description('jsliang 幫助指令') .action(() => { common(); }); program .command('test') .description('測試頻道') .action(() => { console.log('There is jsliang?', true); console.error('隨便報個錯,代表它有問題'); }); program.parse(process.argv);
這時候運行 npm run test
打印效果爲:
其實彩虹色看起來太花裏胡哨了,可是暫時這邊不更改吧,小夥伴們能夠自行更換顏色
那麼,花裏花哨的接入就完畢了,雖然都是 API 複製粘貼工程師,可是作下裝飾搞好看一點仍是能夠有的~
下一篇見!
不折騰的前端,和鹹魚有什麼區別!
jsliang 的文檔庫由 梁峻榮 採用 知識共享 署名-非商業性使用-相同方式共享 4.0 國際 許可協議 進行許可。<br/>基於 https://github.com/LiangJunrong/document-library 上的做品創做。<br/>本許可協議受權以外的使用權限能夠從 https://creativecommons.org/licenses/by-nc-sa/2.5/cn/ 處得到。