summaryrefslogtreecommitdiff
path: root/fs/glob.ts
diff options
context:
space:
mode:
authorNayeem Rahman <muhammed.9939@gmail.com>2019-09-28 14:33:17 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-09-28 09:33:17 -0400
commita472b6732dd37636b7b31128f53d3e6bcf531a73 (patch)
tree11fa3fb56917582c0c88c871e71ff212b39eb841 /fs/glob.ts
parentaf18093498c3fca103bd47305e447ddeda40d9a2 (diff)
Test runner v2 (denoland/deno_std#604)
Original: https://github.com/denoland/deno_std/commit/17a214bbd5b3a058a8126e9f7210992b1b52ba11
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
+ });
+}