-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (43 loc) · 1.23 KB
/
index.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
var argv = require('yargs-parser')(process.argv.slice(2));
var puts = console.log;
var options = {
l: {
description: 'Word Count [default: 3]'
},
s: {
description: 'Separator [default: "-"]'
},
n: {
description: 'Password Count [default: 1]'
},
h: {
description: 'Displays this help'
}
};
this.showHelp = function () {
puts('Generates a memorable password\r\n');
puts('Options:');
var keys = Object.keys(options);
keys.forEach(function (key) {
puts(' -' + key + ': ' + options[key].description);
});
};
this.run = function () {
if (argv.h) {
return this.showHelp();
}
var word_count = argv.l || 3;
var separator = argv.s || "-";
var password_count = argv.n || 1;
var path = require('path'),
fs = require('fs');
var words = fs.readFileSync(path.dirname(fs.realpathSync(__filename)) + '/words.txt', 'utf-8')
.split('\n');
for (var i = 0; i < password_count; i++) {
var selectedWords = [];
for (var j = 0; j < word_count; j++) {
selectedWords.push(words[Math.floor(Math.random() * words.length)]);
}
puts(selectedWords.join(separator));
}
};