Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankag30 authored Sep 26, 2021
0 parents commit 0031a02
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
54 changes: 54 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 <number>", "length of password").parse();

// Set a default value to the value of length, let's say = 8
// program.option("-l, --length <number>", "length of password", "8").parse();

// If we want to save the value and more such commands
program
.option("-l, --length <number>", "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"));
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 5 additions & 0 deletions passwords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
+S2%pV^2
H_ltMjWZ%%5vb5W70SER
qqLR+b_-XCLBv%DOsULJ
UOgCMawa3hGM7He1KkUm
KHqAbNyexfySjmlxDaEo
20 changes: 20 additions & 0 deletions utils/createPassword.js
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions utils/savePassword.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 0031a02

Please sign in to comment.