123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- "use strict";
- window.packageRoot = "packages://sync-code/";
- var Fs = require('fs');
- var Path = require('path');
- // 递归创建目录
- function mkdirsSync(dirname) {
- if (Fs.existsSync(dirname)) {
- return true;
- } else {
- if (mkdirsSync(Path.dirname(dirname))) {
- Fs.mkdirSync(dirname);
- return true;
- }
- }
- }
- //拷贝文件到指定目录,并重建目录结构
- function copyFile(srcPath, destPath) {
- mkdirsSync(Path.dirname(destPath));
- Fs.writeFileSync(destPath, Fs.readFileSync(srcPath));
- }
- Editor.Panel.extend({
- style: `
- h2 {
- color: #f90;
- font-size: 15px;
- width: 200px;
- text-align: left;
- }
- h3 {
-
- }
- .checkDiv{
- margin-top: 15px;
- margin-left: 15px;
- width: 90%;
- word-break: break-all;
- }
- .okstyle {
- margin-top: 30px;
- }
- `,
-
- template: `
- <div class = "checkDiv">
- <h2>当前同步项目</h2>
- <h3 id="projectsText">default<h3>
- </div>
- <div class="okstyle">
- <ui-input id="inputProjectIdx"></ui-input>
- <ui-button v-on:confirm="onAdd">添加</ui-button>
- <ui-button v-on:confirm="onDelete">删除</ui-button>
- <ui-button v-on:confirm="onSync">一键同步</ui-button>
- <div>
-
- <div style="width: 100%; height: 150px;margin: 0 0 0 0;">
- <h2>日志:</h2>
- <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>
- <ui-button v-on:confirm="onClearLog">清空日志</ui-button>
- </div>`,
- $: {
- logTextArea: "#logTextArea",
- inputProjectIdx: "#inputProjectIdx",
- projectsText: "#projectsText",
- },
- ready() {
- let logarea = this.$logTextArea;
- let projectsText = this.$projectsText;
- let inputProjectIdx = this.$inputProjectIdx;
- window.plugin = new window.Vue({
- el: this.shadowRoot,
- created: function () {
- this.initPluginCfg();
- this.readCfg();
- this.updateProjects();
- },
- init: function () {
- },
- data: {
- logView: "",
- hasSyncProj: false,
- AllFileVec: [],
- hasEdit: 0,
- cfgPath: "",
- projectArr:[],//项目数组
- },
- computed: {},
- methods: {
- initPluginCfg() {
- let e = Editor.libraryPath;
- this.rootPath = e.substring(0, e.length - 7);
- this.cfgPath = this.rootPath + "/packages/sync-code/cfg.json";
- },
- addLog(t) {
- let i = new Date;
- this.logView += "[" + i.toLocaleString() + "]: " + t + "\n",
- setTimeout(function () {
- logarea.scrollTop = logarea.scrollHeight;
- }, 10)
- },
- onClearLog(){
- this.logView = "";
- },
- saveCfg() {
- let newData = "";
- for(let i = 0;i<this.projectArr.length;i++){
- newData += this.projectArr[i];
- if(i != this.projectArr.length-1){
- newData += ",";
- }
- }
- Fs.writeFile(this.cfgPath, newData, function (error) {
- });
- this.addLog("配置已保存!");
- },
- readCfg() {
- this.projectArr = [];
- if (Fs.existsSync(this.cfgPath)) {
- let st = Fs.readFileSync(this.cfgPath, "UTF-8");
- if(st != ""){
- let cfgData = st.split(",");
- for(let i = 0;i<cfgData.length;i++){
- let projecIdx = parseInt(cfgData[i]);
- this.projectArr.push(projecIdx);
- }
- this.addLog("读取配置成功!");
- }else{
- this.addLog("配置中未添加任何项目!");
- }
- } else {
- this.addLog("未检测到历史配置!");
- }
- },
- updateProjects(){
- if(this.projectArr.length > 1){
- this.projectArr.sort(function(a, b){return a - b});
- }
-
- let newData = "";
- for(let i = 0;i<this.projectArr.length;i++){
- newData += this.projectArr[i];
- newData += "\t";
- }
- projectsText.innerHTML = newData;
- if(projectsText.innerHTML == ""){
- projectsText.innerHTML = "未添加任何项目";
- }
- this.saveCfg();
- },
- onAdd(){
- let projecIdx = parseInt(inputProjectIdx.value);
- if(!Number.isInteger(projecIdx)){
- this.addLog("请正确输入项目索引(正整数)");
- return;
- }
- let isFind = false;
- for(let i = 0;i<this.projectArr.length;i++){
- if(this.projectArr[i] == projecIdx){
- isFind = true;
- break;
- }
- }
- if(!isFind){
- this.projectArr.push(projecIdx);
- this.addLog("项目添加成功:"+projecIdx);
- }else{
- this.addLog("项目已在同步列表中,无需重复添加!");
- }
- this.updateProjects();
- },
- onDelete(){
- let projecIdx = parseInt(inputProjectIdx.value);
- if(!Number.isInteger(projecIdx)){
- this.addLog("请正确输入项目索引(正整数)");
- return;
- }
- let isFind = false;
- let arr = [];
- for(let i = 0;i<this.projectArr.length;i++){
- if(this.projectArr[i] == projecIdx){
- isFind = true;
- continue;
- }
- arr.push(this.projectArr[i]);
- }
- this.projectArr = arr;
- if(isFind){
- this.addLog("成功移除项目:"+projecIdx);
- }else{
- this.addLog("同步列表未找到项目:"+projecIdx+",无需移除!");
- }
-
- this.updateProjects();
- },
- onSync() {
- this.onClearLog();
- if (this.hasSyncProj) {
- this.addLog("请不要重复点击");
- return;
- };
- if (this.projectArr.length <= 0) {
- this.addLog("未添加任何同步项目!");
- return;
- }
- this.hasSyncProj = true;
- this.AllFileVec = [];
- this.addLog("开始同步..."+this.rootPath + "assets,请耐心等待");
- this.readPath(this.rootPath + "assets");
- this.addLog("共检测到" + this.AllFileVec.length + "个需要修改的文件");
-
- this.beginSync();
- },
- beginSync(){
- for(let idx = 0;idx<this.projectArr.length;idx++){
- let nowProjectIdx = this.projectArr[idx];
- let rootIdx = this.rootPath.indexOf("client_");
- let projectsRootPath = this.rootPath.substring(0,rootIdx);
- //this.addLog("项目跟路径:"+projectsRootPath);
- let nowProjectPath = projectsRootPath + "client_" + nowProjectIdx + "/";
- this.addLog("开始同步项目:client_"+ nowProjectIdx + ","+nowProjectPath);
- let len = this.rootPath.length;
- for (var i = 0; i < this.AllFileVec.length; i++) {
- let scrPath = this.AllFileVec[i];
- let childPath = scrPath.substring(len);
- let destPath = nowProjectPath+childPath;
- //this.addLog("源路径:"+scrPath);
- //this.addLog("目标路径:"+destPath);
- copyFile(scrPath, destPath);
- };
- this.addLog("client_"+ nowProjectIdx + ",同步完成!");
- }
- this.hasSyncProj = false;
- },
- readPath(path) {
- var isFiltrationFile = function (pathStr) {//需要过滤的文件
- let extname = Path.extname(pathStr);//后缀名
- let basename = Path.basename(pathStr);//文件名
-
- if (
- //extname == ".meta" ||
- //basename == "AppStart.js" ||
- //basename == "BaseDefine.js" ||
- //basename == "headUrlList.json" ||
- basename == "ReviewResources.meta" ||
- //basename == "gamemanifest.meta" ||
- basename == "gamemanifest_test.meta"
- ) {
- return true;
- }
- return false;
- }.bind(this);
- var isFiltrationFolder = function (extname) {
- if (extname == "ReviewResources" /*|| extname == "gamemanifest"*/ || extname == "gamemanifest_test") {
- return true;
- } else {
- return false;
- }
- }.bind(this);
- var dirList = Fs.readdirSync(path);
- dirList.forEach(function (item) {
- if (Fs.statSync(path + '/' + item).isDirectory()) {
- if (isFiltrationFolder(item)) {//需要过滤的文件夹
- //this.addLog("已过滤文件夹:" + item);
- } else {
- this.readPath(path + '/' + item);
- }
- } else {
- if (isFiltrationFile(path + '/' + item)) {//需要过滤的文件
- //this.addLog("已过滤文件:" + item);
- } else {
- this.AllFileVec.push(path + '/' + item);
- }
- }
- }.bind(this));
- },
- }
- })
- },
- messages: {
- "encrypt-tool:onBuildFinished": function (e, t) {
- }
- },
- });
|