Skip to content

Commit b18213c

Browse files
committed
Cleanup fs helpers
1 parent 0e73af6 commit b18213c

File tree

5 files changed

+63
-21
lines changed

5 files changed

+63
-21
lines changed

src/commands/manifest/convert_gradle_to_maven.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function convertGradleToMaven(
9090
// // Move the pom file to ...? initial cwd? loc will be an absolute path, or dump to stdout
9191
// if (out === '-') {
9292
// spinner.start('Result:\n```')
93-
// spinner.log(await safeReadFile(loc, 'utf8'))
93+
// spinner.log(await safeReadFile(loc))
9494
// spinner.log('```')
9595
// spinner.successAndStop(`OK`)
9696
// } else {

src/commands/manifest/convert_sbt_to_maven.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function convertSbtToMaven(
7979
// TODO: maybe we can add an option to target a specific file to dump to stdout
8080
if (out === '-' && poms.length === 1) {
8181
logger.log('Result:\n```')
82-
logger.log(await safeReadFile(poms[0] as string, 'utf8'))
82+
logger.log(await safeReadFile(poms[0]!))
8383
logger.log('```')
8484
logger.success(`OK`)
8585
} else if (out === '-') {

src/commands/optimize/get-workspace-globs.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ export async function getWorkspaceGlobs(
2828
path.join(pkgPath, `${PNPM_WORKSPACE}.yml`)
2929
]) {
3030
// eslint-disable-next-line no-await-in-loop
31-
const yml = (await safeReadFile(workspacePath, 'utf8')) as
32-
| string
33-
| undefined
31+
const yml = await safeReadFile(workspacePath)
3432
if (yml) {
3533
try {
3634
workspacePatterns = yamlParse(yml)?.packages

src/utils/fs.ts

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,36 @@ import { promises as fs, readFileSync as fsReadFileSync } from 'node:fs'
22
import path from 'node:path'
33
import process from 'node:process'
44

5+
import constants from '../constants'
6+
7+
import type { Remap } from '@socketsecurity/registry/lib/objects'
58
import type { Abortable } from 'node:events'
6-
import type { ObjectEncodingOptions, OpenMode, PathLike } from 'node:fs'
9+
import type {
10+
ObjectEncodingOptions,
11+
OpenMode,
12+
PathLike,
13+
PathOrFileDescriptor
14+
} from 'node:fs'
715
import type { FileHandle } from 'node:fs/promises'
816

17+
const { abortSignal } = constants
18+
19+
export type FindUpOptions = {
20+
cwd?: string | undefined
21+
signal?: AbortSignal | undefined
22+
}
923
export async function findUp(
1024
name: string | string[],
11-
{ cwd = process.cwd() }: { cwd: string }
25+
{ cwd = process.cwd(), signal = abortSignal }: FindUpOptions
1226
): Promise<string | undefined> {
1327
let dir = path.resolve(cwd)
1428
const { root } = path.parse(dir)
1529
const names = [name].flat()
1630
while (dir && dir !== root) {
1731
for (const name of names) {
32+
if (signal?.aborted) {
33+
return undefined
34+
}
1835
const filePath = path.join(dir, name)
1936
try {
2037
// eslint-disable-next-line no-await-in-loop
@@ -29,16 +46,19 @@ export async function findUp(
2946
return undefined
3047
}
3148

32-
export type ReadFileOptions = ObjectEncodingOptions &
33-
Abortable & {
34-
flag?: OpenMode | undefined
35-
}
49+
export type ReadFileOptions = Remap<
50+
ObjectEncodingOptions &
51+
Abortable & {
52+
flag?: OpenMode | undefined
53+
}
54+
>
3655

3756
export async function readFileBinary(
3857
filepath: PathLike | FileHandle,
3958
options?: ReadFileOptions | undefined
4059
): Promise<Buffer> {
4160
return (await fs.readFile(filepath, {
61+
signal: abortSignal,
4262
...options,
4363
encoding: 'binary'
4464
} as ReadFileOptions)) as Buffer
@@ -48,26 +68,50 @@ export async function readFileUtf8(
4868
filepath: PathLike | FileHandle,
4969
options?: ReadFileOptions | undefined
5070
): Promise<string> {
51-
return (await fs.readFile(filepath, {
71+
return await fs.readFile(filepath, {
72+
signal: abortSignal,
5273
...options,
5374
encoding: 'utf8'
54-
} as ReadFileOptions)) as string
75+
})
5576
}
5677

5778
export async function safeReadFile(
58-
...args: Parameters<typeof fs.readFile>
59-
): Promise<ReturnType<typeof fs.readFile> | undefined> {
79+
filepath: PathLike | FileHandle,
80+
options?: 'utf8' | 'utf-8' | { encoding: 'utf8' | 'utf-8' } | undefined
81+
): Promise<string | undefined>
82+
export async function safeReadFile(
83+
filepath: PathLike | FileHandle,
84+
options?: ReadFileOptions | NodeJS.BufferEncoding | undefined
85+
): Promise<Awaited<ReturnType<typeof fs.readFile>> | undefined> {
6086
try {
61-
return await fs.readFile(...args)
87+
return await fs.readFile(filepath, {
88+
encoding: 'utf8',
89+
signal: abortSignal,
90+
...(typeof options === 'string' ? { encoding: options } : options)
91+
})
6292
} catch {}
6393
return undefined
6494
}
6595

6696
export function safeReadFileSync(
67-
...args: Parameters<typeof fsReadFileSync>
97+
filepath: PathOrFileDescriptor,
98+
options?: 'utf8' | 'utf-8' | { encoding: 'utf8' | 'utf-8' } | undefined
99+
): string | undefined
100+
export function safeReadFileSync(
101+
filepath: PathOrFileDescriptor,
102+
options?:
103+
| {
104+
encoding?: NodeJS.BufferEncoding | undefined
105+
flag?: string | undefined
106+
}
107+
| NodeJS.BufferEncoding
108+
| undefined
68109
): ReturnType<typeof fsReadFileSync> | undefined {
69110
try {
70-
return fsReadFileSync(...args)
111+
return fsReadFileSync(filepath, {
112+
encoding: 'utf8',
113+
...(typeof options === 'string' ? { encoding: options } : options)
114+
})
71115
} catch {}
72116
return undefined
73117
}

src/utils/settings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getSettings(): Settings {
3333
_settings = {} as Settings
3434
const settingsPath = getSettingsPath()
3535
if (settingsPath) {
36-
const raw = safeReadFileSync(settingsPath, 'utf8') as string | undefined
36+
const raw = safeReadFileSync(settingsPath)
3737
if (raw) {
3838
try {
3939
Object.assign(
@@ -95,10 +95,10 @@ export function findSocketYmlSync() {
9595
let dir = process.cwd()
9696
while (dir !== prevDir) {
9797
let ymlPath = path.join(dir, 'socket.yml')
98-
let yml = safeReadFileSync(ymlPath, 'utf8') as string | undefined
98+
let yml = safeReadFileSync(ymlPath)
9999
if (yml === undefined) {
100100
ymlPath = path.join(dir, 'socket.yaml')
101-
yml = safeReadFileSync(ymlPath, 'utf8') as string | undefined
101+
yml = safeReadFileSync(ymlPath)
102102
}
103103
if (typeof yml === 'string') {
104104
try {

0 commit comments

Comments
 (0)