package.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. var fs = require('fs')
  2. let folderPath = 'web-mobile';
  3. let pathRequire = require('path');
  4. let pathArr = pathRequire.resolve(__dirname, '..').split("\\");
  5. let newfolderPath = pathArr[pathArr.length-1];//'FightingSports'
  6. let fullFilePath = folderPath+'/index.html'
  7. let Package =
  8. {
  9. init()
  10. {
  11. let self = this;
  12. this.quoteScriptToIndexHtml(function()
  13. {
  14. console.log("1,引入Script到html成功");
  15. self.copyImg(self,function(picName)
  16. {
  17. console.log("2,拷贝图片成功");
  18. self.changeCssBGPercentageAndPngName(self,picName,function()
  19. {
  20. console.log("3,修改Css背景图片百分比成功");
  21. self.changeFolderName(self,function()
  22. {
  23. console.log("4,修改文件夹名成功");
  24. console.log("\n打包成功!");
  25. })
  26. })
  27. });
  28. });
  29. },
  30. //引入Script到html
  31. quoteScriptToIndexHtml(callback)
  32. {
  33. let self = this;
  34. this.readFile(this,fullFilePath,function(str)
  35. {
  36. let regExp = /<script src="main(.+?)<\/script>/g;// '<script src="main.870be.js" charset="utf-8"></script>';
  37. let targetStr = str.match(regExp);
  38. // console.log(targetStr);
  39. let addStr = '<script type="text/javascript" src="//js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>';
  40. let replaceStr = addStr+'\n'+targetStr;
  41. let newNata = str.replace(targetStr,replaceStr);
  42. self.writeFile(self,newNata,fullFilePath,callback);
  43. });
  44. },
  45. //拷贝图片
  46. copyImg(self,callback)
  47. {
  48. let reg = /splash.+?png/;// splash.85cfd.png
  49. let pngName = self.matchFileInFolder(folderPath,reg);
  50. // console.log('png='+pngName);
  51. if(pngName)
  52. {
  53. fs.unlinkSync(folderPath+'/'+pngName);
  54. let picReg = /splash.+?/;// splash.85cfd.png
  55. let picName = self.matchFileInFolder('./',picReg);
  56. // console.log('picName='+picName);
  57. if(picName)
  58. {
  59. let aPath = folderPath+'/'+file;
  60. fs.copyFile(picName,aPath,function(err)
  61. {
  62. if(err) {
  63. console.log('something wrong was happened')
  64. }
  65. else
  66. {
  67. let aName = self.getFileName(pngName);
  68. // console.log('aName='+aName);
  69. let aTail = self.getFileTail(picName);
  70. // console.log('aTail='+aTail);
  71. let newFileName = aName+'.'+aTail;
  72. // console.log('newFileName='+newFileName);
  73. // console.log('copy file succeed');
  74. let oldPath = folderPath+'/'+picName;
  75. let newPath = folderPath+'/'+newFileName;
  76. self.rename(oldPath,newPath,function()
  77. {
  78. callback(newFileName);
  79. });
  80. }
  81. });
  82. }
  83. }
  84. },
  85. // 修改Css背景图片百分比
  86. changeCssBGPercentageAndPngName(self,picName,callback){
  87. let reg = /style-mobile.+?css/;// style-mobile.6e9cd.css
  88. let cssName = self.matchFileInFolder(folderPath,reg);
  89. if(cssName)
  90. {
  91. let aPath = folderPath+'/'+file;
  92. this.readFile(this,aPath,function(str)
  93. {
  94. //change percentate
  95. let targetStr = 'background-size: 45%;';
  96. let replaceStr = 'background-size: 100%;';
  97. let newNata = str.replace(targetStr,replaceStr);
  98. //change png link
  99. let fileName = self.getFileName(picName);
  100. // console.log('fileName='+fileName);
  101. // console.log('picName='+picName);
  102. let newNata2 = newNata.replace(fileName+'.png',picName);
  103. // console.log(newNata)
  104. self.writeFile(self,newNata2,aPath,callback);
  105. });
  106. }
  107. },
  108. //修改文件夹名
  109. changeFolderName(self,callback)
  110. {
  111. let deleteall = function(path)
  112. {
  113. var files = [];
  114. if(fs.existsSync(path))
  115. {
  116. files = fs.readdirSync(path);
  117. files.forEach(function(file, index)
  118. {
  119. var curPath = path + "/" + file;
  120. if(fs.statSync(curPath).isDirectory())
  121. { // recurse
  122. deleteall(curPath);
  123. }
  124. else
  125. { // delete file
  126. fs.unlinkSync(curPath);
  127. }
  128. });
  129. fs.rmdirSync(path);
  130. }
  131. if(!fs.existsSync(newfolderPath))
  132. {
  133. self.rename(folderPath,newfolderPath,callback)
  134. }
  135. };
  136. // delete FightingSports
  137. deleteall(newfolderPath);
  138. },
  139. readFile(self,path,callback)
  140. {
  141. fs.readFile(path, function (error, data)
  142. {
  143. if (error)
  144. {
  145. // 在这里就可以通过判断 error 来确认是否有错误发生
  146. console.log('读取文件失败了')
  147. }
  148. else
  149. {
  150. // <Buffer 68 65 6c 6c 6f 20 6e 6f 64 65 6a 73 0d 0a>
  151. // 文件中存储的其实都是二进制数据 0 1
  152. // 这里为什么看到的不是 0 和 1 呢?原因是二进制转为 16 进制了
  153. // 但是无论是二进制01还是16进制,人类都不认识
  154. // 所以我们可以通过 toString 方法把其转为我们能认识的字符
  155. // console.log(data.toString())
  156. var str = data.toString();
  157. callback(str);
  158. }
  159. });
  160. },
  161. writeFile(self,newNata,path,callback)
  162. {
  163. // fs.wirteFile有三个参数
  164. // 1,第一个参数是要写入的文件路径
  165. // 2,第二个参数是要写入得内容
  166. // 3,第三个参数是可选参数,表示要写入的文件编码格式,一般就不写,默认就行
  167. // 4,第四个参数是个回调函数 只有一个参数error,来判断是否写入成功
  168. fs.writeFile(path, newNata, error =>
  169. {
  170. if (error) return console.log("写入文件失败,原因是" + error.message);
  171. // console.log("写入成功");
  172. callback();
  173. });
  174. },
  175. rename(oldPath,newPath,callback)
  176. {
  177. //同以目录下的文件更名:
  178. fs.rename(oldPath,newPath, function(err)
  179. {
  180. if(err)
  181. {
  182. throw err;
  183. }
  184. // console.log('rename done!');
  185. callback();
  186. });
  187. },
  188. matchFileInFolder(path,reg)
  189. {
  190. const files = fs.readdirSync(path);
  191. for (file of files) {
  192. let result = reg.test(file);
  193. if(result)
  194. {
  195. return file;
  196. }
  197. }
  198. return null;
  199. },
  200. getFileName(name)
  201. {
  202. let namePartArr = name.split('.');
  203. return name.replace('.'+namePartArr[namePartArr.length-1],'');
  204. // if(name.indexOf('.png')!=-1)
  205. // {
  206. // return name.split('.png')[0];
  207. // }
  208. // else if(name.indexOf('.jpg')!=-1)
  209. // {
  210. // return name.split('.jpg')[0];
  211. // }
  212. },
  213. getFileTail(name)
  214. {
  215. let namePartArr = name.split('.');
  216. return namePartArr[namePartArr.length-1];
  217. // if(name.indexOf('.png')!=-1)
  218. // {
  219. // return 'png';
  220. // }
  221. // else if(name.indexOf('.jpg')!=-1)
  222. // {
  223. // return 'jpg';
  224. // }
  225. // return '';
  226. }
  227. }
  228. let package = Object.create(Package);
  229. package.init();