Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement expandGlob() #617

Merged
merged 20 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
prettier: Refactor to use expandGlob()
Regression. The prettier tests will fail until expandGlob() is fixed
since they try to match absolute paths outside of CWD.
  • Loading branch information
nayeemrmn committed Sep 28, 2019
commit ff97903f489e495fabc531d186f6f48f5846eb18
4 changes: 2 additions & 2 deletions format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ async function main(opts): Promise<void> {
"--ignore",
"node_modules",
"--ignore",
"testdata",
"**/testdata",
"--ignore",
"vendor",
"**/vendor",
"--write"
];

Expand Down
101 changes: 46 additions & 55 deletions prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// This script formats the given source files. If the files are omitted, it
// formats the all files in the repository.
const { args, exit, readFile, writeFile, stdout, stdin, readAll } = Deno;
import { glob, isGlob, GlobOptions } from "../fs/glob.ts";
import { walk, WalkInfo } from "../fs/walk.ts";
import { ExpandGlobOptions, expandGlob, glob } from "../fs/glob.ts";
import { isAbsolute, join } from "../fs/path/mod.ts";
import { WalkInfo } from "../fs/walk.ts";
import { parse } from "../flags/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts";
const { args, cwd, exit, readAll, readFile, stdin, stdout, writeFile } = Deno;

const HELP_MESSAGE = `
Formats the given files. If no arg is passed, then formats the all files.
Expand Down Expand Up @@ -289,65 +290,57 @@ async function formatFromStdin(

/**
* Get the files to format.
* @param selectors The glob patterns to select the files.
* eg `cmd/*.ts` to select all the typescript files in cmd
* @param include The glob patterns to select the files.
* eg `"cmd/*.ts"` to select all the typescript files in cmd
* directory.
* eg `cmd/run.ts` to select `cmd/run.ts` file as only.
* @param ignore The glob patterns to ignore files.
* eg `*_test.ts` to ignore all the test file.
* @param options options to pass to `glob(selector, options)`
* eg `"cmd/run.ts"` to select `cmd/run.ts` file as only.
* @param exclude The glob patterns to ignore files.
* eg `"*_test.ts"` to ignore all the test file.
* @param root The directory from which to apply default globs.
* @returns returns an async iterable object
*/
function getTargetFiles(
selectors: string[],
ignore: string[] = [],
options: GlobOptions = {}
async function* getTargetFiles(
include: string[],
exclude: string[],
root: string = cwd()
): AsyncIterableIterator<WalkInfo> {
const matchers: Array<string | RegExp> = [];

const selectorMap: { [k: string]: boolean } = {};
const expandGlobOpts: ExpandGlobOptions = {
root,
extended: true,
globstar: true,
filepath: true
};

for (const selector of selectors) {
// If the selector already exists then ignore it.
if (selectorMap[selector]) continue;
selectorMap[selector] = true;
if (isGlob(selector) || selector === ".") {
matchers.push(glob(selector, options));
} else {
matchers.push(selector);
// 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 =>
glob(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;
}
}
}

const skip = ignore.map((i: string): RegExp => glob(i, options));

return (async function*(): AsyncIterableIterator<WalkInfo> {
for (const match of matchers) {
if (typeof match === "string") {
const fileInfo = await Deno.stat(match);

if (fileInfo.isDirectory()) {
const files = walk(match, { skip });

for await (const info of files) {
yield info;
}
} else {
const info: WalkInfo = {
filename: match,
info: fileInfo
};

yield info;
}
} else {
const files = walk(".", { match: [match], skip });

for await (const info of files) {
yield info;
}
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)) {
yield walkInfo;
}
}
})();
}
}

async function main(opts): Promise<void> {
Expand All @@ -371,12 +364,10 @@ async function main(opts): Promise<void> {
console.log(HELP_MESSAGE);
exit(0);
}
const options: GlobOptions = { flags: "g" };

const files = getTargetFiles(
args.length ? args : ["."],
Array.isArray(ignore) ? ignore : [ignore],
options
Array.isArray(ignore) ? ignore : [ignore]
);

const tty = Deno.isTTY();
Expand Down