forked from mafintosh/node-gyp-install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (128 loc) · 4.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
var get = require('simple-get')
var map = require('tar-map-stream')
var tar = require('tar-fs')
var zlib = require('zlib')
var fs = require('fs')
var path = require('path')
var pump = require('pump')
var after = require('after-all')
var multi = require('multi-write-stream')
var semver = require('semver')
module.exports = install
function install (opts, cb) {
if (typeof opts === 'function') return install(null, opts)
if (!opts) opts = {}
if (!cb) cb = noop
var log = opts.log
var version = opts.version || process.version
if (version[0] !== 'v') version = 'v' + version
var nightly = opts.nightly !== undefined ? opts.nightly : version.indexOf('nightly') > -1
var io = opts.iojs !== undefined ? opts.iojs : iojsVersion(version)
var platform = opts.platform || process.platform
var defaultIojsUrl = nightly ? 'https://iojs.org/download/nightly/' : 'https://npm.taobao.org/mirrors/iojs/'
var iojsDistUrl = pad(process.env.NVM_IOJS_ORG_MIRROR || defaultIojsUrl)
var nodeDistUrl = pad(process.env.NVM_NODEJS_ORG_MIRROR || 'https://npm.taobao.org/mirrors/node/')
var url = io
? iojsDistUrl + version + '/iojs-' + version + '.tar.gz'
: nodeDistUrl + version + '/node-' + version + '.tar.gz'
var target = path.join(process.env.HOME || process.env.USERPROFILE, '.node-gyp', version.slice(1))
exists(function (err) {
if (!err && !opts.force) {
if (log) log.info('install', 'Header files already fetched')
if (log) log.info('install', 'node-gyp should now work for ' + version)
return cb(null)
}
if (log) log.http('request', url)
get(tlsopts(url), function (err, res) {
if (err) return cb(err)
if (log) log.http(res.statusCode, url)
pump(res, zlib.createGunzip(), map(mapEntry), tar.extract(target, {strip: 1}), function (err) {
if (err) return cb(err)
fetchWindows(function (err) {
if (err) return cb(err)
fs.writeFile(path.join(target, 'installVersion'), '9', function (err) {
if (err) return cb(err)
if (log) log.info('install', 'node-gyp should now work for ' + version)
cb()
})
})
})
})
})
function exists (cb) {
fs.exists(path.join(target, 'installVersion'), function (exists) {
if (!exists) return cb(new Error('missing installVersion'))
fs.exists(path.join(target, 'common.gypi'), function (exists) {
cb(exists ? null : new Error('missing common.gypi'))
})
})
}
function mapEntry (entry) {
return /(\.gypi$)|(\.h$)/.test(entry.name) ? entry : null
}
function fetchWindows (cb) {
if (platform !== 'win32') return cb()
var urls
if (io) {
urls = [
iojsDistUrl + version + '/win-x86/iojs.lib',
iojsDistUrl + version + '/win-x64/iojs.lib'
]
} else if (semver.satisfies(version, '>=4.0.0')) {
urls = [
nodeDistUrl + version + '/win-x86/node.lib',
nodeDistUrl + version + '/win-x64/node.lib'
]
} else {
urls = [
nodeDistUrl + version + '/node.lib',
nodeDistUrl + version + '/x64/node.lib'
]
}
var next = after(cb)
urls.forEach(function (url, index) {
var arch = index === 0 ? 'ia32' : 'x64'
var nodeLib = path.join(target, arch, 'node.lib')
var ioLib = path.join(target, arch, 'iojs.lib')
var parentDir = path.dirname(nodeLib)
var done = next()
if (log) log.http('request', url)
fs.mkdir(parentDir, function () {
get(tlsopts(url), function (err, res) {
if (err) return done(err)
log.http(res.statusCode, url)
pump(res, multi([fs.createWriteStream(nodeLib), fs.createWriteStream(ioLib)]), done)
})
})
})
}
function tlsopts (url) {
var getOpts = {
url: url,
passphrase: process.env.NODE_GYP_INSTALL_PASSPHRASE,
requestCert: true,
rejectUnauthorized: process.env.NODE_GYP_INSTALL_REJECTUNAUTHORIZED === 'true'
}
if (process.env.NODE_GYP_INSTALL_PFX) {
getOpts.pfx = fs.readFileSync(process.env.NODE_GYP_INSTALL_PFX)
}
if (process.env.NODE_GYP_INSTALL_CERT) {
getOpts.cert = fs.readFileSync(process.env.NODE_GYP_INSTALL_CERT)
}
if (process.env.NODE_GYP_INSTALL_KEY) {
getOpts.cert = fs.readFileSync(process.env.NODE_GYP_INSTALL_KEY)
}
if (process.env.NODE_GYP_INSTALL_CA) {
getOpts.ca = [fs.readFileSync(process.env.NODE_GYP_INSTALL_CA)]
}
return getOpts
}
}
function iojsVersion (v) {
return semver.satisfies(v, '>=1.0.0 <4.0.0')
}
function pad (url) {
return url[url.length - 1] === '/' ? url : url + '/'
}
function noop () {}