auto.code.plugin.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * actor jiinfo-team
  3. * date 2020 5 22
  4. * file 自动化编码补丁
  5. */
  6. const fs = require('fs')
  7. const path = require('path')
  8. const chokidar = require('chokidar')
  9. const jsbeautify = require('js-beautify')
  10. // 封装美化
  11. const beautify = function(fileName, str, config) {
  12. if (path.extname(fileName) === '.vue') {
  13. return jsbeautify.html(str, config)
  14. } else if (path.extname(fileName) === '.html') {
  15. return jsbeautify.html(str, config)
  16. } else if (path.extname(fileName) === '.js') {
  17. return jsbeautify.js(str, config)
  18. } else if (path.extname(fileName) === '.css') {
  19. return jsbeautify.css(str, config)
  20. } else if (path.extname(fileName) === '.scss') {
  21. return jsbeautify.css(str, config)
  22. } else if (path.extname(fileName) === '.sass') {
  23. return jsbeautify.css(str, config)
  24. } else if (path.extname(fileName) === '.compass') {
  25. return jsbeautify.css(str, config)
  26. } else {
  27. return str
  28. }
  29. }
  30. class AutoCodePlugin {
  31. // 构造方法
  32. constructor(options) {
  33. // 观察者
  34. this.watcher = []
  35. // 文件操作
  36. class AutoFile {
  37. constructor(options) {
  38. this.watchQueue = options
  39. // 文件数据
  40. this.file = {}
  41. }
  42. // 创建文件数据
  43. setFileData(d) {
  44. if (!d.key) { console.error('缺少文件key信息'); return }
  45. this.file[d.key] = d
  46. }
  47. // 写入文件
  48. write(d) {
  49. // 获取文件主体信息
  50. const bodyData = Object.keys(this.file).reduce((add, d) => {
  51. return add += this.file[d].value
  52. }, '')
  53. // 获取文件数据 && JS美化
  54. const data = beautify(this.watchQueue.outPath, this.watchQueue.out ? this.watchQueue.out(bodyData, this.file) : bodyData,
  55. d.beauty ? d.beauty : { indent_size: 4 }
  56. )
  57. // 写入文件
  58. fs.writeFileSync(this.watchQueue.outPath, data)
  59. }
  60. // 移除文件夹
  61. delete(key) {
  62. delete this.file[key]
  63. }
  64. // 获取文件等级
  65. getLevel(p1, p2) {
  66. const p1Resolve = this.getPath(p1)
  67. const p2Resolve = this.getPath(p2)
  68. const replace = p2Resolve.replace(p1Resolve, '')
  69. return (replace.split('/').length) - 1
  70. }
  71. // 获取路径
  72. getPath(p) {
  73. return path.resolve(p).split(path.sep).join('/')
  74. }
  75. }
  76. // 当监听层级 > 1时候执行 fs.wtachFile轮询
  77. // 当监听层级 = 1时候执行 fs.watch监听
  78. const usePolling = options.some(d => d.maxlevel > 1)
  79. // 启动
  80. options.forEach(d => {
  81. // 创建异步闭包
  82. (async d => {
  83. // 验证必要参数
  84. if (!d.inPath) { console.error('缺少inPath参数'); return }
  85. if (!d.templateEach) { console.error('缺少templateEach参数'); return }
  86. // 文件夹等级(创建默认参数)
  87. const level = Number(d.maxlevel) ? Number(d.maxlevel) : 1
  88. // 输出文本(创建默认参数)
  89. if (!d.out) d.out = template => template
  90. // 输出文件(创建默认参数)
  91. if (!d.outPath) {
  92. const p = path.resolve(d.inPath).split(path.sep)
  93. p[p.length] = 'index.js'
  94. d.outPath = p.join('/')
  95. }
  96. // 实列化
  97. const autoFile = new AutoFile(d)
  98. // 监听层级
  99. const watchLevel = d.maxlevel ? d.maxlevel : 1
  100. // 观察者配置
  101. const op = {
  102. // 层级
  103. depth: watchLevel,
  104. usePolling
  105. }
  106. if (usePolling) {
  107. // 轮询节奏
  108. op.interval = 1000
  109. op.binaryInterval = 1000
  110. }
  111. // 创建观察者
  112. const watcher = chokidar.watch(d.inPath, op)
  113. this.watcher.push(watcher)
  114. // 先写文件
  115. autoFile.write(d)
  116. // 增加
  117. const addFile = (dirName, kind) => {
  118. // 判断是否在最大监听文件目录等级内
  119. const lev = autoFile.getLevel(d.inPath, dirName)
  120. if (lev <= level && lev > 0) {
  121. // 判断环境
  122. let product = (process.env.NODE_ENV === 'production')
  123. // 可配置生产 || 测试环境 AutoCodePlugin.NODE_ENV = 'pro';
  124. if (this.NODE_ENV) product = (process.env.NODE_ENV === this.NODE_ENV)
  125. // 是不是空文件夹
  126. const isEmptyDir = (kind === 'dir' && fs.readdirSync(path.resolve(dirName)).length === 0)
  127. // 判断是不是开发环境 && 是不是文件夹 && 是不是空文件夹 && 存在入口文件config
  128. if (!product && kind === 'dir' && isEmptyDir && d.addIndex) {
  129. // 创建自定义模板
  130. d.addIndex.forEach(autoTempalte => {
  131. ((autoTempalte, dirName) => {
  132. if (autoTempalte.state === 'file') {
  133. // 默认创建文件
  134. let name = ''
  135. if (!autoTempalte.name) name = 'index.js'
  136. if (!autoTempalte.template) autoTempalte.template = () => ''
  137. // 增加命名函数逻辑
  138. if (Array.prototype.toString.call(autoTempalte.name) === '[object Function]') {
  139. name = autoTempalte.name(path.resolve(dirName).split(path.sep).pop(), autoFile.getPath(dirName))
  140. } else {
  141. name = autoTempalte.name
  142. }
  143. const template = beautify(name, autoTempalte.template(path.resolve(dirName).split(path.sep).pop(), autoFile.getPath(dirName)),
  144. d.beauty ? d.beauty : { indent_size: 4 }
  145. )
  146. fs.writeFileSync(path.join(path.resolve(dirName), name), template)
  147. } else {
  148. // 默认创建文件
  149. let name = ''
  150. if (!autoTempalte.name) return
  151. // 增加命名函数逻辑
  152. if (Array.prototype.toString.call(autoTempalte.name) === '[object Function]') {
  153. name = autoTempalte.name(path.resolve(dirName).split(path.sep).pop(), autoFile.getPath(dirName))
  154. } else {
  155. name = autoTempalte.name
  156. }
  157. // 创建文件夹
  158. fs.mkdirSync(path.join(path.resolve(dirName), name))
  159. }
  160. })(autoTempalte, dirName)
  161. })
  162. }
  163. // 创建文件数据
  164. autoFile.setFileData({
  165. // 文件名称
  166. key: autoFile.getPath(dirName),
  167. // 数据内容
  168. value: d.templateEach ? d.templateEach(path.resolve(dirName).split(path.sep).pop(), autoFile.getPath(dirName), kind) : ''
  169. })
  170. autoFile.write(d)
  171. }
  172. }
  173. // 删除
  174. const deleteFile = dirName => {
  175. autoFile.delete(autoFile.getPath(dirName))
  176. autoFile.write(d)
  177. }
  178. // 监听创建文件夹
  179. watcher.on('addDir', dirName => addFile(dirName, 'dir'))
  180. // 监听移除文件夹
  181. watcher.on('unlinkDir', dirName => deleteFile(dirName))
  182. // 是否需要监听文件
  183. if (d.useFile) {
  184. // 防止自己引用自己
  185. const sameFile = (p1, p2) => {
  186. return (path.resolve(p1) === path.resolve(p2))
  187. }
  188. // 监听创建文件夹
  189. watcher.on('add', dirName => {
  190. if (!sameFile(d.outPath, dirName)) addFile(dirName, 'file')
  191. })
  192. // 监听移除文件夹
  193. watcher.on('unlink', dirName => {
  194. if (!sameFile(d.outPath, dirName)) deleteFile(dirName)
  195. })
  196. }
  197. // 监听错误
  198. watcher.on('error', function(error) {
  199. console.error('webpack AutoCodePlugin Error happened: ', error)
  200. })
  201. })(d)
  202. })
  203. }
  204. // 应用函数
  205. apply(compiler) {
  206. // 判断环境
  207. let product = (process.env.NODE_ENV === 'production')
  208. // 可配置生产 || 测试环境 AutoCodePlugin.NODE_ENV = 'pro';
  209. if (this.NODE_ENV) product = (process.env.NODE_ENV === this.NODE_ENV)
  210. // 构建完成结束监听
  211. if (compiler.hooks.done) {
  212. // if (product && compiler.hooks.done) {
  213. // webpack4
  214. compiler.hooks.done.tap('done', () => {
  215. console.log('webpack done')
  216. this.watcher.forEach(d => d.close())
  217. })
  218. } else if (product) {
  219. // < webpack4
  220. compiler.plugin('done', () => {
  221. console.log('webpack done')
  222. this.watcher.forEach(d => d.close())
  223. })
  224. }
  225. }
  226. }
  227. module.exports = AutoCodePlugin