diff --git a/index.js b/index.js new file mode 100644 index 0000000..bb7b65a --- /dev/null +++ b/index.js @@ -0,0 +1,54 @@ +#!/usr/bin/env node +const program = require("commander"); +const chalk = require("chalk"); +const clipboardy = require("clipboardy"); +const createPassword = require("./utils/createPassword"); +const savePassword = require("./utils/savePassword"); + +program.version("1.0.0").description("Simple Password Generator"); + +// program +// .command("generate") +// .action(() => { +// console.log("Generated"); +// }) +// .parse(); + +/* +Adding options (command, description) + program.option("-l, --length", "length of password").parse(); +*/ + +// If we also want the value with the options such as length value +// program.option("-l, --length ", "length of password").parse(); + +// Set a default value to the value of length, let's say = 8 +// program.option("-l, --length ", "length of password", "8").parse(); + +// If we want to save the value and more such commands +program + .option("-l, --length ", "length of password", "8") + .option("-s, --save", "Save Password to passwords.txt") + .option("-nn, --no-numbers", "Remove numbers") + .option("-ns, --no-symbols", "Remove symbols") + .parse(); + +// Get the value - print it +// console.log(program.opts()); + +const { length, save, numbers, symbols } = program.opts(); + +// Get generated Password +const generatePassword = createPassword(length, numbers, symbols); + +// Save to file +if (save) { + savePassword(generatePassword); +} + +// Copy to Clipboard +clipboardy.writeSync(generatePassword); + +// Output Generated Password +console.log(chalk.blue("Generated Password: ") + chalk.bold(generatePassword)); +console.log(chalk.yellow("Password Copied to Clipboard")); diff --git a/package.json b/package.json new file mode 100644 index 0000000..ef119e8 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "passgen", + "version": "1.0.0", + "description": "Password Generator", + "main": "index.js", + "preferGlobal": true, + "bin": "./index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Mayank Agarwal", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2", + "clipboardy": "^2.3.0", + "commander": "^8.2.0" + } +} diff --git a/passwords.txt b/passwords.txt new file mode 100644 index 0000000..90006fa --- /dev/null +++ b/passwords.txt @@ -0,0 +1,5 @@ ++S2%pV^2 +H_ltMjWZ%%5vb5W70SER +qqLR+b_-XCLBv%DOsULJ +UOgCMawa3hGM7He1KkUm +KHqAbNyexfySjmlxDaEo diff --git a/utils/createPassword.js b/utils/createPassword.js new file mode 100644 index 0000000..8a9cde8 --- /dev/null +++ b/utils/createPassword.js @@ -0,0 +1,20 @@ +const alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +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; diff --git a/utils/savePassword.js b/utils/savePassword.js new file mode 100644 index 0000000..60d84dc --- /dev/null +++ b/utils/savePassword.js @@ -0,0 +1,19 @@ +const fs = require("fs"); +const path = require("path"); +const os = require("os"); +const chalk = require("chalk"); + +const savePassword = (password) => { + // 'a' means that we have to append to the existing data + // 666 is the permission + fs.open(path.join(__dirname, "../", "passwords.txt"), "a", 666, (e, id) => { + // EOL - End of Line, same as "\n" for the new line character + fs.write(id, password + os.EOL, null, "utf-8", () => { + fs.close(id, () => { + console.log(chalk.green("Password saved to Passwords.txt")); + }); + }); + }); +}; + +module.exports = savePassword;