summaryrefslogtreecommitdiff
path: root/fs/glob.ts
diff options
context:
space:
mode:
Diffstat (limited to 'fs/glob.ts')
-rw-r--r--fs/glob.ts67
1 files changed, 66 insertions, 1 deletions
diff --git a/fs/glob.ts b/fs/glob.ts
index 506d4012c..b974f2b29 100644
--- a/fs/glob.ts
+++ b/fs/glob.ts
@@ -1,4 +1,7 @@
import { globrex } from "./globrex.ts";
+import { isAbsolute, join } from "./path/mod.ts";
+import { WalkInfo, walk, walkSync } from "./walk.ts";
+const { cwd } = Deno;
export interface GlobOptions {
// Allow ExtGlob features
@@ -41,7 +44,11 @@ export interface GlobOptions {
* @returns A RegExp for the glob pattern
*/
export function glob(glob: string, options: GlobOptions = {}): RegExp {
- return globrex(glob, options).regex;
+ const result = globrex(glob, options);
+ if (options.filepath) {
+ return result.path!.regex;
+ }
+ return result.regex;
}
/** Test whether the given string is a glob */
@@ -76,3 +83,61 @@ export function isGlob(str: string): boolean {
return false;
}
+
+export interface ExpandGlobOptions extends GlobOptions {
+ root?: string;
+ includeDirs?: boolean;
+}
+
+/**
+ * Expand the glob string from the specified `root` directory and yield each
+ * result as a `WalkInfo` object.
+ */
+// TODO: Use a proper glob expansion algorithm.
+// This is a very incomplete solution. The whole directory tree from `root` is
+// walked and parent paths are not supported.
+export async function* expandGlob(
+ globString: string,
+ {
+ root = cwd(),
+ includeDirs = true,
+ extended = false,
+ globstar = false,
+ strict = false,
+ filepath = true,
+ flags = ""
+ }: ExpandGlobOptions = {}
+): AsyncIterableIterator<WalkInfo> {
+ const absoluteGlob = isAbsolute(globString)
+ ? globString
+ : join(root, globString);
+ const globOptions = { extended, globstar, strict, filepath, flags };
+ yield* walk(root, {
+ match: [glob(absoluteGlob, globOptions)],
+ includeDirs
+ });
+}
+
+/** Synchronous version of `expandGlob()`. */
+// TODO: As `expandGlob()`.
+export function* expandGlobSync(
+ globString: string,
+ {
+ root = cwd(),
+ includeDirs = true,
+ extended = false,
+ globstar = false,
+ strict = false,
+ filepath = true,
+ flags = ""
+ }: ExpandGlobOptions = {}
+): IterableIterator<WalkInfo> {
+ const absoluteGlob = isAbsolute(globString)
+ ? globString
+ : join(root, globString);
+ const globOptions = { extended, globstar, strict, filepath, flags };
+ yield* walkSync(root, {
+ match: [glob(absoluteGlob, globOptions)],
+ includeDirs
+ });
+}