|
18 | 18 | * - Supports both read and write operations |
19 | 19 | */ |
20 | 20 |
|
21 | | -import fs from 'node:fs' |
| 21 | +import { existsSync, readFileSync } from 'node:fs' |
| 22 | +import { readFile, writeFile } from 'node:fs/promises' |
22 | 23 | import path from 'node:path' |
23 | 24 |
|
24 | 25 | import { debugDirNs, debugNs } from '@socketsecurity/lib/debug' |
@@ -127,14 +128,14 @@ export async function readSocketJson( |
127 | 128 | defaultOnError = false, |
128 | 129 | ): Promise<CResult<SocketJson>> { |
129 | 130 | const sockJsonPath = path.join(cwd, SOCKET_JSON) |
130 | | - if (!fs.existsSync(sockJsonPath)) { |
| 131 | + if (!existsSync(sockJsonPath)) { |
131 | 132 | debugNs('notice', `miss: ${SOCKET_JSON} not found at ${cwd}`) |
132 | 133 | return { ok: true, data: getDefaultSocketJson() } |
133 | 134 | } |
134 | 135 |
|
135 | 136 | let json = undefined |
136 | 137 | try { |
137 | | - json = await fs.promises.readFile(sockJsonPath, 'utf8') |
| 138 | + json = await readFile(sockJsonPath, 'utf8') |
138 | 139 | } catch (e) { |
139 | 140 | if (defaultOnError) { |
140 | 141 | logger.warn(`Failed to read ${SOCKET_JSON}, using default`) |
@@ -188,13 +189,13 @@ export function readSocketJsonSync( |
188 | 189 | defaultOnError = false, |
189 | 190 | ): CResult<SocketJson> { |
190 | 191 | const sockJsonPath = path.join(cwd, SOCKET_JSON) |
191 | | - if (!fs.existsSync(sockJsonPath)) { |
| 192 | + if (!existsSync(sockJsonPath)) { |
192 | 193 | debugNs('notice', `miss: ${SOCKET_JSON} not found at ${cwd}`) |
193 | 194 | return { ok: true, data: getDefaultSocketJson() } |
194 | 195 | } |
195 | 196 | let jsonContent = undefined |
196 | 197 | try { |
197 | | - jsonContent = fs.readFileSync(sockJsonPath, 'utf8') |
| 198 | + jsonContent = readFileSync(sockJsonPath, 'utf8') |
198 | 199 | } catch (e) { |
199 | 200 | if (defaultOnError) { |
200 | 201 | logger.warn(`Failed to read ${SOCKET_JSON}, using default`) |
@@ -262,7 +263,7 @@ export async function writeSocketJson( |
262 | 263 | } |
263 | 264 |
|
264 | 265 | const filepath = path.join(cwd, SOCKET_JSON) |
265 | | - await fs.promises.writeFile(filepath, `${jsonContent}\n`, 'utf8') |
| 266 | + await writeFile(filepath, `${jsonContent}\n`, 'utf8') |
266 | 267 |
|
267 | 268 | return { ok: true, data: undefined } |
268 | 269 | } |
0 commit comments