summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAxetroy <troy450409405@gmail.com>2019-05-21 20:23:23 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-05-21 08:23:23 -0400
commit915b4f520b16bd5c9bc6ef0ba9e45eba27c4137d (patch)
treec6e00de7b4865101c39f90ea5448e29d44879845
parent7c4e973611af274c25acb38d0b8e674599ab8da7 (diff)
feat(prettier): output to stdout instead of write file by default unless specified --write flag (denoland/deno_std#332)
Original: https://github.com/denoland/deno_std/commit/434007b8ab421cb24a75d2937d275c5048b9c111
-rwxr-xr-xformat.ts3
-rwxr-xr-xprettier/main.ts35
-rw-r--r--prettier/main_test.ts63
-rw-r--r--prettier/testdata/formatted.ts1
4 files changed, 79 insertions, 23 deletions
diff --git a/format.ts b/format.ts
index c4aa35cf7..f0465af50 100755
--- a/format.ts
+++ b/format.ts
@@ -16,7 +16,8 @@ async function main(opts): Promise<void> {
"--ignore",
"testdata",
"--ignore",
- "vendor"
+ "vendor",
+ "--write"
];
if (opts.check) {
diff --git a/prettier/main.ts b/prettier/main.ts
index 0b5034c5f..e1b1b6418 100755
--- a/prettier/main.ts
+++ b/prettier/main.ts
@@ -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"
diff --git a/prettier/main_test.ts b/prettier/main_test.ts
index 5a507afe7..86d6a75a5 100644
--- a/prettier/main_test.ts
+++ b/prettier/main_test.ts
@@ -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();
+});
diff --git a/prettier/testdata/formatted.ts b/prettier/testdata/formatted.ts
new file mode 100644
index 000000000..b7a3fa02f
--- /dev/null
+++ b/prettier/testdata/formatted.ts
@@ -0,0 +1 @@
+console.log(0);