Skip to content

Commit

Permalink
fix: allow relativePaths with onlyDirs
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jul 22, 2024
1 parent 6719a5b commit c82890a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/api/functions/push-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ export type PushDirectoryFunction = (
filters?: FilterPredicate[]
) => void;

function pushDirectoryWithRelativePath(root: string): PushDirectoryFunction {
return function (directoryPath, paths) {
paths.push((directoryPath || ".").substring(root.length));
};
}

function pushDirectoryFilterWithRelativePath(
root: string
): PushDirectoryFunction {
return function (directoryPath, paths, filters) {
const relativePath = directoryPath.substring(root.length);
if (filters!.every((filter) => filter(relativePath, true))) {
paths.push(relativePath);
}
};
}

const pushDirectory: PushDirectoryFunction = (directoryPath, paths) => {
paths.push(directoryPath || ".");
};
Expand All @@ -22,9 +39,13 @@ const pushDirectoryFilter: PushDirectoryFunction = (

const empty: PushDirectoryFunction = () => {};

export function build(options: Options): PushDirectoryFunction {
const { includeDirs, filters } = options;
export function build(root: string, options: Options): PushDirectoryFunction {
const { includeDirs, filters, relativePaths } = options;
if (!includeDirs) return empty;

if (relativePaths)
return filters && filters.length
? pushDirectoryFilterWithRelativePath(root)
: pushDirectoryWithRelativePath(root);
return filters && filters.length ? pushDirectoryFilter : pushDirectory;
}
2 changes: 1 addition & 1 deletion src/api/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Walker<TOutput extends Output> {
* by the javascript engine so there's no function call overhead (in most cases).
*/
this.joinPath = joinPath.build(this.root, options);
this.pushDirectory = pushDirectory.build(options);
this.pushDirectory = pushDirectory.build(this.root, options);
this.pushFile = pushFile.build(options);
this.getArray = getArray.build(options);
this.groupFiles = groupFiles.build(options);
Expand Down

0 comments on commit c82890a

Please sign in to comment.