fast-glob icon indicating copy to clipboard operation
fast-glob copied to clipboard

Allow abort glob progress

Open otakustay opened this issue 5 months ago • 0 comments

Environment

  • OS Version: macOS 14.2.1
  • Node.js Version: 20.10.0

Reference: https://github.com/sindresorhus/globby/issues/261

  1. Can't stop glob from working when timed out or 1 file is found
  2. Not easy to check "at least 1 file matches glob patterns"

Code sample

Have a AbortSignal option to stop stop glob in progress.

const signal = AbortSignal.timeout(200);
try {
  return await fg.glob(patterns, {signal});
}
catch {
  return [];
}

Or have a globExists matching the first file.

async function globExists(patterns, options) {
    const controller = new AbortController();
    for await (const entry of fg.globStream(patterns, {...options, signal: controller.signal})) {
        controller.abort();
        return entry;
    }
}

otakustay avatar Jan 10 '24 01:01 otakustay