Skip to content

Commit

Permalink
feat: Auto set China mirror for prebuild binary packages
Browse files Browse the repository at this point in the history
closes #45
  • Loading branch information
fengmk2 committed Mar 8, 2016
1 parent e6379c1 commit 4d75ee3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ const env = {
npm_config_registry: registry,
};

let binaryMirrors = {};

if (inChina) {
for (const key in config.chineseMirrorEnv) {
env[key] = config.chineseMirrorEnv[key];
}

binaryMirrors = require('../lib/prebuild_mirror_config_china.json');
}

// npm cli will auto set options to npm_xx env.
Expand All @@ -146,6 +150,7 @@ co(function*() {
production,
cacheDir,
env,
binaryMirrors,
};
config.strictSSL = getStrictSSL();
// -g install to npm's global prefix
Expand Down
19 changes: 17 additions & 2 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,25 @@ function* download(pkg, options) {
yield checkShasumAndUngzip(ungzipDir, stream, pkg);
yield fs.writeFile(donefile, Date());

yield utils.addMetaToJSONFile(path.join(ungzipDir, 'package.json'), {
const pkgMeta = {
_from: `${pkg.name}@${pkg.version}`,
_resolved: pkg.dist.tarball,
});
};
const binaryMirror = options.binaryMirrors[pkg.name];
if (binaryMirror && pkg.scripts && pkg.scripts.install) {
// leveldown and sqlite3
if (/prebuild \-\-install/.test(pkg.scripts.install) ||
/prebuild \-\-download/.test(pkg.scripts.install) ||
/node\-pre\-gyp install/.test(pkg.scripts.install)) {
const newBinary = pkg.binary || {};
for (const key in binaryMirror) {
newBinary[key] = binaryMirror[key];
}
pkgMeta.binary = newBinary;
options.console.info(`[${pkg.name}@${pkg.version}] download from binary mirror: %j`, newBinary);
}
}
yield utils.addMetaToJSONFile(path.join(ungzipDir, 'package.json'), pkgMeta);

options.cache[key].done = true;
options.events.emit(key);
Expand Down
2 changes: 2 additions & 0 deletions lib/local_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const dependencies = require('./dependencies');
* - {Object} [env] - postinstall and preinstall scripts custom env.
* - {String} [cacheDir] - tarball cache store dir, default is `$HOME/.npminstall_tarball`.
* if `production` mode enable, `cacheDir` will be disable.
* - {Object} [binaryMirrors] - binary mirror config, default is `{}`
*/
module.exports = function*(options) {
options.events = new EventEmitter();
Expand Down Expand Up @@ -84,6 +85,7 @@ module.exports = function*(options) {
options.remotePackages = 0;
options.registryPackages = 0;
options.gitPackages = 0;
options.binaryMirrors = {};
if (options.production) {
options.cacheDir = '';
} else {
Expand Down
41 changes: 41 additions & 0 deletions lib/prebuild_mirror_config_china.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"leveldown": {
"host": "https://npm.taobao.org/mirrors/leveldown/v{version}"
},
"leveldown-hyper": {
"host": "https://npm.taobao.org/mirrors/leveldown-hyper/v{version}"
},
"mknod": {
"host": "https://npm.taobao.org/mirrors/mknod/v{version}"
},
"couchbase": {
"host": "https://npm.taobao.org/mirrors/couchbase/v{version}"
},
"sodium-prebuilt": {
"host": "https://npm.taobao.org/mirrors/sodium-prebuilt/v{version}"
},
"utp-native": {
"host": "https://npm.taobao.org/mirrors/utp-native/v{version}"
},
"node-tk5": {
"host": "https://npm.taobao.org/mirrors/node-tk5/v{version}"
},
"fuse-bindings": {
"host": "https://npm.taobao.org/mirrors/fuse-bindings/v{version}"
},
"zmq-prebuilt": {
"host": "https://npm.taobao.org/mirrors/zmq-prebuilt/v{version}"
},
"gl": {
"host": "https://npm.taobao.org/mirrors/gl/v{version}"
},
"hackrf": {
"host": "https://npm.taobao.org/mirrors/hackrf/v{version}"
},
"rabin": {
"host": "https://npm.taobao.org/mirrors/rabin/v{version}"
},
"sqlite3": {
"host": "https://npm.taobao.org/mirrors"
}
}

0 comments on commit 4d75ee3

Please sign in to comment.