jmms
Version:
Jmms cli tools, Jmms is a java meta-micro-service framework
145 lines (119 loc) • 5.42 kB
JavaScript
const _ = require('lodash');
const log = require('../../log');
const metas = require('../../metas');
const path = require('path');
const fs = require('fs');
const shelljs = require('shelljs');
const replace = require('replace-in-file');
const gitdown = require('download-repo');
var BaseGenerator = require('../../base');
module.exports = class extends BaseGenerator {
constructor(args, opts) {
super(args, opts, false);
}
initializing() {
const dir = process.cwd();
const api = this._initApi(dir);
const app = api.app;
const web = this._initWeb(dir);
const mvue = this._initMvue();
log.info('Create web project from github:bingo-oss/mvue@' + web.branch + '...');
gitdown("bingo-oss/mvue", {target: 'mvue', branch: web.target}).then(() => {
//init mvue project and cleanup downloaded files.
shelljs.mv(mvue.tpl, web.dir);
shelljs.rm('-rf',mvue.dir);
//replace the variable at mvue template.
replace.sync({files: path.join(web.dir, 'package.json'), from: /\{\{name\}\}/g, to: web.proj});
replace.sync({files: path.join(web.dir, 'config/index.js'), from: /\{\{port\}\}/g, to: web.port});
const proxyTable = 'proxyTable: {\n' +
'\t\t\t"/api": {"target": "' + app.server.baseUrl + '", "pathRewrite": {"^/api": "/api"}},\n' +
'\t\t\t"/assets": {"target": "' + app.server.baseUrl + '", "pathRewrite": {"^/assets": "/assets"}},\n' +
'\t\t\t"/oauth2": {"target": "' + app.server.baseUrl + '", "pathRewrite": {"^/oauth2": "/oauth2"}},\n' +
'\t\t\t"/login": {"target": "' + app.server.baseUrl + '", "pathRewrite": {"/login": "/login"}},\n' +
'\t\t\t"/logout": {"target": "' + app.server.baseUrl + '", "pathRewrite": {"/logout": "/logout"}}\n' +
'\t\t}';
replace.sync({files: path.join(web.dir, 'config/index.js'), from: /proxyTable:\s*\{\}/g, to: proxyTable});
//const apiBaseUrl = "apiBaseUrl: \"/api\"";
fs.writeFileSync(path.join(web.dir, 'static/config/config.js'), 'window.config={\n "configUrl" : "api/web.json"\n}');
//replace.sync({files: path.join(web.dir, 'static/config/config.js'), from: /apiBaseUrl:\s*\"\"/g, to: apiBaseUrl});
replace.sync({files: path.join(web.dir, 'index.html'), from: /mvue模板/g, to: ''});
replace.sync({files: path.join(web.dir, 'src/modules/home/home.vue'), from: /<ul class="topbar-menu">/g, to: '<ul class="topbar-menu" style="display:none">'})
const apiWebPath = '../../' + api.name + '/web';
replace.sync({files: path.join(web.dir, 'config/index.js'), from: /'..\/dist/g, to: '\'' + apiWebPath});
//init api project
this.destinationRoot(api.path);
//update pom.xml
if(web.name != 'web') {
replace.sync({files: api.pom, from: /..\/web\//g, to: '../' + web.name + '/'});
}
if(!fs.existsSync(api.web)) {
fs.mkdirSync(api.web);
}
//Copy tempalte files.
this.fs.copyTpl(this.templatePath('**'), this.destinationRoot());
log.info("Web project created success!!!\n\n");
//npm install
if(shelljs.which('cnpm')){
log.info("cnpm installing....");
shelljs.cd(web.dir);
shelljs.exec('cnpm install');
}else {
log.info("npm installing....");
shelljs.cd(web.dir);
shelljs.exec('npm install');
}
log.info('\n\nNode modules installed, building...')
shelljs.exec('npm run build');
});
}
_initWeb(dir) {
var name = this.options['name'];
if(!name) {
name = 'web';
}
const dev = this.options['dev'] == true;
const info = {
proj : path.basename(dir),
name : name,
dir : path.join(dir, name),
dev : dev,
branch : dev ? 'dev': 'master',
port : '9596'
}
if(fs.existsSync(info.dir)) {
log.error("'" + info.dir + "' already exists, can't create mvue web project!!!");
process.exit(0);
}
return info;
}
_initMvue() {
return {
dir : path.join(process.cwd(), 'mvue'),
tpl : path.join(process.cwd(), 'mvue/template')
}
}
_initApi(dir) {
var name = this.options['api'];
if(!name) {
name = 'api';
}
const apiDir = path.join(dir, name);
const api = {
name: name,
path: apiDir,
pom : path.join(apiDir, 'pom.xml'),
web : path.join(apiDir, 'web'),
app : require('../../app')
};
if(!fs.existsSync(apiDir)) {
log.error("The api project '" + apiDir + '" is not exists, please create it');
process.exit(1);
}
if(!fs.existsSync(api.pom) || !fs.existsSync(path.join(apiDir, 'app/config.json'))) {
log.error("Dir '" + apiDir + "' is not a valid api project!");
process.exit(1);
}
api.app.init(path.join(apiDir, 'app'), {});
return api;
}
};