createComponentMode.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.json');
  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:
  79. '请选择组件分类(输入编号):1布局组件,2操作反馈,3基础组件,4导航组件,5数据录入,6特色组件',
  80. validate(value) {
  81. const pass = /^[1-6]$/.test(value);
  82. if (pass) {
  83. return true;
  84. }
  85. return '输入有误!请输入选项前编号';
  86. }
  87. },
  88. // {
  89. // type: 'confirm',
  90. // name: 'showDemo',
  91. // message: '是否需要DEMO页面?',
  92. // default: true
  93. // },
  94. // {
  95. // type: 'confirm',
  96. // name: 'showTest',
  97. // message: '是否需要单元测试页面?',
  98. // default: true
  99. // },
  100. {
  101. type: 'input',
  102. name: 'author',
  103. message: '组件作者(可署化名):'
  104. }
  105. ])
  106. .then(function (answers) {
  107. // answers.sort = String(sorts.indexOf(answers.sort));
  108. newCpt = Object.assign(newCpt, answers);
  109. createNew();
  110. });
  111. }
  112. function createIndexJs() {
  113. const nameLc = newCpt.name.toLowerCase();
  114. const destPath = path.join('src/packages/__VUE/' + nameLc);
  115. if (!fs.existsSync(destPath)) {
  116. fs.mkdirSync(destPath);
  117. }
  118. // copy(path.join(__dirname, './__template__/**.*'), destPath, function (err: any, file: any) {
  119. // if (err) {
  120. // console.log('拷贝__template__目录失败!');
  121. // }
  122. // createNew();
  123. // });
  124. if (newCpt.type == 'method') return;
  125. return new Promise((resolve, reject) => {
  126. // let content = `import ${newCpt.name} from './src/${nameLc}.vue';
  127. // ${newCpt.name}.install = function(Vue) {
  128. // Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  129. // };
  130. // export default ${newCpt.name}`;
  131. // let content2 = `${newCpt.name}.install = function(Vue) {
  132. // Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  133. // };
  134. // export default ${newCpt.name}`;
  135. // const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
  136. // const filePath = path.join(dirPath, `index.js`);
  137. // if (!fs.existsSync(dirPath)) {
  138. // fs.mkdirSync(filePath);
  139. // }
  140. // if (newCpt.type == 'filter' || newCpt.type == 'directive'){
  141. // content = content2;
  142. // }
  143. // fs.writeFile(filePath, content, (err) => {
  144. // if (err) throw err;
  145. resolve(`生成index.js文件成功`);
  146. // });
  147. });
  148. }
  149. function createVue() {
  150. return new Promise((resolve, reject) => {
  151. const nameLc = newCpt.name.toLowerCase();
  152. let content = demoModel(nameLc).vue;
  153. const dirPath = path.join(__dirname, `../src/packages/__VUE/${nameLc}/`);
  154. const filePath = path.join(dirPath, `index.vue`);
  155. if (!fs.existsSync(dirPath)) {
  156. fs.mkdirSync(filePath);
  157. }
  158. fs.writeFile(filePath, content, (err) => {
  159. if (err) throw err;
  160. resolve(`生成${newCpt.name}.vue文件成功`);
  161. });
  162. });
  163. }
  164. function createDemo() {
  165. return new Promise((resolve, reject) => {
  166. const nameLc = newCpt.name.toLowerCase();
  167. let content = demoModel(nameLc).demo;
  168. const dirPath = path.join(__dirname, '../src/packages/__VUE/' + nameLc);
  169. const filePath = path.join(dirPath, `demo.vue`);
  170. if (!fs.existsSync(dirPath)) {
  171. fs.mkdirSync(filePath);
  172. }
  173. fs.writeFile(filePath, content, (err) => {
  174. if (err) throw err;
  175. resolve(`生成demo.vue文件成功`);
  176. });
  177. });
  178. }
  179. function addToPackageJson() {
  180. return new Promise((resolve, reject) => {
  181. let sort = newCpt.sort;
  182. newCpt.sort = nav[sort - 1].packages.length + 1;
  183. nav[sort - 1].packages.push(newCpt);
  184. config.nav = nav;
  185. // conf.packages.push(newCpt);
  186. const dirPath = path.join(__dirname, `../`);
  187. const filePath = path.join(dirPath, `src/config.json`);
  188. var tempfile = JSON.stringify(config, null, 2);
  189. fs.writeFile(filePath, tempfile, (err) => {
  190. if (err) throw err;
  191. resolve(`修改config.json文件成功`);
  192. });
  193. });
  194. }
  195. function createScss() {
  196. return new Promise((resolve, reject) => {
  197. const nameLc = newCpt.name.toLowerCase();
  198. let content = `.nut-${nameLc} {}`;
  199. const dirPath = path.join(__dirname, '../src/packages/__VUE/' + nameLc);
  200. const filePath = path.join(dirPath, `index.scss`);
  201. if (!fs.existsSync(dirPath)) {
  202. fs.mkdirSync(filePath);
  203. }
  204. fs.writeFile(filePath, content, (err) => {
  205. if (err) throw err;
  206. resolve(`index.scss文件成功`);
  207. });
  208. });
  209. }
  210. function createDoc() {
  211. return new Promise((resolve, reject) => {
  212. const nameLc = newCpt.name.toLowerCase();
  213. let content = demoModel(nameLc).doc;
  214. const dirPath = path.join(__dirname, '../src/packages/__VUE/' + nameLc);
  215. const filePath = path.join(dirPath, `doc.md`);
  216. if (!fs.existsSync(dirPath)) {
  217. fs.mkdirSync(filePath);
  218. }
  219. fs.writeFile(filePath, content, (err) => {
  220. if (err) throw err;
  221. resolve(`doc.md文件成功`);
  222. });
  223. });
  224. }
  225. function createNew() {
  226. createIndexJs()
  227. .then(() => {
  228. if (newCpt.type == 'component' || newCpt.type == 'method') {
  229. return createVue();
  230. } else {
  231. return;
  232. }
  233. })
  234. .then(() => {
  235. return createScss();
  236. })
  237. .then(() => {
  238. return createDemo();
  239. })
  240. .then(() => {
  241. return createDoc();
  242. })
  243. .then(() => {
  244. return addToPackageJson();
  245. })
  246. .then(() => {
  247. console.log('组件模板生成完毕,请开始你的表演~');
  248. process.exit();
  249. });
  250. }
  251. function createComponent() {
  252. init();
  253. }
  254. createComponent();