deployAssetsToOss.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const OSS = require('ali-oss');
  2. const co = require('co');
  3. const path = require('path');
  4. const fs = require('fs');
  5. const cdnpath = `/queenshow/`;
  6. const client = new OSS({
  7. region: 'oss-cn-chengdu',
  8. accessKeyId: 'LTAI4GHqGdgY3b14jVRTzL2w',
  9. accessKeySecret: 'AHubozYEzXzi4fBL0ttLJ5sN1HMKA6',
  10. bucket: 'infishwaibao',
  11. });
  12. function GetSubFiles(dir) {
  13. return new Promise((reslove, reject) => {
  14. fs.readdir(dir, function (err, files) {
  15. reslove(files);
  16. });
  17. });
  18. }
  19. async function GetTotalFiles(dir) {
  20. let subfiles = await GetSubFiles(dir);
  21. let files = [],
  22. len = subfiles.length;
  23. let ret = [];
  24. for (let i = 0; i < len; i++) {
  25. let f = subfiles[i];
  26. let fpath = `${dir}/${f}`;
  27. var stat = fs.lstatSync(fpath);
  28. if (!stat.isDirectory()) {
  29. ret.push(fpath);
  30. } else {
  31. let fsubs = await GetTotalFiles(fpath);
  32. let size = fsubs.length;
  33. for (let k = 0; k < size; k++) {
  34. ret.push(fsubs[k]);
  35. }
  36. }
  37. }
  38. return ret;
  39. }
  40. GetTotalFiles('dist').then((files) => {
  41. handleFile(files);
  42. });
  43. async function handleFile(files) {
  44. let i = 0,
  45. len = files.length;
  46. for (; i < len; i++) {
  47. let fpath = files[i];
  48. if (path.extname(fpath) != '.map') {
  49. await UploadOss(files[i]);
  50. }
  51. }
  52. }
  53. function UploadOss(src) {
  54. return new Promise((resolve, reject) => {
  55. co(function* () {
  56. let fpath = src.substr(5); //去掉前面的dist
  57. console.log('uploading ' + fpath);
  58. var ret = yield client.put(cdnpath + fpath, src, { timeout: 2000000 });
  59. console.log(ret.url);
  60. let i = 0;
  61. while (!ret) {
  62. if (i++ > 3) {
  63. reject(`uploading ${src} error!`);
  64. return;
  65. }
  66. ret = yield client.put(cdnpath + fpath, dist, { timeout: 2000000 });
  67. }
  68. console.log(ret.url);
  69. resolve(ret);
  70. });
  71. });
  72. }