chore: importFs handles it's own error (#9971)

This commit is contained in:
Nikolay Vitkov 2023-04-04 17:29:29 +02:00 committed by GitHub
parent 0b1e20cf5b
commit 327febe2c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 34 deletions

View File

@ -2116,18 +2116,9 @@ export class Page extends EventEmitter {
return;
}
try {
const fs = await importFSPromises();
await fs.writeFile(path, buffer);
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Can only pass a file path in a Node-like environment.'
);
}
throw error;
}
}
/**

View File

@ -883,17 +883,7 @@ export class Frame {
}
if (path) {
let fs: typeof import('fs/promises');
try {
fs = await importFSPromises();
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Can only pass a file path in a Node-like environment.'
);
}
throw error;
}
const fs = await importFSPromises();
content = await fs.readFile(path, 'utf8');
content += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';

View File

@ -369,7 +369,16 @@ export async function importFSPromises(): Promise<
typeof import('fs/promises')
> {
if (!fs) {
try {
fs = await import('fs/promises');
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Cannot write to a path outside of a Node-like environment.'
);
}
throw error;
}
}
return fs;
}
@ -383,17 +392,7 @@ export async function getReadableAsBuffer(
): Promise<Buffer | null> {
const buffers = [];
if (path) {
let fs: typeof import('fs/promises');
try {
fs = await importFSPromises();
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Cannot write to a path outside of a Node-like environment.'
);
}
throw error;
}
const fs = await importFSPromises();
const fileHandle = await fs.open(path, 'w+');
for await (const chunk of readable) {
buffers.push(chunk);