generator-clam
Version:
A Clam generator for Yeoman
151 lines (124 loc) • 3.89 kB
JavaScript
// 'use strict';
var util = require('util');
var path = require('path');
var ClamLogo = require('../app/logo').ClamLogo;
var yeoman = require('yeoman-generator');
var ABC = require('abc-generator');
var gitConfig = require('git-config'),
curGitUser = gitConfig.sync().user,
curUserName = curGitUser.name,
curUserEmail = curGitUser.email;
var AppGenerator = module.exports = function AppGenerator(args, options, config) {
// yeoman.generators.Base.apply(this, arguments);
ABC.UIBase.apply(this, arguments);
this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname, '../package.json')));
this.on('end', function () {
console.log(green('done!'));
}.bind(this));
};
util.inherits(AppGenerator, ABC.UIBase);
AppGenerator.prototype.askFor = function askFor() {
var cb = this.async();
var modsPagesWidgets = false;
try {
abcJSON = require(path.resolve(process.cwd(), 'abc.json'));
} catch (e) {
console.log('abc.json not found');
try {
abcJSON = require(path.resolve(process.cwd(),'..', 'abc.json'));
} catch(e){
try {
abcJSON = require(path.resolve(process.cwd(),'../../', 'abc.json'));
modsPagesWidgets = path.basename(process.cwd());
} catch(e){
console.log('do nothing!');
process.exit();
}
}
}
if (!abcJSON.author) {
abcJSON.author = {
name: '',
email: ''
}
}
if(!abcJSON.name){
abcJSON.name = 'tmp';
}
if(!abcJSON.group){
abcJSON.group = 'groupName';
}
if(!abcJSON.combohtml){
abcJSON.combohtml = 'false';
}
if(!abcJSON.cssCompile){
abcJSON.cssCompile = 'less';
}
this.modsPagesWidgets = modsPagesWidgets;
this.projectName = abcJSON.name;
this.combohtml = abcJSON.combohtml;
this.cssCompile = abcJSON.cssCompile;
this.author = curUserName;
this.email = curUserEmail;
// welcome message
console.log(ClamLogo(this));
console.log('建议您在 src/pages 目录执行该命令!');
var prompts = [{
name: 'mojoName',
message: 'Name of Page?',
default: 'your-page-name',
waring:''
}
];
// your-mojo-name => YourMojoName
function parseName(name){
return name.replace(/\b(\w)|(-\w)/g,function(m){
return m.toUpperCase().replace('-','');
});
}
this.prompt(prompts, function (err, props) {
if (err) {
return this.emit('error', err);
}
var _tname = props.mojoName;
this.mojoName = this.modsPagesWidgets? this.modsPagesWidgets + '/' + props.mojoName : props.mojoName;// your-mod-name
this.modName = parseName(_tname).replace(/^(~|-)/,'');//YourModName
this.packageName = abcJSON.name;// package-name
this.cssCompile = abcJSON.cssCompile;
this.groupName = abcJSON.group;
//this.config = abcJSON.config;
this.config = 'http://g.tbcdn.cn/'+this.groupName+'/'+this.packageName+'/'+abcJSON.version+'/config.js';
this.projectName = parseName(this.packageName); //PackageName
this.srcPath = this.modsPagesWidgets? '../../' : '../';
cb();
}.bind(this));
};
AppGenerator.prototype.files = function files(){
// 如果有mods/widgets/pages,就把前缀替换回来
var mojoName = this.modsPagesWidgets? this.mojoName.replace(/^([^\/]+)\//i,'') : this.mojoName;
this.mkdir(mojoName);
this.mkdir(mojoName+'/img');
var appendix = (this.combohtml === 'true'? '.html' : '.html');
this.template('index.htm',mojoName + '/index'+appendix);
this.template('index.js',mojoName+'/index.js');
this.template('mock.tms.htm',mojoName+'/mock.tms.html');
this.template('index.less',mojoName+'/index.'+this.cssCompile);
};
function consoleColor(str,num){
if (!num) {
num = '32';
}
return "\033[" + num +"m" + str + "\033[0m"
}
function green(str){
return consoleColor(str,32);
}
function yellow(str){
return consoleColor(str,33);
}
function red(str){
return consoleColor(str,31);
}
function blue(str){
return consoleColor(str,34);
}