saveOSSAGitte.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * build成功后的运行逻辑
  3. */
  4. const path = require("path");
  5. const fs = require("fs");
  6. const oss = require("ali-oss");
  7. const { execSync } = require("child_process");
  8. class saveOssGitte {
  9. constructor() {
  10. this.localDir = "dist/";
  11. this.baseOssDir = "topic/activity/";
  12. this.client = new oss({
  13. region: "oss-cn-chengdu",
  14. // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
  15. accessKeyId: "LTAI4GEBqfF1GX4VwsYU2Wpg",
  16. accessKeySecret: "rVIv0E1lRfXOCrAmkFTZnfgWiuv4ea",
  17. bucket: "smcic-index",
  18. });
  19. this.execFun();
  20. // this.delDir(baseOssDir);
  21. // try {
  22. // this.getFiles(path.resolve(__dirname, "./" + this.localDir));
  23. // console.log("数据上传oss---完成");
  24. // } catch (err) {
  25. // console.log("数据上传oss---失败");
  26. // }
  27. }
  28. /**
  29. * 上传git
  30. */
  31. execFun() {
  32. try {
  33. execSync("git add .");
  34. execSync(`git commit -m "${process.argv[2] || "提交"}"`);
  35. execSync("git push -u origin master");
  36. // execSync("git push -u github master");
  37. console.log("上传git---完成");
  38. } catch (e) {
  39. console.log("上传git---失败");
  40. console.error(e);
  41. }
  42. }
  43. /**
  44. * 获得上传目录下的所有文件路径
  45. * 删除oss指定路径下文件
  46. * @param {string} in_dirPath
  47. */
  48. async delDir(in_dirPath) {
  49. let list = await this.client.list({
  50. prefix: in_dirPath,
  51. });
  52. (list.objects || []).splice(0, 1);
  53. // 删除文件
  54. await Promise.all(
  55. list.objects.map(async v => {
  56. try {
  57. await this.client.delete(v.name);
  58. } catch (error) {
  59. error.failObjectName = v.name;
  60. return error;
  61. }
  62. })
  63. );
  64. console.log("oss文件清除---完成");
  65. }
  66. /**
  67. * 获取localDir下的所有文件路径
  68. * @param {string} pathDir
  69. */
  70. async getFiles(pathDir) {
  71. const dirList = fs.readdirSync(pathDir) || [];
  72. for (let i = 0; i < dirList.length; i++) {
  73. const P = [pathDir, dirList[i]];
  74. const stat = fs.lstatSync(P.join("\\"));
  75. if (!stat.isDirectory()) {
  76. await upFileOSS(P);
  77. } else getFiles(P.join("\\"));
  78. }
  79. }
  80. /**
  81. * 上传文件到oss
  82. * @param {string} filePath
  83. */
  84. async upFileOSS(filePath) {
  85. try {
  86. // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
  87. // 如果本地文件的完整路径中未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
  88. let dir = filePath[0].split("\\");
  89. dir = dir.reverse()[0] + "/";
  90. if (dir === this.localDir) dir = "";
  91. await this.client.put(
  92. baseOssDir + dir + filePath[1],
  93. path.normalize(filePath.join("\\"))
  94. // 自定义headers
  95. //,{headers}
  96. );
  97. } catch (e) {
  98. console.error("未成功上传:", filePath.join("\\"));
  99. console.log("err--->", e);
  100. }
  101. }
  102. }
  103. new saveOssGitte();