First, preparation work

1.1 Create Project

$ npm init 

1.2 Installation Dependency

$ npm i commander chalk clipboardy

1.3 Create entry file index.js

  • Take 🌰 : for process.argv

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

    Terminal execution command

    $ node index

    You can see it on the terminal

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

Execute the command

$ node index generate

Third parameter: Generate

Write the command line

2.1 Add version and description

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

Terminal Execution: You can see the description of Passgen

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

2.2 Configure password length command

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

Terminal Execution: You can see the password length command for passgen

Terminal Execution Command:

2.2 Default value added for password length: 8

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

Terminal Execution: Do not set the password length. You can see that the default is -8

Terminal Execution: Set the password length to 10

2.3 Configure the save password command

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



2.4 Configure password format: no numbers

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

Terminal Execution: Numbers by default

Terminal Execution: Sets the clear numeric password

2.5 Configure password format: no symbol

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

Terminal Execution: Signed by default

Terminal Execution: Sets the clear numeric password

Create password from command line

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

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 = createPassword

Terminal Execution: Check the password generation

3.1 add color

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

Terminal execution command: you can see the color change

3.2 Add Clipboard

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

4. Save the password to the corresponding file

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

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 = savePassword

You can see that the Nope. TXT file has been generated in the project and the password has been saved successfully



Configure the NPM module as global passgen

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

Terminal Execution Command:

NPM link command: link the NPM module to the corresponding running project to facilitate debugging and testing of the local module

//index.js #! /usr/bin/env node // First line added

Terminal Execution Command:


Summary: Smash it out ️ Smash it out, Smash it out, Smash it out, Smash it out, Smash it out, Smash it out, Smash it out, Smash it out

Refer to the link: http://nodejs.cn/api/process/process_argv.html