-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.js
executable file
·89 lines (80 loc) · 2.06 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node
'use strict';
const Prompt = require('prompt-password');
const clipboardy = require('clipboardy');
const cowsay = require('cowsay');
const chalk = require('chalk');
const logSymbols = require('log-symbols');
const boxen = require('boxen');
const passw0rd = require('.');
const prompt = new Prompt({
type: 'password',
message: 'Enter your password please',
name: 'pass'
});
let isCopied = false; // For clipboard checking
/**
* Check clipboard if the given password was copied
* @param {string} pass - The password to check
*/
const checkClipboard = pass => {
return clipboardy.readSync() === pass;
};
/**
* Clear clipboard util
* @param {string} res - Result from the pwned password
*/
const clearClipboard = res => {
if (!isCopied) {
return;
}
console.log(`\n${logSymbols.info} clearing clipboard...`);
// Appreciate :)
clipboardy.writeSync(res.pwned ? '' : '😇 Thanks for using a strong password');
};
const showWarning = () => {
const msg = `This password should ${chalk.red('never')} be used.`;
// Use a password manager to have strong, unique passwords for each app
const opt = {
padding: 1,
margin: 0,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round'
};
console.log(boxen(msg, opt));
};
/**
* Display information for the given password
* @param {Object} res - results obtained from passw0rd check
*/
const showResult = res => {
const messages = {
pwned: `${logSymbols.error} Pwned ${chalk.yellow(res.count)} times!`,
safe: `${logSymbols.success} Yay! No records found!`
};
console.log();
if (res.pwned) {
console.log(cowsay.think({text: messages.pwned, e: 'oO'}));
showWarning();
} else {
console.log(cowsay.say({text: messages.safe, f: 'tux'}));
}
return res;
};
/**
* Check given password
* @param {string} pass - user provided password
*/
const checkPass = pass => {
if (!pass) {
console.error(`${logSymbols.warning} Specify a password`);
process.exit(1);
}
isCopied = checkClipboard(pass);
return passw0rd.check(pass);
};
prompt.run()
.then(checkPass)
.then(showResult)
.then(clearClipboard);