Skip to content

Commit

Permalink
update:完善模板别名功能
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed May 9, 2016
1 parent f7676ae commit dd1d1d6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ $ ath app [项目名称]
同时提供了通过携带参数快速创建项目的命令

```
$ ath a --name lo --description 测试 --sass --template default
$ ath a --name lo --description 测试 --sass --template 默认模板
```

Expand All @@ -165,7 +165,7 @@ $ ath a --name lo --description 测试 --sass --template default

参数 `--less` 指定项目使用 `less`

参数 `--template` 指定项目使用的模板,默认模板是 `default`
参数 `--template` 指定项目使用的模板,输入模板名称,默认模板是 `默认模板`

每个参数都是可缺省的。

Expand Down
2 changes: 1 addition & 1 deletion bin/athena
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ program
description: option.description,
sass: option.sass,
less: option.less,
tmpId: option.template
tmpName: option.template
});
// 创建完项目后进行数据上报
app.create(function () {
Expand Down
49 changes: 25 additions & 24 deletions lib/create/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ var Util = require('../util');

var setting = Util.getSetting();

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

/**
* 读取模板缓存
*/
Expand Down Expand Up @@ -94,6 +91,9 @@ var Base = Class.extend({
this.sharedFs = memFs.create();
this.fs = FileEditor.create(this.sharedFs);
this.sourceRoot(path.join(Util.getAthenaPath(), 'tmp'));
// 用于缓存模板JSON列表以快速查询
this.onceReadCache = {waitInit: true};
this.tmpNameList = [];
},

/**
Expand Down Expand Up @@ -172,23 +172,22 @@ var Base = Class.extend({

/**
* @description 渲染模板
* @param {String} tmpid 模板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 (tmpid, 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(tmpid, type, source),
this.templatePath(tmpId, type, source),
this.destinationPath(dest),
data || this,
options
Expand All @@ -198,23 +197,22 @@ var Base = Class.extend({

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

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

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

Expand Down Expand Up @@ -296,18 +294,21 @@ var Base = Class.extend({

/**
* @description 通过模板名称获取模板ID
* @param {string} tmpname
* @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;
getTmpIdByTmpName: function(tmpName) {
if(this.onceReadCache.waitInit) {
readCache(path.join(this.sourceRoot(), '_cache.json')).items.forEach(function(item) {
this.onceReadCache[item.name] = item._id;
this.tmpNameList.push(item.name);
}.bind(this));
this.onceReadCache.waitInit = false;
}
return _onceReadCache[tmpname];
},
if (!tmpName) {
return 'default';
}
return this.onceReadCache[tmpName];
}
});

module.exports = Base;
37 changes: 22 additions & 15 deletions lib/create/task/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var path = require('path');
var chalk = require('chalk');
var inquirer = require('inquirer');
var uuid = require('uuid');
var del = require('del');

var Base = require('../base');
var Util = require('../../util');
Expand Down Expand Up @@ -134,13 +133,12 @@ var App = Base.extend({

var tmpchoices = [];

opts.templatesinfo.items.forEach(function(o,i){
opts.templatesinfo.items.forEach(function (o, i) {
tmpchoices.push({
name : o.name,
// value: o._id
value: o.name
})
})
});
});

if (typeof conf.tmpName !== 'string') {
prompts.push({
Expand All @@ -149,6 +147,16 @@ var App = Base.extend({
message: '请选择项目模板:',
choices: tmpchoices
});
} else {
var tmpId = this.getTmpIdByTmpName(conf.tmpName);
if (tmpId === undefined) {
console.log(chalk.red(' 输入的模板名称有误,请输入正确的模板名称!'));
console.log(' 目前有以下模板可供选择:');
this.tmpNameList.forEach(function (item) {
console.log(' ' + item);
});
return;
}
}

inquirer.prompt(prompts, function (answers) {
Expand All @@ -160,7 +168,7 @@ var App = Base.extend({
answers.tmpName = answers.tmpName || conf.tmpName;

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

if (conf.sass) {
answers.cssPretreatment = 'sass';
Expand Down Expand Up @@ -220,18 +228,17 @@ var App = Base.extend({
this.mkdir(commonModule + '/widget');
this.writeGitKeepFile(commonModule + '/widget');
this.mkdir(commonModule + '/page/gb');

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');
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({tmpname:options.tmpName}, 'app', '_common.scss', commonModule + '/static/sass/_common.scss');
this.copy({tmpName:options.tmpName}, 'app', '_common.scss', commonModule + '/static/sass/_common.scss');
}
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({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({tmpname:options.tmpName}, 'app', '_app-conf.js', conf.appName + '/app-conf.js');
this.copy({tmpname:options.tmpName}, '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: 3 additions & 3 deletions lib/create/task/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ 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.tmpId = appConf.tmpId ? appConf.tmpId : this.getTmpIdByTmpName(conf.tmpName);
conf.moduleId = uuid.v1();
this.mkdir(conf.moduleName);
this.mkdir(conf.moduleName + '/page');
Expand All @@ -179,8 +179,8 @@ var MModule = Base.extend({
}
this.mkdir(conf.moduleName + '/widget');
this.writeGitKeepFile(conf.moduleName + '/widget');
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.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
8 changes: 4 additions & 4 deletions lib/create/task/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ var Page = Base.extend({
var conf = this.conf;
var appConf = require(this.appConfPath);
var conf = this.conf;
conf.tmpId = appConf.tmpId ? appConf.tmpId : 'default';
conf.tmpName = appConf.tmpName || undefined;
conf.tmpId = appConf.tmpId ? appConf.tmpId : this.getTmpIdByTmpName(conf.tmpName);
var pageName = conf.pageName;
var cssFileName = '';
this.mkdir('page/' + pageName);
Expand All @@ -178,9 +178,9 @@ var Page = Base.extend({
} else {
cssFileName = 'page/' + pageName + '/' + pageName + '.css';
}
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.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
14 changes: 7 additions & 7 deletions lib/create/task/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,18 @@ var Widget = Base.extend({
var conf = this.conf;
var appConf = require(this.appConfPath);
var conf = this.conf;
conf.tmpId = appConf.tmpId ? appConf.tmpId : 'default';
conf.tmpName = appConf.tmpName || undefined;
conf.tmpId = appConf.tmpId ? appConf.tmpId : this.getTmpIdByTmpName(conf.tmpName);
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({tmpname:conf.tmpName, tmpid: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({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');
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 @@ -190,9 +190,9 @@ var Widget = Base.extend({
} else {
cssFileName = 'widget/' + widgetName + '/' + widgetName + '.css';
}
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.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
2 changes: 1 addition & 1 deletion lib/create/templates/default/app/_app-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
platform: 'mobile', // 平台 pc or mobile
common: 'gb', // 公共模块名称
moduleList: ['gb'], // 拥有的模块列表
tmpId: '<%= conf.tmpId %>', // 选用模板
tmpName: '<%= conf.tmpName %>', // 选用模板
shtml: { //页面片配置
use: true, //是否使用
needCombo: true // 页面片中链接是否合并
Expand Down

0 comments on commit dd1d1d6

Please sign in to comment.