Skip to content

feat(shared): add ensure-directory utility#1858

Open
iyoda wants to merge 7 commits intocode-yeongyu:devfrom
iyoda:refactor/pr7-add-ensure-directory
Open

feat(shared): add ensure-directory utility#1858
iyoda wants to merge 7 commits intocode-yeongyu:devfrom
iyoda:refactor/pr7-add-ensure-directory

Conversation

@iyoda
Copy link
Contributor

@iyoda iyoda commented Feb 15, 2026

Summary

Add an ensureDirectory utility that wraps the common mkdirSync(path, { recursive: true }) pattern into a semantic, single-purpose function.

Changes

  • New: src/shared/ensure-directory.ts (9 lines) — ensureDirectory(dirPath: string)
  • New: src/shared/ensure-directory.test.ts (86 lines) — 5 tests covering creation, idempotency, nested dirs

API

ensureDirectory(dirPath: string): void  // Creates dir recursively if it doesn't exist

Verification

  • bun run typecheck
  • bun test ✅ (2693 pass, 0 fail)
  • bun run build

PR Stack

This is PR 7 of 9 in the code deduplication refactor series. Depends on #1857. This PR adds the utility; the next PR (8) applies it to 13 production files.

# PR Description
0 #1852 Delete dead code in modules/
2 #1853 Consolidate formatDuration (4→1)
3 #1854 Consolidate getMessageDir (7→1)
4 #1855 Unify fuzzyMatchModel
5 #1856 Add json-cache utility
6 #1857 Apply json-cache to consumers
7 This PR Add ensure-directory utility
8 (next) Apply ensure-directory to production
9 Consolidate ToolContextWithMetadata

Summary by cubic

Add ensureDirectory and a json-cache utility to simplify and harden file I/O. Consolidates shared helpers and removes dead modules to reduce duplication and stabilize storage code.

  • New Features

    • Added shared ensureDirectory(dirPath).
    • Added json-cache helpers: readJsonFile(), writeJsonFile(path, data, { ensureDir }).
  • Refactors

    • Applied json-cache to connected-providers cache and hook storages; directory creation via writeJsonFile({ ensureDir: true }).
    • Consolidated getMessageDir to shared/session-utils and formatDuration to tools/background-task/time-format; updated imports.
    • Unified fuzzyMatchModel and normalizeModelName under shared/model-name-matcher; re-exported from model-availability for compatibility.
    • Deleted dead background-task modules in tools/background-task/modules/.

Written for commit 07d2644. Summary will update on new commits.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 44 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Large refactor with 1699 lines changed; despite no issues found by AI, cannot be 100% sure no regressions occurred.

@iyoda iyoda force-pushed the refactor/pr7-add-ensure-directory branch from 71a3179 to c87c995 Compare February 15, 2026 08:52
iyoda and others added 5 commits February 15, 2026 17:55
- Make model-name-matcher.ts the canonical source for fuzzyMatchModel and normalizeModelName
- Export normalizeModelName from model-name-matcher.ts (was internal)
- Remove duplicate implementations from model-availability.ts
- Add re-exports in model-availability.ts for backward compatibility
- All existing consumers continue to work via their import paths
- Tests pass, typecheck clean, build succeeds
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Replace inline existsSync + readFileSync + JSON.parse + try/catch patterns with readJsonFile<T>(), and replace writeFileSync(path, JSON.stringify(data, null, 2)) with writeJsonFile(path, data, { ensureDir: true }). Consumers retain domain-specific logging and type conversions. Removes ensureCacheDir() helper from connected-providers-cache.ts as writeJsonFile handles directory creation.

Affected modules:
- src/shared/connected-providers-cache.ts (4 functions refactored)
- src/hooks/rules-injector/storage.ts (2 functions refactored)
- src/hooks/agent-usage-reminder/storage.ts (2 functions refactored)
- src/hooks/interactive-bash-session/storage.ts (2 functions refactored)
- src/shared/index.ts (added json-cache barrel export)

Net: -64 lines (removed 112, added 48)
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@iyoda iyoda force-pushed the refactor/pr7-add-ensure-directory branch from c87c995 to 07d2644 Compare February 15, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments