forked from mafintosh/node-gyp-install
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cnpm/merge-upstream
- Loading branch information
Showing
6 changed files
with
237 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "7" | ||
- "6" | ||
- "4" | ||
- "iojs" | ||
- "0.12" | ||
- "0.11" | ||
- "0.10" | ||
script: | ||
- npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,29 @@ | ||
#!/usr/bin/env node | ||
|
||
var request = require('request') | ||
var map = require('tar-map-stream') | ||
var tar = require('tar-fs') | ||
var zlib = require('zlib') | ||
var fs = require('fs') | ||
var path = require('path') | ||
|
||
// var io = parseInt(process.version.slice(1), 10) >= 1 // yolo | ||
var io = process.env.IOJS === 'TRUE' | ||
var iojsDistUrl = process.env.NVM_IOJS_ORG_MIRROR || 'http://npm.taobao.org/mirrors/iojs/' | ||
if (iojsDistUrl[iojsDistUrl.length - 1] !== '/') { | ||
iojsDistUrl += '/' | ||
} | ||
var nodeDistUrl = process.env.NVM_NODEJS_ORG_MIRROR || 'http://npm.taobao.org/mirrors/node/' | ||
if (nodeDistUrl[nodeDistUrl.length - 1] !== '/') { | ||
nodeDistUrl += '/' | ||
} | ||
|
||
var url = io ? | ||
iojsDistUrl + process.version + '/iojs-' + process.version + '.tar.gz' : | ||
nodeDistUrl + process.version + '/node-' + process.version + '.tar.gz' | ||
|
||
var target = path.join(process.env.HOME || process.env.USERPROFILE, '.node-gyp', process.version.slice(1)) | ||
|
||
if (fs.existsSync(path.join(target, 'installVersion'))) { | ||
console.log('Header files already fetched') | ||
console.log('node-gyp should now work for %s', process.version) | ||
process.exit(1) | ||
var install = require('./') | ||
var minimist = require('minimist') | ||
var log = require('npmlog') | ||
var argv = minimist(process.argv, { | ||
booleans: ['iojs', 'nightly', 'quiet', 'force'], | ||
alias: {iojs: 'io', v: 'version', p: 'platform', d: 'dist', q: 'quiet', n: 'nightly', f: 'force'} | ||
}) | ||
|
||
log.heading = 'node-gyp-install' | ||
argv.log = !argv.quiet && log | ||
|
||
if (argv.help) { | ||
console.error( | ||
'node-gyp-install [options]\n' + | ||
' --version, -v (' + process.version + ')\n' + | ||
' --platform, -p (' + process.platform + ')\n' + | ||
' --force, -f\n' + | ||
' --nightly, -n\n' + | ||
' --quiet, -q\n' + | ||
' --iojs\n' | ||
) | ||
process.exit(0) | ||
} | ||
|
||
console.log('Fetching header files from %s', url) | ||
|
||
request(url) | ||
.pipe(zlib.createGunzip()) | ||
.pipe(map(function (entry) { | ||
return /(\.gypi$)|(\.h$)/.test(entry.name) ? entry : null | ||
})) | ||
.pipe(tar.extract(target, {strip: 1})) | ||
.on('finish', function () { | ||
downloadLibs(function () { | ||
fs.writeFileSync(path.join(target, 'installVersion'), '9') // yolo | ||
console.log('node-gyp should now work for %s', process.version) | ||
}) | ||
}) | ||
|
||
function downloadLibs (callback) { | ||
if (process.platform !== 'win32') { | ||
return callback() | ||
} | ||
|
||
var urls | ||
if (io) { | ||
urls = [ | ||
iojsDistUrl + process.version + '/win-x86/iojs.lib', | ||
iojsDistUrl + process.version + '/win-x64/iojs.lib' | ||
] | ||
} else { | ||
urls = [ | ||
nodeDistUrl + process.version + '/node.lib', | ||
nodeDistUrl + process.version + '/x64/node.lib' | ||
] | ||
} | ||
|
||
var count = 0 | ||
var done = function () { | ||
count++ | ||
if (count === 2) { | ||
callback() | ||
} | ||
} | ||
urls.forEach(function (url, index) { | ||
var arch = index === 0 ? 'ia32' : 'x64' | ||
console.log('Fetching windows %s lib from %s', arch, url) | ||
var nodeLib = path.join(process.env.HOME || process.env.USERPROFILE, '.node-gyp', | ||
process.version.slice(1), arch, 'node.lib') | ||
var parentDir = path.dirname(nodeLib) | ||
if (!fs.existsSync(parentDir)) { | ||
fs.mkdirSync(parentDir) | ||
} | ||
request(url).pipe(fs.createWriteStream(nodeLib)).on('finish', function () { | ||
if (io) { | ||
// copy node.lib to iojs.lib | ||
var iojsLib = path.join(parentDir, 'iojs.lib') | ||
fs.createReadStream(nodeLib).pipe(fs.createWriteStream(iojsLib)).on('finish', done) | ||
} else { | ||
done() | ||
} | ||
}) | ||
}) | ||
} | ||
install(argv, function (err) { | ||
if (err) console.error('Error: ' + err.message) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 () {} |
Oops, something went wrong.