1 Star 0 Fork 3

hliu/nvmw

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
get_npm.js 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
hliu 提交于 2020-02-09 22:00 . window nvmw
var util = require('util'),
fs = require('fs'),
path = require('path'),
wget = require('./wget');
var NPM_PKG_JSON_URL = 'https://raw.githubusercontent.com/%s/%s/deps/npm/package.json';
// https://github.com/npm/npm/tags
var NVMW_NPM_MIRROR = process.env.NVMW_NPM_MIRROR || 'http://npm.taobao.org/mirrors/npm';
var BASE_URL = NVMW_NPM_MIRROR + '/v%s.zip';
var targetDir = process.argv[2];
var versions = process.argv[3].split('/');
var binType = versions[0];
var binVersion = versions[1];
if (binType === 'iojs') {
// detect npm version from https://iojs.org/dist/index.json
var NVMW_IOJS_ORG_MIRROR = process.env.NVMW_IOJS_ORG_MIRROR || 'https://iojs.org/dist';
var pkgUri = NVMW_IOJS_ORG_MIRROR + '/index.json';
wget(pkgUri, function (filename, content) {
if (filename === null) {
return noNpmAndExit();
}
var npmVersion;
var items = JSON.parse(content);
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (!npmVersion) {
// make sure has a npm version
npmVersion = item.npm;
}
if (item.version === binVersion && item.npm) {
npmVersion = item.npm;
break;
}
}
if (!npmVersion) {
return noNpmAndExit();
}
downloadNpmZip(npmVersion);
});
} else {
var pkgUri = "https://npm.taobao.org/mirrors/node/index.json";
wget(pkgUri, function (filename, pkg) {
if (filename === null) {
return noNpmAndExit();
}
var _pkg = JSON.parse(pkg);
for(var i = 0,n=_pkg.length;i<n;i++){
var obj = _pkg[i];
if(obj.version == binVersion){
downloadNpmZip(obj.npm);
}
}
});
}
function noNpmAndExit() {
console.error('%s %s does not include npm', binType, binVersion);
process.exit(1);
}
function downloadNpmZip(version) {
var uri = util.format(BASE_URL, version);
wget(uri, function (filename, data) {
if (filename === null) {
console.error('Can\'t get npm: ' + uri);
process.exit(1);
}
fs.writeFile(path.join(targetDir, 'npm.zip'), data, function (err) {
if (err) {
return console.error(err.message);
}
console.log('Download npm %s is done', version);
});
});
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zucchliu_liuhong/nvmw.git
git@gitee.com:zucchliu_liuhong/nvmw.git
zucchliu_liuhong
nvmw
nvmw
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385