index.cjs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * @Description:
  3. * @Version: 1.668
  4. * @Autor: 地虎降天龙
  5. * @Date: 2024-01-08 09:01:48
  6. * @LastEditors: 地虎降天龙
  7. * @LastEditTime: 2025-10-10 14:32:37
  8. */
  9. const compressing = require('compressing')
  10. const fs = require('fs')
  11. var path = require('path')
  12. const readline = require('readline')
  13. // 示例代码
  14. const args = process.argv // 包含了所有的命令行参数,第一个元素为 node 路径,第二个元素为脚本文件名
  15. if (!args[2]) {
  16. console.error('【类型:creat、package、install】 参数错误',args)
  17. return
  18. // process.exit(1)
  19. }
  20. function fileExistsWithCaseSync(filepath) {
  21. var dir = path.dirname(filepath)
  22. if (dir === path.dirname(dir)) {
  23. return true
  24. }
  25. var filenames = fs.readdirSync(dir)
  26. if (filenames.indexOf(path.basename(filepath)) === -1) {
  27. return false
  28. }
  29. return fileExistsWithCaseSync(dir)
  30. }
  31. const packagePlugins = (pluginName) => {
  32. if (!pluginName) {
  33. console.error('打包插件_:【插件名错误】 ')
  34. return
  35. }
  36. const pluginPath = './src/plugins/' + pluginName
  37. // 检查是否存在此插件
  38. if (!fileExistsWithCaseSync(pluginPath)) {
  39. console.error(`打包插件_:不存在名为:【 ${pluginName} 】的插件。地址:${pluginPath}`)
  40. return
  41. }
  42. const packagePath = `./pluginMaker/packages/${pluginName}.zip`
  43. // compressing.zip.compressDir(pluginPath, packagePath, { ignoreBase: false })
  44. // .then(() => {
  45. // console.log(`打包插件_:【 ${pluginName} 】 成功。 地址:${packagePath}`)
  46. // })
  47. // .catch(err => {
  48. // console.error(err)
  49. // })
  50. const zipStream = new compressing.zip.Stream()
  51. zipStream.addEntry(pluginPath, { relativePath: pluginPath, ignoreBase: true })
  52. // zipStream.addEntry(`./public/plugins/${pluginName}`, { relativePath: `./public/plugins/${pluginName}`, ignoreBase: true })
  53. // ✅ 仅当 public/plugins 存在时才添加
  54. const publicPluginPath = `./public/plugins/${pluginName}`
  55. if (fs.existsSync(publicPluginPath)) {
  56. zipStream.addEntry(publicPluginPath, {
  57. relativePath: publicPluginPath,
  58. ignoreBase: true
  59. })
  60. } else {
  61. console.warn(`⚠️ 打包插件_: 未找到 public 目录:${publicPluginPath},已跳过。`)
  62. }
  63. const destStream = fs.createWriteStream(packagePath)
  64. zipStream
  65. .pipe(destStream)
  66. .on('finish', () => {
  67. console.log(`打包插件_:【 ${pluginName} 】 成功。地址:${packagePath}`)
  68. })
  69. .on('error', (err) => {
  70. console.error(err)
  71. })
  72. }
  73. const installPlugins = (pluginName) => {
  74. if (!pluginName) {
  75. console.error('安装插件_:【插件名错误】 ')
  76. return
  77. }
  78. // 检查是否存在此插件包
  79. const packagePath = `./pluginMaker/install/${pluginName}.zip`
  80. if (!fileExistsWithCaseSync(packagePath)) {
  81. console.error(`安装插件_:不存在名为:【 ${pluginName} 】的插件包。地址:${packagePath}`)
  82. return
  83. }
  84. const pluginPath = './src/plugins/' + pluginName
  85. // 检查是否存在此插件目录
  86. if (fileExistsWithCaseSync(pluginPath)) {
  87. console.error(`安装插件_:已经存在名为:【 ${pluginName} 】的插件。目录地址为:${pluginPath}。 如若需要安装请备份和删除该插件`)
  88. return
  89. }
  90. // 解压插件 到 目录
  91. compressing.zip
  92. .uncompress(packagePath, './')
  93. .then(() => {
  94. console.log(`安装插件_:【 ${pluginName} 】 成功。地址:${pluginPath}。安装文件包请自行删除。`)
  95. })
  96. .catch((err) => {
  97. console.error(err)
  98. })
  99. }
  100. const createPlugins = (pluginName) => {
  101. if (!pluginName) {
  102. console.error('新建插件_:【插件名错误】 ')
  103. return
  104. }
  105. const pluginPath = './src/plugins/' + pluginName
  106. // 检查是否存在此插件目录
  107. if (fileExistsWithCaseSync(pluginPath)) {
  108. console.error(`新建插件_:已经存在名为:【 ${pluginName} 】的插件。目录地址为:${pluginPath}。 请取其他的名字`)
  109. return
  110. }
  111. // 创建目录
  112. const publicPath = `./public/plugins/${pluginName}/preview`
  113. fs.mkdir(publicPath, { recursive: true }, (err) => {
  114. if (err) throw err
  115. console.log(`新建插件_:静态资源目录已创建:【 ${publicPath} 】`)
  116. })
  117. // 解压对应的模版代码到对应的目录
  118. compressing.zip
  119. .uncompress('./pluginMaker/template.zip', pluginPath)
  120. .then(() => {
  121. // 要替换的字符及其替换后的内容
  122. const configFile = pluginPath + '/config.js'
  123. // 获得当前日期
  124. const currentDate = new Date()
  125. const year = currentDate.getFullYear() // 获取年份
  126. const month = String(currentDate.getMonth() + 1).padStart(2, '0') // 获取月份(注意月份是从 0 开始的,需要加 1)
  127. const day = String(currentDate.getDate()).padStart(2, '0') // 获取日期
  128. const formattedDate = `${year}-${month}-${day}`
  129. const replaceCharacters = {
  130. template: pluginName,
  131. '2024-01-01': formattedDate,
  132. }
  133. // 读取原始文件内容
  134. fs.readFile(configFile, 'utf8', (err, data) => {
  135. if (err) throw err
  136. // 遍历需要替换的字符列表
  137. for (let oldChar in replaceCharacters) {
  138. const newChar = replaceCharacters[oldChar]
  139. // 在数据中查找并替换指定字符
  140. data = data.replace(new RegExp(oldChar, 'g'), newChar)
  141. }
  142. // 将修改后的内容写入新文件
  143. fs.writeFile(configFile, data, 'utf8', (err) => {
  144. if (err) throw err
  145. console.log(`新建插件_:【 ${configFile} 】config修改完毕`)
  146. })
  147. })
  148. console.log(`新建插件_:【 ${pluginName} 】 成功。地址:${pluginPath}。请自行更改config.js`)
  149. })
  150. .catch((err) => {
  151. console.error(err)
  152. })
  153. }
  154. const removePlugins = (pluginName) => {
  155. if (!pluginName) {
  156. console.error('删除插件_:【插件名错误】 ')
  157. return
  158. }
  159. const pluginPath = './src/plugins/' + pluginName
  160. const publicPath = './public/plugins/' + pluginName
  161. if (!fileExistsWithCaseSync(pluginPath) && !fileExistsWithCaseSync(publicPath)) {
  162. console.error(`删除插件_:未找到名为:【 ${pluginName} 】的插件目录`)
  163. return
  164. }
  165. const rl = readline.createInterface({
  166. input: process.stdin,
  167. output: process.stdout,
  168. })
  169. rl.question(`⚠️ 删除插件_:您确认要删除插件 【${pluginName}】 吗?(代码请确保已妥善保存)输入 yes 确认:`, (answer) => {
  170. if (answer.trim().toLowerCase() === 'yes') {
  171. if (fs.existsSync(pluginPath)) {
  172. fs.rmSync(pluginPath, { recursive: true, force: true })
  173. console.log(`✅ 插件代码目录已删除: ${pluginPath}`)
  174. }
  175. if (fs.existsSync(publicPath)) {
  176. fs.rmSync(publicPath, { recursive: true, force: true })
  177. console.log(`✅ 插件资源目录已删除: ${publicPath}`)
  178. }
  179. } else {
  180. console.log('取消删除操作')
  181. }
  182. rl.close()
  183. })
  184. }
  185. let type = args[2]
  186. if (type === 'create') {
  187. createPlugins(args[3])
  188. } else if (type === 'package') {
  189. packagePlugins(args[3])
  190. } else if (type === 'install') {
  191. installPlugins(args[3])
  192. } else if (type === 'remove') {
  193. removePlugins(args[3])
  194. }