UNPKG

jmms

Version:

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

103 lines (88 loc) 3.54 kB
const _ = require('lodash'); const log = require('../../log'); const path = require('path'); const fs = require('fs'); const chalk = require('chalk'); const shelljs = require('shelljs'); const mkdirp = require('mkdirp'); var BaseGenerator = require('../../base'); module.exports = class extends BaseGenerator { constructor(args, opts) { super(args, opts, false); } initializing() { this.projectName = this.options['name']; this.projectDir = this.options.projectDir; if(!this.projectDir) { this.projectDir = path.join(this.destinationRoot(), this.projectName); this.checkExists = true; }else { this.checkExists = false; } } writing() { //todo: user input const vars = {}; _.merge(vars, require("../../vars")) vars.name = this.projectName; vars.groupId = 'services'; vars.artifactId = vars.name; vars.version = '1.0.0-SNAPSHOT'; //Check is the project dir already exists. const projectDirExists = fs.existsSync(this.projectDir); if(this.checkExists && projectDirExists){ log.error("The project directory '" + this.projectDir + "' already exists, can't create project!"); process.exit(1); } //Create the project dir if not exists. if(!projectDirExists) { fs.mkdirSync(this.projectDir); } this.destinationRoot(this.projectDir); log.info(`\n${chalk.bold("Creating project...")}\n`); //Create all dirs. this._mkdirs('./', this.templatePath()); //Create entities dir mkdirp.sync(this.destinationPath('./app/entities')); //Copy tempalte files. this.fs.copyTpl(this.templatePath('**'), this.destinationRoot(), vars); //Copy dot files. this.fs.copyTpl(this.templatePath('**/.*'), this.destinationRoot(), vars); //Copy .npmignore to .gitignore const npmIgnoreFile = this.templatePath('.npmignore'); const gitIgnoreFile = this.destinationPath('.gitignore'); if(fs.existsSync(npmIgnoreFile) && !fs.existsSync(gitIgnoreFile)) { if(fs.existsSync(this.destinationPath('.npmignore'))) { this.fs.delete(this.destinationPath('.npmignore')); } this.fs.copy(this.templatePath('.npmignore'), this.destinationPath('.gitignore')); } } _mkdirs(prefix, dir) { fs.readdirSync(dir).forEach((name) => { const child = dir + '/' + name; const stats = fs.statSync(child); if(stats && stats.isDirectory()) { const relativePath = prefix + name + '/'; mkdirp.sync(this.destinationPath(relativePath)); this._mkdirs(relativePath, child); } }); } install() { //package the executable jar. //log.info(`\n${chalk.blue('Project files generated! Building the project...')}\n`); //this.spawnCommandSync('mvn',['package', '--file', this.destinationRoot()]); /* //init git repository. if(shelljs.which('git')) { log.info(`\n${chalk.blue('Init git repository.')}`); const cmd = 'git init ' + this.destinationRoot(); shelljs.exec(cmd); } */ } end() { log.info(`\n${chalk.green.bold("Project created at '" + this.projectDir + "' successfully!")}\n`); } };