addComponent.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const conf = require('../config.json');
  2. const readline = require('readline');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const inquirer = require('inquirer');
  6. let newCpt = {};
  7. let rl = readline.createInterface({
  8. input: process.stdin,
  9. output: process.stdout
  10. });
  11. function init() {
  12. inquirer.prompt([
  13. {
  14. type: 'input',
  15. name: 'name',
  16. message: '组件英文名(每个单词的首字母都大写,如TextBox):',
  17. validate: function (value) {
  18. var pass = value && value.match(/^[A-Z]/);
  19. if (pass) {
  20. return true;
  21. }
  22. return '不能为空,且每个单词的首字母都要大写,如TextBox';
  23. }
  24. },
  25. {
  26. type: 'input',
  27. name: 'chnName',
  28. message: '组件中文名(十个字以内):'
  29. },
  30. {
  31. type: 'input',
  32. name: 'desc',
  33. message: '组件描述(五十个字以内):'
  34. },
  35. {
  36. type: 'rawlist',
  37. name: 'type',
  38. message: '请选择组件类型(输入编号):',
  39. choices: ['component', 'filter', 'directive', 'method']
  40. },
  41. {
  42. type: 'confirm',
  43. name: 'showDemo',
  44. message: '是否需要DEMO页面?',
  45. default: true
  46. },
  47. /* {
  48. type: 'input',
  49. name: 'erp',
  50. message: 'ERP'
  51. },
  52. {
  53. type: 'input',
  54. name: 'email',
  55. message: 'Email'
  56. }, */
  57. ]).then(answers => {
  58. newCpt = answers;
  59. createNew();
  60. });
  61. }
  62. function createIndexJs() {
  63. if (newCpt.type == 'method') return;
  64. return new Promise((resolve, reject) => {
  65. const nameLc = newCpt.name.toLowerCase();
  66. let content = `import ${newCpt.name} from './src/${nameLc}.vue';
  67. ${newCpt.name}.install = function(Vue) {
  68. Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  69. };
  70. export default ${newCpt.name}`;
  71. let content2 = `${newCpt.name}.install = function(Vue) {
  72. Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
  73. };
  74. export default ${newCpt.name}`;
  75. const dirPath = path.join(__dirname, `../src/package/${nameLc}/`);
  76. const filePath = path.join(dirPath, `index.js`);
  77. if (!fs.existsSync(dirPath)) {
  78. fs.mkdirSync(dirPath);
  79. }
  80. if (newCpt.type == 'filter' || newCpt.type == 'directive'){
  81. content = content2;
  82. }
  83. fs.writeFile(filePath, content, (err) => {
  84. if (err) throw err;
  85. resolve(`生成index.js文件成功`);
  86. });
  87. });
  88. }
  89. function createVue() {
  90. return new Promise((resolve, reject) => {
  91. const nameLc = newCpt.name.toLowerCase();
  92. let content = `<template>
  93. <div class="nut-${nameLc}"></div>
  94. </template>
  95. <script>
  96. export default {
  97. name:'nut-${nameLc}',
  98. props: {
  99. },
  100. data() {
  101. return {};
  102. },
  103. methods: {
  104. }
  105. }
  106. </script>
  107. <style lang="scss">
  108. </style>`;
  109. const dirPath = path.join(__dirname, `../src/package/${nameLc}/src/`);
  110. const filePath = path.join(dirPath, `${nameLc}.vue`);
  111. if (!fs.existsSync(dirPath)) {
  112. fs.mkdirSync(dirPath);
  113. }
  114. fs.writeFile(filePath, content, (err) => {
  115. if (err) throw err;
  116. resolve(`生成${newCpt.name}.vue文件成功`);
  117. });
  118. });
  119. }
  120. function createView() {
  121. return new Promise((resolve, reject) => {
  122. const nameLc = newCpt.name.toLowerCase();
  123. const fileName = 'viewTpl.vue';
  124. const sourceFile = path.join(__dirname, './' + fileName);
  125. const destPath = path.join(__dirname, '../src/view/', nameLc + '.vue');
  126. const readStream = fs.createReadStream(sourceFile);
  127. const writeStream = fs.createWriteStream(destPath);
  128. readStream.pipe(writeStream);
  129. resolve('生成文档模板文件成功');
  130. });
  131. }
  132. function createDemo() {
  133. return new Promise((resolve, reject) => {
  134. const nameLc = newCpt.name.toLowerCase();
  135. const fileName = 'demoTpl.vue';
  136. const sourceFile = path.join(__dirname, './' + fileName);
  137. const destPath = path.join(__dirname, '../src/demo/', nameLc + '.vue');
  138. const readStream = fs.createReadStream(sourceFile);
  139. const writeStream = fs.createWriteStream(destPath);
  140. readStream.pipe(writeStream);
  141. resolve('生成示例模板文件成功');
  142. });
  143. }
  144. function addToPackageJson() {
  145. return new Promise((resolve, reject) => {
  146. conf.packages.push(newCpt);
  147. const dirPath = path.join(__dirname, `../`);
  148. const filePath = path.join(dirPath, `config.json`);
  149. fs.writeFile(filePath, JSON.stringify(conf, null, 2), (err) => {
  150. if (err) throw err;
  151. resolve(`修改config.json文件成功`);
  152. });
  153. });
  154. }
  155. function createNew() {
  156. createIndexJs().then(() => {
  157. if (newCpt.type == 'component' || newCpt.type == 'method') {
  158. return createVue();
  159. } else {
  160. return;
  161. }
  162. }).then(() => {
  163. return createView();
  164. }).then(() => {
  165. return createDemo();
  166. }).then(() => {
  167. return addToPackageJson();
  168. }).then(() => {
  169. console.log('组件模板生成完毕,请开始你的表演~');
  170. process.exit();
  171. });
  172. }
  173. init();