Create a drive generator using node.js

Time:2021-7-24

€? Equipment out of service

1.1 creating an item

$ npm init 

1.2 installation

$ npm i commander chalk clipboardy

1.3 creating a port file index.js

  • In the following sentence: Вprocess.argv

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

    End point ц Forging ring protection

    $ node index

    You may only be allowed to/ p>

    Process.argv request ц The data group includes the number of times the switch is turned on when the node.js power supply is started? The first unit will be? Process.execpath? The second element addition will be the origin of JavaScript files that are properly exported? What are the other bank numbers?

Gallium ц Forging ring protection

$ node index generate

Chapter 3: number generation

What is it? No. start / stop/ h2>

2.1 adding versions and scanning/ code>

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

End point ц To perform maintenance, you can only scan assgen? br>Create a drive generator using node.js

Keep х Painted gallium ц Exercise protection: it can be added to assgen only? br>Create a drive generator using node.js

2.2 configure password length protection

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

End point ц For maintenance, you can only use the assgen’s personal bed/ p>

Create a drive generator using node.js

End point ц Forging ring protection:

Create a drive generator using node.js

2.2 adding default password length 8

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

End point ц Bank forging? Instead of setting the default screen, you can see that the default screen is used? eight
Create a drive generator using node.js

End point ц Bank protection: set password length? 0
Create a drive generator using node.js

2.3 configuring and saving password protection

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

Create a drive generator using node.js
2.4 configuration password label:? There are no valid characters

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

End point ц Bank forging? By default, there are valid fields in the patient list

Create a drive generator using node.js

End point ц Bank forging? Set clear password

Create a drive generator using node.js

2.5 configuration password label:? There's no answer

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

End point ц Bank forging? By default, the patient list contains the following information:

Create a drive generator using node.js

End point ц Bank forging? Set clear password

Create a drive generator using node.js

()? What’s wrong with you? 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)

To createutils/createPassword.js

// createPassword.js
const alpha = 'qwertyuiopasdfghjklzxcvbnm'
const numbers = '0123456789'
const symbols= '[email protected]#$%^&*_-=+'


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

End point ц Perform maintenance to view the generated cost information/ p>

Create a drive generator using node.js

3.1 adding color

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

End point ц Exercise caution (you can only use it if you have a license)

Create a drive generator using node.js

3.2 adding a front panel/ code>

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

Create a drive generator using node.js

Figure €? Save the file corresponding to the password

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

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

End point ц Bank protection:? You can view the asswords.txt file generated in the item, and then click Save to generate each saving cost? br>
Create a drive generator using node.js

To configure the global NPM mode into passgen

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

End point ц Forging ring protection:

Create a drive generator using node.js

NPM link ring guard:/ Code > connect the PM module to the running item corresponding to the board, and the party shall conduct service test and consultation for the company's local module/ p>//index.js
#!/ Usr / bin / env node // to add a terminal ц Bank protection:? Br > br > from the end of the journey, br br br br > the argon has been bringing the corners to the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the br br br > br > to the end of the journey to the end of the journey from the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the br br br > br > br > br > br > br > br > br > br > br > br > br > br > br > br > what what is the journey of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners of the corners cutting edge/ p> You can use the "control" function http://nodejs.cn/api/process/process_ argv.html