-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (56 loc) · 1.8 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
const ConfigurationCollector = require('./lib/configuration');
const CommandCollector = require('./lib/commands');
module.exports = class Lakeside {
constructor() {
this.configuration = new ConfigurationCollector().resolveConfiguration();
this.commands = new CommandCollector(this.configuration).collectCommands();
}
/**
* Executes the given commands
*
* @param {*} commands - Array of commands to be executed
*/
run(commands) {
let that = this;
const promises = [];
commands.forEach(function (commandArgument) {
const commandKeys = Object.keys(that.commands);
if (commandKeys.includes(commandArgument)) {
promises.push(
new that.commands[commandArgument](that).runCommand()
);
}
else {
throw new Error(`Command '${commandArgument}' does not exist!`);
}
});
Promise.all(promises).then();
}
/**
* Method to show help output and list of available commands
*/
help() {
const stringPadding = 40;
console.log(`lakeside - node.js command runner
Usage:
lakeside [commands...] [flags...]
Flags:
${"--config path/to/config.js(on)".padEnd(stringPadding)}Load config from specified path
${"--help".padEnd(stringPadding)}Show this help
Commands:`);
const that = this;
const commandKeys = Object.keys(that.commands);
const commandLines = [];
commandKeys.forEach(function (command) {
let currentCommand = that.commands[command];
if (currentCommand.commandVisible()) {
commandLines.push(`${currentCommand.commandName().padEnd(stringPadding)}${currentCommand.commandDescription()}`);
}
});
if (commandLines.length > 0) {
commandLines.forEach(function (line) { console.log(line) });
} else {
console.log('-- No commands available');
}
}
}