@midgar/midgar
Advanced tools
Comparing version 1.0.0-alpha.1.21 to 1.0.0-alpha.1.22
@@ -13,3 +13,3 @@ #!/usr/bin/env node | ||
cli.init().then(() => { | ||
cli.program.parse(process.argv) | ||
cli.run() | ||
}) |
@@ -11,3 +11,3 @@ | ||
async function initDir(projectPath, tplPath) { | ||
async function initDir(projectPath, tplPath){ | ||
// Check if the directory is empty | ||
@@ -72,10 +72,5 @@ const stats = await utils.asyncReaddir(tplPath) | ||
init(initPath).then(() => { | ||
process.exit(1) | ||
}).catch((e) => { | ||
console.log(e) | ||
process.exit(0) | ||
}) | ||
await init(initPath) | ||
}, | ||
} | ||
] |
@@ -17,3 +17,3 @@ const path = require('path') | ||
*/ | ||
constructor() { | ||
constructor(argv) { | ||
this.rcConfig = null | ||
@@ -23,2 +23,3 @@ // Path of the config dir | ||
this.argv = argv ? argv : process.argv | ||
this.program = new commander.Command() | ||
@@ -36,3 +37,3 @@ this.program.version('0.0.1') | ||
} else { | ||
console.log('Invalid command: %s\nSee --help for a list of available commands.') | ||
console.log('Invalid command: %s\nSee --help for a list of available commands.', this.program.args.join(' ')) | ||
} | ||
@@ -58,4 +59,3 @@ process.exit(1) | ||
// Parse options to get config dir path | ||
this.program.parseOptions(this.program.normalize(process.argv.slice(2))) | ||
this.program.parseOptions(this.program.normalize(this.argv.slice(2))) | ||
// If option config is set the config path | ||
@@ -123,3 +123,8 @@ if (this.program.config && this.program.config.trim()) { | ||
.action((...args) => { | ||
command.action(args, this.midgar) | ||
command.action(args, this.midgar).then(() => { | ||
this.exit(0) | ||
}).catch(error => { | ||
console.error(error) | ||
this.exit(1) | ||
}) | ||
}) | ||
@@ -129,2 +134,11 @@ } | ||
/** | ||
* Exit function | ||
* | ||
* @param {Binary} code exit code | ||
*/ | ||
exit(code) { | ||
process.exit(code) | ||
} | ||
/** | ||
* Get all plugins cli files and add commands | ||
@@ -139,4 +153,11 @@ */ | ||
} | ||
/** | ||
* Parse argv and run command | ||
*/ | ||
run() { | ||
this.program.parse(this.argv) | ||
} | ||
} | ||
module.exports = Cli |
@@ -42,3 +42,3 @@ /** | ||
finish = true | ||
for (let key = 0; key < plugins.length;i++) { | ||
for (let key = 0; key < plugins.length;key++) { | ||
const name = plugins[key] | ||
@@ -45,0 +45,0 @@ const deps = dependencies[name] |
@@ -42,5 +42,2 @@ const path = require('path') | ||
this.pluginDirs = {} | ||
// Observe events | ||
this._observeEvents() | ||
} | ||
@@ -67,3 +64,5 @@ | ||
async loadPlugins (pluginsPath) { | ||
this.emit('loadPluginsStart', pluginsPath) | ||
timer.start('midgar-plugin-load') | ||
this.midgar.debug('start load plugins from ' + this.options.pluginsPath) | ||
//list plugins | ||
@@ -78,4 +77,6 @@ this.plugins = await utils.asyncMap(pluginsPath, async pluginPath => { | ||
//after Load callback | ||
await this.emit('afterLoadPlugins') | ||
await this.emit('loadPluginsEnd') | ||
await this.emit('midgar:afterLoadPlugins') | ||
const time = timer.getTime('midgar-plugin-load') | ||
this.midgar.debug('plugins loaded in ' + time[0] + 's, ' + time[1] + 'ms') | ||
} | ||
@@ -92,7 +93,3 @@ | ||
plugins.push(plugin) | ||
try { | ||
await asyncWriteFile(path.join(this.midgar.configPath, 'plugins.js'), 'module.exports = ' + JSON.stringify(plugins)) | ||
} catch (error) { | ||
throw error | ||
} | ||
await asyncWriteFile(path.join(this.midgar.configPath, 'plugins.js'), 'module.exports = ' + JSON.stringify(plugins)) | ||
} | ||
@@ -228,3 +225,3 @@ } | ||
if (!this.pluginDirs[dirName] && !plugin.dirs[dir]) return //skip | ||
if (!this.pluginDirs[dirName] && !plugin.dirs[dirName]) return //skip | ||
@@ -361,36 +358,4 @@ // Get the plugin dir path | ||
} | ||
/** | ||
* Observe plugin manager event to bind them | ||
* @private | ||
*/ | ||
_observeEvents() { | ||
//observe start plugin load to get the load time | ||
this.on('loadPluginsStart', plugins => { | ||
timer.start('midgar-plugin-load') | ||
this.midgar.debug('start load plugins from ' + this.options.pluginsPath) | ||
}) | ||
//observe end plugin load to get the load time | ||
this.on('loadPluginsEnd', () => { | ||
const time = timer.getTime('midgar-plugin-load') | ||
this.midgar.debug('plugins loaded in ' + time[0] + 's, ' + time[1] + 'ms') | ||
}) | ||
//bind logger | ||
this.on('debug', msg => { | ||
this.midgar.debug(msg) | ||
}) | ||
this.on('error', error => { | ||
this.midgar.error(error) | ||
}) | ||
this.on('warn', msg => { | ||
this.midgar.warn(msg) | ||
}) | ||
} | ||
} | ||
module.exports = PluginManager |
143
midgar.js
@@ -8,2 +8,3 @@ const express = require('express') | ||
const EventEmitter = require('events') | ||
const http = require('http') | ||
const https = require('https') | ||
@@ -64,2 +65,14 @@ | ||
/** | ||
* Http server | ||
* @type {http.Server} | ||
*/ | ||
this.webServer = null | ||
/** | ||
* Http server for public files | ||
* @type {http.Server} | ||
*/ | ||
this.publicServer = null | ||
/** | ||
* Midgar data object | ||
@@ -78,3 +91,3 @@ * @type {Object} | ||
* Config load flag | ||
* @type {boolean} | ||
* @type {Boolean} | ||
* @private | ||
@@ -110,3 +123,3 @@ */ | ||
ssl: false | ||
}, this.config.web || {}) | ||
}, this.config.web || {}) | ||
@@ -118,3 +131,3 @@ // Peublic web serveur base config | ||
ssl: false | ||
}, this.config.public || {}) | ||
}, this.config.public || {}) | ||
@@ -144,2 +157,5 @@ // Check minimal config | ||
/** | ||
* Create logger instace and init | ||
*/ | ||
async initLogger() { | ||
@@ -187,2 +203,6 @@ // Create the logger instance | ||
/** | ||
* | ||
* @param {*} plugin | ||
*/ | ||
async addPlugin(plugin) { | ||
@@ -223,2 +243,3 @@ const pm = new PluginManager(this) | ||
this.app.use((req, res, next) => { | ||
// Set Midgar intance on request object | ||
req.midgar = this | ||
@@ -237,3 +258,3 @@ // add method to get clean request param | ||
if (value !== null && cleanParams) { | ||
if (value !== null && cleanParams) { | ||
value = this._cleanParam(value) | ||
@@ -266,3 +287,3 @@ if (!req.cleanParams) | ||
this.app.use((err, req, res, next) => { | ||
this.app.use((err , req, res, next) => { | ||
this.error(err) | ||
@@ -280,14 +301,14 @@ }); | ||
_cleanParam(value) { | ||
if (typeof value === 'object') { | ||
const obj = {} | ||
const keys = Object.keys(value) | ||
for (const i in keys) { | ||
let key = keys[i] | ||
key = htmlencode(key) | ||
obj[key] = htmlencode(value[key]) | ||
} | ||
return obj | ||
} else { | ||
return htmlencode(value) | ||
if (typeof value === 'object') { | ||
const obj = {} | ||
const keys = Object.keys(value) | ||
for (const i in keys) { | ||
let key = keys[i] | ||
key = htmlencode(key) | ||
obj[key] = htmlencode(value[key]) | ||
} | ||
return obj | ||
} else { | ||
return htmlencode(value) | ||
} | ||
} | ||
@@ -299,3 +320,3 @@ | ||
*/ | ||
getModeEnv() { | ||
getModeEnv() { | ||
return process.env.MODE_ENV | ||
@@ -353,3 +374,3 @@ } | ||
/** | ||
* @description Start front web serveur | ||
* Start front web serveur | ||
*/ | ||
@@ -372,7 +393,9 @@ async startWebServer() { | ||
await this._startWebServer(this.app, opts).catch(error => { | ||
try { | ||
this.webServer = await this._startWebServer(this.app, opts) | ||
} catch (error) { | ||
this.error('Cannot start the web server') | ||
this.error(error) | ||
process.exit() | ||
}) | ||
} | ||
@@ -404,7 +427,11 @@ this.info('Web server live on ' + opts.port) | ||
await this._startWebServer(this.publicApp, opts).catch( error => { | ||
try { | ||
this.publicServer = await this._startWebServer(this.publicApp, opts) | ||
} catch (error) { | ||
this.error('Cannot start the public web server') | ||
this.error(error) | ||
process.exit() | ||
}) | ||
} | ||
this.info('Server public live on ' + this.config.public.port) | ||
@@ -418,23 +445,16 @@ this.info(this.config.public.path + ' => ' + this.config.public.baseUrl) | ||
*/ | ||
_startWebServer(app, opts) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
if (!opts.ssl) { | ||
app.listen(opts.port, () => { | ||
resolve() | ||
}).on('error', error => { | ||
reject(error) | ||
}) | ||
} else { | ||
https.createServer({ | ||
key: opts.sslKey, | ||
cert: opts.sslCert, | ||
}, app).listen(opts.port, opts.host, () => { | ||
console.log('Http server listen on '+ opts.baseUrl + "\n"); | ||
}) | ||
} | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
async _startWebServer(app, opts) { | ||
if (!opts.ssl) { | ||
const server = http.createServer(app) | ||
await server.listen(opts.port) | ||
return server | ||
} else { | ||
const server = https.createServer({ | ||
key: opts.sslKey, | ||
cert: opts.sslCert, | ||
}, app) | ||
await server.listen(opts.port, opts.host) | ||
return server | ||
} | ||
} | ||
@@ -446,3 +466,6 @@ | ||
*/ | ||
async exit() { | ||
async exit() { | ||
// Stop servers if there a runing | ||
await this.stop() | ||
if (this.logger && this.logger.exit && !this._hasExitLogger) { | ||
@@ -458,2 +481,32 @@ this._hasExitLogger = true | ||
/** | ||
* Stop public and web server | ||
*/ | ||
async stop() { | ||
if (this.publicServer) { | ||
await this._stop(this.publicServer) | ||
} | ||
if (this.webServer) { | ||
await this._stop(this.webServer) | ||
} | ||
} | ||
/** | ||
* Stop an http server | ||
* @param {http.Server} server Server to stop | ||
*/ | ||
_stop(server) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
server.close(() => { | ||
resolve() | ||
}) | ||
} catch(error) { | ||
console.log(error) | ||
reject(error) | ||
} | ||
}) | ||
} | ||
/** | ||
* listen process exit signal signal and graceful exit | ||
@@ -520,3 +573,3 @@ * | ||
*/ | ||
url (route, options = {}) { | ||
url (route /*, options = {}*/) { | ||
return url.resolve(this.config.web.baseUrl, route) | ||
@@ -523,0 +576,0 @@ } |
{ | ||
"name": "@midgar/midgar", | ||
"version": "1.0.0-alpha.1.21", | ||
"version": "1.0.0-alpha.1.22", | ||
"description": "A node js framework over express", | ||
@@ -37,2 +37,3 @@ "main": "midgar.js", | ||
"chai": "^4.2.0", | ||
"chai-arrays": "^2.0.0", | ||
"chai-fs": "^2.0.0", | ||
@@ -39,0 +40,0 @@ "chai-http": "^4.3.0", |
@@ -45,2 +45,4 @@ In Dev don't use this | ||
| [session](https://www.npmjs.com/package/@midgar/session) | Add sessions with [express-session](https://www.npmjs.com/package/express-session) | | ||
| [cors](https://www.npmjs.com/package/@midgar/cors) | Add [cors](https://www.npmjs.com/package/cors) | | ||
| [cors](https://www.npmjs.com/package/@midgar/cors) | Add [cors](https://www.npmjs.com/package/cors) | | ||
[Api documentation](https://midgarjs.github.io/midgar/) in progress ... |
@@ -59,3 +59,3 @@ const { describe, it } = require('mocha') | ||
function cmd (...command) { | ||
return new Promise((accept, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const argv = [binPath].concat([...command]) | ||
@@ -82,3 +82,3 @@ const binp = process.argv[0] | ||
child.on('close', (code) => { | ||
accept({code, stdout, stderr}) | ||
resolve({code, stdout, stderr}) | ||
}) | ||
@@ -85,0 +85,0 @@ |
@@ -7,3 +7,3 @@ const path = require('path') | ||
module.exports = { | ||
web: { | ||
web: { | ||
port: 3000, | ||
@@ -10,0 +10,0 @@ host: 'localhost', |
@@ -7,3 +7,3 @@ const path = require('path') | ||
module.exports = { | ||
web: { | ||
web: { | ||
port: 3000, | ||
@@ -10,0 +10,0 @@ host: 'localhost', |
@@ -15,2 +15,5 @@ const Plugin = require('../../../../plugin') | ||
async init() { | ||
// Add services plugin dir | ||
this.pm.pluginDirs['test'] = 'foo' | ||
this.isInit = true | ||
@@ -17,0 +20,0 @@ } |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
63688
36
2170
47
8
3