refactor(參數): parseIndex 抽到 lib,四支腳本共用
同一段 /^[1-9]\d*$/ 與 BAD_INDEX 已經抄了四份。lib 本來就放著同性質的 parseRepo, 這一支該待在它旁邊。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
|||||||
main,
|
main,
|
||||||
pages,
|
pages,
|
||||||
parseFlags,
|
parseFlags,
|
||||||
|
parseIndex,
|
||||||
parseRepo,
|
parseRepo,
|
||||||
preflight,
|
preflight,
|
||||||
resolveLogin,
|
resolveLogin,
|
||||||
@@ -70,12 +71,6 @@ main(async () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
function parseIndex(value) {
|
|
||||||
if (!/^[1-9]\d*$/.test(value)) {
|
|
||||||
throw new ScriptError('BAD_INDEX', `--index 需為正整數,收到的是 ${value}`);
|
|
||||||
}
|
|
||||||
return Number(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchIssue(login, repo, index, path) {
|
async function fetchIssue(login, repo, index, path) {
|
||||||
const response = await giteaRequest(login, 'GET', path);
|
const response = await giteaRequest(login, 'GET', path);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
giteaRequest,
|
giteaRequest,
|
||||||
main,
|
main,
|
||||||
parseFlags,
|
parseFlags,
|
||||||
|
parseIndex,
|
||||||
parseRepo,
|
parseRepo,
|
||||||
preflight,
|
preflight,
|
||||||
resolveLogin,
|
resolveLogin,
|
||||||
@@ -82,12 +83,6 @@ main(async () => {
|
|||||||
return { repo, index, added, skipped };
|
return { repo, index, added, skipped };
|
||||||
});
|
});
|
||||||
|
|
||||||
function parseIndex(value, what) {
|
|
||||||
if (!/^[1-9]\d*$/.test(String(value))) {
|
|
||||||
throw new ScriptError('BAD_INDEX', `${what} 需為正整數,收到的是 ${value}`);
|
|
||||||
}
|
|
||||||
return Number(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析逗號分隔的議題編號。
|
* 解析逗號分隔的議題編號。
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
giteaRequest,
|
giteaRequest,
|
||||||
main,
|
main,
|
||||||
parseFlags,
|
parseFlags,
|
||||||
|
parseIndex,
|
||||||
parseRepo,
|
parseRepo,
|
||||||
preflight,
|
preflight,
|
||||||
resolveLogin,
|
resolveLogin,
|
||||||
@@ -76,12 +77,6 @@ main(async () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
function parseIndex(value) {
|
|
||||||
if (!/^[1-9]\d*$/.test(value)) {
|
|
||||||
throw new ScriptError('BAD_INDEX', `--index 需為正整數,收到的是 ${value}`);
|
|
||||||
}
|
|
||||||
return Number(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseDueDate(value) {
|
function parseDueDate(value) {
|
||||||
if (value === undefined) return null;
|
if (value === undefined) return null;
|
||||||
|
|||||||
@@ -88,6 +88,19 @@ export function parseFlags(argv, spec = {}) {
|
|||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 驗證議題編號。四支腳本都要做這件事,錯誤碼也該一致。
|
||||||
|
* @param {string|number} value
|
||||||
|
* @param {string} what 出現在錯誤訊息裡的欄位名,例如 '--index'
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
export function parseIndex(value, what = '--index') {
|
||||||
|
if (!/^[1-9]\d*$/.test(String(value))) {
|
||||||
|
throw new ScriptError('BAD_INDEX', `${what} 需為正整數,收到的是 ${value}`);
|
||||||
|
}
|
||||||
|
return Number(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 驗證並正規化 owner/name 形式的 repo。
|
* 驗證並正規化 owner/name 形式的 repo。
|
||||||
* @param {string} value
|
* @param {string} value
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
giteaRequest,
|
giteaRequest,
|
||||||
main,
|
main,
|
||||||
parseFlags,
|
parseFlags,
|
||||||
|
parseIndex,
|
||||||
parseRepo,
|
parseRepo,
|
||||||
preflight,
|
preflight,
|
||||||
resolveLogin,
|
resolveLogin,
|
||||||
@@ -61,12 +62,6 @@ main(async () => {
|
|||||||
return { repo, index, project, alreadyThere: false };
|
return { repo, index, project, alreadyThere: false };
|
||||||
});
|
});
|
||||||
|
|
||||||
function parseIndex(value) {
|
|
||||||
if (!/^[1-9]\d*$/.test(value)) {
|
|
||||||
throw new ScriptError('BAD_INDEX', `--index 需為正整數,收到的是 ${value}`);
|
|
||||||
}
|
|
||||||
return Number(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 把 `--project` 解析成 {id, title}。
|
* 把 `--project` 解析成 {id, title}。
|
||||||
|
|||||||
Reference in New Issue
Block a user