Skip to content

Commit 6753cbc

Browse files
committed
Revert to using sync node:fs
1 parent 3830923 commit 6753cbc

1 file changed

Lines changed: 20 additions & 25 deletions

File tree

src/utils/io.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface FileAdapter {
2121
basename: (path: string, suffix?: string) => string;
2222
}
2323

24-
let cachedFs: typeof import('node:fs/promises') | null = null;
24+
let cachedFs: typeof import('node:fs') | null = null;
2525
let cachedPath: typeof import('path') | null = null;
2626
let cachedOs: typeof import('os') | null = null;
2727
let cachedRequire: NodeRequire | null | undefined = undefined;
@@ -45,11 +45,11 @@ export function getNodeRequire(): NodeRequire {
4545
return cachedRequire;
4646
}
4747

48-
function getFs(): typeof import('node:fs/promises') {
48+
function getFs(): typeof import('node:fs') {
4949
if (!cachedFs) {
5050
try {
5151
const nodeRequire = getNodeRequire();
52-
const fsModule = 'node:fs/promises';
52+
const fsModule = 'node:fs';
5353
cachedFs = nodeRequire(fsModule);
5454
} catch {
5555
throw new Error('File system access is not available in this environment.');
@@ -156,76 +156,71 @@ export function extname(path: string): string {
156156

157157
async function readBinaryFromInput(input: ProcessorInput): Promise<Uint8Array> {
158158
if (typeof input === 'string') {
159-
return await getFs().readFile(input);
159+
return Promise.resolve(getFs().readFileSync(input));
160160
}
161161
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(input)) {
162-
return input;
162+
return Promise.resolve(input);
163163
}
164164
if (input instanceof ArrayBuffer) {
165-
return new Uint8Array(input);
165+
return Promise.resolve(new Uint8Array(input));
166166
}
167-
return input;
167+
return Promise.resolve(input);
168168
}
169169

170170
async function readTextFromInput(
171171
input: ProcessorInput,
172172
encoding: BufferEncoding = 'utf8'
173173
): Promise<string> {
174174
if (typeof input === 'string') {
175-
return await getFs().readFile(input, encoding);
175+
return await Promise.resolve(getFs().readFileSync(input, encoding));
176176
}
177177
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(input)) {
178-
return input.toString(encoding);
178+
return Promise.resolve(input.toString(encoding));
179179
}
180180
if (input instanceof ArrayBuffer) {
181-
return decodeText(new Uint8Array(input));
181+
return Promise.resolve(decodeText(new Uint8Array(input)));
182182
}
183-
return decodeText(input);
183+
return Promise.resolve(decodeText(input));
184184
}
185185

186186
async function writeBinaryToPath(outputPath: string, data: BinaryOutput): Promise<void> {
187-
await getFs().writeFile(outputPath, data);
187+
await getFs().writeFileSync(outputPath, data);
188188
}
189189

190190
async function writeTextToPath(outputPath: string, text: string): Promise<void> {
191-
await getFs().writeFile(outputPath, text, 'utf8');
191+
await getFs().writeFileSync(outputPath, text, 'utf8');
192192
}
193193

194194
async function pathExists(path: string): Promise<boolean> {
195-
try {
196-
await getFs().access(path, F_OK);
197-
return true;
198-
} catch (e) {
199-
return false;
200-
}
195+
return Promise.resolve(getFs().existsSync(path));
201196
}
202197

203198
async function isDirectory(path: string): Promise<boolean> {
204-
return (await getFs().stat(path)).isDirectory();
199+
return Promise.resolve(getFs().statSync(path).isDirectory());
205200
}
206201

207202
async function getFileSize(path: string): Promise<number> {
208-
return (await getFs().stat(path)).size;
203+
return Promise.resolve(getFs().statSync(path).size);
209204
}
210205

211206
async function mkDir(path: string, options?: { recursive?: boolean }): Promise<void> {
212-
await getFs().mkdir(path, options);
207+
getFs().mkdirSync(path, options);
213208
}
214209

215210
async function listDir(path: string): Promise<string[]> {
216-
return await getFs().readdir(path);
211+
return Promise.resolve(getFs().readdirSync(path));
217212
}
218213

219214
async function removePath(
220215
path: string,
221216
options?: { recursive?: boolean; force?: boolean }
222217
): Promise<void> {
223-
await getFs().rm(path, options);
218+
getFs().rmSync(path, options);
224219
}
225220

226221
async function mkTempDir(prefix: string): Promise<string> {
227222
const path = join(getOs().tmpdir(), prefix);
228-
return await getFs().mkdtemp(path);
223+
return Promise.resolve(getFs().mkdtempSync(path));
229224
}
230225

231226
function join(...pathParts: string[]): string {

0 commit comments

Comments
 (0)