内⽹环境怎么解决npm包的批量下载和往npm私有仓库批量上传
tgz依赖
背景:内⽹环境基于nexus搭建好了npm仓库,但是没有任何办法连接互联⽹。
痛点:前端项⽬依赖多且碎,怎么解决npm包批量下载和上传到私有仓库的问题?
解决⽅案:
基于npm view及递归算法命令获取依赖包列表或基于--package-lock-only⽣成pack-lock.json⽂件从中读取依赖包列表
基于npm pack下载tgz格式的依赖
基于npm publish上传依赖到内⽹仓库或基于nexus api上传。
⼀、在互联⽹环境下载
1.1 基于npm view及递归算法命令获取依赖包列表或基于--package-lock-only⽣成pack-lock.json⽂件从中读取依赖包列表;
1.2 遍历上⼀步的依赖列表,基于npm pack命令,逐个下载。
参考代码如下:
const shell = require('shelljs');
const JSON5 = require('json5');
const { exec } = require('child_process');
function download(fileNames = []) {
shell.cd('download');
let count = 0;
fileNames.forEach(fileName => {
// ho(`>>> 正在下载 ${fileName}...`);
const fileExec = (`npm pack ${fileName}`, { async: true, silent: true});
fileExec.stdout
.on('data', () => {
++count;
if (count === fileNames.length) {
shell.cd('..');
}
})
.on('err', () => {
++count
if (count === fileNames.length) {
shell.cd('..');
}
})
})
}
/*
* param packageNameOrWidthVersion,形如react、react@^16.13.0都可以,遵守npm规范即可
* */
function downloadNpm(packageNameOrWidthVersion) {
const nMap = new Map();
function getAllList(pkg) {
if (nMap.has(pkg) || !pkg) {
return
}
nMap.set(pkg, true);
// 寻依赖
// 寻依赖
const execLine = `npm view ${pkg} dependencies --json`;
const execResult = (execLine, { async: false, silent: true });
let deps;
try {
if (execResult.stdout) {
deps = JSON5.parse(execResult.stdout);
/*
* {"loose-envify": "^1.1.0", "object-assign": "^4.1.1", "prop-types": "^15.6.2"} 转换成
* ["loose-envify^1.1.0", "object-assign^4.1.1", "prop-types^15.6.2"]
*/
let depPackages = [];
if (Array.isArray(deps)) {
deps.forEach(dep => {
if (String.apply(dep) === '[object Object]') {
depPackages.push(...Object.keys(dep).map(d => dep[d].includes(' ') ? `${d}@'${dep[d]}'` : `${d}@${dep[d]}`));
}
})
} else if (String.apply(deps) === '[object Object]') {
depPackages = Object.keys(deps).map(dep => deps[dep].includes(' ') ? `${dep}@'${deps[dep]}'` : `${dep}@${deps[dep]}`);parse error怎么解决
}
depPackages.forEach(dep => {
getAllList(dep);
})
}
} catch (e) {
console.debug('getAllList ', pkg, '的dependencies下载报错:', e);
}
}
getAllList(packageNameOrWidthVersion);
download(Array.from(nMap.keys()));
}
function downloadByPackageJsonLockFile(depLockJsonFile = {}) {
const nMap = new Map();
const NotMap = new Map();
const downloadedDir = './download/downloaded'; // 每次下载的⽂件会放在download⾥,publish到仓库后,可以⼿动移动到download/downloaded⾥,⽅便下次避免重复下载    const downloadedArr = fs.readdirSync(downloadedDir);
function getAllList(depJson) {
if (depJson) {
Object.keys(depJson).forEach(dep => {
const depWithVersion = `${dep}@${depJson[dep].version}`;
let tgzFormat = `${dep}-${depJson[dep].version}.tgz`;
// eg: @babel/code-frame-7. -> babel-code-frame-7.
tgzFormat = dep.startsWith('@') ? tgzFormat.split('/').join('-').slice(1) : tgzFormat
if (!nMap.has(depWithVersion) && !downloadedArr.includes(tgzFormat)) {
nMap.set(depWithVersion, true);
getAllList(depJson[dep].dependencies);
} else if (downloadedArr.includes(tgzFormat) && !NotMap.has(tgzFormat)) {
NotMap.set(tgzFormat, true);
}
})
}
}
getAllList(depLockJsonFile.dependencies);
download(Array.from(nMap.keys()));
}
/*
* ⽅式⼀:通过具体npm包下载相关依赖
*/
// downloadNpm('node-nightly@^1.7.3');

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。