index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. "use strict";
  2. window.packageRoot = "packages://sync-code/";
  3. var Fs = require('fs');
  4. var Path = require('path');
  5. // 递归创建目录
  6. function mkdirsSync(dirname) {
  7. if (Fs.existsSync(dirname)) {
  8. return true;
  9. } else {
  10. if (mkdirsSync(Path.dirname(dirname))) {
  11. Fs.mkdirSync(dirname);
  12. return true;
  13. }
  14. }
  15. }
  16. //拷贝文件到指定目录,并重建目录结构
  17. function copyFile(srcPath, destPath) {
  18. mkdirsSync(Path.dirname(destPath));
  19. Fs.writeFileSync(destPath, Fs.readFileSync(srcPath));
  20. }
  21. Editor.Panel.extend({
  22. style: `
  23. h2 {
  24. color: #f90;
  25. font-size: 15px;
  26. width: 200px;
  27. text-align: left;
  28. }
  29. h3 {
  30. }
  31. .checkDiv{
  32. margin-top: 15px;
  33. margin-left: 15px;
  34. width: 90%;
  35. word-break: break-all;
  36. }
  37. .okstyle {
  38. margin-top: 30px;
  39. }
  40. `,
  41. template: `
  42. <div class = "checkDiv">
  43. <h2>当前同步项目</h2>
  44. <h3 id="projectsText">default<h3>
  45. </div>
  46. <div class="okstyle">
  47. <ui-input id="inputProjectIdx"></ui-input>
  48. <ui-button v-on:confirm="onAdd">添加</ui-button>
  49. <ui-button v-on:confirm="onDelete">删除</ui-button>
  50. <ui-button v-on:confirm="onSync">一键同步</ui-button>
  51. <div>
  52. <div style="width: 100%; height: 150px;margin: 0 0 0 0;">
  53. <h2>日志:</h2>
  54. <textarea class="flex-1" id="logTextArea" v-model="logView" style="width: 100%; height: 200px; background: #252525; resize: none; color: #fd942b; border-color: #fd942b;" disabled></textarea>
  55. <ui-button v-on:confirm="onClearLog">清空日志</ui-button>
  56. </div>`,
  57. $: {
  58. logTextArea: "#logTextArea",
  59. inputProjectIdx: "#inputProjectIdx",
  60. projectsText: "#projectsText",
  61. },
  62. ready() {
  63. let logarea = this.$logTextArea;
  64. let projectsText = this.$projectsText;
  65. let inputProjectIdx = this.$inputProjectIdx;
  66. window.plugin = new window.Vue({
  67. el: this.shadowRoot,
  68. created: function () {
  69. this.initPluginCfg();
  70. this.readCfg();
  71. this.updateProjects();
  72. },
  73. init: function () {
  74. },
  75. data: {
  76. logView: "",
  77. hasSyncProj: false,
  78. AllFileVec: [],
  79. hasEdit: 0,
  80. cfgPath: "",
  81. projectArr:[],//项目数组
  82. },
  83. computed: {},
  84. methods: {
  85. initPluginCfg() {
  86. let e = Editor.libraryPath;
  87. this.rootPath = e.substring(0, e.length - 7);
  88. this.cfgPath = this.rootPath + "/packages/sync-code/cfg.json";
  89. },
  90. addLog(t) {
  91. let i = new Date;
  92. this.logView += "[" + i.toLocaleString() + "]: " + t + "\n",
  93. setTimeout(function () {
  94. logarea.scrollTop = logarea.scrollHeight;
  95. }, 10)
  96. },
  97. onClearLog(){
  98. this.logView = "";
  99. },
  100. saveCfg() {
  101. let newData = "";
  102. for(let i = 0;i<this.projectArr.length;i++){
  103. newData += this.projectArr[i];
  104. if(i != this.projectArr.length-1){
  105. newData += ",";
  106. }
  107. }
  108. Fs.writeFile(this.cfgPath, newData, function (error) {
  109. });
  110. this.addLog("配置已保存!");
  111. },
  112. readCfg() {
  113. this.projectArr = [];
  114. if (Fs.existsSync(this.cfgPath)) {
  115. let st = Fs.readFileSync(this.cfgPath, "UTF-8");
  116. if(st != ""){
  117. let cfgData = st.split(",");
  118. for(let i = 0;i<cfgData.length;i++){
  119. let projecIdx = parseInt(cfgData[i]);
  120. this.projectArr.push(projecIdx);
  121. }
  122. this.addLog("读取配置成功!");
  123. }else{
  124. this.addLog("配置中未添加任何项目!");
  125. }
  126. } else {
  127. this.addLog("未检测到历史配置!");
  128. }
  129. },
  130. updateProjects(){
  131. if(this.projectArr.length > 1){
  132. this.projectArr.sort(function(a, b){return a - b});
  133. }
  134. let newData = "";
  135. for(let i = 0;i<this.projectArr.length;i++){
  136. newData += this.projectArr[i];
  137. newData += "\t";
  138. }
  139. projectsText.innerHTML = newData;
  140. if(projectsText.innerHTML == ""){
  141. projectsText.innerHTML = "未添加任何项目";
  142. }
  143. this.saveCfg();
  144. },
  145. onAdd(){
  146. let projecIdx = parseInt(inputProjectIdx.value);
  147. if(!Number.isInteger(projecIdx)){
  148. this.addLog("请正确输入项目索引(正整数)");
  149. return;
  150. }
  151. let isFind = false;
  152. for(let i = 0;i<this.projectArr.length;i++){
  153. if(this.projectArr[i] == projecIdx){
  154. isFind = true;
  155. break;
  156. }
  157. }
  158. if(!isFind){
  159. this.projectArr.push(projecIdx);
  160. this.addLog("项目添加成功:"+projecIdx);
  161. }else{
  162. this.addLog("项目已在同步列表中,无需重复添加!");
  163. }
  164. this.updateProjects();
  165. },
  166. onDelete(){
  167. let projecIdx = parseInt(inputProjectIdx.value);
  168. if(!Number.isInteger(projecIdx)){
  169. this.addLog("请正确输入项目索引(正整数)");
  170. return;
  171. }
  172. let isFind = false;
  173. let arr = [];
  174. for(let i = 0;i<this.projectArr.length;i++){
  175. if(this.projectArr[i] == projecIdx){
  176. isFind = true;
  177. continue;
  178. }
  179. arr.push(this.projectArr[i]);
  180. }
  181. this.projectArr = arr;
  182. if(isFind){
  183. this.addLog("成功移除项目:"+projecIdx);
  184. }else{
  185. this.addLog("同步列表未找到项目:"+projecIdx+",无需移除!");
  186. }
  187. this.updateProjects();
  188. },
  189. onSync() {
  190. this.onClearLog();
  191. if (this.hasSyncProj) {
  192. this.addLog("请不要重复点击");
  193. return;
  194. };
  195. if (this.projectArr.length <= 0) {
  196. this.addLog("未添加任何同步项目!");
  197. return;
  198. }
  199. this.hasSyncProj = true;
  200. this.AllFileVec = [];
  201. this.addLog("开始同步..."+this.rootPath + "assets,请耐心等待");
  202. this.readPath(this.rootPath + "assets");
  203. this.addLog("共检测到" + this.AllFileVec.length + "个需要修改的文件");
  204. this.beginSync();
  205. },
  206. beginSync(){
  207. for(let idx = 0;idx<this.projectArr.length;idx++){
  208. let nowProjectIdx = this.projectArr[idx];
  209. let rootIdx = this.rootPath.indexOf("client_");
  210. let projectsRootPath = this.rootPath.substring(0,rootIdx);
  211. //this.addLog("项目跟路径:"+projectsRootPath);
  212. let nowProjectPath = projectsRootPath + "client_" + nowProjectIdx + "/";
  213. this.addLog("开始同步项目:client_"+ nowProjectIdx + ","+nowProjectPath);
  214. let len = this.rootPath.length;
  215. for (var i = 0; i < this.AllFileVec.length; i++) {
  216. let scrPath = this.AllFileVec[i];
  217. let childPath = scrPath.substring(len);
  218. let destPath = nowProjectPath+childPath;
  219. //this.addLog("源路径:"+scrPath);
  220. //this.addLog("目标路径:"+destPath);
  221. copyFile(scrPath, destPath);
  222. };
  223. this.addLog("client_"+ nowProjectIdx + ",同步完成!");
  224. }
  225. this.hasSyncProj = false;
  226. },
  227. readPath(path) {
  228. var isFiltrationFile = function (pathStr) {//需要过滤的文件
  229. let extname = Path.extname(pathStr);//后缀名
  230. let basename = Path.basename(pathStr);//文件名
  231. if (
  232. //extname == ".meta" ||
  233. //basename == "AppStart.js" ||
  234. //basename == "BaseDefine.js" ||
  235. //basename == "headUrlList.json" ||
  236. basename == "ReviewResources.meta" ||
  237. //basename == "gamemanifest.meta" ||
  238. basename == "gamemanifest_test.meta"
  239. ) {
  240. return true;
  241. }
  242. return false;
  243. }.bind(this);
  244. var isFiltrationFolder = function (extname) {
  245. if (extname == "ReviewResources" /*|| extname == "gamemanifest"*/ || extname == "gamemanifest_test") {
  246. return true;
  247. } else {
  248. return false;
  249. }
  250. }.bind(this);
  251. var dirList = Fs.readdirSync(path);
  252. dirList.forEach(function (item) {
  253. if (Fs.statSync(path + '/' + item).isDirectory()) {
  254. if (isFiltrationFolder(item)) {//需要过滤的文件夹
  255. //this.addLog("已过滤文件夹:" + item);
  256. } else {
  257. this.readPath(path + '/' + item);
  258. }
  259. } else {
  260. if (isFiltrationFile(path + '/' + item)) {//需要过滤的文件
  261. //this.addLog("已过滤文件:" + item);
  262. } else {
  263. this.AllFileVec.push(path + '/' + item);
  264. }
  265. }
  266. }.bind(this));
  267. },
  268. }
  269. })
  270. },
  271. messages: {
  272. "encrypt-tool:onBuildFinished": function (e, t) {
  273. }
  274. },
  275. });