refactor: dedup and drop redundant existence checks#18
Merged
Conversation
…eview)
Findings from a cross-cutting reuse + quality + efficiency review:
- Extract `formatScaffoldReport(name, template, result)` into scaffold.ts
and reuse it in both the MCP tool handler (src/index.ts) and the CLI
(src/cli.ts). Two identical 10-line output blocks collapsed to one.
- Import `SAFE_NAME` from scaffold.ts in src/index.ts instead of
redefining the same regex inline in the Zod schema. Single source of
truth for project-name validation; no drift if the rule tightens.
- Drop three redundant `existsSync` guards:
- Before `readdirSync(finalDest)` — wrap in try/catch for ENOENT so
the readdirSync itself tells us whether the dir exists, removing
a double syscall.
- Before `rm(finalDest, { force: true })` — `force: true` already
tolerates absence.
- Before `readFileSync` in `updatePyproject` — the existing try/catch
already handles ENOENT.
Build + 36/36 tests still pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Post-/simplify review cleanup.
Findings addressed
Reuse (HIGH):
formatScaffoldReport→ reused by MCP handler + CLI (two 10-line duplicate output blocks → one)SAFE_NAMEin index.ts from scaffold.ts (was inline regex duplicate)Efficiency (MEDIUM):
existsSync(finalDest)beforereaddirSync(finalDest)— wrap readdirSync in try/catch for ENOENT (single syscall path)existsSync(finalDest)beforerm(finalDest, { force: true })—force: truehandles absenceexistsSync(path)inupdatePyproject— existing try/catch already handles ENOENTFindings declined (noted as not worth fixing)
format()helper in log.ts — Reviewer flagged as "unnecessary for single caller". Actually has 4 callers (debug/info/warn/error), so inlining would be anti-DRY. Skipped.parseArgsoptions object IS the single source of truth; extracting aFLAGS = {...}const would add indirection for no benefit with only 6 flags. Skipped.ScaffoldOptionsparam "sprawl" — 8 fields all narrowly scoped; splitting required/optional would churn callers. Skipped.sleep()vsAbortSignal.timeout()— saves 2 lines but makes retry backoff less explicit. Skipped.walkFilesrecursion vsreaddirSync({ recursive: true })— ~10-20% faster for deep trees, negligible for typical scaffolds. Skipped.fakeFetch— test-only, bounded per run. Skipped.readVersionreads pkg.json per call —--versionruns at most once. Skipped.Test plan
npm run buildpassesnpm testpasses (36/36)