Skip to content

Commit b827981

Browse files
committed
docs: fix documentation inaccuracies
Fixed 6 documentation issues identified in accuracy audit: Critical: - Fixed detectPackageManager() signature (now sync, no params, returns 'npm' | 'pnpm' | 'yarn' | 'bun' | null) - Removed non-existent constants: LINUX, MIN_SUPPORTED_NODE_VERSION, MAIN_REGISTRY_URL High: - Removed non-existent env functions: getUserProfile(), getColorTerm() Low: - Added missing safeStats import in examples.md All examples updated to use correct APIs and patterns.
1 parent e86000d commit b827981

4 files changed

Lines changed: 136 additions & 164 deletions

File tree

docs/constants.md

Lines changed: 30 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,31 @@ Pre-defined constant values for Node.js versions, npm registry URLs, platform de
1212
## Quick Start
1313

1414
```typescript
15-
import { MIN_SUPPORTED_NODE_VERSION } from '@socketsecurity/lib/constants/node'
16-
import { MAIN_REGISTRY_URL } from '@socketsecurity/lib/constants/npm'
15+
import { WIN32, DARWIN } from '@socketsecurity/lib/constants/platform'
1716

18-
if (process.version < `v${MIN_SUPPORTED_NODE_VERSION}`) {
19-
console.error(`Node.js ${MIN_SUPPORTED_NODE_VERSION}+ required`)
20-
process.exit(1)
17+
if (WIN32) {
18+
console.log('Running on Windows')
19+
} else if (DARWIN) {
20+
console.log('Running on macOS')
21+
} else {
22+
console.log('Running on Linux or other Unix-like OS')
2123
}
22-
23-
console.log(`Registry: ${MAIN_REGISTRY_URL}`)
2424
```
2525

2626
## Node.js Constants
2727

28-
### MIN_SUPPORTED_NODE_VERSION
29-
30-
Minimum supported Node.js version for Socket.dev tools.
31-
32-
```typescript
33-
import { MIN_SUPPORTED_NODE_VERSION } from '@socketsecurity/lib/constants/node'
34-
35-
console.log(MIN_SUPPORTED_NODE_VERSION) // "22.0.0"
36-
```
37-
38-
### Version Checking
39-
40-
```typescript
41-
import { MIN_SUPPORTED_NODE_VERSION } from '@socketsecurity/lib/constants/node'
42-
43-
const currentVersion = process.version.slice(1) // Remove 'v' prefix
44-
45-
if (currentVersion < MIN_SUPPORTED_NODE_VERSION) {
46-
throw new Error(
47-
`Node.js ${MIN_SUPPORTED_NODE_VERSION}+ required. Current: ${currentVersion}`
48-
)
49-
}
50-
```
28+
See `@socketsecurity/lib/constants/node` for Node.js-related constants.
5129

5230
## npm Registry Constants
5331

54-
### MAIN_REGISTRY_URL
55-
56-
The main npm registry URL.
32+
For building npm registry URLs, use string literals directly:
5733

5834
```typescript
59-
import { MAIN_REGISTRY_URL } from '@socketsecurity/lib/constants/npm'
60-
61-
console.log(MAIN_REGISTRY_URL) // "https://registry.npmjs.org"
62-
```
63-
64-
### Building Registry URLs
65-
66-
```typescript
67-
import { MAIN_REGISTRY_URL } from '@socketsecurity/lib/constants/npm'
6835
import { httpJson } from '@socketsecurity/lib/http-request'
6936

70-
// Get package metadata
37+
// Get package metadata from npm registry
7138
const packageName = 'lodash'
72-
const url = `${MAIN_REGISTRY_URL}/${packageName}`
39+
const url = `https://registry.npmjs.org/${packageName}`
7340
const metadata = await httpJson(url)
7441

7542
console.log(metadata.name, metadata['dist-tags'].latest)
@@ -78,10 +45,8 @@ console.log(metadata.name, metadata['dist-tags'].latest)
7845
### Package Tarball URLs
7946

8047
```typescript
81-
import { MAIN_REGISTRY_URL } from '@socketsecurity/lib/constants/npm'
82-
8348
function getTarballUrl(name: string, version: string): string {
84-
return `${MAIN_REGISTRY_URL}/${name}/-/${name}-${version}.tgz`
49+
return `https://registry.npmjs.org/${name}/-/${name}-${version}.tgz`
8550
}
8651

8752
const url = getTarballUrl('lodash', '4.17.21')
@@ -116,28 +81,29 @@ if (DARWIN) {
11681
}
11782
```
11883

119-
### LINUX
84+
### Detecting Linux
12085

121-
Checks if platform is Linux.
86+
There is no `LINUX` constant. To detect Linux or other Unix-like systems, check if the platform is neither Windows nor macOS:
12287

12388
```typescript
124-
import { LINUX } from '@socketsecurity/lib/constants/platform'
89+
import { WIN32, DARWIN } from '@socketsecurity/lib/constants/platform'
12590

126-
if (LINUX) {
127-
console.log('Running on Linux')
91+
const isLinux = !WIN32 && !DARWIN
92+
if (isLinux) {
93+
console.log('Running on Linux or other Unix-like OS')
12894
// Use Linux-specific logic
12995
}
13096
```
13197

13298
### Platform-Specific Logic Example
13399

134100
```typescript
135-
import { WIN32, DARWIN, LINUX } from '@socketsecurity/lib/constants/platform'
136-
import { getHome, getUserProfile } from '@socketsecurity/lib/env'
101+
import { WIN32, DARWIN } from '@socketsecurity/lib/constants/platform'
102+
import { getHome } from '@socketsecurity/lib/env/home'
137103
import path from 'node:path'
138104

139105
function getConfigDir(): string {
140-
const home = getHome() || getUserProfile()
106+
const home = getHome()
141107
if (!home) {
142108
throw new Error('Cannot determine home directory')
143109
}
@@ -146,7 +112,8 @@ function getConfigDir(): string {
146112
return path.join(home, 'AppData', 'Local', 'MyApp')
147113
} else if (DARWIN) {
148114
return path.join(home, 'Library', 'Application Support', 'MyApp')
149-
} else if (LINUX) {
115+
} else {
116+
// Linux or other Unix-like OS
150117
return path.join(home, '.config', 'myapp')
151118
}
152119

@@ -211,10 +178,11 @@ checkNodeVersion()
211178
### npm Package Downloader
212179

213180
```typescript
214-
import { MAIN_REGISTRY_URL } from '@socketsecurity/lib/constants/npm'
215181
import { httpJson, httpDownload } from '@socketsecurity/lib/http-request'
216182
import { Spinner } from '@socketsecurity/lib/spinner'
217183

184+
const NPM_REGISTRY = 'https://registry.npmjs.org'
185+
218186
interface PackageMetadata {
219187
'dist-tags': { latest: string }
220188
versions: Record<string, {
@@ -232,7 +200,7 @@ async function downloadPackage(
232200
// Get package metadata
233201
spinner.start('Fetching package metadata...')
234202
const metadata = await httpJson<PackageMetadata>(
235-
`${MAIN_REGISTRY_URL}/${name}`
203+
`${NPM_REGISTRY}/${name}`
236204
)
237205
spinner.success('Metadata fetched')
238206

@@ -267,7 +235,7 @@ await downloadPackage('lodash', 'latest', '/tmp')
267235
### Cross-Platform Path Builder
268236

269237
```typescript
270-
import { WIN32, DARWIN, LINUX } from '@socketsecurity/lib/constants/platform'
238+
import { WIN32, DARWIN } from '@socketsecurity/lib/constants/platform'
271239
import path from 'node:path'
272240

273241
class PathBuilder {
@@ -321,10 +289,8 @@ console.log(configPath)
321289
### Registry URL Builder
322290

323291
```typescript
324-
import { MAIN_REGISTRY_URL } from '@socketsecurity/lib/constants/npm'
325-
326292
class RegistryClient {
327-
constructor(private baseUrl: string = MAIN_REGISTRY_URL) {}
293+
constructor(private baseUrl: string = 'https://registry.npmjs.org') {}
328294

329295
packageUrl(name: string): string {
330296
return `${this.baseUrl}/${name}`
@@ -386,16 +352,10 @@ await Promise.all([
386352

387353
## Available Constants
388354

389-
### Node.js
390-
- `MIN_SUPPORTED_NODE_VERSION` - Minimum Node.js version ("22.0.0")
391-
392-
### npm Registry
393-
- `MAIN_REGISTRY_URL` - Main npm registry ("https://registry.npmjs.org")
394-
395355
### Platform
396356
- `WIN32` - Boolean, true on Windows
397357
- `DARWIN` - Boolean, true on macOS
398-
- `LINUX` - Boolean, true on Linux
358+
- For Linux detection, use `!WIN32 && !DARWIN`
399359

400360
### Process
401361
- `getAbortSignal()` - Function returning global AbortSignal
@@ -435,5 +395,5 @@ if (!semver.gte(process.version, MIN_SUPPORTED_NODE_VERSION)) {
435395
**Solution:**
436396
Use environment variable or config:
437397
```typescript
438-
const registryUrl = process.env.NPM_REGISTRY || MAIN_REGISTRY_URL
398+
const registryUrl = process.env.NPM_REGISTRY || 'https://registry.npmjs.org'
439399
```

docs/environment.md

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -111,33 +111,7 @@ if (home) {
111111

112112
**Environment Variable:** `HOME` (Unix/Linux/macOS)
113113

114-
### getUserProfile()
115-
116-
**What it does:** Gets the Windows user profile path.
117-
118-
**Returns:** `string | undefined`
119-
120-
**Example:**
121-
```typescript
122-
import { getUserProfile } from '@socketsecurity/lib/env/userprofile'
123-
124-
const profile = getUserProfile()
125-
if (profile) {
126-
const configPath = `${profile}\\.myapp\\config.json`
127-
}
128-
```
129-
130-
**Environment Variable:** `USERPROFILE` (Windows)
131-
132-
### Cross-Platform Home Directory
133-
134-
```typescript
135-
import { getHome } from '@socketsecurity/lib/env/home'
136-
import { getUserProfile } from '@socketsecurity/lib/env/userprofile'
137-
138-
// Compose getters for cross-platform fallback
139-
const homeDir = getHome() || getUserProfile()
140-
```
114+
**Cross-platform note:** For Windows, use `process.env.USERPROFILE` if needed
141115

142116
### getTerm()
143117

@@ -157,23 +131,7 @@ if (term === 'dumb') {
157131

158132
**Environment Variable:** `TERM`
159133

160-
### getColorTerm()
161-
162-
**What it does:** Checks if terminal supports color.
163-
164-
**Returns:** `string | undefined`
165-
166-
**Example:**
167-
```typescript
168-
import { getColorTerm } from '@socketsecurity/lib/env/colorterm'
169-
170-
if (getColorTerm()) {
171-
// Terminal supports color
172-
enableColorOutput()
173-
}
174-
```
175-
176-
**Environment Variable:** `COLORTERM`
134+
**Color support note:** For checking terminal color support, use `process.env.COLORTERM` directly or libraries like `supports-color`
177135

178136
## Testing with Rewiring
179137

@@ -243,11 +201,11 @@ const config = {
243201

244202
```typescript
245203
import { getHome } from '@socketsecurity/lib/env/home'
246-
import { getUserProfile } from '@socketsecurity/lib/env/userprofile'
204+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
247205
import path from 'node:path'
248206

249207
function getConfigDir() {
250-
const home = getHome() || getUserProfile()
208+
const home = getHome() || (WIN32 ? process.env.USERPROFILE : undefined)
251209
if (!home) {
252210
throw new Error('Cannot determine home directory')
253211
}
@@ -259,11 +217,12 @@ function getConfigDir() {
259217
### Terminal Capability Detection
260218

261219
```typescript
262-
import { getTerm, getColorTerm } from '@socketsecurity/lib/env/term'
220+
import { getTerm } from '@socketsecurity/lib/env/term'
263221

264222
function supportsColor() {
265223
const term = getTerm()
266-
return term !== 'dumb' && getColorTerm() !== undefined
224+
const colorTerm = process.env.COLORTERM
225+
return term !== 'dumb' && colorTerm !== undefined
267226
}
268227

269228
function getLogger() {
@@ -288,11 +247,9 @@ All getters follow the pattern `get<VarName>()` and return `string | boolean | u
288247

289248
- **User Directories:**
290249
- `getHome()` - Returns `HOME` (Unix/Linux/macOS)
291-
- `getUserProfile()` - Returns `USERPROFILE` (Windows)
292250

293251
- **Terminal:**
294252
- `getTerm()` - Returns `TERM`
295-
- `getColorTerm()` - Returns `COLORTERM`
296253

297254
- **Path:**
298255
- `getPath()` - Returns `PATH`
@@ -322,9 +279,10 @@ All getters follow the pattern `get<VarName>()` and return `string | boolean | u
322279
**Problem:** Path doesn't work on Windows/Unix.
323280

324281
**Solution:**
325-
Use the fallback pattern:
282+
Use the fallback pattern with `process.env` for Windows:
326283
```typescript
327-
const home = getHome() || getUserProfile()
284+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
285+
const home = getHome() || (WIN32 ? process.env.USERPROFILE : undefined)
328286
```
329287

330288
And use `path.join()` for cross-platform path construction:

docs/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A complete CLI tool that downloads a file, processes it, and provides visual fee
1010
import { Spinner } from '@socketsecurity/lib/spinner'
1111
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1212
import { httpDownload } from '@socketsecurity/lib/http-request'
13-
import { readFileBinary, safeDelete, safeMkdir } from '@socketsecurity/lib/fs'
13+
import { readFileBinary, safeDelete, safeMkdir, safeStats } from '@socketsecurity/lib/fs'
1414
import { spawn } from '@socketsecurity/lib/spawn'
1515

1616
const logger = getDefaultLogger()

0 commit comments

Comments
 (0)