1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-30 11:16:38 -05:00

feat(prettier): output to stdout instead of write file by default unless specified --write flag (denoland/deno_std#332)

Original: 434007b8ab
This commit is contained in:
Axetroy 2019-05-21 20:23:23 +08:00 committed by Ryan Dahl
parent 7c4e973611
commit 915b4f520b
4 changed files with 79 additions and 23 deletions

View file

@ -16,7 +16,8 @@ async function main(opts): Promise<void> {
"--ignore",
"testdata",
"--ignore",
"vendor"
"vendor",
"--write"
];
if (opts.check) {

View file

@ -11,7 +11,7 @@
// 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 } = Deno;
const { args, exit, readFile, writeFile, stdout } = Deno;
import { glob } from "../fs/glob.ts";
import { walk, WalkInfo } from "../fs/walk.ts";
import { parse } from "../flags/mod.ts";
@ -25,6 +25,7 @@ Usage: deno prettier/main.ts [options] [files...]
Options:
-H, --help Show this help message and exit.
--check Check if the source files are formatted.
--write Whether to write to the file, otherwise it will output to stdout, Defaults to false.
--ignore <path> Ignore the given path(s).
JS/TS Styling Options:
@ -54,14 +55,17 @@ Markdown Styling Options:
Defaults to preserve.
Example:
deno prettier/main.ts script1.ts script2.js
deno run prettier/main.ts --write script1.ts script2.js
Formats the files
deno prettier/main.ts --check script1.ts script2.js
deno run prettier/main.ts --check script1.ts script2.js
Checks if the files are formatted
deno prettier/main.ts
deno run prettier/main.ts --write
Formats the all files in the repository
deno run prettier/main.ts script1.ts
Print the formatted code to stdout
`;
// Available parsers
@ -78,6 +82,7 @@ interface PrettierOptions {
arrowParens: string;
proseWrap: string;
endOfLine: string;
write: boolean;
}
const encoder = new TextEncoder();
@ -139,15 +144,20 @@ async function formatFile(
return;
}
const formatted = prettier.format(text, {
const formatted: string = prettier.format(text, {
...prettierOpts,
parser,
plugins: prettierPlugins
});
if (text !== formatted) {
console.log(`Formatting ${filename}`);
await writeFile(filename, encoder.encode(formatted));
const fileUnit8 = encoder.encode(formatted);
if (prettierOpts.write) {
if (text !== formatted) {
console.log(`Formatting ${filename}`);
await writeFile(filename, fileUnit8);
}
} else {
await stdout.write(fileUnit8);
}
}
@ -230,7 +240,8 @@ async function main(opts): Promise<void> {
bracketSpacing: Boolean(opts["bracket-spacing"]),
arrowParens: opts["arrow-parens"],
proseWrap: opts["prose-wrap"],
endOfLine: opts["end-of-line"]
endOfLine: opts["end-of-line"],
write: opts["write"]
};
if (help) {
@ -275,7 +286,8 @@ main(
"semi",
"use-tabs",
"single-quote",
"bracket-spacing"
"bracket-spacing",
"write"
],
default: {
ignore: [],
@ -288,7 +300,8 @@ main(
"bracket-spacing": true,
"arrow-parens": "avoid",
"prose-wrap": "preserve",
"end-of-line": "auto"
"end-of-line": "auto",
write: false
},
alias: {
H: "help"

View file

@ -1,5 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { join } from "../fs/path.ts";
import { EOL } from "../fs/path/constants.ts";
import { assertEquals } from "../testing/asserts.ts";
import { test } from "../testing/mod.ts";
import { xrun } from "./util.ts";
@ -59,7 +60,7 @@ test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
assertEquals(code, 1);
assertEquals(normalizeOutput(stdout), "Some files are not formatted");
var { code, stdout } = await run([...cmd, ...files]);
var { code, stdout } = await run([...cmd, "--write", ...files]);
assertEquals(code, 0);
assertEquals(
normalizeOutput(stdout),
@ -83,7 +84,7 @@ test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
assertEquals(code, 1);
assertEquals(normalizeOutput(stdout), "Some files are not formatted");
var { code, stdout } = await run([...cmd, ...dirs]);
var { code, stdout } = await run([...cmd, "--write", ...dirs]);
assertEquals(code, 0);
assertEquals(
normalizeOutput(stdout),
@ -111,7 +112,7 @@ test(async function testPrettierOptions(): Promise<void> {
const getSourceCode = async (f: string): Promise<string> =>
decoder.decode(await Deno.readFile(f));
await run([...cmd, "--no-semi", file0]);
await run([...cmd, "--no-semi", "--write", file0]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0)
@ -119,7 +120,15 @@ console.log([function foo() {}, function baz() {}, a => {}])
`
);
await run([...cmd, "--print-width", "30", "--tab-width", "4", file0]);
await run([
...cmd,
"--print-width",
"30",
"--tab-width",
"4",
"--write",
file0
]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
@ -131,7 +140,7 @@ console.log([
`
);
await run([...cmd, "--print-width", "30", "--use-tabs", file0]);
await run([...cmd, "--print-width", "30", "--use-tabs", "--write", file0]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
@ -143,14 +152,22 @@ console.log([
`
);
await run([...cmd, "--single-quote", file1]);
await run([...cmd, "--single-quote", "--write", file1]);
assertEquals(
normalizeSourceCode(await getSourceCode(file1)),
`console.log('1');
`
);
await run([...cmd, "--print-width", "30", "--trailing-comma", "all", file0]);
await run([
...cmd,
"--print-width",
"30",
"--trailing-comma",
"all",
"--write",
file0
]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
@ -162,14 +179,14 @@ console.log([
`
);
await run([...cmd, "--no-bracket-spacing", file2]);
await run([...cmd, "--no-bracket-spacing", "--write", file2]);
assertEquals(
normalizeSourceCode(await getSourceCode(file2)),
`console.log({a: 1});
`
);
await run([...cmd, "--arrow-parens", "always", file0]);
await run([...cmd, "--arrow-parens", "always", "--write", file0]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
@ -177,7 +194,7 @@ console.log([function foo() {}, function baz() {}, (a) => {}]);
`
);
await run([...cmd, "--prose-wrap", "always", file3]);
await run([...cmd, "--prose-wrap", "always", "--write", file3]);
assertEquals(
normalizeSourceCode(await getSourceCode(file3)),
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
@ -185,8 +202,32 @@ incididunt ut labore et dolore magna aliqua.
`
);
await run([...cmd, "--end-of-line", "crlf", file2]);
await run([...cmd, "--end-of-line", "crlf", "--write", file2]);
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");
await clearTestdataChanges();
});
test(async function testPrettierPrintToStdout(): Promise<void> {
await clearTestdataChanges();
const file0 = join(testdata, "0.ts");
const file1 = join(testdata, "formatted.ts");
const getSourceCode = async (f: string): Promise<string> =>
decoder.decode(await Deno.readFile(f));
const { stdout } = await run([...cmd, file0]);
// The source file will not change without `--write` flags.
assertEquals(await getSourceCode(file0), "console.log (0)" + EOL);
// The output should be formatted code.
assertEquals(stdout, "console.log(0);" + EOL);
const { stdout: formattedCode } = await run([...cmd, file1]);
// The source file will not change without `--write` flags.
assertEquals(await getSourceCode(file1), "console.log(0);" + EOL);
// The output will be formatted code even it is the same as the source file's content.
assertEquals(formattedCode, "console.log(0);" + EOL);
await clearTestdataChanges();
});

1
prettier/testdata/formatted.ts vendored Normal file
View file

@ -0,0 +1 @@
console.log(0);