Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

tinyglobby

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinyglobby - npm Package Compare versions

Comparing version
0.2.15
to
0.2.16
+132
-148
dist/index.cjs

@@ -1,2 +0,3 @@

//#region rolldown:runtime
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#region \0rolldown/runtime.js
var __create = Object.create;

@@ -22,17 +23,12 @@ var __defProp = Object.defineProperty;

}) : target, mod));
//#endregion
let fs = require("fs");
fs = __toESM(fs);
let path = require("path");
path = __toESM(path);
let url = require("url");
url = __toESM(url);
let fdir = require("fdir");
fdir = __toESM(fdir);
let picomatch = require("picomatch");
picomatch = __toESM(picomatch);
//#region src/utils.ts
const isReadonlyArray = Array.isArray;
const BACKSLASHES = /\\/g;
const isWin = process.platform === "win32";

@@ -44,4 +40,4 @@ const ONLY_PARENT_DIRECTORIES = /^(\/?\.\.)+$/;

const matchers = Array(patternsCount);
const globstarEnabled = !options.noglobstar;
for (let i = 0; i < patternsCount; i++) {
let i, j;
for (i = 0; i < patternsCount; i++) {
const parts = splitPattern(patterns[i]);

@@ -51,3 +47,3 @@ patternsParts[i] = parts;

const partMatchers = Array(partsCount);
for (let j = 0; j < partsCount; j++) partMatchers[j] = (0, picomatch.default)(parts[j], options);
for (j = 0; j < partsCount; j++) partMatchers[j] = (0, picomatch.default)(parts[j], options);
matchers[i] = partMatchers;

@@ -58,3 +54,3 @@ }

if (inputParts[0] === ".." && ONLY_PARENT_DIRECTORIES.test(input)) return true;
for (let i = 0; i < patterns.length; i++) {
for (i = 0; i < patternsCount; i++) {
const patternParts = patternsParts[i];

@@ -64,9 +60,8 @@ const matcher = matchers[i];

const minParts = Math.min(inputPatternCount, patternParts.length);
let j = 0;
j = 0;
while (j < minParts) {
const part = patternParts[j];
if (part.includes("/")) return true;
const match = matcher[j](inputParts[j]);
if (!match) break;
if (globstarEnabled && part === "**") return true;
if (!matcher[j](inputParts[j])) break;
if (!options.noglobstar && part === "**") return true;
j++;

@@ -85,3 +80,3 @@ }

if (absolute) {
const start = isRoot(cwd) ? cwd.length : cwd.length + 1;
const start = cwd.length + +!isRoot(cwd);
return (p, isDir) => p.slice(start, isDir ? -1 : void 0) || ".";

@@ -107,11 +102,10 @@ }

const result = path.posix.relative(cwd, `${root}/${p}`);
if (p.endsWith("/") && result !== "") return `${result}/`;
return result || ".";
return p[p.length - 1] === "/" && result !== "" ? `${result}/` : result || ".";
};
}
const splitPatternOptions = { parts: true };
function splitPattern(path$2) {
function splitPattern(path$1) {
var _result$parts;
const result = picomatch.default.scan(path$2, splitPatternOptions);
return ((_result$parts = result.parts) === null || _result$parts === void 0 ? void 0 : _result$parts.length) ? result.parts : [path$2];
const result = picomatch.default.scan(path$1, splitPatternOptions);
return ((_result$parts = result.parts) === null || _result$parts === void 0 ? void 0 : _result$parts.length) ? result.parts : [path$1];
}

@@ -122,4 +116,4 @@ const ESCAPED_WIN32_BACKSLASHES = /\\(?![()[\]{}!+@])/g;

}
function convertWin32PathToPattern(path$2) {
return escapeWin32Path(path$2).replace(ESCAPED_WIN32_BACKSLASHES, "/");
function convertWin32PathToPattern(path$3) {
return escapeWin32Path(path$3).replace(ESCAPED_WIN32_BACKSLASHES, "/");
}

@@ -135,4 +129,4 @@ /**

const WIN32_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}]|^!|[!+@](?=\())/g;
const escapePosixPath = (path$2) => path$2.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, "\\$&");
const escapeWin32Path = (path$2) => path$2.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, "\\$&");
const escapePosixPath = (path$4) => path$4.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, "\\$&");
const escapeWin32Path = (path$5) => path$5.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, "\\$&");
/**

@@ -164,19 +158,21 @@ * Escapes a path's special characters depending on the platform.

}
function ensureStringArray(value) {
return typeof value === "string" ? [value] : value !== null && value !== void 0 ? value : [];
}
//#endregion
//#region src/index.ts
//#region src/patterns.ts
const PARENT_DIRECTORY = /^(\/?\.\.)+/;
const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g;
const BACKSLASHES = /\\/g;
function normalizePattern(pattern, expandDirectories, cwd, props, isIgnore) {
function normalizePattern(pattern, opts, props, isIgnore) {
var _PARENT_DIRECTORY$exe;
const cwd = opts.cwd;
let result = pattern;
if (pattern.endsWith("/")) result = pattern.slice(0, -1);
if (!result.endsWith("*") && expandDirectories) result += "/**";
if (pattern[pattern.length - 1] === "/") result = pattern.slice(0, -1);
if (result[result.length - 1] !== "*" && opts.expandDirectories) result += "/**";
const escapedCwd = escapePath(cwd);
if (path.default.isAbsolute(result.replace(ESCAPING_BACKSLASHES, ""))) result = path.posix.relative(escapedCwd, result);
else result = path.posix.normalize(result);
const parentDirectoryMatch = PARENT_DIRECTORY.exec(result);
result = (0, path.isAbsolute)(result.replace(ESCAPING_BACKSLASHES, "")) ? path.posix.relative(escapedCwd, result) : path.posix.normalize(result);
const parentDir = (_PARENT_DIRECTORY$exe = PARENT_DIRECTORY.exec(result)) === null || _PARENT_DIRECTORY$exe === void 0 ? void 0 : _PARENT_DIRECTORY$exe[0];
const parts = splitPattern(result);
if (parentDirectoryMatch === null || parentDirectoryMatch === void 0 ? void 0 : parentDirectoryMatch[0]) {
const n = (parentDirectoryMatch[0].length + 1) / 3;
if (parentDir) {
const n = (parentDir.length + 1) / 3;
let i = 0;

@@ -188,4 +184,4 @@ const cwdParts = escapedCwd.split("/");

}
const potentialRoot = path.posix.join(cwd, parentDirectoryMatch[0].slice(i * 3));
if (!potentialRoot.startsWith(".") && props.root.length > potentialRoot.length) {
const potentialRoot = path.posix.join(cwd, parentDir.slice(i * 3));
if (potentialRoot[0] !== "." && props.root.length > potentialRoot.length) {
props.root = potentialRoot;

@@ -206,3 +202,3 @@ props.depthOffset = -n + i;

}
if (part !== props.commonPath[i] || isDynamicPattern(part) || i === parts.length - 1) break;
if (i === parts.length - 1 || part !== props.commonPath[i] || isDynamicPattern(part)) break;
newCommonPath.push(part);

@@ -216,15 +212,13 @@ }

}
function processPatterns({ patterns = ["**/*"], ignore = [], expandDirectories = true }, cwd, props) {
if (typeof patterns === "string") patterns = [patterns];
if (typeof ignore === "string") ignore = [ignore];
function processPatterns(options, patterns, props) {
const matchPatterns = [];
const ignorePatterns = [];
for (const pattern of ignore) {
for (const pattern of options.ignore) {
if (!pattern) continue;
if (pattern[0] !== "!" || pattern[1] === "(") ignorePatterns.push(normalizePattern(pattern, expandDirectories, cwd, props, true));
if (pattern[0] !== "!" || pattern[1] === "(") ignorePatterns.push(normalizePattern(pattern, options, props, true));
}
for (const pattern of patterns) {
if (!pattern) continue;
if (pattern[0] !== "!" || pattern[1] === "(") matchPatterns.push(normalizePattern(pattern, expandDirectories, cwd, props, false));
else if (pattern[1] !== "!" || pattern[2] === "(") ignorePatterns.push(normalizePattern(pattern.slice(1), expandDirectories, cwd, props, true));
if (pattern[0] !== "!" || pattern[1] === "(") matchPatterns.push(normalizePattern(pattern, options, props, false));
else if (pattern[1] !== "!" || pattern[2] === "(") ignorePatterns.push(normalizePattern(pattern.slice(1), options, props, true));
}

@@ -236,43 +230,18 @@ return {

}
function formatPaths(paths, relative) {
for (let i = paths.length - 1; i >= 0; i--) {
const path$2 = paths[i];
paths[i] = relative(path$2);
}
return paths;
}
function normalizeCwd(cwd) {
if (!cwd) return process.cwd().replace(BACKSLASHES, "/");
if (cwd instanceof URL) return (0, url.fileURLToPath)(cwd).replace(BACKSLASHES, "/");
return path.default.resolve(cwd).replace(BACKSLASHES, "/");
}
function getCrawler(patterns, inputOptions = {}) {
const options = process.env.TINYGLOBBY_DEBUG ? {
...inputOptions,
debug: true
} : inputOptions;
const cwd = normalizeCwd(options.cwd);
if (options.debug) log("globbing with:", {
patterns,
options,
cwd
});
if (Array.isArray(patterns) && patterns.length === 0) return [{
sync: () => [],
withPromise: async () => []
}, false];
//#endregion
//#region src/crawler.ts
function buildCrawler(options, patterns) {
const cwd = options.cwd;
const props = {
root: cwd,
commonPath: null,
depthOffset: 0
};
const processed = processPatterns({
...options,
patterns
}, cwd, props);
const processed = processPatterns(options, patterns, props);
if (options.debug) log("internal processing patterns:", processed);
const { absolute, caseSensitiveMatch, debug, dot, followSymbolicLinks, onlyDirectories } = options;
const root = props.root.replace(BACKSLASHES, "");
const matchOptions = {
dot: options.dot,
dot,
nobrace: options.braceExpansion === false,
nocase: options.caseSensitiveMatch === false,
nocase: !caseSensitiveMatch,
noextglob: options.extglob === false,

@@ -282,79 +251,94 @@ noglobstar: options.globstar === false,

};
const matcher = (0, picomatch.default)(processed.match, {
...matchOptions,
ignore: processed.ignore
});
const matcher = (0, picomatch.default)(processed.match, matchOptions);
const ignore = (0, picomatch.default)(processed.ignore, matchOptions);
const partialMatcher = getPartialMatcher(processed.match, matchOptions);
const format = buildFormat(cwd, props.root, options.absolute);
const formatExclude = options.absolute ? format : buildFormat(cwd, props.root, true);
const fdirOptions = {
filters: [options.debug ? (p, isDirectory) => {
const path$2 = format(p, isDirectory);
const matches = matcher(path$2);
if (matches) log(`matched ${path$2}`);
const format = buildFormat(cwd, root, absolute);
const excludeFormatter = absolute ? format : buildFormat(cwd, root, true);
const excludePredicate = (_, p) => {
const relativePath = excludeFormatter(p, true);
return relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
};
let maxDepth;
if (options.deep !== void 0) maxDepth = Math.round(options.deep - props.depthOffset);
const crawler = new fdir.fdir({
filters: [debug ? (p, isDirectory) => {
const path = format(p, isDirectory);
const matches = matcher(path) && !ignore(path);
if (matches) log(`matched ${path}`);
return matches;
} : (p, isDirectory) => matcher(format(p, isDirectory))],
exclude: options.debug ? (_, p) => {
const relativePath = formatExclude(p, true);
const skipped = relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
if (skipped) log(`skipped ${p}`);
else log(`crawling ${p}`);
} : (p, isDirectory) => {
const path = format(p, isDirectory);
return matcher(path) && !ignore(path);
}],
exclude: debug ? (_, p) => {
const skipped = excludePredicate(_, p);
log(`${skipped ? "skipped" : "crawling"} ${p}`);
return skipped;
} : (_, p) => {
const relativePath = formatExclude(p, true);
return relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
},
fs: options.fs ? {
readdir: options.fs.readdir || fs.default.readdir,
readdirSync: options.fs.readdirSync || fs.default.readdirSync,
realpath: options.fs.realpath || fs.default.realpath,
realpathSync: options.fs.realpathSync || fs.default.realpathSync,
stat: options.fs.stat || fs.default.stat,
statSync: options.fs.statSync || fs.default.statSync
} : void 0,
} : excludePredicate,
fs: options.fs,
pathSeparator: "/",
relativePaths: true,
resolveSymlinks: true,
relativePaths: !absolute,
resolvePaths: absolute,
includeBasePath: absolute,
resolveSymlinks: followSymbolicLinks,
excludeSymlinks: !followSymbolicLinks,
excludeFiles: onlyDirectories,
includeDirs: onlyDirectories || !options.onlyFiles,
maxDepth,
signal: options.signal
}).crawl(root);
if (options.debug) log("internal properties:", {
...props,
root
});
return [crawler, cwd !== root && !absolute && buildRelative(cwd, root)];
}
//#endregion
//#region src/index.ts
function formatPaths(paths, mapper) {
if (mapper) for (let i = paths.length - 1; i >= 0; i--) paths[i] = mapper(paths[i]);
return paths;
}
const defaultOptions = {
caseSensitiveMatch: true,
cwd: process.cwd(),
debug: !!process.env.TINYGLOBBY_DEBUG,
expandDirectories: true,
followSymbolicLinks: true,
onlyFiles: true
};
function getOptions(options) {
const opts = {
...defaultOptions,
...options
};
if (options.deep !== void 0) fdirOptions.maxDepth = Math.round(options.deep - props.depthOffset);
if (options.absolute) {
fdirOptions.relativePaths = false;
fdirOptions.resolvePaths = true;
fdirOptions.includeBasePath = true;
}
if (options.followSymbolicLinks === false) {
fdirOptions.resolveSymlinks = false;
fdirOptions.excludeSymlinks = true;
}
if (options.onlyDirectories) {
fdirOptions.excludeFiles = true;
fdirOptions.includeDirs = true;
} else if (options.onlyFiles === false) fdirOptions.includeDirs = true;
props.root = props.root.replace(BACKSLASHES, "");
const root = props.root;
if (options.debug) log("internal properties:", props);
const relative = cwd !== root && !options.absolute && buildRelative(cwd, props.root);
return [new fdir.fdir(fdirOptions).crawl(root), relative];
opts.cwd = (opts.cwd instanceof URL ? (0, url.fileURLToPath)(opts.cwd) : (0, path.resolve)(opts.cwd)).replace(BACKSLASHES, "/");
opts.ignore = ensureStringArray(opts.ignore);
opts.fs && (opts.fs = {
readdir: opts.fs.readdir || fs.readdir,
readdirSync: opts.fs.readdirSync || fs.readdirSync,
realpath: opts.fs.realpath || fs.realpath,
realpathSync: opts.fs.realpathSync || fs.realpathSync,
stat: opts.fs.stat || fs.stat,
statSync: opts.fs.statSync || fs.statSync
});
if (opts.debug) log("globbing with options:", opts);
return opts;
}
async function glob(patternsOrOptions, options) {
if (patternsOrOptions && (options === null || options === void 0 ? void 0 : options.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
const isModern = isReadonlyArray(patternsOrOptions) || typeof patternsOrOptions === "string";
const opts = isModern ? options : patternsOrOptions;
const patterns = isModern ? patternsOrOptions : patternsOrOptions.patterns;
const [crawler, relative] = getCrawler(patterns, opts);
if (!relative) return crawler.withPromise();
return formatPaths(await crawler.withPromise(), relative);
function getCrawler(globInput, inputOptions = {}) {
var _ref;
if (globInput && (inputOptions === null || inputOptions === void 0 ? void 0 : inputOptions.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
const isModern = isReadonlyArray(globInput) || typeof globInput === "string";
const patterns = ensureStringArray((_ref = isModern ? globInput : globInput.patterns) !== null && _ref !== void 0 ? _ref : "**/*");
const options = getOptions(isModern ? inputOptions : globInput);
return patterns.length > 0 ? buildCrawler(options, patterns) : [];
}
function globSync(patternsOrOptions, options) {
if (patternsOrOptions && (options === null || options === void 0 ? void 0 : options.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
const isModern = isReadonlyArray(patternsOrOptions) || typeof patternsOrOptions === "string";
const opts = isModern ? options : patternsOrOptions;
const patterns = isModern ? patternsOrOptions : patternsOrOptions.patterns;
const [crawler, relative] = getCrawler(patterns, opts);
if (!relative) return crawler.sync();
return formatPaths(crawler.sync(), relative);
async function glob(globInput, options) {
const [crawler, relative] = getCrawler(globInput, options);
return crawler ? formatPaths(await crawler.withPromise(), relative) : [];
}
function globSync(globInput, options) {
const [crawler, relative] = getCrawler(globInput, options);
return crawler ? formatPaths(crawler.sync(), relative) : [];
}
//#endregion

@@ -365,2 +349,2 @@ exports.convertPathToPattern = convertPathToPattern;

exports.globSync = globSync;
exports.isDynamicPattern = isDynamicPattern;
exports.isDynamicPattern = isDynamicPattern;
import { FSLike } from "fdir";
//#region src/utils.d.ts
/**
* Converts a path to a pattern depending on the platform.
* Identical to {@link escapePath} on POSIX systems.
* @see {@link https://superchupu.dev/tinyglobby/documentation#convertPathToPattern}
*/
declare const convertPathToPattern: (path: string) => string;
/**
* Escapes a path's special characters depending on the platform.
* @see {@link https://superchupu.dev/tinyglobby/documentation#escapePath}
*/
declare const escapePath: (path: string) => string;
/**
* Checks if a pattern has dynamic parts.
*
* Has a few minor differences with [`fast-glob`](https://github.com/mrmlnc/fast-glob) for better accuracy:
*
* - Doesn't necessarily return `false` on patterns that include `\`.
* - Returns `true` if the pattern includes parentheses, regardless of them representing one single pattern or not.
* - Returns `true` for unfinished glob extensions i.e. `(h`, `+(h`.
* - Returns `true` for unfinished brace expansions as long as they include `,` or `..`.
*
* @see {@link https://superchupu.dev/tinyglobby/documentation#isDynamicPattern}
*/
declare function isDynamicPattern(pattern: string, options?: {
caseSensitiveMatch: boolean;
}): boolean;
//#endregion
//#region src/index.d.ts
//#region src/types.d.ts
type FileSystemAdapter = Partial<FSLike>;
interface GlobOptions {

@@ -127,4 +99,33 @@ /**

}
type FileSystemAdapter = Partial<FSLike>;
//#endregion
//#region src/utils.d.ts
/**
* Converts a path to a pattern depending on the platform.
* Identical to {@link escapePath} on POSIX systems.
* @see {@link https://superchupu.dev/tinyglobby/documentation#convertPathToPattern}
*/
declare const convertPathToPattern: (path: string) => string;
/**
* Escapes a path's special characters depending on the platform.
* @see {@link https://superchupu.dev/tinyglobby/documentation#escapePath}
*/
declare const escapePath: (path: string) => string;
/**
* Checks if a pattern has dynamic parts.
*
* Has a few minor differences with [`fast-glob`](https://github.com/mrmlnc/fast-glob) for better accuracy:
*
* - Doesn't necessarily return `false` on patterns that include `\`.
* - Returns `true` if the pattern includes parentheses, regardless of them representing one single pattern or not.
* - Returns `true` for unfinished glob extensions i.e. `(h`, `+(h`.
* - Returns `true` for unfinished brace expansions as long as they include `,` or `..`.
*
* @see {@link https://superchupu.dev/tinyglobby/documentation#isDynamicPattern}
*/
declare function isDynamicPattern(pattern: string, options?: {
caseSensitiveMatch: boolean;
}): boolean;
//#endregion
//#region src/index.d.ts
/**
* Asynchronously match files following a glob pattern.

@@ -148,2 +149,2 @@ * @see {@link https://superchupu.dev/tinyglobby/documentation#glob}

//#endregion
export { FileSystemAdapter, GlobOptions, convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };
export { type GlobOptions, convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };
import { FSLike } from "fdir";
//#region src/utils.d.ts
/**
* Converts a path to a pattern depending on the platform.
* Identical to {@link escapePath} on POSIX systems.
* @see {@link https://superchupu.dev/tinyglobby/documentation#convertPathToPattern}
*/
declare const convertPathToPattern: (path: string) => string;
/**
* Escapes a path's special characters depending on the platform.
* @see {@link https://superchupu.dev/tinyglobby/documentation#escapePath}
*/
declare const escapePath: (path: string) => string;
/**
* Checks if a pattern has dynamic parts.
*
* Has a few minor differences with [`fast-glob`](https://github.com/mrmlnc/fast-glob) for better accuracy:
*
* - Doesn't necessarily return `false` on patterns that include `\`.
* - Returns `true` if the pattern includes parentheses, regardless of them representing one single pattern or not.
* - Returns `true` for unfinished glob extensions i.e. `(h`, `+(h`.
* - Returns `true` for unfinished brace expansions as long as they include `,` or `..`.
*
* @see {@link https://superchupu.dev/tinyglobby/documentation#isDynamicPattern}
*/
declare function isDynamicPattern(pattern: string, options?: {
caseSensitiveMatch: boolean;
}): boolean;
//#endregion
//#region src/index.d.ts
//#region src/types.d.ts
type FileSystemAdapter = Partial<FSLike>;
interface GlobOptions {

@@ -127,4 +99,33 @@ /**

}
type FileSystemAdapter = Partial<FSLike>;
//#endregion
//#region src/utils.d.ts
/**
* Converts a path to a pattern depending on the platform.
* Identical to {@link escapePath} on POSIX systems.
* @see {@link https://superchupu.dev/tinyglobby/documentation#convertPathToPattern}
*/
declare const convertPathToPattern: (path: string) => string;
/**
* Escapes a path's special characters depending on the platform.
* @see {@link https://superchupu.dev/tinyglobby/documentation#escapePath}
*/
declare const escapePath: (path: string) => string;
/**
* Checks if a pattern has dynamic parts.
*
* Has a few minor differences with [`fast-glob`](https://github.com/mrmlnc/fast-glob) for better accuracy:
*
* - Doesn't necessarily return `false` on patterns that include `\`.
* - Returns `true` if the pattern includes parentheses, regardless of them representing one single pattern or not.
* - Returns `true` for unfinished glob extensions i.e. `(h`, `+(h`.
* - Returns `true` for unfinished brace expansions as long as they include `,` or `..`.
*
* @see {@link https://superchupu.dev/tinyglobby/documentation#isDynamicPattern}
*/
declare function isDynamicPattern(pattern: string, options?: {
caseSensitiveMatch: boolean;
}): boolean;
//#endregion
//#region src/index.d.ts
/**
* Asynchronously match files following a glob pattern.

@@ -148,2 +149,2 @@ * @see {@link https://superchupu.dev/tinyglobby/documentation#glob}

//#endregion
export { FileSystemAdapter, GlobOptions, convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };
export { type GlobOptions, convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };

@@ -1,9 +0,9 @@

import nativeFs from "fs";
import path, { posix } from "path";
import { readdir, readdirSync, realpath, realpathSync, stat, statSync } from "fs";
import { isAbsolute, posix, resolve } from "path";
import { fileURLToPath } from "url";
import { fdir } from "fdir";
import picomatch from "picomatch";
//#region src/utils.ts
const isReadonlyArray = Array.isArray;
const BACKSLASHES = /\\/g;
const isWin = process.platform === "win32";

@@ -15,4 +15,4 @@ const ONLY_PARENT_DIRECTORIES = /^(\/?\.\.)+$/;

const matchers = Array(patternsCount);
const globstarEnabled = !options.noglobstar;
for (let i = 0; i < patternsCount; i++) {
let i, j;
for (i = 0; i < patternsCount; i++) {
const parts = splitPattern(patterns[i]);

@@ -22,3 +22,3 @@ patternsParts[i] = parts;

const partMatchers = Array(partsCount);
for (let j = 0; j < partsCount; j++) partMatchers[j] = picomatch(parts[j], options);
for (j = 0; j < partsCount; j++) partMatchers[j] = picomatch(parts[j], options);
matchers[i] = partMatchers;

@@ -29,3 +29,3 @@ }

if (inputParts[0] === ".." && ONLY_PARENT_DIRECTORIES.test(input)) return true;
for (let i = 0; i < patterns.length; i++) {
for (i = 0; i < patternsCount; i++) {
const patternParts = patternsParts[i];

@@ -35,9 +35,8 @@ const matcher = matchers[i];

const minParts = Math.min(inputPatternCount, patternParts.length);
let j = 0;
j = 0;
while (j < minParts) {
const part = patternParts[j];
if (part.includes("/")) return true;
const match = matcher[j](inputParts[j]);
if (!match) break;
if (globstarEnabled && part === "**") return true;
if (!matcher[j](inputParts[j])) break;
if (!options.noglobstar && part === "**") return true;
j++;

@@ -56,3 +55,3 @@ }

if (absolute) {
const start = isRoot(cwd) ? cwd.length : cwd.length + 1;
const start = cwd.length + +!isRoot(cwd);
return (p, isDir) => p.slice(start, isDir ? -1 : void 0) || ".";

@@ -78,18 +77,17 @@ }

const result = posix.relative(cwd, `${root}/${p}`);
if (p.endsWith("/") && result !== "") return `${result}/`;
return result || ".";
return p[p.length - 1] === "/" && result !== "" ? `${result}/` : result || ".";
};
}
const splitPatternOptions = { parts: true };
function splitPattern(path$1) {
function splitPattern(path) {
var _result$parts;
const result = picomatch.scan(path$1, splitPatternOptions);
return ((_result$parts = result.parts) === null || _result$parts === void 0 ? void 0 : _result$parts.length) ? result.parts : [path$1];
const result = picomatch.scan(path, splitPatternOptions);
return ((_result$parts = result.parts) === null || _result$parts === void 0 ? void 0 : _result$parts.length) ? result.parts : [path];
}
const ESCAPED_WIN32_BACKSLASHES = /\\(?![()[\]{}!+@])/g;
function convertPosixPathToPattern(path$1) {
return escapePosixPath(path$1);
function convertPosixPathToPattern(path) {
return escapePosixPath(path);
}
function convertWin32PathToPattern(path$1) {
return escapeWin32Path(path$1).replace(ESCAPED_WIN32_BACKSLASHES, "/");
function convertWin32PathToPattern(path) {
return escapeWin32Path(path).replace(ESCAPED_WIN32_BACKSLASHES, "/");
}

@@ -105,4 +103,4 @@ /**

const WIN32_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}]|^!|[!+@](?=\())/g;
const escapePosixPath = (path$1) => path$1.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, "\\$&");
const escapeWin32Path = (path$1) => path$1.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, "\\$&");
const escapePosixPath = (path) => path.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, "\\$&");
const escapeWin32Path = (path) => path.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, "\\$&");
/**

@@ -134,19 +132,21 @@ * Escapes a path's special characters depending on the platform.

}
function ensureStringArray(value) {
return typeof value === "string" ? [value] : value !== null && value !== void 0 ? value : [];
}
//#endregion
//#region src/index.ts
//#region src/patterns.ts
const PARENT_DIRECTORY = /^(\/?\.\.)+/;
const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g;
const BACKSLASHES = /\\/g;
function normalizePattern(pattern, expandDirectories, cwd, props, isIgnore) {
function normalizePattern(pattern, opts, props, isIgnore) {
var _PARENT_DIRECTORY$exe;
const cwd = opts.cwd;
let result = pattern;
if (pattern.endsWith("/")) result = pattern.slice(0, -1);
if (!result.endsWith("*") && expandDirectories) result += "/**";
if (pattern[pattern.length - 1] === "/") result = pattern.slice(0, -1);
if (result[result.length - 1] !== "*" && opts.expandDirectories) result += "/**";
const escapedCwd = escapePath(cwd);
if (path.isAbsolute(result.replace(ESCAPING_BACKSLASHES, ""))) result = posix.relative(escapedCwd, result);
else result = posix.normalize(result);
const parentDirectoryMatch = PARENT_DIRECTORY.exec(result);
result = isAbsolute(result.replace(ESCAPING_BACKSLASHES, "")) ? posix.relative(escapedCwd, result) : posix.normalize(result);
const parentDir = (_PARENT_DIRECTORY$exe = PARENT_DIRECTORY.exec(result)) === null || _PARENT_DIRECTORY$exe === void 0 ? void 0 : _PARENT_DIRECTORY$exe[0];
const parts = splitPattern(result);
if (parentDirectoryMatch === null || parentDirectoryMatch === void 0 ? void 0 : parentDirectoryMatch[0]) {
const n = (parentDirectoryMatch[0].length + 1) / 3;
if (parentDir) {
const n = (parentDir.length + 1) / 3;
let i = 0;

@@ -158,4 +158,4 @@ const cwdParts = escapedCwd.split("/");

}
const potentialRoot = posix.join(cwd, parentDirectoryMatch[0].slice(i * 3));
if (!potentialRoot.startsWith(".") && props.root.length > potentialRoot.length) {
const potentialRoot = posix.join(cwd, parentDir.slice(i * 3));
if (potentialRoot[0] !== "." && props.root.length > potentialRoot.length) {
props.root = potentialRoot;

@@ -176,3 +176,3 @@ props.depthOffset = -n + i;

}
if (part !== props.commonPath[i] || isDynamicPattern(part) || i === parts.length - 1) break;
if (i === parts.length - 1 || part !== props.commonPath[i] || isDynamicPattern(part)) break;
newCommonPath.push(part);

@@ -186,15 +186,13 @@ }

}
function processPatterns({ patterns = ["**/*"], ignore = [], expandDirectories = true }, cwd, props) {
if (typeof patterns === "string") patterns = [patterns];
if (typeof ignore === "string") ignore = [ignore];
function processPatterns(options, patterns, props) {
const matchPatterns = [];
const ignorePatterns = [];
for (const pattern of ignore) {
for (const pattern of options.ignore) {
if (!pattern) continue;
if (pattern[0] !== "!" || pattern[1] === "(") ignorePatterns.push(normalizePattern(pattern, expandDirectories, cwd, props, true));
if (pattern[0] !== "!" || pattern[1] === "(") ignorePatterns.push(normalizePattern(pattern, options, props, true));
}
for (const pattern of patterns) {
if (!pattern) continue;
if (pattern[0] !== "!" || pattern[1] === "(") matchPatterns.push(normalizePattern(pattern, expandDirectories, cwd, props, false));
else if (pattern[1] !== "!" || pattern[2] === "(") ignorePatterns.push(normalizePattern(pattern.slice(1), expandDirectories, cwd, props, true));
if (pattern[0] !== "!" || pattern[1] === "(") matchPatterns.push(normalizePattern(pattern, options, props, false));
else if (pattern[1] !== "!" || pattern[2] === "(") ignorePatterns.push(normalizePattern(pattern.slice(1), options, props, true));
}

@@ -206,43 +204,18 @@ return {

}
function formatPaths(paths, relative) {
for (let i = paths.length - 1; i >= 0; i--) {
const path$1 = paths[i];
paths[i] = relative(path$1);
}
return paths;
}
function normalizeCwd(cwd) {
if (!cwd) return process.cwd().replace(BACKSLASHES, "/");
if (cwd instanceof URL) return fileURLToPath(cwd).replace(BACKSLASHES, "/");
return path.resolve(cwd).replace(BACKSLASHES, "/");
}
function getCrawler(patterns, inputOptions = {}) {
const options = process.env.TINYGLOBBY_DEBUG ? {
...inputOptions,
debug: true
} : inputOptions;
const cwd = normalizeCwd(options.cwd);
if (options.debug) log("globbing with:", {
patterns,
options,
cwd
});
if (Array.isArray(patterns) && patterns.length === 0) return [{
sync: () => [],
withPromise: async () => []
}, false];
//#endregion
//#region src/crawler.ts
function buildCrawler(options, patterns) {
const cwd = options.cwd;
const props = {
root: cwd,
commonPath: null,
depthOffset: 0
};
const processed = processPatterns({
...options,
patterns
}, cwd, props);
const processed = processPatterns(options, patterns, props);
if (options.debug) log("internal processing patterns:", processed);
const { absolute, caseSensitiveMatch, debug, dot, followSymbolicLinks, onlyDirectories } = options;
const root = props.root.replace(BACKSLASHES, "");
const matchOptions = {
dot: options.dot,
dot,
nobrace: options.braceExpansion === false,
nocase: options.caseSensitiveMatch === false,
nocase: !caseSensitiveMatch,
noextglob: options.extglob === false,

@@ -252,80 +225,95 @@ noglobstar: options.globstar === false,

};
const matcher = picomatch(processed.match, {
...matchOptions,
ignore: processed.ignore
});
const matcher = picomatch(processed.match, matchOptions);
const ignore = picomatch(processed.ignore, matchOptions);
const partialMatcher = getPartialMatcher(processed.match, matchOptions);
const format = buildFormat(cwd, props.root, options.absolute);
const formatExclude = options.absolute ? format : buildFormat(cwd, props.root, true);
const fdirOptions = {
filters: [options.debug ? (p, isDirectory) => {
const path$1 = format(p, isDirectory);
const matches = matcher(path$1);
if (matches) log(`matched ${path$1}`);
const format = buildFormat(cwd, root, absolute);
const excludeFormatter = absolute ? format : buildFormat(cwd, root, true);
const excludePredicate = (_, p) => {
const relativePath = excludeFormatter(p, true);
return relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
};
let maxDepth;
if (options.deep !== void 0) maxDepth = Math.round(options.deep - props.depthOffset);
const crawler = new fdir({
filters: [debug ? (p, isDirectory) => {
const path = format(p, isDirectory);
const matches = matcher(path) && !ignore(path);
if (matches) log(`matched ${path}`);
return matches;
} : (p, isDirectory) => matcher(format(p, isDirectory))],
exclude: options.debug ? (_, p) => {
const relativePath = formatExclude(p, true);
const skipped = relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
if (skipped) log(`skipped ${p}`);
else log(`crawling ${p}`);
} : (p, isDirectory) => {
const path = format(p, isDirectory);
return matcher(path) && !ignore(path);
}],
exclude: debug ? (_, p) => {
const skipped = excludePredicate(_, p);
log(`${skipped ? "skipped" : "crawling"} ${p}`);
return skipped;
} : (_, p) => {
const relativePath = formatExclude(p, true);
return relativePath !== "." && !partialMatcher(relativePath) || ignore(relativePath);
},
fs: options.fs ? {
readdir: options.fs.readdir || nativeFs.readdir,
readdirSync: options.fs.readdirSync || nativeFs.readdirSync,
realpath: options.fs.realpath || nativeFs.realpath,
realpathSync: options.fs.realpathSync || nativeFs.realpathSync,
stat: options.fs.stat || nativeFs.stat,
statSync: options.fs.statSync || nativeFs.statSync
} : void 0,
} : excludePredicate,
fs: options.fs,
pathSeparator: "/",
relativePaths: true,
resolveSymlinks: true,
relativePaths: !absolute,
resolvePaths: absolute,
includeBasePath: absolute,
resolveSymlinks: followSymbolicLinks,
excludeSymlinks: !followSymbolicLinks,
excludeFiles: onlyDirectories,
includeDirs: onlyDirectories || !options.onlyFiles,
maxDepth,
signal: options.signal
}).crawl(root);
if (options.debug) log("internal properties:", {
...props,
root
});
return [crawler, cwd !== root && !absolute && buildRelative(cwd, root)];
}
//#endregion
//#region src/index.ts
function formatPaths(paths, mapper) {
if (mapper) for (let i = paths.length - 1; i >= 0; i--) paths[i] = mapper(paths[i]);
return paths;
}
const defaultOptions = {
caseSensitiveMatch: true,
cwd: process.cwd(),
debug: !!process.env.TINYGLOBBY_DEBUG,
expandDirectories: true,
followSymbolicLinks: true,
onlyFiles: true
};
function getOptions(options) {
const opts = {
...defaultOptions,
...options
};
if (options.deep !== void 0) fdirOptions.maxDepth = Math.round(options.deep - props.depthOffset);
if (options.absolute) {
fdirOptions.relativePaths = false;
fdirOptions.resolvePaths = true;
fdirOptions.includeBasePath = true;
}
if (options.followSymbolicLinks === false) {
fdirOptions.resolveSymlinks = false;
fdirOptions.excludeSymlinks = true;
}
if (options.onlyDirectories) {
fdirOptions.excludeFiles = true;
fdirOptions.includeDirs = true;
} else if (options.onlyFiles === false) fdirOptions.includeDirs = true;
props.root = props.root.replace(BACKSLASHES, "");
const root = props.root;
if (options.debug) log("internal properties:", props);
const relative = cwd !== root && !options.absolute && buildRelative(cwd, props.root);
return [new fdir(fdirOptions).crawl(root), relative];
opts.cwd = (opts.cwd instanceof URL ? fileURLToPath(opts.cwd) : resolve(opts.cwd)).replace(BACKSLASHES, "/");
opts.ignore = ensureStringArray(opts.ignore);
opts.fs && (opts.fs = {
readdir: opts.fs.readdir || readdir,
readdirSync: opts.fs.readdirSync || readdirSync,
realpath: opts.fs.realpath || realpath,
realpathSync: opts.fs.realpathSync || realpathSync,
stat: opts.fs.stat || stat,
statSync: opts.fs.statSync || statSync
});
if (opts.debug) log("globbing with options:", opts);
return opts;
}
async function glob(patternsOrOptions, options) {
if (patternsOrOptions && (options === null || options === void 0 ? void 0 : options.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
const isModern = isReadonlyArray(patternsOrOptions) || typeof patternsOrOptions === "string";
const opts = isModern ? options : patternsOrOptions;
const patterns = isModern ? patternsOrOptions : patternsOrOptions.patterns;
const [crawler, relative] = getCrawler(patterns, opts);
if (!relative) return crawler.withPromise();
return formatPaths(await crawler.withPromise(), relative);
function getCrawler(globInput, inputOptions = {}) {
var _ref;
if (globInput && (inputOptions === null || inputOptions === void 0 ? void 0 : inputOptions.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
const isModern = isReadonlyArray(globInput) || typeof globInput === "string";
const patterns = ensureStringArray((_ref = isModern ? globInput : globInput.patterns) !== null && _ref !== void 0 ? _ref : "**/*");
const options = getOptions(isModern ? inputOptions : globInput);
return patterns.length > 0 ? buildCrawler(options, patterns) : [];
}
function globSync(patternsOrOptions, options) {
if (patternsOrOptions && (options === null || options === void 0 ? void 0 : options.patterns)) throw new Error("Cannot pass patterns as both an argument and an option");
const isModern = isReadonlyArray(patternsOrOptions) || typeof patternsOrOptions === "string";
const opts = isModern ? options : patternsOrOptions;
const patterns = isModern ? patternsOrOptions : patternsOrOptions.patterns;
const [crawler, relative] = getCrawler(patterns, opts);
if (!relative) return crawler.sync();
return formatPaths(crawler.sync(), relative);
async function glob(globInput, options) {
const [crawler, relative] = getCrawler(globInput, options);
return crawler ? formatPaths(await crawler.withPromise(), relative) : [];
}
function globSync(globInput, options) {
const [crawler, relative] = getCrawler(globInput, options);
return crawler ? formatPaths(crawler.sync(), relative) : [];
}
//#endregion
export { convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };
export { convertPathToPattern, escapePath, glob, globSync, isDynamicPattern };
{
"name": "tinyglobby",
"version": "0.2.15",
"version": "0.2.16",
"description": "A fast and minimal alternative to globby and fast-glob",

@@ -41,14 +41,14 @@ "type": "module",

"fdir": "^6.5.0",
"picomatch": "^4.0.3"
"picomatch": "^4.0.4"
},
"devDependencies": {
"@biomejs/biome": "^2.2.3",
"@types/node": "^24.3.1",
"@types/picomatch": "^4.0.2",
"@biomejs/biome": "^2.4.10",
"@types/node": "^25.5.2",
"@types/picomatch": "^4.0.3",
"fast-glob": "^3.3.3",
"fs-fixture": "^2.8.1",
"glob": "^11.0.3",
"tinybench": "^5.0.1",
"tsdown": "^0.14.2",
"typescript": "^5.9.2"
"fs-fixture": "^2.13.0",
"glob": "^13.0.6",
"tinybench": "^6.0.0",
"tsdown": "^0.21.7",
"typescript": "^6.0.2"
},

@@ -55,0 +55,0 @@ "engines": {

@@ -11,3 +11,3 @@ # tinyglobby

This library uses only two subdependencies, compared to `globby`'s [23](https://npmgraph.js.org/?q=globby@14.1.0)
This library uses only two subdependencies, compared to `globby`'s [23](https://npmgraph.js.org/?q=globby@16.2.0)
and `fast-glob`'s [17](https://npmgraph.js.org/?q=fast-glob@3.3.3).

@@ -14,0 +14,0 @@