azput.js 1.5 KB

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