Repository navigation
Expand file tree
/
Copy pathdownloadFile.js
More file actions
122 lines (105 loc) · 3.23 KB
/
Copy pathdownloadFile.js
File metadata and controls
122 lines (105 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// src/charts/visit/const/index.js
const fs = require("fs");
const path = require("path");
const https = require("https");
const http = require("http");
const { URL } = require("url");
// 配置文件路径
const configFilePath = path.join(__dirname, "downloadFile.json");
// 第3个参数 环境
let branch = process.argv[2] || "main"; // main v6.4
// 除了固定的分支以外,其余都尽量使用main分支
if (!/^v[0-9]+\.[0-9]+$/.test(branch)) {
branch = "main";
}
// 读取配置文件
const readConfig = () => {
const configData = fs.readFileSync(configFilePath);
return JSON.parse(configData);
};
const checkFileExistsSync = (filePath) => {
try {
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
};
// 删除文件的函数
const deleteFile = (filePath) => {
if (checkFileExistsSync(filePath)) {
fs.unlink(filePath, (err) => {
if (err) {
console.error(`删除文件失败: ${err.message}`);
return;
}
console.log(`文件已成功删除: ${filePath}`);
});
}
};
// 下载文件
const downloadFile = (fileUrl, outputPath, cb) => {
fileUrl = fileUrl.replace(/\<BRANCH\>/, branch);
const url = new URL(fileUrl);
const h = fileUrl.startsWith("https") ? https : http;
// 目标目录可能不存在(如 07-configuration 在提交态无文件),先递归创建
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
const file = fs.createWriteStream(outputPath);
h.get(url, (response) => {
response.pipe(file);
file.on("finish", () => {
file.close();
console.log(`文件已下载到: ${outputPath}`);
// 移除readme文件
const o = outputPath.split("/");
o.pop();
o.push("README.md");
deleteFile(o.join("/"));
cb();
});
}).on("error", (err) => {
// 删除下载失败留下的空文件:文件缺失会让后续构建死链检测报错,
// 而不是把空页面静默发布;unlink 在 Node 10+ 必须传回调
fs.unlink(outputPath, () => {});
console.error(`下载失败: ${err.message}`);
process.exitCode = 1;
});
};
function insertMetaToMarkdown(filePath, metaInfo) {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}
// 将 meta 信息插入到文件开头
const updatedContent = metaInfo + data;
// 写入更新后的内容到文件
fs.writeFile(filePath, updatedContent, "utf8", (err) => {
if (err) {
console.error("Error writing file:", err);
return;
}
console.log("Meta information inserted successfully!");
});
});
}
function jsonToMeta(jsonData) {
let metaString = "---\n";
for (const [key, value] of Object.entries(jsonData)) {
metaString += `${key}: ${value}\n`;
}
metaString += "---\n";
return metaString;
}
// 主函数
const main = () => {
const configs = readConfig();
configs.forEach((config) => {
const { url, output, meta } = config;
downloadFile(url, output, () => {
insertMetaToMarkdown(output, jsonToMeta(meta));
});
});
};
// 运行主函数
main();