迁移npm包到nexus

背景:外包项目完工后,需要搭建客户开发环境,因此需要将对应的npm开发包迁移到客户nexus

步骤:

1.先从前端工程中的yarn.lock文件中获取组件列表

2.使用python脚本下载组件到本地

3.使用pythons脚本上传npm包到nexus仓库

第2不脚本如下:

python 复制代码
# -*- coding: utf-8 -*-
import os
import requests
import re
# 设置下载文件保存目录
download_dir = "C:\\Users\\onlyo\\Desktop\\LJ\\npmdowlod\\pkg"

# 从文件中读取 URL 列表
url_file = "C:\Users\\onlyo\\Desktop\\LJ\\npmdowlod\\url.txt"

# 创建下载目录(如果不存在)
#os.makedirs(download_dir, exist_ok=True)

# 打开 URL 列表文件
with open(url_file, "r") as file:
    print("Hello, World!1")
    # 逐行读取 URL
    for line in file:
        print(line)
        # 提取 URL 链接部分
        #url = line.strip().split()[1].strip('"')
        url = re.search(r'"(https?://.*?)"', line).group(1)
        print("读取到的url为:"+str(url))
        # 检查 URL 是否以 "https://" 开头
        if url.startswith("http://"):
            # 提取文件名
            filename = url.rsplit("/", 1)[-1]
            #last_slash_index = url.rfind("/")
           # hash_index = url.rfind("\"")

            # 提取子字符串
            #filename = url[last_slash_index + 1 : hash_index]
            # 下载文件
            print("下载文件:", filename)
            response = requests.get(url)
            file_path = os.path.join(download_dir, filename)
            with open(file_path, "wb") as output_file:
                output_file.write(response.content)

第三步脚本如下:

python 复制代码
# -*- coding: utf-8 -*-
import os  
import requests  
import glob  
# 远程Nexus仓库的URL和认证信息  
nexus_url = 'http://192.168.2.230:8081/service/rest/v1/components'  
headers = {  
    'accept': 'application/json',  
    'Content-Type': 'multipart/form-data'  
}  
auth = ('admin', 'admin')  # 替换为你的Nexus仓库的用户名和密码  
  
# 本地npm包的路径和要上传的远程仓库名  
local_path = 'D:\\lj2\\'  # 替换为包含.tgz文件的本地文件夹路径  
remote_repo = 'npm-imbr-hosted'  # 替换为要上传到的远程仓库名称  
  
# 遍历本地文件夹中的.tgz文件  
for tgz_file in glob.glob(os.path.join(local_path, '*.tgz')): 
    print('开始上传'+tgz_file)
    auth = ('admin', 'admin')  # 替换为你的Nexus仓库的用户名和密码    
    url = 'http://192.168.2.230:8081/service/rest/v1/components?repository=npm-imbr-hosted'  
    files = {'file': open(tgz_file, 'rb')}  # 这里替换为你的文件路径  
    response = requests.post(url,auth=auth, files=files)  
    print('完成')
    print(response.text)  # 打印服务器响应
相关推荐
We་ct2 小时前
LeetCode 77. 组合:DFS回溯+剪枝,高效求解组合问题
开发语言·前端·算法·leetcode·typescript·深度优先·剪枝
KerwinChou_CN2 小时前
什么是流式输出,后端怎么生成,前端怎么渲染
前端
爱上妖精的尾巴2 小时前
8-20 WPS JS宏 正则表达式-懒惰匹配
服务器·前端·javascript
网络点点滴2 小时前
组件通信props方式
前端·javascript·vue.js
二十雨辰2 小时前
[小结]-线上Bug监控
前端·bug
前端技术2 小时前
【鸿蒙实战】从零打造智能物联网家居控制系统:HarmonyOS Next分布式能力的完美诠释
java·前端·人工智能·分布式·物联网·前端框架·harmonyos
CHU7290352 小时前
指尖践行环保——旧衣服回收小程序前端功能玩法详解
前端·小程序
LawrenceLan2 小时前
38.Flutter 零基础入门(三十八):网络请求实战 http、dio —— 获取列表与刷新 UI
开发语言·前端·flutter·dart
csdn_aspnet3 小时前
Asp.Net Core 10.0 中的 Blazor 增强功能
前端·后端·asp.net·blazor·.net10
SuperEugene3 小时前
Excel 上传解析 + 导出实战:Vue+xlsx 避坑指南|Vue生态精选
前端·javascript·vue.js·excel·xlsx·vxetable