GitLab Viewer Publish and Deploy Project

GitLab Viewer Publish and Deploy Project!

目前為 2023-05-23 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitLab Viewer Publish and Deploy Project
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.02
  5. // @description GitLab Viewer Publish and Deploy Project!
  6. // @author Sean
  7. // @match http://192.168.0.200/fe3project/*
  8. // @icon http://192.168.0.200/assets/favicon-7901bd695fb93edb07975966062049829afb56cf11511236e61bcf425070e36e.png
  9. // @require https://cdn.bootcdn.net/ajax/libs/vue/2.7.14/vue.min.js
  10. // @require https://unpkg.com/element-ui/lib/index.js
  11. // @require https://cdn.bootcdn.net/ajax/libs/jszip/3.7.1/jszip.min.js
  12. // @resource myFontFile https://element.eleme.io/2.11/static/element-icons.535877f.woff
  13. // @grant GM_getResourceURL
  14. // @grant GM_addStyle
  15. // @grant GM_getResourceText
  16. // @resource ElementCSS https://unpkg.com/element-ui/lib/theme-chalk/index.css
  17. // @grant GM_xmlhttpRequest
  18. // @license MIT
  19. // @run-at document-end
  20. // ==/UserScript==
  21.  
  22. (function() {
  23. 'use strict';
  24.  
  25. const fontUrl = 'https://element.eleme.io/2.11/static/element-icons.535877f.woff';
  26.  
  27. // 添加样式规则,将字体应用到指定元素上
  28. GM_addStyle(`
  29. @font-face {
  30. font-family: element-icons;
  31. src: url(${fontUrl}) format("woff");
  32. }
  33. `);
  34.  
  35. GM_addStyle(GM_getResourceText('ElementCSS'));
  36.  
  37. let epointCss = ".epoint-tool {position: fixed; bottom: 0%; right:10px; transform: translateY(-50%);}";
  38. epointCss += ".el-row { padding: 3px 0;} .el-dialog__body .el-tree{min-height: 420px; max-height: 500px;overflow: auto;}";
  39. epointCss += ".deploy-body {height: 306px;}"
  40. epointCss += ".deploy-body .el-loading-spinner {margin-top: -50px;}";
  41. // 添加注入样式
  42. let extraStyleElement = document.createElement("style");
  43. extraStyleElement.innerHTML = epointCss;
  44. document.head.appendChild(extraStyleElement);
  45.  
  46. const MyComponent = {
  47. template: `<div class="epoint-wrap">
  48. <div class="epoint-tool">
  49. <el-row><el-button type="primary" icon="el-icon-search" round @click="viewProject">查看</el-button></el-row>
  50. <el-row>
  51. <el-tooltip content="一键部署到170服务器" placement="top" effect="light">
  52. <el-button type="primary" icon="el-icon-s-unfold" round @click="doDeploy">部署</el-button>
  53. </el-tooltip>
  54. </el-row>
  55. <el-row>
  56. <el-tooltip content="一键发布到项目案例库" placement="top" effect="light">
  57. <el-button type="primary" icon="el-icon-upload" round @click="publish">发布</el-button>
  58. </el-tooltip>
  59. </el-row>
  60. </div>
  61. <el-dialog
  62. title="目录结构"
  63. width="900px"
  64. :append-to-body="true"
  65. :visible.sync="dialogVisible"
  66. :before-close="handleClose">
  67. <el-tree
  68. class="filter-tree"
  69. :data="data"
  70. :props="defaultProps"
  71. node-key="id"
  72. default-expand-all
  73. @node-click="handleNodeClick"
  74. v-loading="loadingTree"
  75. ref="tree">
  76. </el-tree>
  77. </el-dialog>
  78. <el-dialog
  79. title="部署"
  80. width="420px"
  81. :visible.sync="depDialogVisible">
  82. <div class="deploy-body"
  83. v-loading="loading"
  84. element-loading-text="正在打包部署至 170 服务器,请耐心等待"
  85. element-loading-spinner="el-icon-loading">
  86. </div>
  87. </el-dialog>
  88. </div>`,
  89. data() {
  90. return {
  91. dialogVisible: false, // 查看目录结构弹窗
  92. data: [], // 目录结构树的数据结构
  93. defaultProps: {
  94. children: 'children',
  95. label: 'label'
  96. },
  97. loadingTree: false,
  98. depDialogVisible: false, // 部署中弹窗
  99. loading: false,
  100. projectLibUrl: 'http://192.168.201.159:9999/webapp/pages/default/onlinecase.html', // 项目案例库地址
  101. projectIsDeployed: false, // 项目是否部署过
  102. projectFtpUrl: '', // ftp路径
  103. projectEntryUrl: 'http://192.168.219.170/showcase/', // 项目案例库发布表单的入口页面
  104. };
  105. },
  106. methods: {
  107. handleClose(done) {
  108. done();
  109. /*
  110. this.$confirm('确认关闭?')
  111. .then(_ => {
  112. done();
  113. })
  114. .catch(_ => {});*/
  115. },
  116. handleNodeClick(data) {
  117. console.log(data);
  118. if(data.type === 'folder') {
  119. return false;
  120. }
  121. var self = this;
  122. var entry = data.entry;
  123. if(self.projectFtpUrl) {
  124. window.open('http://192.168.219.170' + self.projectFtpUrl + data.entry)
  125. } else {
  126. this.$confirm('资源未部署,部署至 170 服务器后可查看,是否部署?')
  127. .then(_ => {
  128. this.depDialogVisible = true;
  129. this.loading = true;
  130. // 部署
  131. this.getDeployInfo({ type: '1' }, (data)=> {
  132. this.loading = false;
  133. this.depDialogVisible = false;
  134.  
  135. this.data = data.custom.detail;
  136. this.projectFtpUrl = data.custom.ftpUrl;
  137.  
  138. this.$alert('部署成功!', '提示', {
  139. confirmButtonText: '确定',
  140. callback: action => {
  141. window.open('http://192.168.219.170' + self.projectFtpUrl + entry)
  142. }
  143. });
  144.  
  145. });
  146. })
  147. .catch(_ => {});
  148. }
  149. },
  150. // 部署
  151. doDeploy () {
  152. this.$confirm('此操作将把 GitLab 资源打包部署至 170 服务器,已部署过的项目会进行覆盖部署,是否继续?', '提示', {
  153. confirmButtonText: '确定',
  154. cancelButtonText: '取消',
  155. type: 'warning'
  156. }).then(() => {
  157. this.depDialogVisible = true;
  158. this.loading = true;
  159. // 部署
  160. this.getDeployInfo({ type: '1' }, (data)=> {
  161. this.loading = false;
  162. this.depDialogVisible = false;
  163.  
  164. this.$message({
  165. type: 'success',
  166. message: '部署成功!'
  167. });
  168.  
  169. this.data = data.custom.detail;
  170. this.projectFtpUrl = data.custom.ftpUrl;
  171. // 打开查看弹窗
  172. this.viewProject();
  173. });
  174. }).catch(() => {
  175. this.$message({
  176. type: 'info',
  177. message: '已取消部署'
  178. });
  179. });
  180. },
  181. // 发布项目案例库
  182. publish () {
  183. this.$confirm('此操作将把项目发布至 <a href="'+ this.projectLibUrl +'" target="_blank">项目案例库</a>,已发布过的项目案例库会有重复项,是否继续?', '提示', {
  184. confirmButtonText: '确定',
  185. cancelButtonText: '取消',
  186. dangerouslyUseHTMLString: true,
  187. type: 'warning'
  188. }).then(() => {
  189. const themes = document.querySelectorAll('.badge-secondary');
  190. let keys = [];
  191.  
  192. for(let i = 0, l = themes.length; i < l; i++) {
  193. if(themes[0].innerText == '智能设备' && i > 0) {
  194. keys.push(themes[i].innerText);
  195. } else if (themes[0].innerText !== '智能设备' && i > 1) {
  196. keys.push(themes[i].innerText);
  197. }
  198. }
  199. // 存在更多主题的情况
  200. const moreKeyEl = document.querySelector('.gl-w-full .text-nowrap');
  201. if(moreKeyEl) {
  202. const moreKeyElContent = moreKeyEl.getAttribute('data-content');
  203. const regex = />([^<]+)</g;
  204. const matches = moreKeyElContent.match(regex);
  205. const results = matches.filter(function(match) {
  206. return match.length > 3;
  207. });
  208. const moreKeyData = results.map(function(match) {
  209. return match.substring(2, match.length - 2);
  210. });
  211.  
  212. if(moreKeyData && moreKeyData.length) {
  213. keys = keys.concat(moreKeyData);
  214. }
  215. }
  216.  
  217. // 组织项目案例库所需参数
  218. const projectName = document.querySelector('.home-panel-title').innerText;
  219. const projectBU = themes[0] ? themes[0].innerText : null;
  220. const projectKeys = keys.join(' ');
  221. const entryUrl = this.projectEntryUrl;
  222. let projectType = themes[1] ? themes[1].innerText : null;
  223.  
  224. if(themes[0].innerText == '智能设备') {
  225. projectType = themes[1] ? themes[0].innerText : null;
  226. } else {
  227. projectType = themes[1] ? themes[1].innerText : null;
  228. }
  229.  
  230. const destUrl = this.projectLibUrl + '?projectName=' + projectName + '&projectBU=' + projectBU + '&projectType=' + projectType + '&projectKeys=' + projectKeys + '&entryUrl=' + entryUrl + '&git=' + window.location.href;
  231.  
  232. this.$message({
  233. type: 'success',
  234. message: destUrl
  235. });
  236.  
  237. window.open(destUrl);
  238.  
  239. }).catch(() => {
  240. this.$message({
  241. type: 'info',
  242. message: '已取消发布'
  243. });
  244. });
  245. },
  246. // 查看项目
  247. viewProject () {
  248. this.dialogVisible = true;
  249. this.loadingTree = true;
  250. let self = this;
  251.  
  252. // 发送ajax请求,查看是否进行过部署
  253. this.getDeployInfo((data)=> {
  254. // 有部署信息,直接赋值,
  255. if(true) {
  256. self.projectIsDeployed = true;
  257. self.data = data.custom.detail;
  258. self.projectFtpUrl = data.custom.ftpUrl;
  259. self.loadingTree = false;
  260.  
  261.  
  262. } else {
  263. // 无部署信息,仅查看文件目录
  264. getZipResource((data)=> {
  265. self.data = data;
  266. self.loadingTree = false;
  267. });
  268. }
  269. });
  270.  
  271. },
  272. // 项目部署信息
  273. getDeployInfo(params, callback) {
  274. const projectId = document.body.getAttribute('data-project-id');
  275. const downloadUrl = window.location.origin + document.querySelector('.gl-button.btn-sm.btn-confirm').getAttribute('href');
  276. const name = document.querySelectorAll('.breadcrumb-item')[0].innerText + '-main';
  277. const author = document.querySelector('.current-user .gl-font-weight-bold').innerText.trim();
  278.  
  279. if(typeof params == 'function') {
  280. callback = params;
  281. params = null;
  282. }
  283.  
  284. if(projectId && projectId.length && downloadUrl) {
  285. fetch('http://192.168.118.49:3000/api/getDeployInfo', {
  286. method: 'POST',
  287. // 允许跨域请求
  288. mode: 'cors',
  289. headers: {
  290. 'Accept': 'application/json',
  291. 'Content-Type': 'application/json'
  292. },
  293. body: JSON.stringify({//post请求参数
  294. projectId: projectId,
  295. downloadUrl: downloadUrl,
  296. type: (params && params.type !== undefined) ? params.type : '0',// 0 代表查看, 1代表部署
  297. name: name,
  298. author: author, // 增加上传人,用来生成根目录下的readme.html
  299. })
  300. })
  301. .then(response => response.text())
  302. .then((result) => {
  303. var data = JSON.parse(result);
  304. callback && callback(data);
  305. })
  306. .catch(error => console.error(error));
  307. } else {
  308. console.error('部署信息请求参数error');
  309. }
  310. },
  311. // 设置入口
  312. setProjectEntry(){
  313. const firstNode = findFirstFileNode(this.data);
  314. if(firstNode) {
  315. this.projectEntryUrl = 'http://192.168.219.170' + this.projectFtpUrl + firstNode.entry;
  316. }
  317. }
  318. },
  319. mounted() {
  320.  
  321. }
  322. };
  323.  
  324. const placeholder = document.createElement('div');
  325.  
  326. // 创建 Vue 实例并挂载到页面
  327. const vueInstance = new Vue({
  328. el: placeholder,
  329. components: {
  330. MyComponent
  331. },
  332. methods: {
  333. },
  334. template: `<my-component></my-component>`
  335. });
  336.  
  337. // 等待页面加载完成
  338. window.addEventListener('load', function() {
  339. // 将占位元素追加到 body 元素中
  340. document.body.appendChild(vueInstance.$el);
  341. });
  342.  
  343. // 将文件条目组织成嵌套结构
  344. function organizeFileEntries(fileEntries) {
  345. const root = {
  346. label: document.querySelector('.home-panel-title').innerText || document.getElementById('project_name_edit').value,
  347. type: 'folder',
  348. children: []
  349. };
  350.  
  351. // 创建嵌套结构
  352. fileEntries.forEach(entry => {
  353. const pathSegments = entry.name.split('/');
  354. let currentFolder = root;
  355.  
  356. // 遍历路径中的每个部分,创建相应的文件夹节点
  357. for (let i = 0; i < pathSegments.length - 1; i++) {
  358. const folderName = pathSegments[i];
  359. let folder = currentFolder.children.find(child => child.label === folderName);
  360.  
  361. if(isExcludeFolder(entry.name)) {
  362. continue;
  363. }
  364.  
  365. if (!folder) {
  366. folder = {
  367. label: folderName,
  368. type: 'folder',
  369. children: []
  370. };
  371. currentFolder.children.push(folder);
  372. }
  373.  
  374. currentFolder = folder;
  375. }
  376.  
  377. // 创建文件节点并添加到相应的文件夹中
  378. const fileName = pathSegments[pathSegments.length - 1];
  379.  
  380. if(fileName && fileName.length && isIncludeFile(fileName) && !isExcludeFolder(entry.name)) {
  381. const fileNode = {
  382. label: fileName,
  383. type: 'file',
  384. entry: entry.name
  385. };
  386. currentFolder.children.push(fileNode);
  387. }
  388. });
  389.  
  390. return [root];
  391. }
  392. // 是否排除的文件夹
  393. function isExcludeFolder(entry) {
  394. if(entry.indexOf('css') > -1 ||
  395. entry.indexOf('scss') > -1 ||
  396. entry.indexOf('js') > -1 ||
  397. entry.indexOf('images') > -1 ||
  398. entry.indexOf('fui') > -1 ||
  399. entry.indexOf('lib') > -1 ||
  400. entry.indexOf('test') > -1 ||
  401. entry.indexOf('font') > -1 ||
  402. entry.indexOf('frame/fui') > -1) {
  403. return true;
  404. } else {
  405. return false;
  406. }
  407. }
  408. // 是否包含的文件
  409. function isIncludeFile(fileName) {
  410. if(fileName.indexOf('.html') > -1) {
  411. return true;
  412. } else {
  413. return false;
  414. }
  415. }
  416.  
  417. function getZipResource(callback) {
  418. const downloadUrl = window.location.origin + document.querySelector('.gl-button.btn-sm.btn-confirm').getAttribute('href');
  419.  
  420. fetch(downloadUrl)
  421. .then(response => response.arrayBuffer())
  422. .then(data => {
  423. // 将 ZIP 文件的二进制数据传递给 JSZip 进行解析
  424. return JSZip.loadAsync(data);
  425. })
  426. .then(zip => {
  427. // 获取 ZIP 文件中的所有条目(文件和目录)
  428. const zipEntries = Object.values(zip.files);
  429. const treeData = organizeFileEntries(zipEntries)
  430.  
  431. callback && callback(treeData);
  432.  
  433. })
  434. .catch(error => {
  435. console.error(error);
  436. });
  437. }
  438.  
  439. // 树结构第一个节点数据
  440. function findFirstFileNode(tree) {
  441. // 遍历树的节点
  442. for (let i = 0; i < tree.length; i++) {
  443. const node = tree[i];
  444.  
  445. // 如果节点的类型为 file,则返回该节点
  446. if (node.type === 'file') {
  447. return node;
  448. }
  449.  
  450. // 如果节点有子节点,则递归调用该函数查找子节点中的第一个 file 节点
  451. if (node.children && node.children.length > 0) {
  452. const fileNode = findFirstFileNode(node.children);
  453. if (fileNode) {
  454. return fileNode;
  455. }
  456. }
  457. }
  458.  
  459. // 如果没有找到 file 节点,则返回 null
  460. return null;
  461. }
  462. })();