summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Hayden <andyhayden1@gmail.com>2019-03-07 23:01:40 -0800
committerRyan Dahl <ry@tinyclouds.org>2019-03-08 02:01:39 -0500
commit4b96bfa8c702da9ae2b86400f395f6a571294b38 (patch)
treef8863e020e76d7cf7e484067a874e33b0fb6da16
parentf90f62fa52eccd020026b6899de1b3c7ae0288b6 (diff)
Use fs.walk rather than git ls-files (denoland/deno_std#241)
Original: https://github.com/denoland/deno_std/commit/afaf343f376eaafc668d553322e5700c0c8c223f
-rwxr-xr-xformat.ts3
-rw-r--r--prettier/README.md6
-rwxr-xr-xprettier/main.ts85
-rw-r--r--prettier/main_test.ts21
4 files changed, 41 insertions, 74 deletions
diff --git a/format.ts b/format.ts
index fa538d53f..3a28d6650 100755
--- a/format.ts
+++ b/format.ts
@@ -8,10 +8,11 @@ async function main(opts) {
const args = [
`deno${executableSuffix}`,
"--allow-write",
- "--allow-run",
"--allow-read",
"prettier/main.ts",
"--ignore",
+ "node_modules",
+ "--ignore",
"testdata",
"--ignore",
"vendor"
diff --git a/prettier/README.md b/prettier/README.md
index cbfce3e17..3a8d5a0c2 100644
--- a/prettier/README.md
+++ b/prettier/README.md
@@ -7,19 +7,19 @@ Prettier APIs and tools for deno
To formats the source files, run:
```console
-deno --allow-run --allow-write https://deno.land/std/prettier/main.ts
+deno --allow-read --allow-write https://deno.land/std/prettier/main.ts
```
You can format only specific files by passing the arguments.
```console
-deno --allow-run --allow-write https://deno.land/std/prettier/main.ts path/to/script.ts
+deno --allow-read --allow-write https://deno.land/std/prettier/main.ts path/to/script.ts
```
You can format files on specific directory by passing the directory's path.
```console
-deno --allow-run --allow-write https://deno.land/std/prettier/main.ts path/to/script.ts
+deno --allow-read --allow-write https://deno.land/std/prettier/main.ts path/to/script.ts
```
## Use API
diff --git a/prettier/main.ts b/prettier/main.ts
index 286cba58c..72b47f4bb 100755
--- a/prettier/main.ts
+++ b/prettier/main.ts
@@ -2,8 +2,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, readAll, lstat, exit, readFile, writeFile } = Deno;
-import { xrun } from "./util.ts";
+const { args, exit, readFile, writeFile } = Deno;
+type FileInfo = Deno.FileInfo;
+import { glob } from "../fs/glob.ts";
+import { walk } from "../fs/walk.ts";
import { parse } from "../flags/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts";
@@ -34,45 +36,6 @@ type ParserLabel = "typescript" | "babel" | "markdown" | "json";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
-// Lists files in the given directory.
-// TODO: Replace git usage with deno's API calls
-async function listFiles(dir: string = "."): Promise<string[]> {
- return decoder
- .decode(
- await readAll(
- xrun({
- args: ["git", "ls-files", dir],
- stdout: "piped"
- }).stdout
- )
- )
- .trim()
- .split(/\r?\n/);
-}
-
-async function getSourceFiles(args: string[]): Promise<string[]> {
- if (args.length === 0) {
- return listFiles();
- }
-
- const results = args.map(async path => {
- if ((await lstat(path)).isDirectory()) {
- return listFiles(path);
- }
-
- return path;
- });
-
- return [].concat(...(await Promise.all(results)));
-}
-
-// Filters out the files which contains any pattern in the given ignoreList.
-function filterIgnoreList(files: string[], ignoreList: string[]): string[] {
- return files.filter(path =>
- ignoreList.every(pattern => !path.includes(pattern))
- );
-}
-
async function readFileIfExists(filename: string): Promise<string | null> {
let data;
try {
@@ -159,17 +122,16 @@ function selectParser(path: string): ParserLabel | null {
* If paths are empty, then checks all the files.
*/
async function checkSourceFiles(
- args: string[],
- ignoreList: string[]
+ files: AsyncIterableIterator<FileInfo>
): Promise<void> {
- const checks = [];
+ const checks: Array<Promise<boolean>> = [];
- filterIgnoreList(await getSourceFiles(args), ignoreList).forEach(file => {
- const parser = selectParser(file);
+ for await (const file of files) {
+ const parser = selectParser(file.path);
if (parser) {
- checks.push(checkFile(file, parser));
+ checks.push(checkFile(file.path, parser));
}
- });
+ }
const results = await Promise.all(checks);
@@ -187,17 +149,16 @@ async function checkSourceFiles(
* If paths are empty, then formats all the files.
*/
async function formatSourceFiles(
- args: string[],
- ignoreList: string[]
+ files: AsyncIterableIterator<FileInfo>
): Promise<void> {
- const formats = [];
+ const formats: Array<Promise<void>> = [];
- filterIgnoreList(await getSourceFiles(args), ignoreList).forEach(file => {
- const parser = selectParser(file);
+ for await (const file of files) {
+ const parser = selectParser(file.path);
if (parser) {
- formats.push(formatFile(file, parser));
+ formats.push(formatFile(file.path, parser));
}
- });
+ }
await Promise.all(formats);
exit(0);
@@ -210,14 +171,18 @@ async function main(opts): Promise<void> {
console.log(HELP_MESSAGE);
exit(0);
}
-
- const ignoreList: string[] = Array.isArray(ignore) ? ignore : [ignore];
-
+ const options = { flags: "g" };
+ const skip = Array.isArray(ignore)
+ ? ignore.map((i: string) => glob(i, options))
+ : [glob(ignore, options)];
+ const match =
+ args.length > 0 ? args.map((a: string) => glob(a, options)) : undefined;
+ const files = walk(".", { match, skip });
try {
if (check) {
- await checkSourceFiles(args, ignoreList);
+ await checkSourceFiles(files);
} else {
- await formatSourceFiles(args, ignoreList);
+ await formatSourceFiles(files);
}
} catch (e) {
console.log(e);
diff --git a/prettier/main_test.ts b/prettier/main_test.ts
index 70ecc96a8..ed2274a6b 100644
--- a/prettier/main_test.ts
+++ b/prettier/main_test.ts
@@ -1,7 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { join } from "../fs/path.ts";
+import { assertEquals } from "../testing/asserts.ts";
import { test } from "../testing/mod.ts";
import { xrun, executableSuffix } from "./util.ts";
-import { assertEquals } from "../testing/asserts.ts";
const { readAll } = Deno;
const decoder = new TextDecoder();
@@ -24,7 +25,7 @@ const cmd = [
"--allow-read",
"prettier/main.ts"
];
-const testdata = "prettier/testdata";
+const testdata = join("prettier", "testdata");
function normalizeOutput(output: string): string {
return output
@@ -43,7 +44,7 @@ async function clearTestdataChanges(): Promise<void> {
test(async function testPrettierCheckAndFormatFiles() {
await clearTestdataChanges();
- const files = [`${testdata}/0.ts`, `${testdata}/1.js`];
+ const files = [join(testdata, "0.ts"), join(testdata, "1.js")];
var { code, stdout } = await run([...cmd, "--check", ...files]);
assertEquals(code, 1);
@@ -53,8 +54,8 @@ test(async function testPrettierCheckAndFormatFiles() {
assertEquals(code, 0);
assertEquals(
normalizeOutput(stdout),
- `Formatting prettier/testdata/0.ts
-Formatting prettier/testdata/1.js`
+ `Formatting ./prettier/testdata/0.ts
+Formatting ./prettier/testdata/1.js`
);
var { code, stdout } = await run([...cmd, "--check", ...files]);
@@ -67,7 +68,7 @@ Formatting prettier/testdata/1.js`
test(async function testPrettierCheckAndFormatDirs() {
await clearTestdataChanges();
- const dirs = [`${testdata}/foo`, `${testdata}/bar`];
+ const dirs = [join(testdata, "foo"), join(testdata, "bar")];
var { code, stdout } = await run([...cmd, "--check", ...dirs]);
assertEquals(code, 1);
@@ -77,10 +78,10 @@ test(async function testPrettierCheckAndFormatDirs() {
assertEquals(code, 0);
assertEquals(
normalizeOutput(stdout),
- `Formatting prettier/testdata/bar/0.ts
-Formatting prettier/testdata/bar/1.js
-Formatting prettier/testdata/foo/0.ts
-Formatting prettier/testdata/foo/1.js`
+ `Formatting ./prettier/testdata/bar/0.ts
+Formatting ./prettier/testdata/bar/1.js
+Formatting ./prettier/testdata/foo/0.ts
+Formatting ./prettier/testdata/foo/1.js`
);
var { code, stdout } = await run([...cmd, "--check", ...dirs]);