国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

利用Node.js創(chuàng )建一個(gè)密碼生成器的全步驟

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:技術(shù)爬行之旅 欄目: JavaScript 歡迎投稿:712375056

目錄

        一、 準備工作

        1.1 創(chuàng )建項目

        $ npm init 
        

        1.2 安裝依賴(lài)

        $ npm i commander chalk clipboardy
        

        1.3 創(chuàng )建入口文件index.js

        舉個(gè)🌰:來(lái)了解process.argv

        // index.js
        console.log(process.argv)
        

        終端執行命令

        $ node index
        

        在終端可以看到

        process.argv 屬性返回數組,其中包含啟動(dòng) Node.js 進(jìn)程時(shí)傳入的命令行參數。 第一個(gè)元素將是 process.execPath。 第二個(gè)元素將是正在執行的 JavaScript 文件的路徑。 其余元素將是任何其他命令行參數.

        執行命令

        $ node index generate
        

        第三個(gè)參數:generate

        二、 編寫(xiě)命令行

        2.1 添加版本和描述

        // index.js
        const program = require('commander');
        program.version('1.0.0').description('Simple password generator').parse()
        

        終端執行命令:可以看到passgen的描述

        繼續執行命令:可以看到passgen的版本

        2.2 配置密碼長(cháng)度命令

        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())
        

        終端執行命令:可以看到passgen的密碼長(cháng)度命令

        終端執行命令:

        2.2 密碼長(cháng)度添加默認值:8

        program.option('-l --length <number>', 'length of password', '8').parse()
        console.log(program.opts())
        

        終端執行命: 不設置密碼長(cháng)度,可以看到使用的是默認值-8

        終端執行命令: 設置密碼長(cháng)度為10

        2.3 配置保存密碼命令

        program.option('-l --length <number>', 'length of password', '8')
        .option('-s --save', 'save password to password.txt').parse()
        

        2.4 配置密碼格式: 沒(méi)有數字

        .option('-nn --no-number', 'remove numbers').parse()
        

        終端執行命: 默認情況下有數字

        終端執行命: 設置清除數字密碼

        2.5 配置密碼格式: 沒(méi)有符號

        .option('-ns --no-symbols', 'remove symbols').parse()
        

        終端執行命: 默認情況下有符號

        終端執行命: 設置清除數字密碼

        三、 解析命令行-創(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
        

        終端執行命令:查看密碼生成情況

        3.1 添加color

        // index.js
        const chalk = require('chalk');
        log(chalk.blue('Generated Password: ') + chalk.bold(generatedPassword))
        

        終端執行命令:可以看到顏色有變化

        3.2 添加剪貼板

        // index.js
        const clipboardy = require('clipboardy');
        // Copy to clipboardy
        clipboardy.writeSync(generatedPassword)
        log(chalk.yellow('Password copied to clipboardy!'))
        

        四、 保存密碼到對應的文件

        // 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
        

        終端執行命令: 可以看到項目中生成passwords.txt文件,并且密碼已經(jīng)保存成功

        五、將本地npm模塊配置成全局passgen

        // package.json
          "preferGlobal": true,
          "bin":"./index.js",
        

        終端執行命令:

        npm link命令:將npm 模塊鏈接到對應的運行項目中去,方便對本地模塊進(jìn)行調試和測試

        //index.js
        #!/usr/bin/env node //首行添加
        

        終端執行命令:

        總結:大功告成✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️

        參考鏈接:…

        總結

        到此這篇關(guān)于利用Node.js創(chuàng )建一個(gè)密碼生成器的文章就介紹到這了,更多相關(guān)Node.js密碼生成器內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

        免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

        亚洲国产精品一区二区第四页| 久久天天躁狠狠躁夜夜不卡| 国产成人午夜高潮毛片| 中文字幕久久久久人妻| 国产AV无码专区亚洲精品| 久久午夜夜伦鲁鲁片无码免费|