Skip to content

Commit 97aefff

Browse files
committed
chore: minor fixes and updates
Miscellaneous code quality and consistency improvements. Changes: - Update src/bin.ts, src/debug.ts, src/effects/text-shimmer.ts, src/spinner.ts - Update src/constants/packages.ts - Update CLAUDE.md with v2 conventions and env system patterns
1 parent 9976760 commit 97aefff

6 files changed

Lines changed: 59 additions & 33 deletions

File tree

CLAUDE.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ Blank lines between groups, alphabetical within groups.
160160

161161
#### Path Aliases Usage
162162
- **Internal imports**: Always use path aliases for internal modules
163-
-`import { CI } from '#env/ci'`
164-
-`import { CI } from '../env/ci'`
163+
-`import { getCI } from '#env/ci'`
164+
-`import { getCI } from '../env/ci'`
165165
- **External modules**: Regular imports
166166
-`import path from 'node:path'`
167167

@@ -252,13 +252,37 @@ Use `pnpm run build:watch` or `pnpm run dev` for development with automatic rebu
252252
### Common Patterns
253253

254254
#### Environment Variables
255-
Access via typed helpers in `src/env/`:
255+
Access via typed getter functions in `src/env/`:
256256
```typescript
257-
import { CI } from '#env/ci'
258-
import { NODE_ENV } from '#env/node-env'
259-
import { getEnv } from '#env/getters'
257+
import { getCI } from '#env/ci'
258+
import { getNodeEnv } from '#env/node-env'
259+
import { isTest } from '#env/test'
260260
```
261261

262+
Each env module exports a pure getter function that accesses only its own environment variable. For fallback logic, compose multiple getters:
263+
```typescript
264+
import { getHome } from '#env/home'
265+
import { getUserProfile } from '#env/userprofile'
266+
267+
const homeDir = getHome() || getUserProfile() // Cross-platform fallback
268+
```
269+
270+
**Testing with rewiring:**
271+
Environment getters support test rewiring without modifying `process.env`:
272+
```typescript
273+
import { setEnv, clearEnv, resetEnv } from '#env/rewire'
274+
import { getCI } from '#env/ci'
275+
276+
// In test
277+
setEnv('CI', '1')
278+
expect(getCI()).toBe(true)
279+
280+
clearEnv('CI') // Clear single override
281+
resetEnv() // Clear all overrides (use in afterEach)
282+
```
283+
284+
This allows isolated tests without polluting the global process.env state.
285+
262286
#### File System Operations
263287
Use utilities from `#lib/fs`:
264288
```typescript

src/bin.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
* Provides cross-platform bin path lookup, command execution, and path normalization.
44
*/
55

6-
import { APPDATA } from '#env/appdata'
7-
import { HOME } from '#env/home'
8-
import { LOCALAPPDATA } from '#env/localappdata'
9-
import { XDG_DATA_HOME } from '#env/xdg-data-home'
6+
import { getHome } from '#env/home'
7+
import { getAppdata, getLocalappdata } from '#env/windows'
8+
import { getXdgDataHome } from '#env/xdg'
109

1110
import { WIN32 } from '#constants/platform'
1211
import { readJsonSync } from './fs'
@@ -259,10 +258,10 @@ export function findRealPnpm(): string {
259258
const commonPaths = WIN32
260259
? [
261260
// Windows common paths.
262-
path?.join(APPDATA as string, 'npm', 'pnpm.cmd'),
263-
path?.join(APPDATA as string, 'npm', 'pnpm'),
264-
path?.join(LOCALAPPDATA as string, 'pnpm', 'pnpm.cmd'),
265-
path?.join(LOCALAPPDATA as string, 'pnpm', 'pnpm'),
261+
path?.join(getAppdata() as string, 'npm', 'pnpm.cmd'),
262+
path?.join(getAppdata() as string, 'npm', 'pnpm'),
263+
path?.join(getLocalappdata() as string, 'pnpm', 'pnpm.cmd'),
264+
path?.join(getLocalappdata() as string, 'pnpm', 'pnpm'),
266265
'C:\\Program Files\\nodejs\\pnpm.cmd',
267266
'C:\\Program Files\\nodejs\\pnpm',
268267
].filter(Boolean)
@@ -271,10 +270,10 @@ export function findRealPnpm(): string {
271270
'/usr/local/bin/pnpm',
272271
'/usr/bin/pnpm',
273272
path?.join(
274-
(XDG_DATA_HOME as string) || `${HOME as string}/.local/share`,
273+
(getXdgDataHome() as string) || `${getHome() as string}/.local/share`,
275274
'pnpm/pnpm',
276275
),
277-
path?.join(HOME as string, '.pnpm/pnpm'),
276+
path?.join(getHome() as string, '.pnpm/pnpm'),
278277
].filter(Boolean)
279278

280279
return findRealBin('pnpm', commonPaths) ?? ''
@@ -290,8 +289,11 @@ export function findRealYarn(): string {
290289
const commonPaths = [
291290
'/usr/local/bin/yarn',
292291
'/usr/bin/yarn',
293-
path?.join(HOME as string, '.yarn/bin/yarn'),
294-
path?.join(HOME as string, '.config/yarn/global/node_modules/.bin/yarn'),
292+
path?.join(getHome() as string, '.yarn/bin/yarn'),
293+
path?.join(
294+
getHome() as string,
295+
'.config/yarn/global/node_modules/.bin/yarn',
296+
),
295297
].filter(Boolean)
296298

297299
return findRealBin('yarn', commonPaths) ?? ''
@@ -585,7 +587,7 @@ export function resolveBinPathSync(binPath: string): string {
585587

586588
// Handle special case where pnpm path in CI has extra segments.
587589
// In setup-pnpm GitHub Action, the path might be malformed like:
588-
// /home/runner/setup-pnpm/node_modules/.bin/pnpm/bin/pnpm.cjs
590+
// /home/user/setup-pnpm/node_modules/.bin/pnpm/bin/pnpm.cjs
589591
// This happens when the shell script contains a relative path that
590592
// when resolved, creates an invalid nested structure.
591593
if (isPnpmOrYarn && binPath.includes('/.bin/pnpm/bin/')) {

src/constants/packages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Package metadata, defaults, extensions, and lifecycle scripts.
33
*/
44

5-
import { npm_lifecycle_event } from '#env/npm-lifecycle-event'
5+
import { getNpmLifecycleEvent as getNpmLifecycleEventEnv } from '#env/npm'
66

77
let _lifecycleScriptNames: string[]
88
let _packageDefaultNodeRange: string | undefined
@@ -56,7 +56,7 @@ export function getPackageExtensions(): Iterable<[string, unknown]> {
5656

5757
// NPM lifecycle event.
5858
export function getNpmLifecycleEvent(): string | undefined {
59-
return npm_lifecycle_event
59+
return getNpmLifecycleEventEnv()
6060
}
6161

6262
// Lifecycle script names.

src/debug.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55

66
import { getSpinner } from '#constants/process'
7-
import { DEBUG } from '#env/debug'
8-
import { SOCKET_DEBUG } from '#env/socket-debug'
7+
import { getDebug } from '#env/debug'
8+
import { getSocketDebug } from '#env/socket'
99
import isUnicodeSupported from './external/@socketregistry/is-unicode-supported'
1010
import debugJs from './external/debug'
1111

@@ -48,8 +48,8 @@ function getDebugJsInstance(namespace: string) {
4848
return inst
4949
}
5050
if (
51-
!DEBUG &&
52-
SOCKET_DEBUG &&
51+
!getDebug() &&
52+
getSocketDebug() &&
5353
(namespace === 'error' || namespace === 'notice')
5454
) {
5555
debugJs.enable(namespace)
@@ -166,7 +166,7 @@ function extractOptions(namespaces: NamespacesOrOptions): DebugOptions {
166166
/*@__NO_SIDE_EFFECTS__*/
167167
function isEnabled(namespaces: string | undefined) {
168168
// Check if debugging is enabled at all
169-
if (!SOCKET_DEBUG) {
169+
if (!getSocketDebug()) {
170170
return false
171171
}
172172
if (typeof namespaces !== 'string' || !namespaces || namespaces === '*') {
@@ -360,7 +360,7 @@ export function debugCache(
360360
key: string,
361361
meta?: unknown | undefined,
362362
): void {
363-
if (!SOCKET_DEBUG) {
363+
if (!getSocketDebug()) {
364364
return
365365
}
366366
// Get caller info with stack offset of 3 (caller -> debugCache -> getCallerInfo).
@@ -381,7 +381,7 @@ export function debugCache(
381381
*/
382382
/*@__NO_SIDE_EFFECTS__*/
383383
function isDebugNs(namespaces: string | undefined): boolean {
384-
return !!SOCKET_DEBUG && isEnabled(namespaces)
384+
return !!getSocketDebug() && isEnabled(namespaces)
385385
}
386386

387387
/**
@@ -413,7 +413,7 @@ function debugLog(...args: unknown[]): void {
413413
*/
414414
/*@__NO_SIDE_EFFECTS__*/
415415
function isDebug(): boolean {
416-
return !!SOCKET_DEBUG
416+
return !!getSocketDebug()
417417
}
418418

419419
/**

src/effects/text-shimmer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import { ANSI_RESET, stripAnsi } from '../ansi'
1616
import { isArray } from '../arrays'
17-
import { CI } from '#env/ci'
17+
import { getCI } from '#env/ci'
1818

1919
import type {
2020
ShimmerColorGradient,
@@ -252,7 +252,7 @@ export function applyShimmer(
252252
const plainText = stripAnsi(text)
253253

254254
// No shimmer effect in CI or when direction is 'none'.
255-
if (CI || !plainText || direction === DIR_NONE) {
255+
if (getCI() || !plainText || direction === DIR_NONE) {
256256
const styleCode = stylesToAnsi(styles)
257257

258258
// Support gradient colors (array of colors, one per character).

src/spinner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import type { Writable } from 'node:stream'
77

88
// Note: getAbortSignal is imported lazily to avoid circular dependencies.
9-
import { CI } from '#env/ci'
9+
import { getCI } from '#env/ci'
1010
import { generateSocketSpinnerFrames } from './effects/pulse-frames'
1111
import type {
1212
ShimmerColorGradient,
@@ -1286,7 +1286,7 @@ export function Spinner(options?: SpinnerOptions | undefined): Spinner {
12861286
warning: desc(_Spinner.prototype.warn),
12871287
warningAndStop: desc(_Spinner.prototype.warnAndStop),
12881288
})
1289-
_defaultSpinner = CI
1289+
_defaultSpinner = getCI()
12901290
? ciSpinner
12911291
: (getCliSpinners('socket') as SpinnerStyle)
12921292
}

0 commit comments

Comments
 (0)