Skip to content

Commit

Permalink
Move exclude handling inside of expandGlob()
Browse files Browse the repository at this point in the history
Adds an exclude field to ExpandGlopOptions. This should be the final
API.
  • Loading branch information
nayeemrmn committed Sep 29, 2019
1 parent b6af0a7 commit f1d05be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 55 deletions.
21 changes: 17 additions & 4 deletions fs/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function isGlob(str: string): boolean {

export interface ExpandGlobOptions extends GlobOptions {
root?: string;
exclude?: string[];
includeDirs?: boolean;
}

Expand All @@ -88,12 +89,15 @@ export interface ExpandGlobOptions extends GlobOptions {
* 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.
// - This is a very incomplete solution. The whole directory tree from `root` is
// walked and parent paths are not supported.
// - We use the `g` flag to support path prefixes when specifying excludes.
// Replace with a solution that does this more correctly.
export async function* expandGlob(
globString: string,
{
root = cwd(),
exclude = [],
includeDirs = true,
extended = false,
globstar = false,
Expand All @@ -103,8 +107,12 @@ export async function* expandGlob(
const absoluteGlob = isAbsolute(globString)
? globString
: join(root, globString);
const globOptions: GlobOptions = { extended, globstar, strict };
yield* walk(root, {
match: [globToRegExp(absoluteGlob, { extended, globstar, strict })],
match: [globToRegExp(absoluteGlob, globOptions)],
skip: exclude.map(
(s: string): RegExp => globToRegExp(s, { ...globOptions, flags: "g" })
),
includeDirs
});
}
Expand All @@ -115,6 +123,7 @@ export function* expandGlobSync(
globString: string,
{
root = cwd(),
exclude = [],
includeDirs = true,
extended = false,
globstar = false,
Expand All @@ -124,8 +133,12 @@ export function* expandGlobSync(
const absoluteGlob = isAbsolute(globString)
? globString
: join(root, globString);
const globOptions: GlobOptions = { extended, globstar, strict };
yield* walkSync(root, {
match: [globToRegExp(absoluteGlob, { extended, globstar, strict })],
match: [globToRegExp(absoluteGlob, globOptions)],
skip: exclude.map(
(s: string): RegExp => globToRegExp(s, { ...globOptions, flags: "g" })
),
includeDirs
});
}
28 changes: 5 additions & 23 deletions prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
// This script formats the given source files. If the files are omitted, it
// formats the all files in the repository.
import { parse } from "../flags/mod.ts";
import {
ExpandGlobOptions,
WalkInfo,
expandGlob,
globToRegExp
} from "../fs/mod.ts";
import { isAbsolute, join } from "../fs/path/mod.ts";
import { ExpandGlobOptions, WalkInfo, expandGlob } from "../fs/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts";
const { args, cwd, exit, readAll, readFile, stdin, stdout, writeFile } = Deno;

Expand Down Expand Up @@ -310,40 +304,28 @@ async function* getTargetFiles(
): AsyncIterableIterator<WalkInfo> {
const expandGlobOpts: ExpandGlobOptions = {
root,
exclude,
includeDirs: true,
extended: true,
globstar: true,
strict: false
};

// TODO: We use the `g` flag here to support path prefixes when specifying
// excludes. Replace with a solution that does this more correctly.
const excludePathPatterns = exclude.map(
(s: string): RegExp =>
globToRegExp(isAbsolute(s) ? s : join(root, s), {
...expandGlobOpts,
flags: "g"
})
);
const shouldInclude = ({ filename }: WalkInfo): boolean =>
!excludePathPatterns.some((p: RegExp): boolean => !!filename.match(p));

async function* expandDirectory(d: string): AsyncIterableIterator<WalkInfo> {
for await (const walkInfo of expandGlob("**/*", {
...expandGlobOpts,
root: d,
includeDirs: false
})) {
if (shouldInclude(walkInfo)) {
yield walkInfo;
}
yield walkInfo;
}
}

for (const globString of include) {
for await (const walkInfo of expandGlob(globString, expandGlobOpts)) {
if (walkInfo.info.isDirectory()) {
yield* expandDirectory(walkInfo.filename);
} else if (shouldInclude(walkInfo)) {
} else {
yield walkInfo;
}
}
Expand Down
40 changes: 12 additions & 28 deletions testing/runner.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#!/usr/bin/env -S deno -A
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { parse } from "../flags/mod.ts";
import {
ExpandGlobOptions,
WalkInfo,
expandGlob,
globToRegExp
} from "../fs/mod.ts";
import { ExpandGlobOptions, expandGlob } from "../fs/mod.ts";
import { isWindows } from "../fs/path/constants.ts";
import { isAbsolute, join } from "../fs/path/mod.ts";
import { join } from "../fs/path/mod.ts";
import { RunTestsOptions, runTests } from "./mod.ts";
const { DenoError, ErrorKind, args, cwd, exit } = Deno;

Expand Down Expand Up @@ -79,38 +74,21 @@ export async function* findTestModules(

const expandGlobOpts: ExpandGlobOptions = {
root,
exclude: excludePaths,
includeDirs: true,
extended: true,
globstar: true,
strict: false
};

// TODO: We use the `g` flag here to support path prefixes when specifying
// excludes. Replace with a solution that does this more correctly.
const excludePathPatterns = excludePaths.map(
(s: string): RegExp =>
globToRegExp(isAbsolute(s) ? s : join(root, s), {
...expandGlobOpts,
flags: "g"
})
);
const excludeUrlPatterns = excludeUrls.map(
(url: string): RegExp => RegExp(url)
);
const shouldIncludePath = ({ filename }: WalkInfo): boolean =>
!excludePathPatterns.some((p: RegExp): boolean => !!filename.match(p));
const shouldIncludeUrl = (url: string): boolean =>
!excludeUrlPatterns.some((p: RegExp): boolean => !!url.match(p));

async function* expandDirectory(d: string): AsyncIterableIterator<string> {
for (const dirGlob of DIR_GLOBS) {
for await (const walkInfo of expandGlob(dirGlob, {
...expandGlobOpts,
root: d,
includeDirs: false
})) {
if (shouldIncludePath(walkInfo)) {
yield filePathToUrl(walkInfo.filename);
}
yield filePathToUrl(walkInfo.filename);
}
}
}
Expand All @@ -119,12 +97,18 @@ export async function* findTestModules(
for await (const walkInfo of expandGlob(globString, expandGlobOpts)) {
if (walkInfo.info.isDirectory()) {
yield* expandDirectory(walkInfo.filename);
} else if (shouldIncludePath(walkInfo)) {
} else {
yield filePathToUrl(walkInfo.filename);
}
}
}

const excludeUrlPatterns = excludeUrls.map(
(url: string): RegExp => RegExp(url)
);
const shouldIncludeUrl = (url: string): boolean =>
!excludeUrlPatterns.some((p: RegExp): boolean => !!url.match(p));

yield* includeUrls.filter(shouldIncludeUrl);
}

Expand Down

0 comments on commit f1d05be

Please sign in to comment.