UNPKG

jmms

Version:

Jmms cli tools, Jmms is a java meta-micro-service framework

160 lines (129 loc) 6.05 kB
const _ = require('lodash'); const log = require('../log'); const utils = require('../utils'); const fs = require('fs'); const path = require('path'); const proc = require('child_process'); const metas = require('../metas'); var BaseGenerator = require('../base'); module.exports = class extends BaseGenerator { constructor(args, opts) { super(args, opts); } initializing() { /* if(this.options['update'] == true) { this.composeWith(require.resolve('../update'), { meta : metas.update, }); } else if(this.options['build'] == true) { this.composeWith(require.resolve('../package'), { meta : metas.package, }); } */ } install() { this._runApplication(); /* this._findOrCreateJar((jar) => { this._runApplication(jar); }); */ } _runApplication(jar) { this.server = this.app.server; this.swaggerUrl = this.app.api.baseUrl + "/swagger.json"; log.debug("swagger uri : " + this.swaggerUrl); var port = this.server.port; var uiPort = port + 1; const gServer = this.server; utils.checkPorts([port,uiPort], () => { const update = this.options['update'] == true; const appDir = path.join(this.destinationRoot(), 'app'); const dirProp = '-Djmms.dir=' + appDir; const devProp = '-Djmms.dev=true'; const testProp = '-Djmms.test=true'; const portProp = '-Dserver.port=' + port; const logbackFile = this.destinationRoot() + '/src/main/resources/logback-test.xml'; const loggingProp = fs.existsSync(logbackFile) ? '-Dlogging.config=' + logbackFile : ''; const profile = this.options['profile']; const profileProp = profile == 'true' ? '' : ('-Dspring.profiles.active=' + (_.isString(profile) ? profile : 'dev')); const suspend = this.options['suspend'] == true ? 'y' : 'n'; const debug = this.options['remote_debug'] == true ? '-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=' + suspend + ',address=5005 ' : ''; //mvn spring-boot:run -Drun.jvmArguments="-D..." const cmd = "mvn"; const jvmArgs = '-Drun.jvmArguments=' + debug + _.pull([dirProp, devProp, testProp, loggingProp, profileProp, portProp], '').join(' '); const args = _.pull([update ? '-U' : '', 'spring-boot:run', jvmArgs], ''); //const cmd = "java"; //const args = _.pull([dirProp, testProp, loggingProp, profileProp, portProp, '-jar', jar], ''); //removes empty item. log.exec(cmd, args); this._waitAndLaunchSwaggerUI(gServer, uiPort); const mvn = require('maven').create(); mvn.execute(args); //windows CTRL+C -> Could not exec java: Application finished with exit code: 1 //const child = this.spawnCommand(cmd, args); //process.on('exit', () => child.kill()); }); } _waitAndLaunchSwaggerUI(server, uiPort) { const url = this.swaggerUrl; const request = require('simple-get'); const hostUI = this._hostSwaggerUI; const generator = this; generator.isHostUI = false; const o = setInterval(function () { try { request(url, (err, res) => { if(!err && res.statusCode == 200) { log.debug('200 OK from ' + url + ' , starting ui...'); hostUI(generator, url, server, uiPort); clearInterval(o); } }); }catch(e) {} }, 1000); } _hostSwaggerUI(generator, swaggerUrl, server, uiPort) { if(generator.isHostUI === true) { log.debug("swagger-ui already has been hosted"); return; } generator.isHostUI = true; const express = require('express'); const swaggerUIDir = path.dirname(require.resolve('swagger-ui-dist')); //const paiiUIDir = path.dirname(require.resolve('paii-dist')); const swaggerHtml = fs.readFileSync(path.join(__dirname,'swagger_index.html'),{encoding:'utf-8'}) .replace('http://petstore.swagger.io/v2/swagger.json', swaggerUrl) .replace('http://localhost:3200/oauth2-redirect.html', 'http://localhost:' + uiPort + '/oauth2-redirect.html') .replace('</title>', '</title><base href="/">'); log.debug('swagger ui prepared at ' + swaggerUIDir); /* const paiiHtml = fs.readFileSync(path.join(paiiUIDir,'index.html'),{encoding:'utf-8'}) .replace('http://petstore.swagger.io/v2/swagger.json', swaggerUrl) .replace('</title>', '</title><base href="/paii/">'); log.debug('paii ui prepared at ' + paiiUIDir); */ const app = express(); app.use('/', serveSwaggerUi); app.use('/', express.static(swaggerUIDir)); //app.use('/paii', servePaiiUi); //app.use('/paii/', express.static(paiiUIDir)); log.debug('Listen on port ' + uiPort); app.listen(uiPort); log.info(log.chalk.green("\nService running at " + server.baseUrl + ", now you can browse it from swagger ui http://localhost:" + uiPort + "\n")); function serveSwaggerUi(req,res,next) { return /^\/?$/.test(req.path) ? res.status(200).send(swaggerHtml) : next(); }; function servePaiiUi(req,res,next) { return /^\/?$/.test(req.path) ? res.status(200).send(paiiHtml) : next(); }; } };