2da45eba8edde1be000ca6094cf4e11f5d3a43ce
[platform/upstream/nodejs.git] / deps / npm / node_modules / node-gyp / lib / process-release.js
1 var semver = require('semver')
2   , url = require('url')
3   , path = require('path')
4
5   , bitsre = /\/win-(x86|x64)\//
6   , bitsreV3 = /\/win-(x86|ia32|x64)\// // io.js v3.x.x shipped with "ia32" but should
7                                         // have been "x86"
8
9 // Captures all the logic required to determine download URLs, local directory and
10 // file names. Inputs come from command-line switches (--target, --dist-url),
11 // `process.version` and `process.release` where it exists.
12 function processRelease (argv, gyp, defaultVersion, defaultRelease) {
13   var version = argv[0] || gyp.opts.target || defaultVersion
14     , versionSemver = semver.parse(version)
15     , overrideDistUrl = gyp.opts['dist-url'] || gyp.opts.disturl
16     , isDefaultVersion
17     , isIojs
18     , name
19     , distBaseUrl
20     , baseUrl
21     , libUrl32
22     , libUrl64
23
24   if (!versionSemver) {
25     // not a valid semver string, nothing we can do
26     return { version: version }
27   }
28   // flatten version into String
29   version = versionSemver.version
30
31   // defaultVersion should come from process.version so ought to be valid semver
32   isDefaultVersion = version === semver.parse(defaultVersion).version
33
34   // can't use process.release if we're using --target=x.y.z
35   if (!isDefaultVersion)
36     defaultRelease = null
37
38   if (defaultRelease) {
39     // v3 onward, has process.release
40     name = defaultRelease.name.replace(/io\.js/, 'iojs') // remove the '.' for directory naming purposes
41     isIojs = name === 'iojs'
42   } else {
43     // old node or alternative --target=
44     // semver.satisfies() doesn't like prerelease tags so test major directly
45     isIojs = versionSemver.major >= 1 & versionSemver.major < 4
46     name = isIojs ? 'iojs' : 'node'
47   }
48
49   // check for the nvm.sh standard mirror env variables
50   if (!overrideDistUrl) {
51     if (isIojs && process.env.NVM_IOJS_ORG_MIRROR)
52       overrideDistUrl = process.env.NVM_IOJS_ORG_MIRROR
53     else if (process.env.NVM_NODEJS_ORG_MIRROR)
54       overrideDistUrl = process.env.NVM_NODEJS_ORG_MIRROR
55   }
56
57
58   if (overrideDistUrl)
59     distBaseUrl = overrideDistUrl.replace(/\/+$/, '')
60   else
61     distBaseUrl = isIojs ? 'https://iojs.org/download/release' : 'https://nodejs.org/dist'
62   distBaseUrl += '/v' + version + '/'
63
64   // new style, based on process.release so we have a lot of the data we need
65   if (defaultRelease && defaultRelease.headersUrl && !overrideDistUrl) {
66     baseUrl = url.resolve(defaultRelease.headersUrl, './')
67     libUrl32 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || distBaseUrl, 'x86', versionSemver.major)
68     libUrl64 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || distBaseUrl, 'x64', versionSemver.major)
69
70     return {
71       version: version,
72       semver: versionSemver,
73       name: name,
74       baseUrl: baseUrl,
75       tarballUrl: defaultRelease.headersUrl,
76       shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
77       versionDir: (name !== 'node' ? name + '-' : '') + version,
78       libUrl32: libUrl32,
79       libUrl64: libUrl64,
80       libPath32: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl32).path)),
81       libPath64: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl64).path))
82     }
83   }
84
85   // older versions without process.release are captured here and we have to make
86   // a lot of assumptions, additionally if you --target=x.y.z then we can't use the
87   // current process.release
88
89   baseUrl = distBaseUrl
90   libUrl32 = resolveLibUrl(name, baseUrl, 'x86', versionSemver.major)
91   libUrl64 = resolveLibUrl(name, baseUrl, 'x64', versionSemver.major)
92   // making the bold assumption that anything with a version number >3.0.0 will
93   // have a *-headers.tar.gz file in its dist location, even some frankenstein
94   // custom version
95   tarballUrl = url.resolve(baseUrl, name + '-v' + version + (versionSemver.major >= 3 ? '-headers' : '') + '.tar.gz')
96
97   return {
98     version: version,
99     semver: versionSemver,
100     name: name,
101     baseUrl: baseUrl,
102     tarballUrl: tarballUrl,
103     shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
104     versionDir: (name !== 'node' ? name + '-' : '') + version,
105     libUrl32: libUrl32,
106     libUrl64: libUrl64,
107     libPath32: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl32).path)),
108     libPath64: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl64).path))
109   }
110 }
111
112 function normalizePath (p) {
113   return path.normalize(p).replace(/\\/g, '/')
114 }
115
116 function resolveLibUrl (name, defaultUrl, arch, versionMajor) {
117   var base = url.resolve(defaultUrl, './')
118     , hasLibUrl = bitsre.test(defaultUrl) || (versionMajor === 3 && bitsreV3.test(defaultUrl))
119
120   if (!hasLibUrl) {
121     // let's assume it's a baseUrl then
122     if (versionMajor >= 1)
123       return url.resolve(base, 'win-' + arch  +'/' + name + '.lib')
124     // prior to io.js@1.0.0 32-bit node.lib lives in /, 64-bit lives in /x64/
125     return url.resolve(base, (arch === 'x64' ? 'x64/' : '') + name + '.lib')
126   }
127
128   // else we have a proper url to a .lib, just make sure it's the right arch
129   return defaultUrl.replace(versionMajor === 3 ? bitsreV3 : bitsre, '/win-' + arch + '/')
130 }
131
132 module.exports = processRelease