First, preparation

1.1 Creating a Project

$ npm init 
Copy the code

1.2 Installation Dependencies

$ npm i commander chalk clipboardy
Copy the code

1.3 Creating the entry file index.js

  • Take 🌰 : to understandprocess.argv
// index.js
console.log(process.argv)
Copy the code

Terminal Execution command

$ node index
Copy the code

You can see it on the terminal

The process.argv property returns an array containing the command-line arguments passed in when the Node.js process is started. The first element will be process.execPath. The second element will be the path to the JavaScript file being executed. The remaining elements will be any other command line arguments.

Execute the command

$ node index generate
Copy the code

The third parameter: generate

Write the command line

2.1 Adding the version and Description

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

The terminal runs the command: The description of Passgen is displayed

Continue with the command: You can see the version of Passgen

2.2 Commands for Configuring password Length

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())Copy the code

The terminal runs the command: The password length command passgen is displayed

The terminal runs the following command:

2.2 Password Length Default value: 8

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

Terminal execution: do not set the password length, you can see that the default value -8 is used

Terminal run the following command to set the password length to 10

2.3 Configuring commands to Save passwords

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

2.4 Setting the Password Format: No digits

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

Terminal execution: number by default

Terminal execution: Set clear numeric password

2.5 Password Format: No sign

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

Terminal execution: signed by default

Terminal execution: Set clear numeric password

Parse command line – create password

// 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)Copy the code

Create 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 = createPasswordCopy the code

Terminal run the following command to view the password generation

3.1 add color

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

Terminal command: You can see the color change

3.2 Adding a Clipboard

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

4. Save the password to the corresponding file

// index.js
const savePassword = require('./utils/savePassword')
// Save to file
if (save) savePassword(generatedPassword)
Copy the code

Create 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 = savePasswordCopy the code

Run the following command on the terminal: Selected. TXT file is generated in the project and the password is saved successfully

5. Configure the local NPM module as global Passgen

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

The terminal runs the following command:

NPM link command: links the NPM module to the corresponding running project, which facilitates debugging and testing of the local module

//index.js #! /usr/bin/env node // the first line is addedCopy the code

The terminal runs the following command:


All in all, we have done our best. All in all, we have done our best

Reference link: nodejs.cn/api/process…