-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (77 loc) · 2.23 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
require('dotenv').config();
const dirFunc = './functions/',
dirModul = './modules/',
port = parseInt(process.env.PORT) || 1900;
global.log = require('./core/log4j').log;
global.logd = require('./core/log4j').ldebug;
global.fun = {};
global.fs = require('fs');
global.path = require('path');
global.codePath = __dirname;
global.fun = [];
global.modules = [];
if (process.argv[2] === 'dev') {
process.env.NODE_ENV = process.argv[2]
log('warn', `monoIntegrator iniciado en modo Desarrollador`);
} else {
log('info', `monoIntegrator iniciado en modo Productivo`);
}
global.walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file));
});
return filelist;
};
try {
const files = walkSync(dirFunc);
files.forEach(file => {
if (file.endsWith('.js')) {
try {
const functionName = file.split('/');
const nombreFuncion = functionName[functionName.length - 1].replace('.js', '');
fun[nombreFuncion] = require(`./${file}`);
if (process.env.TRACE === 'true' || process.env.NODE_ENV == 'prd') {
log("info", `Función ${nombreFuncion} detectada`);
}
} catch (error) {
log(
'error',
`Existe un inconveniente al importar la funcion ubicada en ${file}: ${error}`,
);
}
}
});
} catch (error) {
log(
'error',
`Existe un inconveniente al recorrer los archivos en Funciones: ${error}`,
);
}
try {
const moduleFiles = walkSync(dirModul);
moduleFiles.forEach(file => {
if (file.endsWith('/main.js')) {
const moduleDir = path.dirname(file);
const moduleName = path.basename(moduleDir);
try {
modules[moduleName] = require(`./${file}`);
if (process.env.TRACE === 'true' || process.env.NODE_ENV === 'prd') {
log('info', `Módulo ${moduleName} detectado`);
}
} catch (error) {
log('error', `Error al importar el módulo ${moduleName}: ${error}`);
}
}
});
} catch (error) {
log('error', `Error al recorrer los directorios de módulos: ${error}`);
}
const appserver = require('./server');
appserver.listen(port, () => {
log(
'info',
`Servidor iniciado en el puerto ${port} bajo PID ${process.pid}`,
);
});