@bscotch/sprite-source
Version:
Art pipeline scripting module for GameMaker sprites.
34 lines • 1.12 kB
JavaScript
import fsp from 'node:fs/promises';
import { FIO_RETRY_DELAY, MAX_FIO_RETRIES } from './constants.js';
import { SpriteSourceError } from './utility.js';
/**
* A wrapper around `fs.readdir` that retries a few times and
* uses the `withFileTypes` option.
*/
async function _readdirSafe(dir, withFileTypes) {
let retries = 0;
let files = [];
let error = null;
while (retries < MAX_FIO_RETRIES) {
error = null;
try {
// @ts-expect-error withFileTypes can only be `true` in the types
files = await fsp.readdir(dir.toString(), { withFileTypes });
return files;
}
catch (err) {
error = new SpriteSourceError(`Failed to read directory ${dir}`, err);
retries++;
await new Promise((resolve) => setTimeout(resolve, FIO_RETRY_DELAY));
continue;
}
}
throw error;
}
export async function readdirSafe(dir) {
return await _readdirSafe(dir, false);
}
export async function readdirSafeWithFileTypes(dir) {
return await _readdirSafe(dir, true);
}
//# sourceMappingURL=safeFs.js.map