解析 Cargo.lock 文件支持不同的 github 仓库

This commit is contained in:
2024-02-05 01:07:14 +08:00
parent a6231247bd
commit 45cb74394e

View File

@@ -50,12 +50,15 @@ const parse = (str) => {
/** /**
* @param {string} commit 提交 Hash 值 * @param {string} commit 提交 Hash 值
* @param {string} repo github 仓库名
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
const fetchCragoLock = async (commit) => { const fetchCragoLock = async (repo, commit) => {
const url = `https://raw.githubusercontent.com/shadowsocks/shadowsocks-rust/${commit}/Cargo.lock`; const path = require("node:path");
const pathName = path.join(repo, commit, "Cargo.lock");
const url = new URL(pathName, "https://raw.githubusercontent.com/").toString();
if (global.fetch) { if (global.fetch) {
const resp = await fetch(url); const resp = await global.fetch(url, { method: "GET", mode: "cors" });
if (resp.status !== 200) { if (resp.status !== 200) {
const text = await resp.text(); const text = await resp.text();
throw new Error(text); throw new Error(text);
@@ -86,17 +89,29 @@ const fetchCragoLock = async (commit) => {
const commitPrefix = "-c"; const commitPrefix = "-c";
const outputPrefix = "-o"; const outputPrefix = "-o";
const repoPrefix = "-r";
const showUsage = () => { const showUsage = () => {
const { basename } = require('node:path'); const { basename } = require('node:path');
const usage = `Usage: ${basename(__filename)} ${commitPrefix} <commit_full_hash | branch | tag> [${outputPrefix} output_file_path]`; const usage = `Usage: ${basename(__filename)} ${repoPrefix} <github repo> ${commitPrefix} <commit_full_hash | branch | tag> [${outputPrefix} output_file_path]`;
console.error(usage); console.error(usage);
} }
const parseCmdArgs = () => { const parseCmdArgs = () => {
const argStartIndex = process.argv.indexOf(__filename) + 1; const argStartIndex = process.argv.indexOf(__filename) + 1;
const args = process.argv.slice(argStartIndex); const args = process.argv.slice(argStartIndex);
if (args.length !== 2 && args.length !== 4) { if (args.length !== 4 && args.length !== 6) {
showUsage();
return;
}
const repoIndex = args.indexOf(repoPrefix);
if (repoIndex === -1) {
showUsage();
return;
}
const repo = args.at(repoIndex + 1);
if (!repo) {
showUsage(); showUsage();
return; return;
} }
@@ -112,7 +127,7 @@ const parseCmdArgs = () => {
return; return;
} }
const cmdArgs = { commit, output: '' }; const cmdArgs = { repo, commit, output: '' };
const outputIndex = args.indexOf(outputPrefix); const outputIndex = args.indexOf(outputPrefix);
if (outputIndex === -1) { if (outputIndex === -1) {
return cmdArgs; return cmdArgs;
@@ -126,12 +141,13 @@ const parseCmdArgs = () => {
const doTask = async () => { const doTask = async () => {
const cmdArgs = parseCmdArgs(); const cmdArgs = parseCmdArgs();
const repo = cmdArgs?.repo;
const commit = cmdArgs?.commit; const commit = cmdArgs?.commit;
const output = cmdArgs?.output; const output = cmdArgs?.output;
if (!commit) return; if (!repo || !commit) return;
try { try {
const content = await fetchCragoLock(cmdArgs.commit); const content = await fetchCragoLock(repo, commit);
const result = parse(content); const result = parse(content);
if (!output) { if (!output) {
console.log(result); console.log(result);