createCptTpl.js 6.4 KB

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