1 var semver = require('semver')
3 , path = require('path')
4 , log = require('npmlog')
6 // versions where -headers.tar.gz started shipping
7 , headersTarballRange = '>= 3.0.0 || ~0.12.10 || ~0.10.42'
8 , bitsre = /\/win-(x86|x64)\//
9 , bitsreV3 = /\/win-(x86|ia32|x64)\// // io.js v3.x.x shipped with "ia32" but should
12 // Captures all the logic required to determine download URLs, local directory and
13 // file names. Inputs come from command-line switches (--target, --dist-url),
14 // `process.version` and `process.release` where it exists.
15 function processRelease (argv, gyp, defaultVersion, defaultRelease) {
16 var version = (semver.valid(argv[0]) && argv[0]) || gyp.opts.target || defaultVersion
17 , versionSemver = semver.parse(version)
18 , overrideDistUrl = gyp.opts['dist-url'] || gyp.opts.disturl
30 // not a valid semver string, nothing we can do
31 return { version: version }
33 // flatten version into String
34 version = versionSemver.version
36 // defaultVersion should come from process.version so ought to be valid semver
37 isDefaultVersion = version === semver.parse(defaultVersion).version
39 // can't use process.release if we're using --target=x.y.z
40 if (!isDefaultVersion)
44 // v3 onward, has process.release
45 name = defaultRelease.name.replace(/io\.js/, 'iojs') // remove the '.' for directory naming purposes
46 isIojs = name === 'iojs'
48 // old node or alternative --target=
49 // semver.satisfies() doesn't like prerelease tags so test major directly
50 isIojs = versionSemver.major >= 1 && versionSemver.major < 4
51 name = isIojs ? 'iojs' : 'node'
54 // check for the nvm.sh standard mirror env variables
55 if (!overrideDistUrl) {
57 if (process.env.IOJS_ORG_MIRROR) {
58 overrideDistUrl = process.env.IOJS_ORG_MIRROR
59 } else if (process.env.NVM_IOJS_ORG_MIRROR) {// remove on next semver-major
60 overrideDistUrl = process.env.NVM_IOJS_ORG_MIRROR
62 'NVM_IOJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, ' +
63 'please use IOJS_ORG_MIRROR')
66 if (process.env.NODEJS_ORG_MIRROR) {
67 overrideDistUrl = process.env.NODEJS_ORG_MIRROR
68 } else if (process.env.NVM_NODEJS_ORG_MIRROR) {// remove on next semver-major
69 overrideDistUrl = process.env.NVM_NODEJS_ORG_MIRROR
71 'NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, ' +
72 'please use NODEJS_ORG_MIRROR')
79 distBaseUrl = overrideDistUrl.replace(/\/+$/, '')
81 distBaseUrl = isIojs ? 'https://iojs.org/download/release' : 'https://nodejs.org/dist'
82 distBaseUrl += '/v' + version + '/'
84 // new style, based on process.release so we have a lot of the data we need
85 if (defaultRelease && defaultRelease.headersUrl && !overrideDistUrl) {
86 baseUrl = url.resolve(defaultRelease.headersUrl, './')
87 libUrl32 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || distBaseUrl, 'x86', versionSemver.major)
88 libUrl64 = resolveLibUrl(name, defaultRelease.libUrl || baseUrl || distBaseUrl, 'x64', versionSemver.major)
92 semver: versionSemver,
95 tarballUrl: defaultRelease.headersUrl,
96 shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
97 versionDir: (name !== 'node' ? name + '-' : '') + version,
100 libPath32: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl32).path)),
101 libPath64: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl64).path))
105 // older versions without process.release are captured here and we have to make
106 // a lot of assumptions, additionally if you --target=x.y.z then we can't use the
107 // current process.release
109 baseUrl = distBaseUrl
110 libUrl32 = resolveLibUrl(name, baseUrl, 'x86', versionSemver.major)
111 libUrl64 = resolveLibUrl(name, baseUrl, 'x64', versionSemver.major)
112 // making the bold assumption that anything with a version number >3.0.0 will
113 // have a *-headers.tar.gz file in its dist location, even some frankenstein
115 canGetHeaders = semver.satisfies(versionSemver, headersTarballRange)
116 tarballUrl = url.resolve(baseUrl, name + '-v' + version + (canGetHeaders ? '-headers' : '') + '.tar.gz')
120 semver: versionSemver,
123 tarballUrl: tarballUrl,
124 shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
125 versionDir: (name !== 'node' ? name + '-' : '') + version,
128 libPath32: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl32).path)),
129 libPath64: normalizePath(path.relative(url.parse(baseUrl).path, url.parse(libUrl64).path))
133 function normalizePath (p) {
134 return path.normalize(p).replace(/\\/g, '/')
137 function resolveLibUrl (name, defaultUrl, arch, versionMajor) {
138 var base = url.resolve(defaultUrl, './')
139 , hasLibUrl = bitsre.test(defaultUrl) || (versionMajor === 3 && bitsreV3.test(defaultUrl))
142 // let's assume it's a baseUrl then
143 if (versionMajor >= 1)
144 return url.resolve(base, 'win-' + arch +'/' + name + '.lib')
145 // prior to io.js@1.0.0 32-bit node.lib lives in /, 64-bit lives in /x64/
146 return url.resolve(base, (arch === 'x64' ? 'x64/' : '') + name + '.lib')
149 // else we have a proper url to a .lib, just make sure it's the right arch
150 return defaultUrl.replace(versionMajor === 3 ? bitsreV3 : bitsre, '/win-' + arch + '/')
153 module.exports = processRelease