summaryrefslogtreecommitdiff
path: root/prettier
diff options
context:
space:
mode:
authorNayeem Rahman <muhammed.9939@gmail.com>2019-10-02 18:59:27 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-10-02 13:59:27 -0400
commit2f90225c89926932a34eb40758e2af0d1742e1f8 (patch)
tree21466759c5a9ff26cff1397736fc6a10ff9880fd /prettier
parentaca225330521840d63835bb1ba27ce13a7f65093 (diff)
Implement expandGlob() and expandGlobSync() (denoland/deno_std#617)
fs/glob.ts: - Improve prototypes for expandGlob() and expandGlobSync() from denoland/deno_std#604. - Rename glob() to globToRegExp(). - Add normalizeGlob() and joinGlobs(). - Extract GlobToRegExpOptions from GlobOptions, remove the strict and filepath options. fs/globrex.ts: - Add GlobrexOptions. fs/path/constants.ts: - Add SEP_PATTERN. fs/walk.ts: - Add WalkOptions::includeFiles - Default WalkOptions::includeDirs to true. - Don't traverse directories matching a skip pattern. - Remove walkSync()'s default root value. prettier: - Refactor to use expandGlob(). testing: - Make findTestModules() an async generator. Original: https://github.com/denoland/deno_std/commit/8c90bd9d0b1c78b023d36462ffaa9446ef22490c
Diffstat (limited to 'prettier')
-rwxr-xr-xprettier/main.ts87
1 files changed, 33 insertions, 54 deletions
diff --git a/prettier/main.ts b/prettier/main.ts
index 1afda89d1..bd9238be2 100755
--- a/prettier/main.ts
+++ b/prettier/main.ts
@@ -23,11 +23,10 @@
// 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 { parse } from "../flags/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;
const HELP_MESSAGE = `
Formats the given files. If no arg is passed, then formats the all files.
@@ -289,65 +288,47 @@ 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,
+ exclude,
+ includeDirs: true,
+ extended: true,
+ globstar: 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);
+ async function* expandDirectory(d: string): AsyncIterableIterator<WalkInfo> {
+ for await (const walkInfo of expandGlob("**/*", {
+ ...expandGlobOpts,
+ root: d,
+ includeDirs: false
+ })) {
+ 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;
- }
+ for (const globString of include) {
+ for await (const walkInfo of expandGlob(globString, expandGlobOpts)) {
+ if (walkInfo.info.isDirectory()) {
+ yield* expandDirectory(walkInfo.filename);
} else {
- const files = walk(".", { match: [match], skip });
-
- for await (const info of files) {
- yield info;
- }
+ yield walkInfo;
}
}
- })();
+ }
}
async function main(opts): Promise<void> {
@@ -371,12 +352,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();