内网私仓全流程搭建记录(二)-npm私仓提交与拉取
1、npm私仓依赖下载及本地上传
方法一
1)使用Pycharm创建py文档,写入如下py代码:
import os
import re
import aiohttp
import asyncio
from urllib.parse import urlparse
from concurrent.futures import ThreadPoolExecutor
# 创建存储文件夹
download_folder = "sourcenpm"
os.makedirs(download_folder, exist_ok=True)
# 读取文件内容
with open("package-lock.json", "r") as file:
content = file.read()
# 使用正则表达式匹配resolved字段的值
pattern = r'"resolved":\s+"(https:\/\/[^"]+\.tgz)"'
matches = re.findall(pattern, content)
# 异步下载函数
async def download_file(session, url):
async with session.get(url) as response:
if response.status == 200:
parsed_url = urlparse(url)
filename = os.path.basename(parsed_url.path)
full_filename = os.path.join(download_folder, filename)
content = await response.read()
with open(full_filename, "wb") as output_file:
output_file.write(content)
print(f"文件 {filename} 下载成功")
else:
print(f"无法下载文件 {url}")
async def download_with_semaphore(semaphore, session, url):
async with semaphore:
await download_file(session, url)
async def main():
# 创建异步HTTP会话
async with aiohttp.ClientSession() as session:
semaphore = asyncio.Semaphore(8) # 限制并发数为8
tasks = []
for url in matches:
tasks.append(download_with_semaphore(semaphore, session, url))
await asyncio.gather(*tasks)
print("下载完成")
# 运行异步下载
if __name__ == "__main__":
asyncio.run(main())
2)将vue项目中的package-lock.json文件放到本py脚本目录下,执行该py脚本,package-lock.json中的依赖文件自动下载到新建的sourcenpm文件夹中,下载完成后将sourcenpm文件夹拷贝到内网中;
3)编写上传脚本npmimport.sh,win10环境下,使用powershell执行./npmimport.sh命令,代码如下:
find . -type f -name '*.tgz' | sed "s|^\./||" | xargs -I '{}' \
curl -u 'admin:123456' -X 'POST' -v 'http://127.0.0.1:19999/service/rest/v1/components?repository=npm-test' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'npm.asset=@{};type=application/x-compressed';
在Linux中执行./npmimport.sh -u admin -p 123456 -v http://127.0.0.1:19999/service/rest/v1/components?repository=npm-test命令,代码如下:
find . -type f -name '*.tgz' | sed "s|^\./||" | xargs -I '{}' \
curl -u "$USERNAME:$PASSWORD" \
-v ${REPO_URL} \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'npm.asset=@{};type=application/x-compressed';
方法二、
1)将本地编译后代码目录路径下已生成的node_modules拷贝到内网中,在node_modules文件夹中新建npm2nexus.sh文件,执行./npm2nexus.sh命令,npm2nexus.sh内容如下:
#!/bin/bash #通过本脚本批量上传本地node_modules中的npm依赖包到私仓nexus3内 #将本本脚本文件npm2nexus.sh放在本地node_modules文件夹中即可 file_name="package.json" # 要查找的文件名 search_path="D:\VueWorkspace\RuoYi-Vue\ruoyi-ui\node_modules" # 本地node_modules路径 result=$(find $search_path -type f -iname "$file_name") # 使用find命令查找文件 if [ ! -z "$result" ]; then # 如果结果不为空 while IFS= read -r file; do # 对于每个找到的文件 echo "File found at: $(dirname "$file")" # 输出文件所在的目录 # 这里可以添加其他操作或者调用函数处理该文件 filepath=$(dirname "$file") cd "$filepath" npm publish --registry 'http://127.0.0.1:19999/repository/npm01/' #nexus私仓地址 cd "$search_path" done <<< "$result" else echo "No files matching '$file_name' were found." # 没有找到符合条件的文件时的提示信息 fi
注:无论通过下载还是直接通过node_modules文件上传,都可能存在相关依赖包无法正常下载或上传等情况,需要区别对待,或单独补充。
原创帖,转载请注明出处及作者,标注严禁转载帖请勿转载,谢谢!