一、 筹备工作

1.1 创立我的项目

$ npm init 

1.2 装置依赖

$ npm i commander chalk clipboardy

1.3 创立入口文件index.js

  • 举个:来理解process.argv

    // index.jsconsole.log(process.argv)

    终端执行命令

    $ node index

    在终端能够看到

    process.argv 属性返回数组,其中蕴含启动 Node.js 过程时传入的命令行参数。 第一个元素将是 process.execPath。 第二个元素将是正在执行的 JavaScript 文件的门路。 其余元素将是任何其余命令行参数.

执行命令

$ node index generate

第三个参数:generate

二、 编写命令行

2.1 增加版本和形容

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

终端执行命令:能够看到passgen的形容

继续执行命令:能够看到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())

终端执行命令:能够看到passgen的明码长度命令

终端执行命令:

2.2 明码长度增加默认值:8

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

终端执行命: 不设置明码长度,能够看到应用的是默认值-8

终端执行命令: 设置明码长度为10

2.3 配置保留明码命令

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


2.4 配置明码格局: 没有数字

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

终端执行命: 默认状况下有数字

终端执行命: 设置革除数字明码

2.5 配置明码格局: 没有符号

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

终端执行命: 默认状况下有符号

终端执行命: 设置革除数字明码

三、 解析命令行-创立明码

// index.jsconst program = require('commander');const createPassword = require('./utils/createPassword')const log = console.logprogram.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 passwordconst generatedPassword = createPassword(length, numbers, symbols)// Output generated passwordlog(generatedPassword)

创立utils/createPassword.js

// createPassword.jsconst 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.jsconst chalk = require('chalk');log(chalk.blue('Generated Password: ') + chalk.bold(generatedPassword))

终端执行命令:能够看到色彩有变动

3.2 增加剪贴板

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

四、 保留明码到对应的文件

// index.jsconst savePassword = require('./utils/savePassword')// Save to fileif (save) savePassword(generatedPassword)

创立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文件,并且明码曾经保留胜利

五、将本地npm模块配置成全局passgen

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

终端执行命令:

npm link命令:将npm 模块链接到对应的运行我的项目中去,不便对本地模块进行调试和测试

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

终端执行命令:


总结:功败垂成✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️✌️

参考链接:http://nodejs.cn/api/process/process_argv.html