unpublish.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. module.exports = exports = unpublish;
  3. exports.usage = 'Unpublishes pre-built binary (requires aws-sdk)';
  4. const log = require('npmlog');
  5. const versioning = require('./util/versioning.js');
  6. const napi = require('./util/napi.js');
  7. const s3_setup = require('./util/s3_setup.js');
  8. const url = require('url');
  9. function unpublish(gyp, argv, callback) {
  10. const package_json = gyp.package_json;
  11. const napi_build_version = napi.get_napi_build_version_from_command_args(argv);
  12. const opts = versioning.evaluate(package_json, gyp.opts, napi_build_version);
  13. const config = {};
  14. s3_setup.detect(opts, config);
  15. const s3 = s3_setup.get_s3(config);
  16. const key_name = url.resolve(config.prefix, opts.package_name);
  17. const s3_opts = {
  18. Bucket: config.bucket,
  19. Key: key_name
  20. };
  21. s3.headObject(s3_opts, (err, meta) => {
  22. if (err && err.code === 'NotFound') {
  23. console.log('[' + package_json.name + '] Not found: https://' + s3_opts.Bucket + '.s3.amazonaws.com/' + s3_opts.Key);
  24. return callback();
  25. } else if (err) {
  26. return callback(err);
  27. } else {
  28. log.info('unpublish', JSON.stringify(meta));
  29. s3.deleteObject(s3_opts, (err2, resp) => {
  30. if (err2) return callback(err2);
  31. log.info(JSON.stringify(resp));
  32. console.log('[' + package_json.name + '] Success: removed https://' + s3_opts.Bucket + '.s3.amazonaws.com/' + s3_opts.Key);
  33. return callback();
  34. });
  35. }
  36. });
  37. }