(url: string, forceUpdate = false)
| 63 | } |
| 64 | |
| 65 | async function loadConfigResource(url: string, forceUpdate = false) { |
| 66 | const parseAndSetConfig = (content: string) => { |
| 67 | const fullConfig = JSON.parse(content); |
| 68 | if (!fullConfig.resources) { |
| 69 | throw new Error("配置文件格式错误,缺少resources 字段"); |
| 70 | } |
| 71 | const regex = |
| 72 | /^https:\/\/raw\.githubusercontent\.com\/([^\/]+)\/([^\/]+)\/refs\/heads\/([^\/]+)\/(.+)$/; |
| 73 | const match = url.match(regex); |
| 74 | if (!match) throw new Error("URL格式不正确"); |
| 75 | const [, repo_owner, repo_name, branch] = match; |
| 76 | const base_url_template = |
| 77 | "https://github.com/${repo_owner}/${repo_name}/raw/${branch}"; |
| 78 | if (!repo_owner || !repo_name || !branch) { |
| 79 | throw new Error("URL格式不正确, 缺少 repo_owner, repo_name, branch等信息"); |
| 80 | } |
| 81 | |
| 82 | resourceBaseUrl = base_url_template |
| 83 | .replace("${repo_owner}", repo_owner) |
| 84 | .replace("${repo_name}", repo_name) |
| 85 | .replace("${branch}", branch); |
| 86 | |
| 87 | config = fullConfig.resources; |
| 88 | console.log( |
| 89 | `配置加载成功,当前分支: ${branch}, 资源基础URL: ${resourceBaseUrl}` |
| 90 | ); |
| 91 | }; |
| 92 | |
| 93 | if (!forceUpdate && Object.keys(config || {}).length > 0) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | const response = await axios.get(url, { responseType: "arraybuffer" }); |
| 98 | const content = Buffer.from(response.data).toString("utf-8"); |
| 99 | parseAndSetConfig(content); |
| 100 | } |
| 101 | |
| 102 | loadConfigResource(baseConfigURL).catch(() => { |
| 103 | console.log("初始配置加载失败,将在首次使用时重试"); |
no test coverage detected