Skip to content

Commit 7cff814

Browse files
authored
Refactor Shell Option, More Type Safety, Meaningful Option, Support Run with Standard Schema which provide type-safety command line (#4)
2 parents 4c08c3b + ed49ae2 commit 7cff814

12 files changed

Lines changed: 1439 additions & 210 deletions

File tree

.changeset/chubby-cups-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@thaitype/shell': major
3+
---
4+
5+
Refactor Shell Option, More Type Safety, Meaningful Option, Support Run with Standard Schema which provide type-safety command line

CLAUDE.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,25 @@ src/
2626

2727
### Shell Class Design
2828

29-
The `Shell` class (`src/shell.ts`) is the only export and implements:
29+
The `Shell` class (`src/shell.ts`) provides three public methods:
30+
31+
1. **`run()`** - Recommended for most use cases. Throws on error.
32+
- Returns `StrictResult` with stdout and stderr only
33+
- Command either succeeds or throws an exception
34+
35+
2. **`safeRun()`** - Never throws. Returns error result instead.
36+
- Returns `SafeResult` with stdout, stderr, exitCode, and success flag
37+
- Always succeeds, check `result.success` to determine outcome
38+
39+
3. **`execute()`** - Low-level method with explicit throwOnError control
40+
- Pass `{ throwOnError: true }` for run() behavior
41+
- Pass `{ throwOnError: false }` for safeRun() behavior
42+
43+
**Implementation Details:**
3044

3145
1. **Output Modes** - Three strategies for handling stdout/stderr:
3246
- `capture`: Pipes output for programmatic access (default)
33-
- `live`: Inherits stdio, streams to console in real-time
47+
- `live`: Inherits stdio, streams to console in real-time (returns null for stdout/stderr)
3448
- `all`: Combines both - captures AND streams simultaneously
3549

3650
Implementation detail: Maps output modes to execa stdio configuration using a `stdioMap` object
@@ -50,9 +64,10 @@ The `Shell` class (`src/shell.ts`) is the only export and implements:
5064

5165
### Key Interfaces
5266

53-
- `ShellOptions`: Constructor configuration (defaultOutputMode, dryRun, verbose, throwOnError, throwMode, logger)
67+
- `ShellOptions`: Constructor configuration (defaultOutputMode, dryRun, verbose, throwMode, logger)
5468
- `RunOptions`: Per-command overrides, extends `ExecaOptions` from execa
55-
- `RunResult`: Structured return value (stdout, stderr, exitCode, isError, isSuccess)
69+
- `StrictResult`: Return type for `run()` (stdout, stderr)
70+
- `SafeResult`: Return type for `safeRun()` (stdout, stderr, exitCode, success)
5671

5772
## Development Commands
5873

@@ -73,9 +88,10 @@ The build uses Babel's `annotate-pure-calls` plugin for better tree-shaking.
7388
pnpm test # Run tests in watch mode (Vitest)
7489
pnpm test:ci # Run tests once (for CI)
7590
pnpm test:coverage # Generate coverage report
91+
pnpm test:coverage:feedback # Run coverage with detailed feedback on uncovered lines
7692
```
7793

78-
Note: Currently no test files exist in the repository. Tests should be added as `*.test.ts` or `*.spec.ts` files (they're excluded from builds).
94+
Tests are located in `test/shell.test.ts` and focus on Shell class logic (not execa features).
7995

8096
### Linting & Type Checking
8197
```bash
@@ -115,7 +131,12 @@ this.logger?.(`$ ${args.join(' ')}`);
115131
```
116132

117133
### Error Handling Strategy
118-
When catching `ExecaError`, the class checks `throwOnError` before re-throwing. If disabled, it returns a `RunResult` with `isError: true` instead of throwing.
134+
Three approaches to error handling:
135+
- `run()`: Always throws on error (uses `reject: true` in execa)
136+
- `safeRun()`: Never throws, returns result with `success: false` (uses `reject: false` in execa)
137+
- `execute({ throwOnError })`: Explicit control over throw behavior
138+
139+
When using `safeRun()` or `execute({ throwOnError: false })`, check `result.success` to determine if the command succeeded.
119140

120141
### Stdio Configuration
121142
Output modes are implemented by mapping to execa's stdio arrays:

0 commit comments

Comments
 (0)