-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
68 lines (59 loc) · 1.6 KB
/
utils.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
const fs = require('fs');
const validateConfig = (config, rules) => {
const error = (message) => {
console.error(`Configuration error: ${message}`);
process.exit(1);
};
Object.keys(config).forEach(key => {
if (!(key in rules)) {
error(`Illegal key, ${key}, in config.`);
}
});
Object.keys(rules).forEach(key => {
rules[key].forEach(rule => {
const message = rule(config[key]);
if (message) {
error(message);
}
});
});
} ;
const readConfig = (path, validation) => {
let config;
try {
config = JSON.parse(fs.readFileSync(path));
validateConfig(config, validation);
} catch (e) {
if (e instanceof SyntaxError) {
console.log('Invalid syntax');
process.exit(1);
} else {
throw e;
}
}
return config;
};
const removeConfig = (path) => {
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
};
const writeConfig = (path, config) => {
fs.writeFileSync(path, JSON.stringify(config));
};
const showHelp = () => {
console.log(`
pw - a super simple password generating wizard.
By default it will bring up a wizard to generate a password and then will display said generated password.
The wizard contains two questions:
Character Types: The types of characters that make up the password. Choices are: uppercase, lowercase, numbers, and symbols.
Length: The length of the password
Options:
--help\tBrings up this display
--save\tSave options as new defaults
--reset\tRestore original defaults
--version\tDisplays the current version
--cli\tSkip the wizard and run with defaults
`);
};
module.exports = { readConfig, removeConfig, showHelp, validateConfig, writeConfig };