From 95e60269504adc33cbd15aa819192fca3b46767a Mon Sep 17 00:00:00 2001 From: Jeffery Date: Thu, 17 Sep 2026 08:23:26 +0000 Subject: [PATCH] =?UTF-8?q?refactor(lib):=20=E9=96=8B=E5=95=9F=E7=9B=AE?= =?UTF-8?q?=E6=A8=99=E5=B0=88=E6=A1=88=20git=20repo=20=E7=9A=84=E9=82=A3?= =?UTF-8?q?=E5=B9=BE=E8=A1=8C=E6=94=B6=E9=80=B2=20lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 「路徑不是 repo 就報 NOT_A_GIT_REPO」加上「把 cwd 綁進 runGit」原本在 branch-prep 與 commit-split 各寫一份。錯誤碼要一致,而這件事寫第三遍就該收起來了。 --- scripts/branch-prep.js | 10 ++-------- scripts/lib.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/scripts/branch-prep.js b/scripts/branch-prep.js index bee193a..398c756 100644 --- a/scripts/branch-prep.js +++ b/scripts/branch-prep.js @@ -23,9 +23,7 @@ * node scripts/branch-prep.js --source <來源分支> --slug <英文-kebab> * [--type feat] [--path <目標專案>] [--dry-run] */ -import { existsSync } from 'node:fs'; -import { join } from 'node:path'; -import { ScriptError, main, parseFlags, runGit } from './lib.js'; +import { ScriptError, main, openGitRepo, parseFlags } from './lib.js'; /** 需求描述的長度上限。超過就換一個短的說法,不要靠截斷。 */ const SLUG_MAX = 40; @@ -43,11 +41,7 @@ main(async () => { const source = flags.source; const branch = buildBranchName(source, flags.slug, flags.type); - if (!existsSync(join(path, '.git'))) { - throw new ScriptError('NOT_A_GIT_REPO', `${path} 不是 git repo;請用 --path 指向目標專案的根目錄`); - } - - const git = (...args) => runGit(args, { cwd: path }); + const git = openGitRepo(path); checkClean(git, path); const hasOrigin = git('remote').split('\n').includes('origin'); diff --git a/scripts/lib.js b/scripts/lib.js index 9380f47..dca6f77 100644 --- a/scripts/lib.js +++ b/scripts/lib.js @@ -379,6 +379,22 @@ export function runGit(args, { cwd } = {}) { } } +/** + * 開一個目標專案的 git repo,回傳綁在它身上的執行器。 + * + * 碰目標專案 git 的腳本都從這裡進去:路徑不是 repo 時的錯誤碼要一致, + * 而「把 cwd 綁進 runGit」這件事寫第三遍就該收起來了。 + * + * @param {string} path 目標專案的根目錄 + * @returns {(...args: string[]) => string} 綁定 cwd 的 git 執行器 + */ +export function openGitRepo(path) { + if (!existsSync(join(path, '.git'))) { + throw new ScriptError('NOT_A_GIT_REPO', `${path} 不是 git repo;請用 --path 指向目標專案的根目錄`); + } + return (...args) => runGit(args, { cwd: path }); +} + // ── 四層前置檢查 ─────────────────────────────────────────────────── /**