Skip to content

Commit 5f78c16

Browse files
committed
refactor(config): use safeMkdirSync for directory creation
Replace manual EEXIST error handling with safeMkdirSync from @socketsecurity/lib/fs. This simplifies the code and ensures consistent error handling for directory creation. Changes: - Import safeMkdirSync from @socketsecurity/lib/fs - Replace try-catch blocks around mkdirSync with safeMkdirSync calls - Remove duplicate EEXIST error handling code Benefits: - Cleaner, more maintainable code - Consistent error handling across the codebase - Automatic handling of race conditions in concurrent scenarios Dependencies: - Requires @socketsecurity/lib with safeMkdirSync support (committed in socket-lib: feat(fs): add safeMkdir and safeMkdirSync)
1 parent 3be7e06 commit 5f78c16

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

packages/cli/src/utils/config.mts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
* - updateConfigValue: Persist configuration changes
2222
*/
2323

24-
import { mkdirSync, writeFileSync } from 'node:fs'
24+
import { writeFileSync } from 'node:fs'
2525
import path from 'node:path'
2626

2727
import config from '@socketsecurity/config'
2828
import { debugDirNs, debugNs } from '@socketsecurity/lib/debug'
29-
import { safeReadFileSync } from '@socketsecurity/lib/fs'
29+
import { safeMkdirSync, safeReadFileSync } from '@socketsecurity/lib/fs'
3030
import { logger } from '@socketsecurity/lib/logger'
3131
import { naturalCompare } from '@socketsecurity/lib/sorts'
3232

@@ -120,14 +120,7 @@ function getConfigValues(): LocalConfig {
120120
updateConfigValue(CONFIG_KEY_API_TOKEN, token)
121121
}
122122
} else {
123-
try {
124-
mkdirSync(socketAppDataPath, { recursive: true })
125-
} catch (e: any) {
126-
// Ignore EEXIST error - directory already exists.
127-
if (e?.code !== 'EEXIST') {
128-
throw e
129-
}
130-
}
123+
safeMkdirSync(socketAppDataPath, { recursive: true })
131124
}
132125
}
133126
}
@@ -351,14 +344,7 @@ export function updateConfigValue<Key extends keyof LocalConfig>(
351344
_pendingSave = false
352345
const socketAppDataPath = getSocketAppDataPath()
353346
if (socketAppDataPath) {
354-
try {
355-
mkdirSync(socketAppDataPath, { recursive: true })
356-
} catch (e: any) {
357-
// Ignore EEXIST error - directory already exists.
358-
if (e?.code !== 'EEXIST') {
359-
throw e
360-
}
361-
}
347+
safeMkdirSync(socketAppDataPath, { recursive: true })
362348
const configFilePath = path.join(socketAppDataPath, 'config.json')
363349
writeFileSync(
364350
configFilePath,

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)