-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize glob implementation (#1537)
Adds an abstraction between glob implementation and the filesystem so it can be applied against arbitrary sources of hierarchical data. Also moves doc generation to a different theme that seems better maintained.
- Loading branch information
Showing
16 changed files
with
1,266 additions
and
1,305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* @license | ||
* Copyright 2022-2024 Matter.js Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { GLOBSTAR, Minimatch, ParseReturnFiltered } from "minimatch"; | ||
import { FilesystemSync } from "./file.js"; | ||
|
||
export class GlobError extends Error {} | ||
|
||
export function globSync(pattern: string | string[], fs = FilesystemSync()) { | ||
if (typeof pattern === "string") { | ||
return [...globOneSync(pattern, fs)]; | ||
} | ||
|
||
const result = Array<string>(); | ||
for (const p of pattern) { | ||
result.push(...globOneSync(p, fs)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function globOneSync(pattern: string, fs: FilesystemSync) { | ||
// Parse the glob | ||
const mm = new Minimatch(pattern.replace(/\\/g, "/"), {}); | ||
const results = new Set<string>(); | ||
for (const part of mm.set) { | ||
for (const path of globOnePartSync(mm, part, fs)) { | ||
results.add(path); | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
function globOnePartSync(mm: Minimatch, segments: ParseReturnFiltered[], fs: FilesystemSync) { | ||
// Find the starting path | ||
let rootPath = ""; | ||
let didOne = false; | ||
while (typeof segments[0] === "string") { | ||
if (didOne) { | ||
rootPath += "/"; | ||
} else { | ||
didOne = true; | ||
} | ||
rootPath += segments.shift() as string; | ||
} | ||
|
||
// If we are out of segments, this is not a glob. Just check for presence | ||
if (!segments.length) { | ||
const stat = fs.stat(rootPath); | ||
|
||
if (stat?.[rootPath.endsWith("/") ? "isDirectory" : "isFile"]) { | ||
return [rootPath]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
// Walk filesystem and apply glob | ||
const results = new Set<string>(); | ||
|
||
function match(path: string, segments: ParseReturnFiltered[]) { | ||
// If the filter is empty then match current path | ||
if (!segments.length) { | ||
results.add(path); | ||
return; | ||
} | ||
|
||
// If filter starts without magic then just stat that one path | ||
if (typeof segments[0] === "string") { | ||
const subpath = fs.resolve(path, segments[0]); | ||
if (fs.stat(fs.resolve(path, segments[0]))) { | ||
match(subpath, segments.slice(1)); | ||
return; | ||
} | ||
} | ||
|
||
// If filter is just GLOBSTAR then all paths match but search continues | ||
if (segments.length === 1 && segments[0] === GLOBSTAR) { | ||
results.add(path); | ||
} | ||
|
||
// Filter starts with magic so load directory entries to match | ||
const subnames = fs.readdir(path); | ||
if (!subnames) { | ||
return; | ||
} | ||
|
||
// Test each directory entry | ||
for (const subname of subnames) { | ||
const subpath = fs.resolve(path, subname); | ||
|
||
// Anything but GLOBSTAR is 1:1 subname/segment match | ||
if (segments[0] !== GLOBSTAR) { | ||
if (mm.matchOne([subname], segments, true)) { | ||
match(subpath, segments.slice(1)); | ||
} | ||
continue; | ||
} | ||
|
||
// GLOBSTAR matches nothing so test second segment | ||
if (segments.length > 1) { | ||
if (mm.matchOne([subname], segments.slice(1), true)) { | ||
match(subpath, segments.slice(2)); | ||
} | ||
} | ||
|
||
// GLOBSTAR matches everything | ||
match(subpath, segments); | ||
} | ||
} | ||
|
||
match(rootPath, segments); | ||
|
||
return results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.