You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
#!/usr/bin/env python3 |
|
# -*- coding: utf-8 -*- |
|
''' |
|
Author: Alvis Zhao |
|
Date: 2020-08-04 12:15:30 |
|
LastEditTime: 2020-08-14 11:43:39 |
|
Description: 解析package-lock.json下载npm缓存包 |
|
''' |
|
|
|
import os |
|
import json |
|
import urllib.request |
|
|
|
dependencyDict = {} |
|
|
|
def analyzeDependency(dependencies): |
|
for key in dependencies: |
|
dependenciesInfo = dependencies[key] |
|
|
|
if "resolved" in dependenciesInfo: |
|
url = dependenciesInfo["resolved"] |
|
version = dependenciesInfo["version"] |
|
# 存入字典去重 |
|
dependencyDict[key + "-" + version] = url |
|
|
|
if "dependencies" in dependenciesInfo: |
|
childDependencies = dependenciesInfo["dependencies"] |
|
analyzeDependency(childDependencies) |
|
|
|
output = "offline-packages" |
|
|
|
if not os.path.exists(output): |
|
os.mkdir(output) |
|
|
|
f = open('package-lock.json', 'r') |
|
content = f.read() |
|
contentDict = json.loads(content) |
|
dependencies = contentDict["dependencies"] |
|
# 分析并保存依赖 |
|
analyzeDependency(dependencies) |
|
|
|
# 按照key排序下载 |
|
sortedKeys = sorted(dependencyDict) |
|
for key in sortedKeys: |
|
url = dependencyDict[key] |
|
params = url.split("?")[0].split("/") |
|
filename = params[len(params) -1] |
|
outputFile = output + os.sep + filename |
|
print("downloading " + " " + key + " " + url) |
|
if not os.path.isfile(outputFile): |
|
urllib.request.urlretrieve(url, outputFile) |
|
|
|
f.close()
|
|
|