1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 01:29:14 -05:00

Use fs.walk rather than git ls-files (#241)

This commit is contained in:
Andy Hayden 2019-03-07 23:01:40 -08:00 committed by Ryan Dahl
parent 395392912d
commit afaf343f37
4 changed files with 41 additions and 74 deletions

View file

@ -8,10 +8,11 @@ async function main(opts) {
const args = [ const args = [
`deno${executableSuffix}`, `deno${executableSuffix}`,
"--allow-write", "--allow-write",
"--allow-run",
"--allow-read", "--allow-read",
"prettier/main.ts", "prettier/main.ts",
"--ignore", "--ignore",
"node_modules",
"--ignore",
"testdata", "testdata",
"--ignore", "--ignore",
"vendor" "vendor"

View file

@ -7,19 +7,19 @@ Prettier APIs and tools for deno
To formats the source files, run: To formats the source files, run:
```console ```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. You can format only specific files by passing the arguments.
```console ```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. You can format files on specific directory by passing the directory's path.
```console ```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 ## Use API

View file

@ -2,8 +2,10 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// This script formats the given source files. If the files are omitted, it // This script formats the given source files. If the files are omitted, it
// formats the all files in the repository. // formats the all files in the repository.
const { args, readAll, lstat, exit, readFile, writeFile } = Deno; const { args, exit, readFile, writeFile } = Deno;
import { xrun } from "./util.ts"; type FileInfo = Deno.FileInfo;
import { glob } from "../fs/glob.ts";
import { walk } from "../fs/walk.ts";
import { parse } from "../flags/mod.ts"; import { parse } from "../flags/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts"; import { prettier, prettierPlugins } from "./prettier.ts";
@ -34,45 +36,6 @@ type ParserLabel = "typescript" | "babel" | "markdown" | "json";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const decoder = new TextDecoder(); 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> { async function readFileIfExists(filename: string): Promise<string | null> {
let data; let data;
try { try {
@ -159,17 +122,16 @@ function selectParser(path: string): ParserLabel | null {
* If paths are empty, then checks all the files. * If paths are empty, then checks all the files.
*/ */
async function checkSourceFiles( async function checkSourceFiles(
args: string[], files: AsyncIterableIterator<FileInfo>
ignoreList: string[]
): Promise<void> { ): Promise<void> {
const checks = []; const checks: Array<Promise<boolean>> = [];
filterIgnoreList(await getSourceFiles(args), ignoreList).forEach(file => { for await (const file of files) {
const parser = selectParser(file); const parser = selectParser(file.path);
if (parser) { if (parser) {
checks.push(checkFile(file, parser)); checks.push(checkFile(file.path, parser));
}
} }
});
const results = await Promise.all(checks); const results = await Promise.all(checks);
@ -187,17 +149,16 @@ async function checkSourceFiles(
* If paths are empty, then formats all the files. * If paths are empty, then formats all the files.
*/ */
async function formatSourceFiles( async function formatSourceFiles(
args: string[], files: AsyncIterableIterator<FileInfo>
ignoreList: string[]
): Promise<void> { ): Promise<void> {
const formats = []; const formats: Array<Promise<void>> = [];
filterIgnoreList(await getSourceFiles(args), ignoreList).forEach(file => { for await (const file of files) {
const parser = selectParser(file); const parser = selectParser(file.path);
if (parser) { if (parser) {
formats.push(formatFile(file, parser)); formats.push(formatFile(file.path, parser));
}
} }
});
await Promise.all(formats); await Promise.all(formats);
exit(0); exit(0);
@ -210,14 +171,18 @@ async function main(opts): Promise<void> {
console.log(HELP_MESSAGE); console.log(HELP_MESSAGE);
exit(0); exit(0);
} }
const options = { flags: "g" };
const ignoreList: string[] = Array.isArray(ignore) ? ignore : [ignore]; 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 { try {
if (check) { if (check) {
await checkSourceFiles(args, ignoreList); await checkSourceFiles(files);
} else { } else {
await formatSourceFiles(args, ignoreList); await formatSourceFiles(files);
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);

View file

@ -1,7 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // 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 { test } from "../testing/mod.ts";
import { xrun, executableSuffix } from "./util.ts"; import { xrun, executableSuffix } from "./util.ts";
import { assertEquals } from "../testing/asserts.ts";
const { readAll } = Deno; const { readAll } = Deno;
const decoder = new TextDecoder(); const decoder = new TextDecoder();
@ -24,7 +25,7 @@ const cmd = [
"--allow-read", "--allow-read",
"prettier/main.ts" "prettier/main.ts"
]; ];
const testdata = "prettier/testdata"; const testdata = join("prettier", "testdata");
function normalizeOutput(output: string): string { function normalizeOutput(output: string): string {
return output return output
@ -43,7 +44,7 @@ async function clearTestdataChanges(): Promise<void> {
test(async function testPrettierCheckAndFormatFiles() { test(async function testPrettierCheckAndFormatFiles() {
await clearTestdataChanges(); 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]); var { code, stdout } = await run([...cmd, "--check", ...files]);
assertEquals(code, 1); assertEquals(code, 1);
@ -53,8 +54,8 @@ test(async function testPrettierCheckAndFormatFiles() {
assertEquals(code, 0); assertEquals(code, 0);
assertEquals( assertEquals(
normalizeOutput(stdout), normalizeOutput(stdout),
`Formatting prettier/testdata/0.ts `Formatting ./prettier/testdata/0.ts
Formatting prettier/testdata/1.js` Formatting ./prettier/testdata/1.js`
); );
var { code, stdout } = await run([...cmd, "--check", ...files]); var { code, stdout } = await run([...cmd, "--check", ...files]);
@ -67,7 +68,7 @@ Formatting prettier/testdata/1.js`
test(async function testPrettierCheckAndFormatDirs() { test(async function testPrettierCheckAndFormatDirs() {
await clearTestdataChanges(); await clearTestdataChanges();
const dirs = [`${testdata}/foo`, `${testdata}/bar`]; const dirs = [join(testdata, "foo"), join(testdata, "bar")];
var { code, stdout } = await run([...cmd, "--check", ...dirs]); var { code, stdout } = await run([...cmd, "--check", ...dirs]);
assertEquals(code, 1); assertEquals(code, 1);
@ -77,10 +78,10 @@ test(async function testPrettierCheckAndFormatDirs() {
assertEquals(code, 0); assertEquals(code, 0);
assertEquals( assertEquals(
normalizeOutput(stdout), normalizeOutput(stdout),
`Formatting prettier/testdata/bar/0.ts `Formatting ./prettier/testdata/bar/0.ts
Formatting prettier/testdata/bar/1.js Formatting ./prettier/testdata/bar/1.js
Formatting prettier/testdata/foo/0.ts Formatting ./prettier/testdata/foo/0.ts
Formatting prettier/testdata/foo/1.js` Formatting ./prettier/testdata/foo/1.js`
); );
var { code, stdout } = await run([...cmd, "--check", ...dirs]); var { code, stdout } = await run([...cmd, "--check", ...dirs]);