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