-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.js
190 lines (157 loc) · 5.19 KB
/
application.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var { parseArgsStringToArgv } = require('string-argv');
const commander = require('commander');
const InputHandler = require('./inputHandler')
const OutputHandler = require('./outputHandler')
const Command = require('./command')
const CommandNotFoundException = require('./exceptions/commandNotFoundException')
const lodash = require("lodash")
class Application {
static $bootstrappers = [];
$commands = [];
constructor($app) {
this.$app = $app;
this.setAutoExit(1);
commander.exitOverride(()=>{});
commander.showSuggestionAfterError(true);
this.bootstrap();
}
setAutoExit(status) {
process.exitCode = status
}
getAutoExit() {
return process.exitCode
}
run($input = []) {
return commander.parseAsync($input, {
from: 'user'
})
}
call($command, $arguments = []) {
$command = this.parseCommand($command);
$command = $command.slice(0, 1);
const commandArguments = $command.slice(1, $command.length)
if (Array.isArray($arguments)) {
$arguments = $arguments.concat(commandArguments);
}
if (!this.has($command)) {
throw new CommandNotFoundException(`The command ${$command} does not exist.`);
}
return this.run(
$command.concat(commandArguments.concat(lodash.flatten(this.createInputFromArguments($arguments))))
);
}
createInputFromArguments($arguments) {
if (Array.isArray($arguments)) {
return $arguments.map(arg => this.createInputFromArguments(arg))
}
if (typeof $arguments === 'object') {
return Object.entries($arguments).map(([key, value]) => {
if (value === true) {
return key;
} else {
if (key.startsWith('-')) {
return `${key}=${value}`;
} else {
return { [key]: value }
}
}
});
}
return $arguments
}
callCommand() {
return this.call(...arguments)
}
has($command) {
return this.$commands.includes($command[0])
}
getCommandName($input) {
let $command = this.getCommand($input)._
return $command.length ? $command[0] : ''
}
bootstrap() {
for (let $bootstrapper of Application.$bootstrappers) {
$bootstrapper(this);
}
}
parseCommand($command) {
return parseArgsStringToArgv($command)
}
resolve($command) {
return this.add(this.$app.make($command));
}
resolveCommands($commands) {
if (Array.isArray($commands)) {
for (let $command of $commands) {
this.resolve($command);
}
} else if (typeof $commands == 'object') {
for (let $command in $commands) {
this.resolve($command);
}
} else {
this.resolve($commands);
}
return this;
}
static addBootstraper(callback) {
this.$bootstrappers.push(callback)
}
getApp() {
return this.$app;
}
parseArgument(arg) {
if (Array.isArray(arg)) {
const [key, value] = arg;
return { key, value };
}
else if (typeof arg === 'object') {
const [key, value] = Object.entries(arg).pop();
return { key, value };
} else if (typeof arg === 'string') {
return {
value: arg
}
}
}
add(command) {
if (command instanceof Command != true) {
throw new Error(`Instance of [@ostro/console/command] was not available on [${command.constructor.name}]`)
}
const self = this;
this.$commands.push(command.$signature);
let options = command.$options || [];
let commandArguments = command.$arguments || [];
let cmd = commander.command(command.$signature);
cmd.description(command.$description);
for (let option of options) {
cmd.addOption(option.$handler);
}
for (let argument of commandArguments) {
cmd.addArgument(argument.$handler);
}
command.setApp(this.$app);
command.setConsole(this);
cmd.action(function (arg) {
const processedArgs = this.processedArgs.filter(v => v);
let args = {};
let opts = this.opts();
processedArgs.forEach((arg, i) => {
const { key, value } = self.parseArgument(arg);
const cmdArg = this._args.find(arg => arg._name == key);
if (key && key == cmdArg?._name) {
args[key] = value;
} else {
if (!key) {
args[cmdArg?._name || this._args[i]?._name] = processedArgs[i] || null;
}
}
});
let newCommand = Object.create(command);
command.input = new InputHandler(args, opts);
command.output = new OutputHandler();
return newCommand.handle()
})
}
}
module.exports = Application