diff --git a/bin/athena b/bin/athena index 15ed977..841a228 100755 --- a/bin/athena +++ b/bin/athena @@ -200,7 +200,7 @@ program description: option.description, sass: option.sass, less: option.less, - tmpId: option.template + tmpName: option.template }); // 创建完项目后进行数据上报 app.create(function () { diff --git a/lib/create/base.js b/lib/create/base.js index 544df14..7558d27 100644 --- a/lib/create/base.js +++ b/lib/create/base.js @@ -24,9 +24,6 @@ var Util = require('../util'); var setting = Util.getSetting(); -// 用于缓存模板JSON列表以快速查询 -var _onceReadCache = {waitInit:true}; - /** * 读取模板缓存 */ @@ -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 = []; }, /** @@ -172,7 +172,7 @@ var Base = Class.extend({ /** * @description 渲染模板 - * @param {String} tmpid 模板ID + * @param {String} tmpId 模板ID * @param {String} type 创建类型,如app * @param {String} source 模板文件名 * @param {String} dest 生成目标文件路径 @@ -180,15 +180,14 @@ var Base = Class.extend({ * @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 @@ -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; }, @@ -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; diff --git a/lib/create/task/app.js b/lib/create/task/app.js index e762bef..5dad912 100644 --- a/lib/create/task/app.js +++ b/lib/create/task/app.js @@ -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'); @@ -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({ @@ -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) { @@ -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'; @@ -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') { diff --git a/lib/create/task/module.js b/lib/create/task/module.js index b128938..06a5894 100644 --- a/lib/create/task/module.js +++ b/lib/create/task/module.js @@ -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'); @@ -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 () { diff --git a/lib/create/task/page.js b/lib/create/task/page.js index 9441f0d..6e49592 100644 --- a/lib/create/task/page.js +++ b/lib/create/task/page.js @@ -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); @@ -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') { diff --git a/lib/create/task/widget.js b/lib/create/task/widget.js index 5c5ecc9..997f48a 100644 --- a/lib/create/task/widget.js +++ b/lib/create/task/widget.js @@ -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'; @@ -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') { diff --git a/lib/create/templates/default/app/_app-conf.js b/lib/create/templates/default/app/_app-conf.js index e5139ce..4120184 100644 --- a/lib/create/templates/default/app/_app-conf.js +++ b/lib/create/templates/default/app/_app-conf.js @@ -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 // 页面片中链接是否合并