-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (114 loc) · 2.61 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
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
const fs = require('fs');
const inquirer = require('inquirer');
const {
pw,
LOWERCASE,
NUMBERS,
SYMBOLS,
UPPERCASE,
VALID_TYPE_CHOICES
} = require('./pw');
const { readConfig, removeConfig, showHelp, writeConfig } = require('./utils');
// defaults
const DEFAULT_TYPE_CHOICES = [UPPERCASE, LOWERCASE, NUMBERS];
const DEFAULT_LENGTH = 40;
// config path
const CONFIG_PATH = './.config.json';
let hasWizard = true;
//// arguments
const args = {
'--save': (configPath, config) => {
writeConfig(configPath, config);
},
'--help': () => {
showHelp();
process.exit(0);
},
'--reset': (configPath) => {
removeConfig(configPath);
},
'--version': () => {
const pkg = require('./package.json');
console.log(pkg.version);
process.exit(0);
},
'--cli': () => {
hasWizard = false;
}
};
// validate arguments
process.argv.slice(2).forEach(arg => {
if (!(arg in args)) {
console.error(`Invalid argument ${arg}.`);
console.log('Use `pw --help` for help.');
process.exit(1);
}
});
// helper
const arg = (arg, ...others) => {
if (process.argv.includes(arg)) {
args[arg](...others);
}
};
// help display
arg('--help');
// version
arg('--version');
// skip wizard
arg('--cli');
//config
const configValidation = {
'types': [
value => !value && '`types` is required.',
value => !Array.isArray(value) && '`types` must be an array.',
values => {
for (let i = 0; i < values.length; i++) {
const value = values[i];
return !VALID_TYPE_CHOICES.includes(value) &&
`\`types\` value ${value} is not valid. Valid values are ${VALID_TYPE_CHOICES.join(', ')}`;
}
}
],
'length': [
value => !value && '`length` is required.' ,
value => (!Number.isInteger(parseInt(value)) || value <= 0) && '`length` must be an non-negative integer.'
]
};
let config = {
types: DEFAULT_TYPE_CHOICES,
length: DEFAULT_LENGTH
};
arg('--reset', CONFIG_PATH);
// load config
if (fs.existsSync(CONFIG_PATH)) {
config = readConfig(CONFIG_PATH, configValidation);
} else {
console.log('No config file found. Run `pw --config` to update prefernces.');
}
// wizard
const promt = () => inquirer.prompt([
{
type: 'checkbox',
message: `Character types (default is ${config.types.join(', ')})`,
name: 'types',
default: config.types,
choices: VALID_TYPE_CHOICES
},
{
type: 'input',
message: 'Length',
name: 'length',
default: config.length,
validate: input => /^\d*$/.test(input)
}
]);
// main
if (hasWizard) {
const prompt = promt();
prompt.then(answer => {
arg('--save', CONFIG_PATH, answer);
console.log(pw(answer.types, answer.length));
});
} else {
console.log(pw(DEFAULT_TYPE_CHOICES, DEFAULT_LENGTH));
}