azput.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* eslint-disable camelcase */
  2. const { BlobServiceClient } = require('@azure/storage-blob');
  3. const minimist = require('minimist');
  4. const path = require('node:path');
  5. const { ELECTRON_ARTIFACTS_BLOB_STORAGE } = process.env;
  6. if (!ELECTRON_ARTIFACTS_BLOB_STORAGE) {
  7. console.error('Missing required ELECTRON_ARTIFACTS_BLOB_STORAGE environment variable.');
  8. process.exit(1);
  9. }
  10. const blobServiceClient = BlobServiceClient.fromConnectionString(ELECTRON_ARTIFACTS_BLOB_STORAGE);
  11. const args = minimist(process.argv.slice(2));
  12. let { prefix = '/', key_prefix = '', _: files } = args;
  13. if (prefix && !prefix.endsWith(path.sep)) prefix = path.resolve(prefix) + path.sep;
  14. function filenameToKey (file) {
  15. file = path.resolve(file);
  16. if (file.startsWith(prefix)) file = file.substr(prefix.length - 1);
  17. return key_prefix + (path.sep === '\\' ? file.replace(/\\/g, '/') : file);
  18. }
  19. let anErrorOccurred = false;
  20. function next (done) {
  21. const file = files.shift();
  22. if (!file) return done();
  23. const key = filenameToKey(file);
  24. const [containerName, ...keyPath] = key.split('/');
  25. const blobKey = keyPath.join('/');
  26. console.log(`Uploading '${file}' to container '${containerName}' with key '${blobKey}'...`);
  27. const containerClient = blobServiceClient.getContainerClient(containerName);
  28. const blockBlobClient = containerClient.getBlockBlobClient(blobKey);
  29. blockBlobClient.uploadFile(file)
  30. .then((uploadBlobResponse) => {
  31. console.log(`Upload block blob ${blobKey} successfully: https://artifacts.electronjs.org/${key}`, uploadBlobResponse.requestId);
  32. })
  33. .catch((err) => {
  34. console.error(err);
  35. anErrorOccurred = true;
  36. })
  37. .then(() => next(done));
  38. }
  39. next(() => {
  40. process.exit(anErrorOccurred ? 1 : 0);
  41. });