createCptTpl.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const conf = require('../src/config.json');
  2. const readline = require('readline');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const inquirer = require('inquirer');
  6. const copy = require('copy');
  7. let sorts = [...conf.sorts];
  8. let newCpt = {
  9. version: '1.0.0'
  10. };
  11. function init() {
  12. inquirer.prompt([
  13. {
  14. type: 'input',
  15. name: 'name',
  16. message: '组件英文名(每个单词的首字母都大写,如TextBox):',
  17. validate(value) {
  18. const repeat = conf.packages.find(item => {
  19. return item.name === value;
  20. })
  21. if (repeat) {
  22. return '该组件名已存在!';
  23. }
  24. const pass = value && value.match(/^[A-Z]/);
  25. if (pass) {
  26. return true;
  27. }
  28. return '不能为空,且每个单词的首字母都要大写,如TextBox';
  29. }
  30. },
  31. {
  32. type: 'input',
  33. name: 'chnName',
  34. message: '组件中文名(十个字以内):',
  35. validate(value) {
  36. const pass = value && value.length <= 10;
  37. if (pass) {
  38. return true;
  39. }
  40. return '不能为空,且不能超过十个字符';
  41. }
  42. },
  43. {
  44. type: 'input',
  45. name: 'desc',
  46. message: '组件描述(五十个字以内):'
  47. },
  48. {
  49. type: 'rawlist',
  50. name: 'type',
  51. message: '请选择组件类型(输入编号):',
  52. choices: ['component', 'filter', 'directive', 'method'],
  53. validate(value) {
  54. const pass = value && /^[1-4]$/.test(value);
  55. if (pass) {
  56. return true;
  57. }
  58. return '输入有误!请输入选项前编号';
  59. }
  60. },
  61. {
  62. type: 'rawlist',
  63. name: 'sort',
  64. message: '请选择组件分类(输入编号):',
  65. choices: sorts,
  66. validate(value) {
  67. const pass = /^[1-7]$/.test(value);
  68. if (pass) {
  69. return true;
  70. }
  71. return '输入有误!请输入选项前编号';
  72. }
  73. },
  74. {
  75. type: 'confirm',
  76. name: 'showDemo',
  77. message: '是否需要DEMO页面?',
  78. default: true
  79. },
  80. {
  81. type: 'input',
  82. name: 'author',
  83. message: '组件作者(可署化名):'
  84. }
  85. ]).then(answers => {
  86. answers.sort = String(sorts.indexOf(answers.sort));
  87. newCpt = Object.assign(newCpt, answers);
  88. createDir();
  89. });
  90. }
  91. function createIndexJs() {
  92. if (newCpt.type == 'method') return;
  93. return new Promise((resolve, reject) => {
  94. const nameLc = newCpt.name.toLowerCase();
  95. let content = `import ${newCpt.name} from './${nameLc}.vue';
  96. import './${nameLc}.scss';
  97. ${newCpt.name}.install = function(Vue) {
  98. Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  99. };
  100. export default ${newCpt.name}`;
  101. let content2 = `${newCpt.name}.install = function(Vue) {
  102. Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  103. };
  104. export default ${newCpt.name}`;
  105. const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
  106. const filePath = path.join(dirPath, `index.js`);
  107. if (!fs.existsSync(dirPath)) {
  108. fs.mkdirSync(dirPath);
  109. }
  110. if (newCpt.type == 'filter' || newCpt.type == 'directive') {
  111. content = content2;
  112. }
  113. fs.writeFile(filePath, content, (err) => {
  114. if (err) throw err;
  115. resolve(`生成index.js文件成功`);
  116. });
  117. });
  118. }
  119. function createVue() {
  120. return new Promise((resolve, reject) => {
  121. const nameLc = newCpt.name.toLowerCase();
  122. let content = `<template>
  123. <div class="nut-${nameLc}"></div>
  124. </template>
  125. <script>
  126. export default {
  127. name:'nut-${nameLc}',
  128. props: {
  129. },
  130. data() {
  131. return {};
  132. },
  133. methods: {
  134. }
  135. }
  136. </script>`;
  137. const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
  138. const filePath = path.join(dirPath, `${nameLc}.vue`);
  139. if (!fs.existsSync(dirPath)) {
  140. fs.mkdirSync(dirPath);
  141. }
  142. fs.writeFile(filePath, content, (err) => {
  143. if (err) throw err;
  144. resolve(`生成${newCpt.name}.vue文件成功`);
  145. });
  146. });
  147. }
  148. function createScss() {
  149. return new Promise((resolve, reject) => {
  150. const nameLc = newCpt.name.toLowerCase();
  151. let content = `.nut-${nameLc}{
  152. }`;
  153. const dirPath = path.join(__dirname, `../src/packages/${nameLc}/`);
  154. const filePath = path.join(dirPath, `${nameLc}.scss`);
  155. if (!fs.existsSync(dirPath)) {
  156. fs.mkdirSync(dirPath);
  157. }
  158. fs.writeFile(filePath, content, (err) => {
  159. if (err) throw err;
  160. resolve(`生成${newCpt.name}.scss文件成功`);
  161. });
  162. });
  163. }
  164. function createDir() {
  165. const nameLc = newCpt.name.toLowerCase();
  166. const destPath = path.join(__dirname, '../src/packages/' + nameLc + '/');
  167. if (!fs.existsSync(destPath)) {
  168. fs.mkdirSync(destPath);
  169. }
  170. copy(path.join(__dirname, './__template__/**.*'), destPath, function (err, file) {
  171. if (err) {
  172. console.log('拷贝__template__目录失败!');
  173. }
  174. createNew();
  175. });
  176. }
  177. function addToPackageJson() {
  178. return new Promise((resolve, reject) => {
  179. conf.packages.push(newCpt);
  180. const dirPath = path.join(__dirname, `../src/`);
  181. const filePath = path.join(dirPath, `config.json`);
  182. fs.writeFile(filePath, JSON.stringify(conf, null, 2), (err) => {
  183. if (err) throw err;
  184. resolve(`修改config.json文件成功`);
  185. });
  186. });
  187. }
  188. function createNew() {
  189. createIndexJs().then(() => {
  190. if (newCpt.type == 'component' || newCpt.type == 'method') {
  191. return createVue();
  192. } else {
  193. return;
  194. }
  195. }).then(() => {
  196. return createScss();
  197. }).then(() => {
  198. return addToPackageJson();
  199. }).then(() => {
  200. console.log('组件模板生成完毕,请开始你的表演~');
  201. process.exit();
  202. });
  203. }
  204. init();