-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
37 lines (31 loc) · 1.05 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
const program = require('commander');
const fs = require('fs');
const path = require('path');
function getMappedCommands() {
return fs.readdirSync(path.resolve(__dirname, './commands')).reduce((commands, command) => {
const commandWithoutExtension = command.replace('.js', '');
return Object.assign(commands, {
[commandWithoutExtension]: require(`./commands/${command}`)
});
}, {});
}
function findAndExecuteOption(commands) {
Object.keys(commands).forEach((command) => {
const commandCallback = commands[command];
const commandValue = program[command];
const commandWasCalled = program.hasOwnProperty(command);
if (commandWasCalled) {
commandCallback(commandValue);
}
});
}
module.exports = (args = process.argv) => {
program
.option('a, add <alias>', 'Add user')
.option('r, remove <alias>', 'Remove user')
.option('u, use <alias>', 'Use user')
.option('l, list', 'List users')
.option('w, whoami', 'Current logged in user')
.parse(args);
findAndExecuteOption(getMappedCommands());
};