generatePage.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // generatePage.js 文件,放置生成组件的代码
  2. const chalk = require('chalk')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const resolve = (...file) => path.resolve(__dirname, ...file)
  6. const log = message => console.log(chalk.yellow(`${message}`))
  7. const successLog = message => console.log(chalk.green(`${message}`))
  8. const errorLog = error => console.log(chalk.red(`${error}`))
  9. const {
  10. indexPage,
  11. addEditPage,
  12. servicePage
  13. // modelPage
  14. } = require('./template')
  15. const generateFile = (path, data) => {
  16. if (fs.existsSync(path)) {
  17. errorLog(`${path}文件已存在`)
  18. return
  19. }
  20. return new Promise((resolve, reject) => {
  21. fs.writeFile(path, data, 'utf8', err => {
  22. if (err) {
  23. errorLog(err.message)
  24. reject(err)
  25. } else {
  26. resolve(true)
  27. }
  28. })
  29. })
  30. }
  31. log('请输入要生成的页面名称:')
  32. let componentName = ''
  33. process.stdin.on('data', async chunk => {
  34. const inputName = String(chunk)
  35. .trim()
  36. .toString()
  37. // 页面组件目录路径
  38. const componentDirectory = resolve('../src/views', inputName)
  39. // apis-service 目录路径
  40. const apisServiceDirectory = resolve('../src/apis/service', inputName)
  41. // // apis-model 目录路径
  42. // const apisModelDirectory = resolve("../src/apis/model", inputName);
  43. // vue组件路径--首页--列表页
  44. const listTablePath = resolve(componentDirectory, 'index.vue')
  45. // vue组件路径--添加修改详情
  46. const addEditPath = resolve(componentDirectory, 'addEdit.vue')
  47. // service文件路径
  48. const servicePath = resolve(apisServiceDirectory, 'index.js')
  49. // // model文件路径
  50. // const moldePath = resolve(apisModelDirectory, "index.js");
  51. // views页面文件目录
  52. const hasComponentDirectory = fs.existsSync(componentDirectory)
  53. if (hasComponentDirectory) {
  54. errorLog(`${inputName}组件目录已存在,请重新输入`)
  55. return
  56. } else {
  57. log(`正在生成 component 目录 ${componentDirectory}`)
  58. await dotExistDirectoryCreate(componentDirectory)
  59. }
  60. // service文件目录
  61. const hasService = fs.existsSync(apisServiceDirectory)
  62. if (hasService) {
  63. errorLog(`${inputName}service目录已存在,请重新输入`)
  64. return
  65. } else {
  66. log(`正在生成 apis/service 目录 ${apisServiceDirectory}`)
  67. await dotExistDirectoryCreate(apisServiceDirectory)
  68. }
  69. // model 文件目录
  70. // const hasModel = fs.existsSync(apisModelDirectory);
  71. // if (hasModel) {
  72. // errorLog(`${inputName}组件目录已存在,请重新输入`);
  73. // return;
  74. // } else {
  75. // log(`正在生成 apis/model 目录 ${apisModelDirectory}`);
  76. // await dotExistDirectoryCreate(apisModelDirectory);
  77. // }
  78. try {
  79. if (inputName.includes('/')) {
  80. const inputArr = inputName.split('/')
  81. componentName = inputArr[inputArr.length - 1]
  82. } else {
  83. componentName = inputName
  84. }
  85. log(`正在生成 views-列表 文件 ${listTablePath}`)
  86. await generateFile(listTablePath, indexPage(inputName, componentName))
  87. log(`正在生成 views-添加修改详情 文件 ${addEditPath}`)
  88. await generateFile(addEditPath, addEditPage(inputName, componentName))
  89. log(`正在生成 apis-service 文件 ${servicePath}`)
  90. await generateFile(servicePath, servicePage)
  91. // log(`正在生成 apis-model 文件 ${moldePath}`);
  92. // await generateFile(moldePath, modelPage(componentName));
  93. successLog('生成成功')
  94. } catch (e) {
  95. errorLog(e.message)
  96. }
  97. process.stdin.emit('end')
  98. })
  99. process.stdin.on('end', () => {
  100. log('exit')
  101. process.exit()
  102. })
  103. function dotExistDirectoryCreate(directory) {
  104. return new Promise(resolve => {
  105. mkdirs(directory, function() {
  106. resolve(true)
  107. })
  108. })
  109. }
  110. // 递归创建目录
  111. function mkdirs(directory, callback) {
  112. var exists = fs.existsSync(directory)
  113. if (exists) {
  114. callback()
  115. } else {
  116. mkdirs(path.dirname(directory), function() {
  117. fs.mkdirSync(directory)
  118. callback()
  119. })
  120. }
  121. }