util.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var path = require('path')
  2. var github = require('github-from-package')
  3. var home = require('os').homedir
  4. var crypto = require('crypto')
  5. var expandTemplate = require('expand-template')()
  6. function getDownloadUrl (opts) {
  7. var pkgName = opts.pkg.name.replace(/^@\w+\//, '')
  8. return expandTemplate(urlTemplate(opts), {
  9. name: pkgName,
  10. package_name: pkgName,
  11. version: opts.pkg.version,
  12. major: opts.pkg.version.split('.')[0],
  13. minor: opts.pkg.version.split('.')[1],
  14. patch: opts.pkg.version.split('.')[2],
  15. prerelease: opts.pkg.version.split('-')[1],
  16. build: opts.pkg.version.split('+')[1],
  17. abi: opts.abi || process.versions.modules,
  18. node_abi: process.versions.modules,
  19. runtime: opts.runtime || 'node',
  20. platform: opts.platform,
  21. arch: opts.arch,
  22. libc: opts.libc || process.env.LIBC || '',
  23. configuration: (opts.debug ? 'Debug' : 'Release'),
  24. module_name: opts.pkg.binary && opts.pkg.binary.module_name,
  25. tag_prefix: opts['tag-prefix'] || 'v'
  26. })
  27. }
  28. function getApiUrl (opts) {
  29. return github(opts.pkg).replace('github.com', 'api.github.com/repos') + '/releases'
  30. }
  31. function getAssetUrl (opts, assetId) {
  32. return getApiUrl(opts) + '/assets/' + assetId
  33. }
  34. function urlTemplate (opts) {
  35. if (typeof opts.download === 'string') {
  36. return opts.download
  37. }
  38. var packageName = '{name}-v{version}-{runtime}-v{abi}-{platform}{libc}-{arch}.tar.gz'
  39. var hostMirrorUrl = getHostMirrorUrl(opts)
  40. if (hostMirrorUrl) {
  41. return hostMirrorUrl + '/{tag_prefix}{version}/' + packageName
  42. }
  43. if (opts.pkg.binary && opts.pkg.binary.host) {
  44. return [
  45. opts.pkg.binary.host,
  46. opts.pkg.binary.remote_path,
  47. opts.pkg.binary.package_name || packageName
  48. ].map(function (path) {
  49. return trimSlashes(path)
  50. }).filter(Boolean).join('/')
  51. }
  52. return github(opts.pkg) + '/releases/download/{tag_prefix}{version}/' + packageName
  53. }
  54. function getHostMirrorUrl (opts) {
  55. var propName = 'npm_config_' + (opts.pkg.name || '').replace(/[^a-zA-Z0-9]/g, '_') + '_binary_host'
  56. return process.env[propName] || process.env[propName + '_mirror']
  57. }
  58. function trimSlashes (str) {
  59. if (str) return str.replace(/^\.\/|^\/|\/$/g, '')
  60. }
  61. function cachedPrebuild (url) {
  62. var digest = crypto.createHash('md5').update(url).digest('hex').slice(0, 6)
  63. return path.join(prebuildCache(), digest + '-' + path.basename(url).replace(/[^a-zA-Z0-9.]+/g, '-'))
  64. }
  65. function npmCache () {
  66. var env = process.env
  67. return env.npm_config_cache || (env.APPDATA ? path.join(env.APPDATA, 'npm-cache') : path.join(home(), '.npm'))
  68. }
  69. function prebuildCache () {
  70. return path.join(npmCache(), '_prebuilds')
  71. }
  72. function tempFile (cached) {
  73. return cached + '.' + process.pid + '-' + Math.random().toString(16).slice(2) + '.tmp'
  74. }
  75. exports.getDownloadUrl = getDownloadUrl
  76. exports.getApiUrl = getApiUrl
  77. exports.getAssetUrl = getAssetUrl
  78. exports.urlTemplate = urlTemplate
  79. exports.cachedPrebuild = cachedPrebuild
  80. exports.prebuildCache = prebuildCache
  81. exports.npmCache = npmCache
  82. exports.tempFile = tempFile