mirror of
https://github.com/denoland/deno.git
synced 2024-12-26 09:10:40 -05:00
Use fs.walk rather than git ls-files (#241)
This commit is contained in:
parent
395392912d
commit
afaf343f37
4 changed files with 41 additions and 74 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Reference in a new issue