"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const glob = require("glob"); const Stream = require("node:stream"); const fs = require("node:fs"); const fsPromises = require("node:fs/promises"); function distinctValues(list) { return [...new Set(list)].sort(); } async function filesToStream(files) { let passthrough = new Stream.PassThrough(); let queueSize = files.length; for (const file of files) { const stats = await fsPromises.stat(file); if (stats.isFile()) { const stream = fs.createReadStream(file); passthrough = stream.pipe(passthrough, { end: false }); stream.once("end", () => { queueSize--; if (queueSize === 0) { passthrough.end(); } }); } else { throw new Error("Not a file: " + file); } } return passthrough; } async function streamToString(stream) { const chunks = []; for await (const chunk of stream) { chunks.push(chunk); } return typeof chunks[0] === "string" ? chunks.join("") : Buffer.concat(chunks).toString("utf8"); } const defaultOptions = { glob: {}, stream: false }; function globcat(patterns, options, callback) { if (typeof options === "function") { callback = options; options = {}; } const config = Object.assign({}, defaultOptions, options); patterns = Array.isArray(patterns) ? patterns : [patterns]; const promise = glob.glob(patterns, { ...config.glob ?? {}, withFileTypes: false }).then((files) => { if (files.length === 0) { return Stream.Readable.from([""]); } return filesToStream(distinctValues(files)); }).then((stream) => { const result = config.stream ? stream : streamToString(stream); return result; }).then((result) => { if (callback) { return callback(null, result); } else { return result; } }).catch((err) => { if (callback) { callback(err); } else { throw err; } }); if (!callback) { return promise; } return void 0; } exports.globcat = globcat;