Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul options creation #88

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
196dde2
change: overhaul fdir options building
Torathion Feb 16, 2025
bce72c0
perf: optimize getPartialMatcher
Torathion Feb 16, 2025
5381f1a
perf: optimize getPartialMatcher
Torathion Feb 16, 2025
7b92ffa
perf: overhaul tinyglobby options building
Torathion Feb 19, 2025
a31df97
change: move validateInput into crawl
Torathion Feb 19, 2025
5269a6d
change: centralize more options logic
Torathion Feb 19, 2025
7af95a7
revert: InternalProps extension
Torathion Feb 19, 2025
cb01187
perf: inline validateInput
Torathion Feb 19, 2025
7d112ed
perf: remove redundant check
Torathion Feb 19, 2025
4bb3b66
change: extend defaultOptions
Torathion Feb 19, 2025
1570eb1
Merge branch 'main' of https://github.com/Torathion/tinyglobby
Torathion Feb 20, 2025
ee67f8c
change: overhaul fdir options building
Torathion Feb 16, 2025
e635ae8
perf: overhaul tinyglobby options building
Torathion Feb 19, 2025
1054032
change: move validateInput into crawl
Torathion Feb 19, 2025
daff839
change: centralize more options logic
Torathion Feb 19, 2025
b611c4a
revert: InternalProps extension
Torathion Feb 19, 2025
b0cdc8c
perf: inline validateInput
Torathion Feb 19, 2025
b5017c4
perf: remove redundant check
Torathion Feb 19, 2025
17ca952
change: extend defaultOptions
Torathion Feb 19, 2025
3b862df
Merge branch 'fdir-overhaul' of https://github.com/Torathion/tinyglob…
Torathion Feb 20, 2025
32a5fce
add and optimize changes from 0.2.12
Torathion Feb 20, 2025
9de3b72
lint
Torathion Feb 20, 2025
eabce34
lint again
Torathion Feb 20, 2025
c4f5f11
perf: replace startsWith with direct comparison for less overhead
Torathion Feb 20, 2025
8cf0612
fix: specifically set dot to false for code stability
Torathion Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/fdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { posix } from 'node:path';
import { type PathsOutput, fdir } from 'fdir';
import type { APIBuilder } from 'fdir/dist/builder/api-builder';
import picomatch from 'picomatch';
import type { GlobOptions, InternalProps, PartialMatcherOptions, ProcessedPatterns } from './types';
import { getPartialMatcher, log } from './utils.ts';

// #region getRelativePath
// TODO: this is slow, find a better way to do this
export function getRelativePath(path: string, cwd: string, root: string): string {
return posix.relative(cwd, `${root}/${path}`) || '.';
}
// #endregion

// #region processPath
function processPath(path: string, cwd: string, root: string, isDirectory: boolean, absolute?: boolean) {
const relativePath = absolute ? path.slice(root.length + 1) || '.' : path;

if (root === cwd) {
return isDirectory && relativePath !== '.' ? relativePath.slice(0, -1) : relativePath;
}

return getRelativePath(relativePath, cwd, root);
}
// #endregion processPath

// #region formatPaths
export function formatPaths(paths: string[], cwd: string, root: string): string[] {
for (let i = paths.length - 1; i >= 0; i--) {
const path = paths[i];
paths[i] = getRelativePath(path, cwd, root) + (!path || path.endsWith('/') ? '/' : '');
}
return paths;
}
// #endregion formatPaths

// #region buildFdir
export function buildFdir(
options: GlobOptions,
props: InternalProps,
processed: ProcessedPatterns,
cwd: string,
root: string
): APIBuilder<PathsOutput> {
const { absolute, debug, followSymbolicLinks, onlyDirectories } = options;
const nocase = !options.caseSensitiveMatch;

const matcher = picomatch(processed.match, {
dot: options.dot,
nocase,
ignore: processed.ignore
});

const partialMatcherOptions: PartialMatcherOptions = { dot: options.dot, nocase };
const ignore = picomatch(processed.ignore, partialMatcherOptions);
const partialMatcher = getPartialMatcher(processed.match, partialMatcherOptions);

return new fdir({
filters: [
(p, isDirectory) => {
const path = processPath(p, cwd, root, isDirectory, absolute);
const matches = matcher(path);
if (debug && matches) {
log(`matched ${path}`);
}
return matches;
}
],
exclude: (_, p) => {
const relativePath = processPath(p, cwd, root, true, true);
const skipped = (relativePath !== '.' && !partialMatcher(relativePath)) || ignore(relativePath);
if (debug) {
log(`${skipped ? 'skipped' : 'crawling'} ${p}`);
}
return skipped;
},
pathSeparator: '/',
relativePaths: !absolute,
resolvePaths: absolute,
includeBasePath: absolute,
resolveSymlinks: followSymbolicLinks,
excludeSymlinks: !followSymbolicLinks,
excludeFiles: onlyDirectories,
includeDirs: onlyDirectories || !options.onlyFiles,
maxDepth: options.deep && Math.round(options.deep - props.depthOffset)
}).crawl(root);
}
// #endregion buildFdir
Loading