publish.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. module.exports = exports = publish;
  3. exports.usage = 'Publishes pre-built binary (requires aws-sdk)';
  4. const fs = require('fs');
  5. const path = require('path');
  6. const log = require('npmlog');
  7. const versioning = require('./util/versioning.js');
  8. const napi = require('./util/napi.js');
  9. const s3_setup = require('./util/s3_setup.js');
  10. const existsAsync = fs.exists || path.exists;
  11. const url = require('url');
  12. function publish(gyp, argv, callback) {
  13. const package_json = gyp.package_json;
  14. const napi_build_version = napi.get_napi_build_version_from_command_args(argv);
  15. const opts = versioning.evaluate(package_json, gyp.opts, napi_build_version);
  16. const tarball = opts.staged_tarball;
  17. existsAsync(tarball, (found) => {
  18. if (!found) {
  19. return callback(new Error('Cannot publish because ' + tarball + ' missing: run `node-pre-gyp package` first'));
  20. }
  21. log.info('publish', 'Detecting s3 credentials');
  22. const config = {};
  23. s3_setup.detect(opts, config);
  24. const s3 = s3_setup.get_s3(config);
  25. const key_name = url.resolve(config.prefix, opts.package_name);
  26. const s3_opts = {
  27. Bucket: config.bucket,
  28. Key: key_name
  29. };
  30. log.info('publish', 'Authenticating with s3');
  31. log.info('publish', config);
  32. log.info('publish', 'Checking for existing binary at ' + opts.hosted_path);
  33. s3.headObject(s3_opts, (err, meta) => {
  34. if (meta) log.info('publish', JSON.stringify(meta));
  35. if (err && err.code === 'NotFound') {
  36. // we are safe to publish because
  37. // the object does not already exist
  38. log.info('publish', 'Preparing to put object');
  39. const s3_put_opts = {
  40. ACL: 'public-read',
  41. Body: fs.createReadStream(tarball),
  42. Key: key_name,
  43. Bucket: config.bucket
  44. };
  45. log.info('publish', 'Putting object', s3_put_opts.ACL, s3_put_opts.Bucket, s3_put_opts.Key);
  46. try {
  47. s3.putObject(s3_put_opts, (err2, resp) => {
  48. log.info('publish', 'returned from putting object');
  49. if (err2) {
  50. log.info('publish', 's3 putObject error: "' + err2 + '"');
  51. return callback(err2);
  52. }
  53. if (resp) log.info('publish', 's3 putObject response: "' + JSON.stringify(resp) + '"');
  54. log.info('publish', 'successfully put object');
  55. console.log('[' + package_json.name + '] published to ' + opts.hosted_path);
  56. return callback();
  57. });
  58. } catch (err3) {
  59. log.info('publish', 's3 putObject error: "' + err3 + '"');
  60. return callback(err3);
  61. }
  62. } else if (err) {
  63. log.info('publish', 's3 headObject error: "' + err + '"');
  64. return callback(err);
  65. } else {
  66. log.error('publish', 'Cannot publish over existing version');
  67. log.error('publish', "Update the 'version' field in package.json and try again");
  68. log.error('publish', 'If the previous version was published in error see:');
  69. log.error('publish', '\t node-pre-gyp unpublish');
  70. return callback(new Error('Failed publishing to ' + opts.hosted_path));
  71. }
  72. });
  73. });
  74. }