1 var semver = require('semver')
3 , path = require('path')
5 , bitsre = /\/win-(x86|x64)\//
6 , bitsreV3 = /\/win-(x86|ia32|x64)\// // io.js v3.x.x shipped with "ia32" but should
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
25 // not a valid semver string, nothing we can do
26 return { version: version }
28 // flatten version into String
29 version = versionSemver.version
31 // defaultVersion should come from process.version so ought to be valid semver
32 isDefaultVersion = version === semver.parse(defaultVersion).version
34 // can't use process.release if we're using --target=x.y.z
35 if (!isDefaultVersion)
39 // v3 onward, has process.release
40 name = defaultRelease.name.replace(/io\.js/, 'iojs') // remove the '.' for directory naming purposes
41 isIojs = name === 'iojs'
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'
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
59 distBaseUrl = overrideDistUrl.replace(/\/+$/, '')
61 distBaseUrl = isIojs ? 'https://iojs.org/download/release' : 'https://nodejs.org/dist'
62 distBaseUrl += '/v' + version + '/'
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)
72 semver: versionSemver,
75 tarballUrl: defaultRelease.headersUrl,
76 shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
77 versionDir: (name !== 'node' ? name + '-' : '') + version,
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))
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
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
95 tarballUrl = url.resolve(baseUrl, name + '-v' + version + (versionSemver.major >= 3 ? '-headers' : '') + '.tar.gz')
99 semver: versionSemver,
102 tarballUrl: tarballUrl,
103 shasumsUrl: url.resolve(baseUrl, 'SHASUMS256.txt'),
104 versionDir: (name !== 'node' ? name + '-' : '') + version,
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))
112 function normalizePath (p) {
113 return path.normalize(p).replace(/\\/g, '/')
116 function resolveLibUrl (name, defaultUrl, arch, versionMajor) {
117 var base = url.resolve(defaultUrl, './')
118 , hasLibUrl = bitsre.test(defaultUrl) || (versionMajor === 3 && bitsreV3.test(defaultUrl))
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')
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 + '/')
132 module.exports = processRelease