saveOSSAGitte.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. (async function Init() {
  9. const localDir = "dist/";
  10. const dirPath = path.resolve(__dirname, "./" + localDir);
  11. const baseOssDir = "topic/activity/";
  12. const 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. /**
  20. * 获取localDir下的所有文件路径
  21. * @param {string} pathDir
  22. */
  23. async function getFiles(pathDir) {
  24. const dirList = fs.readdirSync(pathDir) || [];
  25. for (let i = 0; i < dirList.length; i++) {
  26. const P = [pathDir, dirList[i]];
  27. const stat = fs.lstatSync(P.join("\\"));
  28. if (!stat.isDirectory()) {
  29. await upFileOSS(P);
  30. } else getFiles(P.join("\\"));
  31. }
  32. }
  33. /**
  34. * 获得上传目录下的所有文件路径
  35. * @param {string} in_dirPath
  36. */
  37. async function delDir(in_dirPath) {
  38. let list = await client.list({
  39. prefix: in_dirPath,
  40. });
  41. (list.objects || []).splice(0, 1);
  42. // 删除文件
  43. await Promise.all(
  44. list.objects.map(async v => {
  45. try {
  46. await client.delete(v.name);
  47. } catch (error) {
  48. error.failObjectName = v.name;
  49. return error;
  50. }
  51. })
  52. );
  53. console.log("oss文件清除---完成");
  54. }
  55. /**
  56. * 上传文件到oss
  57. * @param {string} filePath
  58. */
  59. async function upFileOSS(filePath) {
  60. try {
  61. // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
  62. // 如果本地文件的完整路径中未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
  63. let dir = filePath[0].split("\\");
  64. dir = dir.reverse()[0] + "/";
  65. if (dir === localDir) dir = "";
  66. await client.put(
  67. baseOssDir + dir + filePath[1],
  68. path.normalize(filePath.join("\\"))
  69. // 自定义headers
  70. //,{headers}
  71. );
  72. } catch (e) {
  73. console.error("未成功上传:", filePath.join("\\"));
  74. console.log("err--->", e);
  75. }
  76. }
  77. /**
  78. * 上传git
  79. */
  80. function execFun() {
  81. try {
  82. execSync("git add .");
  83. execSync(`git commit -m "${process.argv[2] || "提交"}"`);
  84. execSync("git push -u origin master");
  85. // execSync("git push -u github master");
  86. console.log("上传git---完成");
  87. } catch (e) {
  88. console.log("上传git---失败");
  89. console.error(e);
  90. }
  91. }
  92. execFun();
  93. await delDir(baseOssDir);
  94. try {
  95. await getFiles(dirPath);
  96. console.log("数据上传oss---完成");
  97. } catch (err) {
  98. console.log("数据上传oss---失败");
  99. }
  100. })();