-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
192 lines (155 loc) · 3.78 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
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
#!/usr/bin/env node
const prog = require('caporal');
const path = require('path');
const _ = require('lodash');
const LovaClass = require(path.join(__dirname, 'includes/abstract/lovaclass.js'));
const Command = require(path.join(__dirname, 'includes/abstract/command.js'));
const Logger = require(path.join(__dirname, 'includes/logger.js'));
const DB = require(path.join(__dirname, 'includes/db.js'));
class Program {
constructor(options = {}) {
// default config
this._config = {
"name": "LovaCLISampleApp",
"debug": true,
"version": "1.0.0",
"paths": {
"models": path.join(__dirname, "../app/models"),
"commands": path.join(__dirname, "../app/commands"),
"tests": path.join(__dirname, "../app/tests")
}
};
// extend config with passed parameters
_.merge(this._config, options);
// load and initialize sub-classes
this._logger = new Logger(this._config);
if (this._config.database) {
this._db = new DB({
config: this._config,
logger: this._logger,
program: this
});
} else {
this._db = null;
}
// initialize caporal program. https://www.npmjs.com/package/caporal
this._prog = prog
.version(this._config.version)
.logger(this.logger)
.description(this.config.name);
this._fatalErrorCatcher = (e)=>{
this.exit(e);
};
this._executionErrorCatcher = (e)=>{
};
this._prog.fatalError = this._fatalErrorCatcher;
//
this._commands = {};
this._prog.program = this;
}
get prog() {
return this._prog;
}
get logger() {
return this._logger;
}
get db() {
return this._db;
}
get config() {
return this._config;
}
async loadCommand(filename) {
let name = path.basename(filename, path.extname(filename));
let inc = require(filename);
if (Command.isPrototypeOf(inc)) {
let command = new inc({
program: this,
name: name
});
await command.init();
this._commands[name] = command;
return command;
}
return null;
}
async execute(name, args = [], options = {}) {
this._prog.fatalError = this._executionErrorCatcher;
if (typeof(this._commands[name]) === undefined) {
throw new Error('Invalid command name: '+name);
}
let commandResults = await this._commands[name].execute(args, options);
this._prog.fatalError = this._fatalErrorCatcher;
return commandResults;
}
async init(handleImmediate = true) {
try {
let resources = require(path.join(__dirname, 'includes/resources.js'));
let filenames = await resources.loadPaths(this.config.paths.commands);
for (let filename of filenames) {
await this.loadCommand(filename);
}
} catch(e) {
this.exit(e);
}
if (handleImmediate) {
await this.handle();
}
}
async handle() {
try {
this.prog.parse(process.argv);
} catch(e) {
this.exit(e);
}
}
exit(e = null) {
if (e instanceof Error) {
if (this.config.debug) {
this.logger.error(e.stack);
} else {
this.logger.error(""+e);
}
process.exit(1); // @todo: get code from Error object?
} else if (e) {
this.logger.error(""+e);
process.exit(1);
} else {
process.exit();
}
}
handleCatcher(handler) {
let program = this;
if (handler && handler.constructor && handler.constructor.name == 'AsyncFunction') {
let newHandler = async function(args, options, logger) {
try {
return await handler(args, options, logger);
} catch(e) {
program.exit(e);
}
return null;
}
return newHandler;
} else {
let newHandler = function(args, options, logger) {
try {
return handler(args, options, logger);
} catch(e) {
program.exit(e);
}
return null;
}
return newHandler;
}
}
};
if (!module.parent) {
let program = new Program();
program.init();
} else {
module.exports = {
Program: Program,
Command: Command,
LovaClass: LovaClass
};
}