-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0031a02
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |