createComponentMode.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // 创建模板
  2. const inquirer = require('inquirer');
  3. // import { ROOT_PACKAGE_PATH } from '../util/dic';
  4. const path = require('path');
  5. const fs = require('fs');
  6. const config =require("../src/config");
  7. const demoModel=require("./demo")
  8. const nav=config.nav;
  9. var newCpt = {
  10. version: '3.0.0',
  11. name: '',
  12. type: '',
  13. cName: '',
  14. desc: '',
  15. sort: '',
  16. show: true,
  17. author: ''
  18. };
  19. function init() {
  20. inquirer
  21. .prompt([
  22. {
  23. type: 'input',
  24. name: 'name',
  25. message: '组件英文名(每个单词的首字母都大写,如TextBox):',
  26. validate(value) {
  27. let repeat = false;
  28. for (var i = 0; i < nav.length; i++) {
  29. for (var j = 0; j < nav[i].packages.length; j++) {
  30. if (nav[i].packages[j].name === value) {
  31. repeat = true;
  32. }
  33. }
  34. }
  35. if (repeat) {
  36. return '该组件名已存在!';
  37. }
  38. const pass = value && value.match(/^[A-Z]/);
  39. if (pass) {
  40. return true;
  41. }
  42. return '不能为空,且每个单词的首字母都要大写,如TextBox';
  43. }
  44. },
  45. {
  46. type: 'input',
  47. name: 'cName',
  48. message: '组件中文名(十个字以内):',
  49. validate(value) {
  50. const pass = value && value.length <= 10;
  51. if (pass) {
  52. return true;
  53. }
  54. return '不能为空,且不能超过十个字符';
  55. }
  56. },
  57. {
  58. type: 'input',
  59. name: 'desc',
  60. message: '组件描述(五十个字以内):'
  61. },
  62. {
  63. type: 'rawlist',
  64. name: 'type',
  65. message: '请选择组件类型(输入编号):目前只支持组建模板',
  66. choices: ['component', 'filter', 'directive', 'method'],
  67. validate(value) {
  68. const pass = value && /^[1-4]$/.test(value);
  69. if (pass) {
  70. return true;
  71. }
  72. return '输入有误!请输入选项前编号';
  73. }
  74. },
  75. {
  76. type: 'input',
  77. name: 'sort',
  78. message: '请选择组件分类(输入编号):1布局组件,2操作反馈,3基础组件,4导航组件,5数据录入,6业务组件',
  79. validate(value) {
  80. const pass = /^[1-6]$/.test(value);
  81. if (pass) {
  82. return true;
  83. }
  84. return '输入有误!请输入选项前编号';
  85. }
  86. },
  87. // {
  88. // type: 'confirm',
  89. // name: 'showDemo',
  90. // message: '是否需要DEMO页面?',
  91. // default: true
  92. // },
  93. // {
  94. // type: 'confirm',
  95. // name: 'showTest',
  96. // message: '是否需要单元测试页面?',
  97. // default: true
  98. // },
  99. {
  100. type: 'input',
  101. name: 'author',
  102. message: '组件作者(可署化名):'
  103. }
  104. ])
  105. .then(function(answers) {
  106. // answers.sort = String(sorts.indexOf(answers.sort));
  107. newCpt = Object.assign(newCpt, answers);
  108. createNew();
  109. });
  110. }
  111. function createIndexJs() {
  112. const nameLc = newCpt.name.toLowerCase();
  113. const destPath = path.join('src/packages/' + nameLc);
  114. if (!fs.existsSync(destPath)) {
  115. fs.mkdirSync(destPath);
  116. }
  117. // copy(path.join(__dirname, './__template__/**.*'), destPath, function (err: any, file: any) {
  118. // if (err) {
  119. // console.log('拷贝__template__目录失败!');
  120. // }
  121. // createNew();
  122. // });
  123. if (newCpt.type == 'method') return;
  124. return new Promise((resolve, reject) => {
  125. // let content = `import ${newCpt.name} from './src/${nameLc}.vue';
  126. // ${newCpt.name}.install = function(Vue) {
  127. // Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  128. // };
  129. // export default ${newCpt.name}`;
  130. // let content2 = `${newCpt.name}.install = function(Vue) {
  131. // Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  132. // };
  133. // export default ${newCpt.name}`;
  134. // const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
  135. // const filePath = path.join(dirPath, `index.js`);
  136. // if (!fs.existsSync(dirPath)) {
  137. // fs.mkdirSync(filePath);
  138. // }
  139. // if (newCpt.type == 'filter' || newCpt.type == 'directive'){
  140. // content = content2;
  141. // }
  142. // fs.writeFile(filePath, content, (err) => {
  143. // if (err) throw err;
  144. resolve(`生成index.js文件成功`);
  145. // });
  146. });
  147. }
  148. function createVue() {
  149. return new Promise((resolve, reject) => {
  150. const nameLc = newCpt.name.toLowerCase();
  151. let content = demoModel(nameLc).vue;
  152. const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
  153. const filePath = path.join(dirPath, `index.vue`);
  154. if (!fs.existsSync(dirPath)) {
  155. fs.mkdirSync(filePath);
  156. }
  157. fs.writeFile(filePath, content, err => {
  158. if (err) throw err;
  159. resolve(`生成${newCpt.name}.vue文件成功`);
  160. });
  161. });
  162. }
  163. function createDemo() {
  164. return new Promise((resolve, reject) => {
  165. const nameLc = newCpt.name.toLowerCase();
  166. let content = demoModel(nameLc).demo;
  167. const dirPath = path.join(__dirname, '../src/packages/' + nameLc);
  168. const filePath = path.join(dirPath, `demo.vue`);
  169. if (!fs.existsSync(dirPath)) {
  170. fs.mkdirSync(filePath);
  171. }
  172. fs.writeFile(filePath, content, err => {
  173. if (err) throw err;
  174. resolve(`生成demo.vue文件成功`);
  175. });
  176. });
  177. }
  178. function addToPackageJson() {
  179. return new Promise((resolve, reject) => {
  180. let sort=newCpt.sort;
  181. newCpt.sort=nav[sort-1].packages.length+1;
  182. nav[sort-1].packages.push(newCpt);
  183. config.nav=nav;
  184. // conf.packages.push(newCpt);
  185. const dirPath = path.join(__dirname, `../`);
  186. const filePath = path.join(dirPath, `src/config.js`);
  187. var tempfile="module.exports = "+JSON.stringify(config, null, 2)+";"
  188. fs.writeFile(filePath, tempfile, (err) => {
  189. if (err) throw err;
  190. resolve(`修改config.json文件成功`);
  191. });
  192. });
  193. }
  194. function createScss() {
  195. return new Promise((resolve, reject) => {
  196. const nameLc = newCpt.name.toLowerCase();
  197. let content = `.nut-${nameLc} {}`;
  198. const dirPath = path.join(__dirname, '../src/packages/' + nameLc);
  199. const filePath = path.join(dirPath, `index.scss`);
  200. if (!fs.existsSync(dirPath)) {
  201. fs.mkdirSync(filePath);
  202. }
  203. fs.writeFile(filePath, content, err => {
  204. if (err) throw err;
  205. resolve(`index.scss文件成功`);
  206. });
  207. });
  208. }
  209. function createDoc() {
  210. return new Promise((resolve, reject) => {
  211. const nameLc = newCpt.name.toLowerCase();
  212. let content = demoModel(nameLc).doc;
  213. const dirPath = path.join(__dirname, '../src/packages/' + nameLc);
  214. const filePath = path.join(dirPath, `doc.md`);
  215. if (!fs.existsSync(dirPath)) {
  216. fs.mkdirSync(filePath);
  217. }
  218. fs.writeFile(filePath, content, err => {
  219. if (err) throw err;
  220. resolve(`doc.md文件成功`);
  221. });
  222. });
  223. }
  224. function createNew() {
  225. createIndexJs()
  226. .then(() => {
  227. if (newCpt.type == 'component' || newCpt.type == 'method') {
  228. return createVue();
  229. } else {
  230. return;
  231. }
  232. })
  233. .then(() => {
  234. return createScss();
  235. })
  236. .then(() => {
  237. return createDemo();
  238. })
  239. .then(() => {
  240. return createDoc();
  241. })
  242. .then(() => {
  243. return addToPackageJson();
  244. })
  245. .then(() => {
  246. console.log('组件模板生成完毕,请开始你的表演~');
  247. process.exit();
  248. });
  249. }
  250. function createComponent() {
  251. init();
  252. }
  253. createComponent();