利用Node.js創(chuàng)建一個密碼生成器的全步驟
一、 準(zhǔn)備工作
1.1 創(chuàng)建項目
$ npm init
1.2 安裝依賴
$ npm i commander chalk clipboardy
1.3 創(chuàng)建入口文件index.js
舉個🌰:來了解process.argv
// index.js console.log(process.argv)
終端執(zhí)行命令
$ node index
在終端可以看到
process.argv 屬性返回數(shù)組,其中包含啟動 Node.js 進(jìn)程時傳入的命令行參數(shù)。 第一個元素將是 process.execPath。 第二個元素將是正在執(zhí)行的 JavaScript 文件的路徑。 其余元素將是任何其他命令行參數(shù).
執(zhí)行命令
$ node index generate
第三個參數(shù):generate
二、 編寫命令行
2.1 添加版本和描述
// index.js
const program = require('commander');
program.version('1.0.0').description('Simple password generator').parse()
終端執(zhí)行命令:可以看到passgen的描述

繼續(xù)執(zhí)行命令:可以看到passgen的版本

2.2 配置密碼長度命令
const program = require('commander');
program.version('1.0.0').description('Simple password generator')
program.option('-l --length <number>', 'length of password').parse()
console.log(program.opts())
終端執(zhí)行命令:可以看到passgen的密碼長度命令

終端執(zhí)行命令:

2.2 密碼長度添加默認(rèn)值:8
program.option('-l --length <number>', 'length of password', '8').parse()
console.log(program.opts())
終端執(zhí)行命: 不設(shè)置密碼長度,可以看到使用的是默認(rèn)值-8

終端執(zhí)行命令: 設(shè)置密碼長度為10

2.3 配置保存密碼命令
program.option('-l --length <number>', 'length of password', '8')
.option('-s --save', 'save password to password.txt').parse()

2.4 配置密碼格式: 沒有數(shù)字
.option('-nn --no-number', 'remove numbers').parse()
終端執(zhí)行命: 默認(rèn)情況下有數(shù)字

終端執(zhí)行命: 設(shè)置清除數(shù)字密碼

2.5 配置密碼格式: 沒有符號
.option('-ns --no-symbols', 'remove symbols').parse()
終端執(zhí)行命: 默認(rèn)情況下有符號

終端執(zhí)行命: 設(shè)置清除數(shù)字密碼

三、 解析命令行-創(chuàng)建密碼
// index.js
const program = require('commander');
const createPassword = require('./utils/createPassword')
const log = console.log
program.version('1.0.0').description('Simple password generator')
program
.option('-l --length <number>', 'length of password', '8')
.option('-s --save', 'save password to password.txt')
.option('-nn --no-numbers', 'remove numbers')
.option('-ns --no-symbols', 'remove symbols').parse()
const {length, save, numbers, symbols} = program.opts()
// Get generated password
const generatedPassword = createPassword(length, numbers, symbols)
// Output generated password
log(generatedPassword)
創(chuàng)建utils/createPassword.js
// createPassword.js
const alpha = 'qwertyuiopasdfghjklzxcvbnm'
const numbers = '0123456789'
const symbols= '!@#$%^&*_-=+'
const createPassword = (length = 8, hasNumbers = true, hasSymbols = true) => {
let chars = alpha
hasNumbers ? (chars += numbers): ''
hasSymbols ? (chars += symbols): ''
return generatePassword(length, chars)
}
const generatePassword = (length, chars) => {
let password = ''
for(let i = 0; i < length; i++){
password+= chars.charAt(Math.floor(Math.random()*chars.length))
}
return password
}
module.exports = createPassword
終端執(zhí)行命令:查看密碼生成情況

3.1 添加color
// index.js
const chalk = require('chalk');
log(chalk.blue('Generated Password: ') + chalk.bold(generatedPassword))
終端執(zhí)行命令:可以看到顏色有變化

3.2 添加剪貼板
// index.js
const clipboardy = require('clipboardy');
// Copy to clipboardy
clipboardy.writeSync(generatedPassword)
log(chalk.yellow('Password copied to clipboardy!'))

四、 保存密碼到對應(yīng)的文件
// index.js
const savePassword = require('./utils/savePassword')
// Save to file
if (save) savePassword(generatedPassword)
創(chuàng)建utils/savePassword.js
const fs = require('fs')
const path = require('path')
const os = require('os')
const chalk = require('chalk')
const savePassword = (password) =>{
fs.open(path.join(__dirname, '../', 'passwords.txt'), 'a', '666', (e, id) => {
fs.write(id, password + os.EOL, null, 'utf-8', ()=>{
fs.close(id, ()=>{
console.log(chalk.green('Password saved to passwords.txt'))
})
})
})
}
module.exports = savePassword
終端執(zhí)行命令: 可以看到項目中生成passwords.txt文件,并且密碼已經(jīng)保存成功


五、將本地npm模塊配置成全局passgen
// package.json "preferGlobal": true, "bin":"./index.js",
終端執(zhí)行命令:

npm link命令:將npm 模塊鏈接到對應(yīng)的運(yùn)行項目中去,方便對本地模塊進(jìn)行調(diào)試和測試
//index.js #!/usr/bin/env node //首行添加
終端執(zhí)行命令:

總結(jié):大功告成✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️
參考鏈接:nodejs.cn/api/process…
總結(jié)
到此這篇關(guān)于利用Node.js創(chuàng)建一個密碼生成器的文章就介紹到這了,更多相關(guān)Node.js密碼生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何理解JS函數(shù)防抖和函數(shù)節(jié)流
函數(shù)防抖和函數(shù)節(jié)流都是對函數(shù)進(jìn)行特殊的設(shè)置,減少該函數(shù)在某一時間段內(nèi)頻繁觸發(fā)帶來的副作用。二者只是采用的設(shè)置方式和原理不一樣,其最終的目的是一樣的。2021-05-05

