1- import { existsSync , promises as fs , readFileSync , statSync } from 'node:fs'
1+ import { promises as fs } from 'node:fs'
22import path from 'node:path'
33
44import { remove } from '@socketsecurity/registry/lib/fs'
@@ -7,19 +7,6 @@ import { pEach } from '@socketsecurity/registry/lib/promises'
77import constants from '../constants.mts'
88import { globNodeModules } from './glob.mts'
99
10- import type { Remap } from '@socketsecurity/registry/lib/objects'
11- import type { Abortable } from 'node:events'
12- import type {
13- BigIntStats ,
14- ObjectEncodingOptions ,
15- OpenMode ,
16- PathLike ,
17- PathOrFileDescriptor ,
18- StatSyncOptions ,
19- Stats ,
20- } from 'node:fs'
21- import type { FileHandle } from 'node:fs/promises'
22-
2310export async function removeNodeModules ( cwd = process . cwd ( ) ) {
2411 const nodeModulesPaths = await globNodeModules ( cwd )
2512 await pEach (
@@ -64,118 +51,3 @@ export async function findUp(
6451 }
6552 return undefined
6653}
67-
68- export function isDirectorySync ( filepath : string ) : boolean {
69- return existsSync ( filepath ) && ! ! safeStatsSync ( filepath ) ?. isDirectory ( )
70- }
71-
72- export type ReadFileOptions = Remap <
73- ObjectEncodingOptions &
74- Abortable & {
75- flag ?: OpenMode | undefined
76- }
77- >
78-
79- export async function readFileBinary (
80- filepath : PathLike | FileHandle ,
81- options ?: ReadFileOptions | undefined ,
82- ) : Promise < Buffer > {
83- return ( await fs . readFile ( filepath , {
84- // Lazily access constants.abortSignal.
85- signal : constants . abortSignal ,
86- ...options ,
87- encoding : 'binary' ,
88- } as ReadFileOptions ) ) as Buffer
89- }
90-
91- export async function readFileUtf8 (
92- filepath : PathLike | FileHandle ,
93- options ?: ReadFileOptions | undefined ,
94- ) : Promise < string > {
95- return await fs . readFile ( filepath , {
96- // Lazily access constants.abortSignal.
97- signal : constants . abortSignal ,
98- ...options ,
99- encoding : 'utf8' ,
100- } )
101- }
102-
103- export async function safeReadFile (
104- filepath : PathLike | FileHandle ,
105- options ?: 'utf8' | 'utf-8' | { encoding : 'utf8' | 'utf-8' } | undefined ,
106- ) : Promise < string | undefined >
107-
108- export async function safeReadFile (
109- filepath : PathLike | FileHandle ,
110- options ?: ReadFileOptions | NodeJS . BufferEncoding | undefined ,
111- ) : Promise < Awaited < ReturnType < typeof fs . readFile > > | undefined > {
112- try {
113- return await fs . readFile ( filepath , {
114- encoding : 'utf8' ,
115- // Lazily access constants.abortSignal.
116- signal : constants . abortSignal ,
117- ...( typeof options === 'string' ? { encoding : options } : options ) ,
118- } )
119- } catch { }
120- return undefined
121- }
122-
123- export function safeReadFileSync (
124- filepath : PathOrFileDescriptor ,
125- options ?: 'utf8' | 'utf-8' | { encoding : 'utf8' | 'utf-8' } | undefined ,
126- ) : string | undefined
127-
128- export function safeReadFileSync (
129- filepath : PathOrFileDescriptor ,
130- options ?:
131- | {
132- encoding ?: NodeJS . BufferEncoding | undefined
133- flag ?: string | undefined
134- }
135- | NodeJS . BufferEncoding
136- | undefined ,
137- ) : ReturnType < typeof readFileSync > | undefined {
138- try {
139- return readFileSync ( filepath , {
140- encoding : 'utf8' ,
141- ...( typeof options === 'string' ? { encoding : options } : options ) ,
142- } )
143- } catch { }
144- return undefined
145- }
146-
147- export function safeStatsSync (
148- filepath : PathLike ,
149- options ?: undefined ,
150- ) : Stats | undefined
151-
152- export function safeStatsSync (
153- filepath : PathLike ,
154- options ?: StatSyncOptions & {
155- bigint ?: false | undefined
156- } ,
157- ) : Stats | undefined
158-
159- export function safeStatsSync (
160- filepath : PathLike ,
161- options : StatSyncOptions & {
162- bigint : true
163- } ,
164- ) : BigIntStats | undefined
165-
166- export function safeStatsSync (
167- filepath : PathLike ,
168- options : StatSyncOptions & {
169- bigint : boolean
170- } ,
171- ) : Stats | BigIntStats | undefined
172-
173- export function safeStatsSync (
174- filepath : PathLike ,
175- options ?: StatSyncOptions ,
176- ) : Stats | BigIntStats | undefined {
177- try {
178- return statSync ( filepath , { throwIfNoEntry : false , ...options } )
179- } catch { }
180- return undefined
181- }
0 commit comments