From 2bbde0c226d220307e46cc74e414320ac74aeddd Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Mon, 4 Mar 2019 19:06:05 +0100 Subject: [PATCH] Refactoring + Enhance UTs + Enhance doc (denoland/deno_std#230) Original: https://github.com/denoland/deno_std/commit/e2fd507cfd28fa6f4900668d4a80b78a5e97f01b --- fs/glob.ts | 44 +++++++++++++++++++++++++++++++++++++++++++- fs/glob_test.ts | 28 ++++++++++++++++++++++++++-- fs/globrex.ts | 9 +-------- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/fs/glob.ts b/fs/glob.ts index 1031bd75d5..8c64c45b35 100644 --- a/fs/glob.ts +++ b/fs/glob.ts @@ -1,6 +1,48 @@ import { FileInfo } from "deno"; -import { globrex, GlobOptions } from "./globrex.ts"; +import { globrex } from "./globrex.ts"; +export interface GlobOptions { + // Allow ExtGlob features + extended?: boolean; + // When globstar is true, '/foo/**' is equivelant + // to '/foo/*' when globstar is false. + // Having globstar set to true is the same usage as + // using wildcards in bash + globstar?: boolean; + // be laissez faire about mutiple slashes + strict?: boolean; + // Parse as filepath for extra path related features + filepath?: boolean; + // Flag to use in the generated RegExp + flags?: string; +} + +/** + * Generate a regex based on glob pattern and options + * This was meant to be using the the `fs.walk` function + * but can be used anywhere else. + * @param glob - Glob pattern to be used + * @param options - Specific options for the glob pattern + * @returns A RegExp for the glob pattern + * @example + * Looking for all the `ts` files + * ```typescript + * walkSync(".", { + * match: [glob("*.ts")] + * }) + * ``` + * @example + * Looking for all the `.json` files in any subfolder + * of the `a` folder + * ```typescript + * walkSync(".", { + * match: [glob(join("a", "**", "*.json"),flags: "g", + * extended: true, + * globstar: true + * })] + * }) + * ``` + */ export function glob(glob: string, options: GlobOptions = {}): RegExp { return globrex(glob, options).regex; } diff --git a/fs/glob_test.ts b/fs/glob_test.ts index 50e6abef89..bd5e2543c6 100644 --- a/fs/glob_test.ts +++ b/fs/glob_test.ts @@ -106,7 +106,6 @@ testWalk( match: [ glob(join("a", "**", "*.ts"), { flags: "g", - extended: true, globstar: true }) ] @@ -116,6 +115,31 @@ testWalk( } ); +testWalk( + async (d: string) => { + await mkdir(d + "/a"); + await mkdir(d + "/a/unicorn"); + await mkdir(d + "/a/deno"); + await mkdir(d + "/a/raptor"); + await touch(d + "/a/raptor/x.ts"); + await touch(d + "/a/deno/x.ts"); + await touch(d + "/a/unicorn/x.ts"); + }, + async function globInWalkFolderExtended() { + const arr = await walkArray(".", { + match: [ + glob(join("a", "+(raptor|deno)", "*.ts"), { + flags: "g", + extended: true + }) + ] + }); + assert.equal(arr.length, 2); + assert.equal(arr[0], "./a/deno/x.ts"); + assert.equal(arr[1], "./a/raptor/x.ts"); + } +); + testWalk( async (d: string) => { await touch(d + "/x.ts"); @@ -124,7 +148,7 @@ testWalk( }, async function globInWalkWildcardExtension() { const arr = await walkArray(".", { - match: [glob("x.*", { flags: "g", extended: true, globstar: true })] + match: [glob("x.*", { flags: "g", globstar: true })] }); console.log(arr); assert.equal(arr.length, 2); diff --git a/fs/globrex.ts b/fs/globrex.ts index 06a6b79bf3..9905cbb732 100644 --- a/fs/globrex.ts +++ b/fs/globrex.ts @@ -3,6 +3,7 @@ // Copyright (c) 2018 Terkel Gjervig Nielsen import * as deno from "deno"; +import { GlobOptions } from "./glob.ts"; const isWin = deno.platform.os === "win"; const SEP = isWin ? `\\\\+` : `\\/`; @@ -12,14 +13,6 @@ const WILDCARD = `([^/]*)`; const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`; const WILDCARD_SEGMENT = `([^${SEP_ESC}]*)`; -export interface GlobOptions { - extended?: boolean; - globstar?: boolean; - strict?: boolean; - filepath?: boolean; - flags?: string; -} - /** * Convert any glob pattern to a JavaScript Regexp object * @param {String} glob Glob pattern to convert