解析 Cargo.lock 添加版本连接符配置

This commit is contained in:
2024-08-23 03:49:39 +08:00
parent 5f1f4404f6
commit f75f446a8e

View File

@@ -31,9 +31,10 @@ const parseItem = (str) => {
} }
/** /**
* @param {string} str * @param {string} str 依赖文本
* @param {string=} concatenation 依赖项版本连接符
*/ */
const parse = (str) => { const parse = (str, concatenation = '@') => {
const pkgSeparator = '[[package]]'; const pkgSeparator = '[[package]]';
const idx = str.indexOf(pkgSeparator); const idx = str.indexOf(pkgSeparator);
if (idx >= 0) { if (idx >= 0) {
@@ -41,7 +42,7 @@ const parse = (str) => {
} }
const arr = str.split(pkgSeparator).filter(Boolean); const arr = str.split(pkgSeparator).filter(Boolean);
const list = arr.map(parseItem).filter(Boolean).map((item) => item.join("-")); const list = arr.map(parseItem).filter(Boolean).map((item) => item.join(concatenation));
const result = list.join('\n'); const result = list.join('\n');
return result return result
} }
@@ -90,10 +91,11 @@ const fetchCragoLock = async (repo, commit) => {
const commitPrefix = "-c"; const commitPrefix = "-c";
const outputPrefix = "-o"; const outputPrefix = "-o";
const repoPrefix = "-r"; const repoPrefix = "-r";
const concatPrefix = "-vc"
const showUsage = () => { const showUsage = () => {
const { basename } = require('node:path'); const { basename } = require('node:path');
const usage = `Usage: ${basename(__filename)} ${repoPrefix} <github repo> ${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] [${concatPrefix} version-concatenation]`;
console.error(usage); console.error(usage);
} }
@@ -127,7 +129,14 @@ const parseCmdArgs = () => {
return; return;
} }
const cmdArgs = { repo, commit, output: '' }; /** @type {string | undefined} */
let versionConcatenation;
const versionConcatenationIndex = args.indexOf(concatPrefix);
if (versionConcatenationIndex !== -1) {
versionConcatenation = args.at(versionConcatenationIndex + 1);
}
const cmdArgs = { repo, commit, output: '', versionConcatenation };
const outputIndex = args.indexOf(outputPrefix); const outputIndex = args.indexOf(outputPrefix);
if (outputIndex === -1) { if (outputIndex === -1) {
return cmdArgs; return cmdArgs;
@@ -146,9 +155,10 @@ const doTask = async () => {
const output = cmdArgs?.output; const output = cmdArgs?.output;
if (!repo || !commit) return; if (!repo || !commit) return;
try { try {
const content = await fetchCragoLock(repo, commit); const content = await fetchCragoLock(repo, commit);
const result = parse(content); const result = parse(content, cmdArgs.versionConcatenation);
if (!output) { if (!output) {
console.log(result); console.log(result);
return return