asset.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var get = require('simple-get')
  2. var util = require('./util')
  3. var proxy = require('./proxy')
  4. var noop = Object.assign({
  5. http: function () {},
  6. silly: function () {}
  7. }, require('noop-logger'))
  8. function findAssetId (opts, cb) {
  9. var downloadUrl = util.getDownloadUrl(opts)
  10. var apiUrl = util.getApiUrl(opts)
  11. var log = opts.log || noop
  12. log.http('request', 'GET ' + apiUrl)
  13. var reqOpts = proxy({
  14. url: apiUrl,
  15. json: true,
  16. headers: {
  17. 'User-Agent': 'simple-get',
  18. Authorization: 'token ' + opts.token
  19. }
  20. }, opts)
  21. var req = get.concat(reqOpts, function (err, res, data) {
  22. if (err) return cb(err)
  23. log.http(res.statusCode, apiUrl)
  24. if (res.statusCode !== 200) return cb(err)
  25. // Find asset id in release
  26. for (var release of data) {
  27. if (release.tag_name === opts['tag-prefix'] + opts.pkg.version) {
  28. for (var asset of release.assets) {
  29. if (asset.browser_download_url === downloadUrl) {
  30. return cb(null, asset.id)
  31. }
  32. }
  33. }
  34. }
  35. cb(new Error('Could not find GitHub release for version'))
  36. })
  37. req.setTimeout(30 * 1000, function () {
  38. req.abort()
  39. })
  40. }
  41. module.exports = findAssetId