Skip to content

Commit

Permalink
Merge pull request #10 from Manjiz/manjiz
Browse files Browse the repository at this point in the history
模板别名
  • Loading branch information
luckyadam committed May 9, 2016
2 parents b85b2ff + c1ee370 commit f7676ae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 32 deletions.
41 changes: 34 additions & 7 deletions lib/create/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var Util = require('../util');

var setting = Util.getSetting();

// 用于缓存模板JSON列表以快速查询
var _onceReadCache = {waitInit:true};

/**
* 读取模板缓存
*/
Expand Down Expand Up @@ -169,22 +172,23 @@ var Base = Class.extend({

/**
* @description 渲染模板
* @param {String} tmpname 模板ID
* @param {String} tmpid 模板ID
* @param {String} type 创建类型,如app
* @param {String} source 模板文件名
* @param {String} dest 生成目标文件路径
* @param {Object} data 模板数据
* @param {Object} options 生成选项
* @return {Object} this
*/
template: function (tmpname, type, source, dest, data, options) {
template: function (tmpid, type, source, dest, data, options) {
if (typeof dest !== 'string') {
options = data;
data = dest;
dest = source;
}

this.fs.copyTpl(
this.templatePath(tmpname, type, source),
this.templatePath(tmpid, type, source),
this.destinationPath(dest),
data || this,
options
Expand All @@ -194,15 +198,23 @@ var Base = Class.extend({

/**
* @description 拷贝并渲染模板
* @param {String} tmpname 模板ID
* @param {Object} tpl {tmpname, tmpid}
* @param {String} type 创建类型,如app
* @param {String} source 模板文件名
* @param {String} dest 生成目标文件路径
* @return {Object} this
*/
copy: function (tmpname, type, source, dest) {
copy: function (tpl, type, source, dest) {
var tmpid = 'default';
dest = dest || source;
this.template(tmpname, type, source, dest);

if(tpl.tmpname) {
tmpid = this.getTmpidByTmpname(tpl.tmpname);
} else {
tmpid = tpl.tmpid || tmpid;
}

this.template(tmpid, type, source, dest);
return this;
},

Expand Down Expand Up @@ -280,7 +292,22 @@ var Base = Class.extend({
if (!Util.existsSync(tmpPath)) {
this.sourceRoot(path.join(__dirname));
}
}
},

/**
* @description 通过模板名称获取模板ID
* @param {string} tmpname
*/
getTmpidByTmpname: function(tmpname) {
var that = this;
if(_onceReadCache.waitInit) {
readCache(path.join(that.sourceRoot(), '_cache.json')).items.forEach(function(item) {
_onceReadCache[item.name] = item._id;
});
_onceReadCache.waitInit = false;
}
return _onceReadCache[tmpname];
},
});

module.exports = Base;
33 changes: 19 additions & 14 deletions lib/create/task/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ var App = Base.extend({
opts.templatesinfo.items.forEach(function(o,i){
tmpchoices.push({
name : o.name,
value: o._id
// value: o._id
value: o.name
})
})

if (typeof conf.tmpId !== 'string') {
if (typeof conf.tmpName !== 'string') {
prompts.push({
type: 'list',
name: 'tmpId',
name: 'tmpName',
message: '请选择项目模板:',
choices: tmpchoices
});
Expand All @@ -156,7 +157,11 @@ var App = Base.extend({
}
answers.appName = answers.appName || conf.appName;
answers.appDescription = answers.appDescription || conf.description;
answers.tmpId = answers.tmpId || conf.tmpId;
answers.tmpName = answers.tmpName || conf.tmpName;

//兼容旧方案
answers.tmpId = this.getTmpidByTmpname(answers.tmpName);

if (conf.sass) {
answers.cssPretreatment = 'sass';
} else if (conf.less) {
Expand All @@ -180,15 +185,15 @@ var App = Base.extend({
* @param {String} [options.appName] - 项目名称
* @param {String} [options.date] - 创建日期
* @param {String} [options.author] - 作者
* @param {String} [options.tmpId] - 模板id
* @param {String} [options.tmpName] - 模板名称
* @param {Function} cb - 创建完后的回调
*/
write: function (options, cb) {
this.conf = _.assign({
appName: null,
date: null,
author: null,
tmpId: 'default'
tmpName: '默认模板'
}, options);
var conf = this.conf;
var commonModule = conf.appName + '/' + 'gb';
Expand Down Expand Up @@ -216,17 +221,17 @@ var App = Base.extend({
this.writeGitKeepFile(commonModule + '/widget');
this.mkdir(commonModule + '/page/gb');

this.copy(options.tmpId ,'app', '_gb.css', commonModule + '/page/gb/gb.css');
this.copy(options.tmpId ,'app', '_gb.js', commonModule + '/page/gb/gb.js');
this.copy(options.tmpId ,'app', '_gb.html', commonModule + '/page/gb/gb.html');
this.copy({tmpname:options.tmpName}, 'app', '_gb.css', commonModule + '/page/gb/gb.css');
this.copy({tmpname:options.tmpName}, 'app', '_gb.js', commonModule + '/page/gb/gb.js');
this.copy({tmpname:options.tmpName}, 'app', '_gb.html', commonModule + '/page/gb/gb.html');
if (conf.cssPretreatment === 'sass') {
this.copy(options.tmpId ,'app', '_common.scss', commonModule + '/static/sass/_common.scss');
this.copy({tmpname:options.tmpName}, 'app', '_common.scss', commonModule + '/static/sass/_common.scss');
}
this.copy(options.tmpId ,'app', '_module-conf.js', commonModule + '/module-conf.js');
this.copy(options.tmpId ,'app', '_static-conf.js', commonModule + '/static-conf.js');
this.copy({tmpname:options.tmpName}, 'app', '_module-conf.js', commonModule + '/module-conf.js');
this.copy({tmpname:options.tmpName}, 'app', '_static-conf.js', commonModule + '/static-conf.js');

this.copy(options.tmpId ,'app', '_app-conf.js', conf.appName + '/app-conf.js');
this.copy(options.tmpId ,'app', 'editorconfig', conf.appName + '/.editorconfig');
this.copy({tmpname:options.tmpName}, 'app', '_app-conf.js', conf.appName + '/app-conf.js');
this.copy({tmpname:options.tmpName}, 'app', 'editorconfig', conf.appName + '/.editorconfig');

this.fs.commit(function () {
if (typeof cb === 'function') {
Expand Down
6 changes: 4 additions & 2 deletions lib/create/task/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ var MModule = Base.extend({
var appConf = require(this.appConfPath);
var conf = this.conf;
conf.tmpId = appConf.tmpId ? appConf.tmpId : 'default';
//-xz160506---
conf.tmpName = appConf.tmpName || undefined;
conf.moduleId = uuid.v1();
this.mkdir(conf.moduleName);
this.mkdir(conf.moduleName + '/page');
Expand All @@ -177,8 +179,8 @@ var MModule = Base.extend({
}
this.mkdir(conf.moduleName + '/widget');
this.writeGitKeepFile(conf.moduleName + '/widget');
this.copy(conf.tmpId || 'default','module' ,'_module-conf.js', conf.moduleName + '/module-conf.js');
this.copy(conf.tmpId || 'default','module' ,'_static-conf.js', conf.moduleName + '/static-conf.js');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId},'module' ,'_module-conf.js', conf.moduleName + '/module-conf.js');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId},'module' ,'_static-conf.js', conf.moduleName + '/static-conf.js');

this.fs.commit(function () {

Expand Down
7 changes: 4 additions & 3 deletions lib/create/task/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var Page = Base.extend({
var appConf = require(this.appConfPath);
var conf = this.conf;
conf.tmpId = appConf.tmpId ? appConf.tmpId : 'default';
conf.tmpName = appConf.tmpName || undefined;
var pageName = conf.pageName;
var cssFileName = '';
this.mkdir('page/' + pageName);
Expand All @@ -177,9 +178,9 @@ var Page = Base.extend({
} else {
cssFileName = 'page/' + pageName + '/' + pageName + '.css';
}
this.copy(conf.tmpId , 'page' , 'page.css', cssFileName);
this.copy(conf.tmpId , 'page' , 'page.js', 'page/' + pageName + '/' + pageName + '.js');
this.copy(conf.tmpId , 'page' , 'page.json', 'page/' + pageName + '/' + pageName + '.json');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'page' , 'page.css', cssFileName);
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'page' , 'page.js', 'page/' + pageName + '/' + pageName + '.js');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'page' , 'page.json', 'page/' + pageName + '/' + pageName + '.json');

this.fs.commit(function () {
if (typeof cb === 'function') {
Expand Down
13 changes: 7 additions & 6 deletions lib/create/task/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,17 @@ var Widget = Base.extend({
var appConf = require(this.appConfPath);
var conf = this.conf;
conf.tmpId = appConf.tmpId ? appConf.tmpId : 'default';
conf.tmpName = appConf.tmpName || undefined;
var widgetName = conf.widgetName;
var cssFileName = '';
this.mkdir('widget/' + widgetName);
this.mkdir('widget/' + widgetName + '/images');
this.writeGitKeepFile('widget/' + widgetName + '/images');
if (!conf.cms) {
this.copy(conf.tmpId , 'widget','widget.html', 'widget/' + widgetName + '/' + widgetName + '.html');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'widget','widget.html', 'widget/' + widgetName + '/' + widgetName + '.html');
} else {
this.copy(conf.tmpId , 'widget','widget_cms.html', 'widget/' + widgetName + '/' + widgetName + '.html');
this.copy(conf.tmpId , 'widget','data.json', 'widget/' + widgetName + '/' + 'data.json');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'widget','widget_cms.html', 'widget/' + widgetName + '/' + widgetName + '.html');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'widget','data.json', 'widget/' + widgetName + '/' + 'data.json');
}
if (conf.cssPretreatment === 'sass') {
cssFileName = 'widget/' + widgetName + '/' + widgetName + '.scss';
Expand All @@ -189,9 +190,9 @@ var Widget = Base.extend({
} else {
cssFileName = 'widget/' + widgetName + '/' + widgetName + '.css';
}
this.copy(conf.tmpId , 'widget','widget.css', cssFileName);
this.copy(conf.tmpId , 'widget','widget.js', 'widget/' + widgetName + '/' + widgetName + '.js');
this.copy(conf.tmpId , 'widget','widget.json', 'widget/' + widgetName + '/' + widgetName + '.json');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'widget','widget.css', cssFileName);
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'widget','widget.js', 'widget/' + widgetName + '/' + widgetName + '.js');
this.copy({tmpname:conf.tmpName, tmpid:conf.tmpId}, 'widget','widget.json', 'widget/' + widgetName + '/' + widgetName + '.json');

this.fs.commit(function () {
if (typeof cb === 'function') {
Expand Down

0 comments on commit f7676ae

Please sign in to comment.