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 17adf62
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 59 deletions.
29 changes: 21 additions & 8 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 @@ -94,17 +95,23 @@ export async function* expandGlob(
globString: string,
{
root = cwd(),
exclude = [],
includeDirs = true,
extended = false,
globstar = false,
strict = false
}: ExpandGlobOptions = {}
): AsyncIterableIterator<WalkInfo> {
const absoluteGlob = isAbsolute(globString)
? globString
: join(root, globString);
const absGlob = isAbsolute(globString) ? globString : join(root, globString);
const absExclude = exclude.map((s: string): string =>
isAbsolute(s) ? s : join(root, s)
);
const globOptions: GlobOptions = { extended, globstar, strict };
yield* walk(root, {
match: [globToRegExp(absoluteGlob, { extended, globstar, strict })],
match: [globToRegExp(absGlob, globOptions)],
skip: absExclude.map(
(s: string): RegExp => globToRegExp(s, { ...globOptions, flags: "g" })
),
includeDirs
});
}
Expand All @@ -115,17 +122,23 @@ export function* expandGlobSync(
globString: string,
{
root = cwd(),
exclude = [],
includeDirs = true,
extended = false,
globstar = false,
strict = false
}: ExpandGlobOptions = {}
): IterableIterator<WalkInfo> {
const absoluteGlob = isAbsolute(globString)
? globString
: join(root, globString);
const absGlob = isAbsolute(globString) ? globString : join(root, globString);
const absExclude = exclude.map((s: string): string =>
isAbsolute(s) ? s : join(root, s)
);
const globOptions: GlobOptions = { extended, globstar, strict };
yield* walkSync(root, {
match: [globToRegExp(absoluteGlob, { extended, globstar, strict })],
match: [globToRegExp(absGlob, globOptions)],
skip: absExclude.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 17adf62

Please sign in to comment.