-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
136 lines (115 loc) · 3.06 KB
/
app.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
'use strict';
const Chalk = require('chalk');
const Yargs = require('yargs');
const Inquirer = require('inquirer');
const FpCode = require('node-flower-password');
const Clip = require('cliparoo');
let gArgv = null;
let arrLength = [];
const inputPassword = {
'type': 'password',
'name': 'password',
'message': 'Password:',
'validate': function(input) {
if (!input) {
return 'Enter your password';
}
return true;
}
};
const inputKey = {
type: 'input',
name: 'key',
message: 'Key:',
'validate': function(input) {
if (!input) {
return 'Enter your key';
}
return true;
}
};
const inputLength = {
type: 'list',
name: 'length',
message: 'Length:',
default: '16',
choices: []
};
main();
function initArrLength() {
for (let i = 2; i < 33; i++) {
inputLength.choices.push('' + i);
}
}
function init() {
initArrLength();
gArgv = Yargs
.option('p', {
alias: 'password',
describe: 'your password',
demand: false,
type: 'string'
})
.option('k', {
alias: 'key',
describe: 'your key',
demand: false,
type: 'string'
})
.option('l', {
alias: 'length',
describe: 'code length',
choices: inputLength.choices.map(n => parseInt(n, 10)),
demand: false,
default: 16,
type: 'number'
})
.usage('Usage: $0 [-p \"password\"] [-k \"key\"] [-l \"length\"]')
.example('$0 -p 123 -k 456 -l 32', 'KD06E9f528be13dc03fc7879e4ca11cB')
.help('h')
.alias('h', 'help')
.epilog('Copyright 2012 - 2016 xLsDg')
.argv;
}
function main() {
init();
let arrInput = [];
arrInput.push(gArgv.password);
arrInput.push(gArgv.key);
arrInput.push(gArgv.length);
if (!gArgv.password) {
arrInput.push(inputPassword);
}
if (!gArgv.key) {
arrInput.push(inputKey);
}
if (!gArgv.length) {
arrInput.push(inputLength);
}
if (arrInput.length > 3) {
return menuMain.apply(this, arrInput).then(procAws);
} else {
return showCode(gArgv.password, gArgv.key, gArgv.length);
}
}
function procAws(aws) {
return showCode(aws.password, aws.key, aws.length);
}
function showMenu() {
return menuMain(null, null, null, inputPassword, inputKey, inputLength).then(procAws);
}
function showCode(password, key, length) {
let code = FpCode(password, key, length);
return Clip(code, function(err) {
return console.log('\r\n' + Chalk.white.bold(`Code: `) + Chalk.cyan(code) + ' copied!\r\n');
});
}
function menuMain(password, key, length) {
return Inquirer.prompt(Array.prototype.slice.call(arguments, 3)).then(function(aws) {
aws['password'] = aws.password || password;
aws['key'] = aws.key || key;
aws['length'] = aws.length ? parseInt(aws.length, 10) : (length || 16);
return aws;
});
}