From 54e7755a67658d7d0596c034e80b74b3b1ff8702 Mon Sep 17 00:00:00 2001 From: axetroy Date: Tue, 18 Jun 2019 00:54:55 +0800 Subject: [PATCH 01/52] prettier: support read code from stdin and output to stdout --- prettier/main.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 584b90b3959e..413feafeafd8 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, stdout } = Deno; +const { args, exit, readFile, writeFile, stdout, stdin, readAll } = Deno; import { glob, isGlob, GlobOptions } from "../fs/glob.ts"; import { walk, WalkInfo } from "../fs/walk.ts"; import { parse } from "../flags/mod.ts"; @@ -26,6 +26,7 @@ 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. + --from-stdin Read the typescript/javascript code from stdin and output formatted code to stdout. --ignore Ignore the given path(s). JS/TS Styling Options: @@ -227,6 +228,19 @@ async function formatSourceFiles( exit(0); } +/** + * Format source code + */ +function format(text: string, prettierOpts: PrettierOptions): string { + const formatted: string = prettier.format(text, { + ...prettierOpts, + parser: selectParser("stdin.ts"), + plugins: prettierPlugins + }); + + return formatted; +} + /** * Get the files to format. * @param selectors The glob patterns to select the files. @@ -306,6 +320,15 @@ async function main(opts): Promise { write: opts["write"] }; + const isReadFromStdin: boolean = opts["from-stdin"]; + + if (isReadFromStdin) { + const byte = await readAll(stdin); + const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); + stdout.write(new TextEncoder().encode(formattedCode)); + return; + } + if (help) { console.log(HELP_MESSAGE); exit(0); @@ -348,7 +371,8 @@ main( "use-tabs", "single-quote", "bracket-spacing", - "write" + "write", + "from-stdin" ], default: { ignore: [], @@ -362,7 +386,8 @@ main( "arrow-parens": "avoid", "prose-wrap": "preserve", "end-of-line": "auto", - write: false + write: false, + "from-stdin": false }, alias: { H: "help" From d829718e0518ba8b3abb4408026ae8a101448079 Mon Sep 17 00:00:00 2001 From: axetroy Date: Tue, 18 Jun 2019 01:15:05 +0800 Subject: [PATCH 02/52] add example for usage --- prettier/main.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/prettier/main.ts b/prettier/main.ts index 413feafeafd8..2b81fa9ecb1b 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -67,6 +67,9 @@ Example: deno run prettier/main.ts script1.ts Print the formatted code to stdout + + cat script1.ts | deno run prettier/main.ts --from-stdin + Read code from stdin and print the formatted code to stdout `; // Available parsers From e447763f1e88ba07118e7b8dfc6384552953de87 Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 20 Jun 2019 21:58:40 +0800 Subject: [PATCH 03/52] remove from-stdin --- prettier/main.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 596049aa04f2..b6eca41f0530 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -330,9 +330,7 @@ async function main(opts): Promise { write: opts["write"] }; - const isReadFromStdin: boolean = opts["from-stdin"]; - - if (isReadFromStdin) { + if (!Deno.isTTY().stdin) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); stdout.write(new TextEncoder().encode(formattedCode)); @@ -381,8 +379,7 @@ main( "use-tabs", "single-quote", "bracket-spacing", - "write", - "from-stdin" + "write" ], default: { ignore: [], @@ -396,8 +393,7 @@ main( "arrow-parens": "avoid", "prose-wrap": "preserve", "end-of-line": "auto", - write: false, - "from-stdin": false + write: false }, alias: { H: "help" From 9d905a00662581e865967ea57cdbff400cc0433f Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 20 Jun 2019 23:03:04 +0800 Subject: [PATCH 04/52] add test case for reading code from stdin and format --- prettier/main_test.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index a2bc2570227d..d3ebb46d4fdd 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -2,7 +2,7 @@ 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 { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; const { readAll, execPath } = Deno; @@ -233,3 +233,36 @@ test(async function testPrettierPrintToStdout(): Promise { emptyDir(tempDir); }); + +test(async function testPrettierReadFromStdin(): Promise { + // same with command: + // echo 'console.log("hello world" )' | deno run -A ./prettier/main.ts + + const ps1 = xrun({ + args: [`echo`, `console.log("hello world" )`], + stdout: "piped" + }); + + const ps2 = xrun({ + args: ["deno", "run", "-A", "./prettier/main.ts"], + stdout: "piped", + stdin: "piped" + }); + + await Deno.copy(ps2.stdin, ps1.stdout!); + + const status1 = await ps1.status(); // wait process 1 exit. + assertEquals(status1.code, 0); + ps2.stdin.close(); // then close process 2 stdin + const status2 = await ps2.status(); + assertEquals(status2.code, 0); + + const formattedCode = new TextDecoder().decode(await readAll(ps2.stdout!)); + + assertEquals(formattedCode, `console.log("hello world");` + EOL); + + ps1.close(); + ps2.close(); +}); + +runIfMain(import.meta); From 787d3d59d3d1d846476e89ef461124fc75229594 Mon Sep 17 00:00:00 2001 From: axetroy Date: Thu, 20 Jun 2019 23:32:41 +0800 Subject: [PATCH 05/52] update --- prettier/main.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/prettier/main.ts b/prettier/main.ts index b6eca41f0530..5ec24807b428 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -330,6 +330,8 @@ async function main(opts): Promise { write: opts["write"] }; + console.log(Deno.isTTY()); + if (!Deno.isTTY().stdin) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); From f5484f43dade07ba987337e02b6f4ee6108e36b0 Mon Sep 17 00:00:00 2001 From: axetroy Date: Fri, 21 Jun 2019 09:35:39 +0800 Subject: [PATCH 06/52] update --- prettier/main.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/prettier/main.ts b/prettier/main.ts index 5ec24807b428..d8d74409b178 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -330,6 +330,7 @@ async function main(opts): Promise { write: opts["write"] }; + console.log("tty:"); console.log(Deno.isTTY()); if (!Deno.isTTY().stdin) { From 5bb600195dfc9d463e5b7edd251c61662a2e79fb Mon Sep 17 00:00:00 2001 From: axetroy Date: Fri, 21 Jun 2019 09:39:19 +0800 Subject: [PATCH 07/52] update --- prettier/main.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/prettier/main.ts b/prettier/main.ts index d8d74409b178..8631e6514976 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -333,7 +333,9 @@ async function main(opts): Promise { console.log("tty:"); console.log(Deno.isTTY()); - if (!Deno.isTTY().stdin) { + const tty = Deno.isTTY(); + + if (!tty.stdin && tty.stdout) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); stdout.write(new TextEncoder().encode(formattedCode)); From 6c787cf65b00c2e9bae0729a1471a8f3f4513454 Mon Sep 17 00:00:00 2001 From: axetroy Date: Fri, 21 Jun 2019 09:46:12 +0800 Subject: [PATCH 08/52] update --- prettier/main.ts | 3 --- prettier/main_test.ts | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 8631e6514976..f49ec3cf7354 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -330,9 +330,6 @@ async function main(opts): Promise { write: opts["write"] }; - console.log("tty:"); - console.log(Deno.isTTY()); - const tty = Deno.isTTY(); if (!tty.stdin && tty.stdout) { diff --git a/prettier/main_test.ts b/prettier/main_test.ts index d3ebb46d4fdd..86e1e23ffc53 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,6 +235,10 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { + const tty = Deno.isTTY(); + if (!tty.stdin) { + return; + } // same with command: // echo 'console.log("hello world" )' | deno run -A ./prettier/main.ts From ffcc04bec4de0e2e8c5d01c4763d7cb1f6af00c8 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:08:43 +0800 Subject: [PATCH 09/52] update --- prettier/main.ts | 2 ++ prettier/main_test.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index f49ec3cf7354..21ac1c6345d6 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -332,6 +332,8 @@ async function main(opts): Promise { const tty = Deno.isTTY(); + console.log(tty) + if (!tty.stdin && tty.stdout) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 86e1e23ffc53..7b5035c572b8 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -1,7 +1,7 @@ // 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 { assertEquals, assert } from "../testing/asserts.ts"; import { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; @@ -253,7 +253,17 @@ test(async function testPrettierReadFromStdin(): Promise { stdin: "piped" }); - await Deno.copy(ps2.stdin, ps1.stdout!); + // if (!ps2.stdin) { + // assert(!!ps2.stdin, "process should have stdin."); + // return; + // } + + // if (!ps1.stdout) { + // assert(!!ps2.stdin, "process should have stdin."); + // return; + // } + + await Deno.copy(ps2.stdin!, ps1.stdout!); const status1 = await ps1.status(); // wait process 1 exit. assertEquals(status1.code, 0); From 876de567f99ea2782efc0e2970dd7af36414f4fe Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:11:01 +0800 Subject: [PATCH 10/52] update --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 7b5035c572b8..02b4f75a7658 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -1,7 +1,7 @@ // 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, assert } from "../testing/asserts.ts"; +import { assertEquals } from "../testing/asserts.ts"; import { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; From af51bd53d4373953a38042b3c05faf8d80e02182 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:14:10 +0800 Subject: [PATCH 11/52] update --- prettier/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main.ts b/prettier/main.ts index 21ac1c6345d6..e32e775bea47 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -332,7 +332,7 @@ async function main(opts): Promise { const tty = Deno.isTTY(); - console.log(tty) + console.log(tty); if (!tty.stdin && tty.stdout) { const byte = await readAll(stdin); From f1c63a97ca127f6269ac90c2e21a0b3b0589e75e Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:17:04 +0800 Subject: [PATCH 12/52] update --- prettier/main_test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 02b4f75a7658..fa1a151f68d0 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -1,7 +1,7 @@ // 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 { assert, assertEquals } from "../testing/asserts.ts"; import { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; @@ -253,15 +253,15 @@ test(async function testPrettierReadFromStdin(): Promise { stdin: "piped" }); - // if (!ps2.stdin) { - // assert(!!ps2.stdin, "process should have stdin."); - // return; - // } + if (!ps2.stdin) { + assert(!!ps2.stdin, "process should have stdin."); + return; + } - // if (!ps1.stdout) { - // assert(!!ps2.stdin, "process should have stdin."); - // return; - // } + if (!ps1.stdout) { + assert(!!ps2.stdin, "process should have stdin."); + return; + } await Deno.copy(ps2.stdin!, ps1.stdout!); From 727571300659336a5cdb690c02601c7ad3eea131 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:20:18 +0800 Subject: [PATCH 13/52] update --- prettier/main.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index e32e775bea47..f49ec3cf7354 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -332,8 +332,6 @@ async function main(opts): Promise { const tty = Deno.isTTY(); - console.log(tty); - if (!tty.stdin && tty.stdout) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); From db677fe343bfc4c80f4eb079328e7e5a2f8521e2 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:24:43 +0800 Subject: [PATCH 14/52] update --- prettier/main_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index fa1a151f68d0..00c512614851 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -254,12 +254,12 @@ test(async function testPrettierReadFromStdin(): Promise { }); if (!ps2.stdin) { - assert(!!ps2.stdin, "process should have stdin."); + assert(!!ps2.stdin, "process 1 should have stdin."); return; } if (!ps1.stdout) { - assert(!!ps2.stdin, "process should have stdin."); + assert(!!ps1.stdout, "process 2 should have stdout."); return; } From 7a3b205469c63de6a7744f1e394aa17ce0610918 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:30:57 +0800 Subject: [PATCH 15/52] update --- prettier/main_test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 00c512614851..f8d3491d284a 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -242,11 +242,15 @@ test(async function testPrettierReadFromStdin(): Promise { // same with command: // echo 'console.log("hello world" )' | deno run -A ./prettier/main.ts + console.log(`run command1: echo 'console.log("hello world" )'`); + const ps1 = xrun({ args: [`echo`, `console.log("hello world" )`], stdout: "piped" }); + console.log(`run command2: deno run -A ./prettier/main.ts`); + const ps2 = xrun({ args: ["deno", "run", "-A", "./prettier/main.ts"], stdout: "piped", @@ -263,6 +267,8 @@ test(async function testPrettierReadFromStdin(): Promise { return; } + console.log(`pipe command1's stdout to command2's stdin`); + await Deno.copy(ps2.stdin!, ps1.stdout!); const status1 = await ps1.status(); // wait process 1 exit. From 0c1f4d775268b15eac867f44ff3ac565237e50e0 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:34:58 +0800 Subject: [PATCH 16/52] update --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index f8d3491d284a..1512e16c44f9 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -279,7 +279,7 @@ test(async function testPrettierReadFromStdin(): Promise { const formattedCode = new TextDecoder().decode(await readAll(ps2.stdout!)); - assertEquals(formattedCode, `console.log("hello world");` + EOL); + assertEquals(formattedCode, `console.log("hello world");` + EOL + "\n"); ps1.close(); ps2.close(); From c2813a8366dcbca6325b06bb987fe7b1612425ff Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 23 Jun 2019 20:38:28 +0800 Subject: [PATCH 17/52] update --- prettier/main_test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 1512e16c44f9..2c59ab7a6c4f 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,10 +235,6 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { - const tty = Deno.isTTY(); - if (!tty.stdin) { - return; - } // same with command: // echo 'console.log("hello world" )' | deno run -A ./prettier/main.ts @@ -279,7 +275,7 @@ test(async function testPrettierReadFromStdin(): Promise { const formattedCode = new TextDecoder().decode(await readAll(ps2.stdout!)); - assertEquals(formattedCode, `console.log("hello world");` + EOL + "\n"); + assertEquals(formattedCode, `console.log("hello world");` + EOL); ps1.close(); ps2.close(); From 077ba774c5c651569e0cdb890c0191836b70b4e5 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 11:30:22 +0800 Subject: [PATCH 18/52] update --- prettier/main.ts | 2 +- prettier/main_test.ts | 55 +++++++++++++++---------------------------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index f49ec3cf7354..5ec5783664bb 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -332,7 +332,7 @@ async function main(opts): Promise { const tty = Deno.isTTY(); - if (!tty.stdin && tty.stdout) { + if (!tty.stdin && (tty.stdout || tty.stderr)) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); stdout.write(new TextEncoder().encode(formattedCode)); diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 2c59ab7a6c4f..ffdd326912a7 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,50 +235,33 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { - // same with command: - // echo 'console.log("hello world" )' | deno run -A ./prettier/main.ts - - console.log(`run command1: echo 'console.log("hello world" )'`); - - const ps1 = xrun({ - args: [`echo`, `console.log("hello world" )`], + const inputCode = "console.log('abc' )"; + let p1 = Deno.run({ + args: ["echo", inputCode], stdout: "piped" }); - console.log(`run command2: deno run -A ./prettier/main.ts`); - - const ps2 = xrun({ - args: ["deno", "run", "-A", "./prettier/main.ts"], - stdout: "piped", - stdin: "piped" + let p2 = Deno.run({ + args: ["deno", "run", "./prettier/main.ts"], + stdin: "piped", + stdout: "piped" }); - if (!ps2.stdin) { - assert(!!ps2.stdin, "process 1 should have stdin."); - return; - } - - if (!ps1.stdout) { - assert(!!ps1.stdout, "process 2 should have stdout."); - return; - } + const streamed = Deno.copy(p2.stdin, p1.stdout); + const n = await streamed; + console.log("bytes copied:", n); + assertEquals(n, new TextEncoder().encode(inputCode + EOL).length); - console.log(`pipe command1's stdout to command2's stdin`); - - await Deno.copy(ps2.stdin!, ps1.stdout!); - - const status1 = await ps1.status(); // wait process 1 exit. + const status1 = await p1.status(); assertEquals(status1.code, 0); - ps2.stdin.close(); // then close process 2 stdin - const status2 = await ps2.status(); + assertEquals(status1.success, true); + p2.stdin.close(); + const status2 = await p2.status(); assertEquals(status2.code, 0); - - const formattedCode = new TextDecoder().decode(await readAll(ps2.stdout!)); - - assertEquals(formattedCode, `console.log("hello world");` + EOL); - - ps1.close(); - ps2.close(); + assertEquals(status2.success, true); + const stdout = await Deno.readAll(p2.stdout); + const formattedCode = new TextDecoder().decode(stdout); + assertEquals(formattedCode, `console.log("abc");` + EOL); }); runIfMain(import.meta); From 2b254c226f7584beebe147b2951e42c8cda29564 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 11:32:54 +0800 Subject: [PATCH 19/52] update --- prettier/main_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index ffdd326912a7..a34d5801b635 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -1,7 +1,7 @@ // 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 { assert, assertEquals } from "../testing/asserts.ts"; +import { assertEquals } from "../testing/asserts.ts"; import { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; @@ -236,12 +236,12 @@ test(async function testPrettierPrintToStdout(): Promise { test(async function testPrettierReadFromStdin(): Promise { const inputCode = "console.log('abc' )"; - let p1 = Deno.run({ + const p1 = Deno.run({ args: ["echo", inputCode], stdout: "piped" }); - let p2 = Deno.run({ + const p2 = Deno.run({ args: ["deno", "run", "./prettier/main.ts"], stdin: "piped", stdout: "piped" From c9302d90e9e15e8757bd61e0e4d72ee79f3d370f Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 11:39:02 +0800 Subject: [PATCH 20/52] update --- prettier/main_test.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index a34d5801b635..bf3a37661dd0 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -1,7 +1,7 @@ // 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 { assertEquals, assert } from "../testing/asserts.ts"; import { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; @@ -247,6 +247,16 @@ test(async function testPrettierReadFromStdin(): Promise { stdout: "piped" }); + if (!p2.stdin) { + assert(false, "There should be stdin here."); + return; + } + + if (!p1.stdout) { + assert(false, "There should be stdin here."); + return; + } + const streamed = Deno.copy(p2.stdin, p1.stdout); const n = await streamed; console.log("bytes copied:", n); From 2d17da77a835e3c715d40a182fca3f380117cdcf Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 11:41:54 +0800 Subject: [PATCH 21/52] update --- prettier/main_test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index bf3a37661dd0..28e9745e0849 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -252,8 +252,13 @@ test(async function testPrettierReadFromStdin(): Promise { return; } + if (!p2.stdout) { + assert(false, "There should be stdout here."); + return; + } + if (!p1.stdout) { - assert(false, "There should be stdin here."); + assert(false, "There should be stdout here."); return; } From 34d9420716f31e32d6cd0960a87729619c32906f Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 11:50:04 +0800 Subject: [PATCH 22/52] update --- prettier/main_test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 28e9745e0849..4adeaa14d133 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,7 +235,7 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { - const inputCode = "console.log('abc' )"; + const inputCode = `console.log("abc" )`; const p1 = Deno.run({ args: ["echo", inputCode], stdout: "piped" @@ -265,7 +265,6 @@ test(async function testPrettierReadFromStdin(): Promise { const streamed = Deno.copy(p2.stdin, p1.stdout); const n = await streamed; console.log("bytes copied:", n); - assertEquals(n, new TextEncoder().encode(inputCode + EOL).length); const status1 = await p1.status(); assertEquals(status1.code, 0); From b6ab4587d5389698030d5d8431425ce0975d2c08 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 11:55:04 +0800 Subject: [PATCH 23/52] update --- prettier/main_test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 4adeaa14d133..b827a40293e0 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -237,7 +237,7 @@ test(async function testPrettierPrintToStdout(): Promise { test(async function testPrettierReadFromStdin(): Promise { const inputCode = `console.log("abc" )`; const p1 = Deno.run({ - args: ["echo", inputCode], + args: ["echo", `${inputCode}`], stdout: "piped" }); @@ -274,7 +274,9 @@ test(async function testPrettierReadFromStdin(): Promise { assertEquals(status2.code, 0); assertEquals(status2.success, true); const stdout = await Deno.readAll(p2.stdout); - const formattedCode = new TextDecoder().decode(stdout); + const formattedCode = new TextDecoder("utf-8").decode(stdout); + console.log("formatted code: "); + console.log(formattedCode); assertEquals(formattedCode, `console.log("abc");` + EOL); }); From 7a80229c08472dfb9f83d7a6a8608e04b2d95720 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 12:00:03 +0800 Subject: [PATCH 24/52] update --- prettier/main_test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index b827a40293e0..72781f69c286 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,6 +235,7 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { + console.log(Deno.isTTY()); const inputCode = `console.log("abc" )`; const p1 = Deno.run({ args: ["echo", `${inputCode}`], From 7e5940dec0658e412875fd9090eedba0f44102cd Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 12:07:52 +0800 Subject: [PATCH 25/52] update --- prettier/main.ts | 9 +++++++-- prettier/main_test.ts | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 5ec5783664bb..3149d1cf1280 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -23,7 +23,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, stdout, stdin, readAll } = Deno; +const { args, exit, readFile, writeFile, stdout, stdin, readAll, env } = Deno; import { glob, isGlob, GlobOptions } from "../fs/glob.ts"; import { walk, WalkInfo } from "../fs/walk.ts"; import { parse } from "../flags/mod.ts"; @@ -315,6 +315,7 @@ function getTargetFiles( async function main(opts): Promise { const { help, ignore, check, _: args } = opts; + const e = env(); const prettierOpts: PrettierOptions = { printWidth: Number(opts["print-width"]), @@ -331,8 +332,12 @@ async function main(opts): Promise { }; const tty = Deno.isTTY(); + const isTTY = tty.stdin || tty.stdout || tty.stderr; - if (!tty.stdin && (tty.stdout || tty.stderr)) { + if ( + (isTTY && !tty.stdin && (tty.stdout || tty.stderr)) || + e["TEST_READ_FROM_STDIN"] + ) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); stdout.write(new TextEncoder().encode(formattedCode)); diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 72781f69c286..5aa98ae1b467 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -245,7 +245,10 @@ test(async function testPrettierReadFromStdin(): Promise { const p2 = Deno.run({ args: ["deno", "run", "./prettier/main.ts"], stdin: "piped", - stdout: "piped" + stdout: "piped", + env: { + TEST_READ_FROM_STDIN: "1" + } }); if (!p2.stdin) { From 426cc3e6489814250432943f8b9e87e04fe83795 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 12:18:51 +0800 Subject: [PATCH 26/52] update --- prettier/main.ts | 14 +++++++++----- prettier/main_test.ts | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 3149d1cf1280..b68099eda479 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -315,7 +315,6 @@ function getTargetFiles( async function main(opts): Promise { const { help, ignore, check, _: args } = opts; - const e = env(); const prettierOpts: PrettierOptions = { printWidth: Number(opts["print-width"]), @@ -334,10 +333,15 @@ async function main(opts): Promise { const tty = Deno.isTTY(); const isTTY = tty.stdin || tty.stdout || tty.stderr; - if ( - (isTTY && !tty.stdin && (tty.stdout || tty.stderr)) || - e["TEST_READ_FROM_STDIN"] - ) { + let shouldReadFromStdin = false; + + if (!isTTY) { + shouldReadFromStdin = !!env()["TEST_READ_FROM_STDIN"]; + } else { + shouldReadFromStdin = !tty.stdin && (tty.stdout || tty.stderr); + } + + if (shouldReadFromStdin) { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); stdout.write(new TextEncoder().encode(formattedCode)); diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 5aa98ae1b467..d1d5aff9b8e3 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -243,7 +243,7 @@ test(async function testPrettierReadFromStdin(): Promise { }); const p2 = Deno.run({ - args: ["deno", "run", "./prettier/main.ts"], + args: ["deno", "run", "--allow-env", "./prettier/main.ts"], stdin: "piped", stdout: "piped", env: { From f35958e1a977d98a8565d289ba458b3740e1ff9d Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 12:25:19 +0800 Subject: [PATCH 27/52] update --- format.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/format.ts b/format.ts index f0465af50998..5b51e48f3dea 100755 --- a/format.ts +++ b/format.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env deno run --allow-run --allow-write --allow-read +#!/usr/bin/env deno run --allow-run --allow-write --allow-read --allow-env // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. const { exit, args, execPath } = Deno; import { parse } from "./flags/mod.ts"; @@ -10,6 +10,7 @@ async function main(opts): Promise { "run", "--allow-write", "--allow-read", + "--allow-env", "prettier/main.ts", "--ignore", "node_modules", From 390581015539c7f3c69df731b950eef54272030d Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 14:08:41 +0800 Subject: [PATCH 28/52] update --- format.ts | 3 +-- prettier/main.ts | 18 +++++++----------- prettier/main_test.ts | 7 ++----- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/format.ts b/format.ts index 5b51e48f3dea..f0465af50998 100755 --- a/format.ts +++ b/format.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env deno run --allow-run --allow-write --allow-read --allow-env +#!/usr/bin/env deno run --allow-run --allow-write --allow-read // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. const { exit, args, execPath } = Deno; import { parse } from "./flags/mod.ts"; @@ -10,7 +10,6 @@ async function main(opts): Promise { "run", "--allow-write", "--allow-read", - "--allow-env", "prettier/main.ts", "--ignore", "node_modules", diff --git a/prettier/main.ts b/prettier/main.ts index b68099eda479..5dc0f775e357 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -23,7 +23,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, stdout, stdin, readAll, env } = Deno; +const { args, exit, readFile, writeFile, stdout, stdin, readAll } = Deno; import { glob, isGlob, GlobOptions } from "../fs/glob.ts"; import { walk, WalkInfo } from "../fs/walk.ts"; import { parse } from "../flags/mod.ts"; @@ -331,15 +331,9 @@ async function main(opts): Promise { }; const tty = Deno.isTTY(); - const isTTY = tty.stdin || tty.stdout || tty.stderr; - let shouldReadFromStdin = false; - - if (!isTTY) { - shouldReadFromStdin = !!env()["TEST_READ_FROM_STDIN"]; - } else { - shouldReadFromStdin = !tty.stdin && (tty.stdout || tty.stderr); - } + const shouldReadFromStdin = + (!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["from-stdin"]; if (shouldReadFromStdin) { const byte = await readAll(stdin); @@ -390,7 +384,8 @@ main( "use-tabs", "single-quote", "bracket-spacing", - "write" + "write", + "from-stdin" ], default: { ignore: [], @@ -404,7 +399,8 @@ main( "arrow-parens": "avoid", "prose-wrap": "preserve", "end-of-line": "auto", - write: false + write: false, + "from-stdin": false }, alias: { H: "help" diff --git a/prettier/main_test.ts b/prettier/main_test.ts index d1d5aff9b8e3..11676b0a5f16 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -243,12 +243,9 @@ test(async function testPrettierReadFromStdin(): Promise { }); const p2 = Deno.run({ - args: ["deno", "run", "--allow-env", "./prettier/main.ts"], + args: ["deno", "run", "./prettier/main.ts", "--from-stdin"], stdin: "piped", - stdout: "piped", - env: { - TEST_READ_FROM_STDIN: "1" - } + stdout: "piped" }); if (!p2.stdin) { From 78351a9cc34c1103fbb5d2664bee90d1decdc894 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 14:22:28 +0800 Subject: [PATCH 29/52] update --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 11676b0a5f16..ad2b95caed6b 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -278,7 +278,7 @@ test(async function testPrettierReadFromStdin(): Promise { const formattedCode = new TextDecoder("utf-8").decode(stdout); console.log("formatted code: "); console.log(formattedCode); - assertEquals(formattedCode, `console.log("abc");` + EOL); + assertEquals(formattedCode, `console.log("abc");` + "\n"); }); runIfMain(import.meta); From e6bf9a82eed95122c831ee47220fc9c253aa9ec8 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 14:27:06 +0800 Subject: [PATCH 30/52] update --- prettier/main_test.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index ad2b95caed6b..912fa3717e19 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,7 +235,6 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { - console.log(Deno.isTTY()); const inputCode = `console.log("abc" )`; const p1 = Deno.run({ args: ["echo", `${inputCode}`], @@ -276,8 +275,6 @@ test(async function testPrettierReadFromStdin(): Promise { assertEquals(status2.success, true); const stdout = await Deno.readAll(p2.stdout); const formattedCode = new TextDecoder("utf-8").decode(stdout); - console.log("formatted code: "); - console.log(formattedCode); assertEquals(formattedCode, `console.log("abc");` + "\n"); }); From 2632e69fc55f7701b7134c7659447b419b7ddb09 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 15:46:15 +0800 Subject: [PATCH 31/52] update useage --- prettier/README.md | 14 +++++++++++--- prettier/main.ts | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/prettier/README.md b/prettier/README.md index 3a8d5a0c27dc..2fd7f26f17a3 100644 --- a/prettier/README.md +++ b/prettier/README.md @@ -6,22 +6,30 @@ Prettier APIs and tools for deno To formats the source files, run: -```console +```bash deno --allow-read --allow-write https://deno.land/std/prettier/main.ts ``` You can format only specific files by passing the arguments. -```console +```bash 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 +```bash deno --allow-read --allow-write https://deno.land/std/prettier/main.ts path/to/script.ts ``` +You can format the input plain text stream. + +Because there is no way to identify which file type is from the stream, only formatted typescript is currently supported. + +```bash +cat path/to/script.ts | deno https://deno.land/std/prettier/main.ts +``` + ## Use API You can use APIs of prettier as the following: diff --git a/prettier/main.ts b/prettier/main.ts index 5dc0f775e357..67cd06687343 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -76,6 +76,9 @@ Example: deno run prettier/main.ts script1.ts Print the formatted code to stdout + cat script1.ts | deno run prettier/main.ts + Read the Typescript code from stdin and + output formatted code to stdout. `; // Available parsers From bcddbd161b0252df9f62d2ae8ead1e5d90a44bdf Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 21:39:42 +0800 Subject: [PATCH 32/52] refactor --- prettier/main.ts | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 67cd06687343..5aeb54304888 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -253,6 +253,15 @@ function format(text: string, prettierOpts: PrettierOptions): string { return formatted; } +/** + * Format code from stdin and output to stdout + */ +async function formatFromStdin(prettierOpts: PrettierOptions) { + const byte = await readAll(stdin); + const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); + await stdout.write(new TextEncoder().encode(formattedCode)); +} + /** * Get the files to format. * @param selectors The glob patterns to select the files. @@ -333,18 +342,6 @@ async function main(opts): Promise { write: opts["write"] }; - const tty = Deno.isTTY(); - - const shouldReadFromStdin = - (!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["from-stdin"]; - - if (shouldReadFromStdin) { - const byte = await readAll(stdin); - const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); - stdout.write(new TextEncoder().encode(formattedCode)); - return; - } - if (help) { console.log(HELP_MESSAGE); exit(0); @@ -357,14 +354,21 @@ async function main(opts): Promise { options ); + const tty = Deno.isTTY(); + + const shouldReadFromStdin = + (!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["from-stdin"]; + try { - if (check) { + if (shouldReadFromStdin) { + await formatFromStdin(prettierOpts); + } else if (check) { await checkSourceFiles(files, prettierOpts); } else { await formatSourceFiles(files, prettierOpts); } } catch (e) { - console.log(e); + console.error(e); exit(1); } } @@ -409,4 +413,7 @@ main( H: "help" } }) -); +).catch(err => { + console.error(err); + exit(1); +}); From a252c2fb4e6fb61df4bcb2c33418174fd85fabc1 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 21:43:27 +0800 Subject: [PATCH 33/52] fix eslint --- prettier/main.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 5aeb54304888..c7094292e9ff 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -256,7 +256,7 @@ function format(text: string, prettierOpts: PrettierOptions): string { /** * Format code from stdin and output to stdout */ -async function formatFromStdin(prettierOpts: PrettierOptions) { +async function formatFromStdin(prettierOpts: PrettierOptions): Promise { const byte = await readAll(stdin); const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); await stdout.write(new TextEncoder().encode(formattedCode)); @@ -413,7 +413,9 @@ main( H: "help" } }) -).catch(err => { - console.error(err); - exit(1); -}); +).catch( + (err): void => { + console.error(err); + exit(1); + } +); From b9863903f75f8736c811b56dc66b92dd5db640e5 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 22:04:17 +0800 Subject: [PATCH 34/52] add test case when stdin is invalid typescript code --- prettier/main_test.ts | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 912fa3717e19..895510964608 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -276,6 +276,68 @@ test(async function testPrettierReadFromStdin(): Promise { const stdout = await Deno.readAll(p2.stdout); const formattedCode = new TextDecoder("utf-8").decode(stdout); assertEquals(formattedCode, `console.log("abc");` + "\n"); + p2.close(); + p1.close(); +}); + +test(async function testPrettierReadInvalidCodeFromStdin(): Promise { + const inputCode = `InvalidTypescriptCode##@@!!`; + const p1 = Deno.run({ + args: ["echo", `${inputCode}`], + stdout: "piped" + }); + + const p2 = Deno.run({ + args: ["deno", "run", "./prettier/main.ts", "--from-stdin"], + stdin: "piped", + stdout: "piped", + stderr: "piped" + }); + + if (!p2.stdin) { + assert(false, "There should be stdin here."); + return; + } + + if (!p2.stdout) { + assert(false, "There should be stdout here."); + return; + } + + if (!p2.stderr) { + assert(false, "There should be stderr here."); + return; + } + + if (!p1.stdout) { + assert(false, "There should be stdout here."); + return; + } + + const streamed = Deno.copy(p2.stdin, p1.stdout); + const n = await streamed; + console.log("bytes copied:", n); + + const status1 = await p1.status(); + assertEquals(status1.code, 0); + assertEquals(status1.success, true); + p2.stdin.close(); + const status2 = await p2.status(); + assertEquals(status2.code, 1); + assertEquals(status2.success, false); + const stdoutOutput = new TextDecoder("utf-8").decode( + await Deno.readAll(p2.stdout) + ); + const stderrOutput = new TextDecoder("utf-8").decode( + await Deno.readAll(p2.stderr) + ); + assertEquals(stdoutOutput, ""); + assertEquals( + stderrOutput.split("\n")[0], + "SyntaxError: Invalid character. (1:22)" + ); + p2.close(); + p1.close(); }); runIfMain(import.meta); From 972eeabf5d07deb99591149f8a4c1edc7d0e6626 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 22:09:51 +0800 Subject: [PATCH 35/52] fix --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 895510964608..5399a9278779 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -333,7 +333,7 @@ test(async function testPrettierReadInvalidCodeFromStdin(): Promise { ); assertEquals(stdoutOutput, ""); assertEquals( - stderrOutput.split("\n")[0], + stderrOutput.split(EOL)[0], "SyntaxError: Invalid character. (1:22)" ); p2.close(); From a692dd1b9cddd6a39be3226322f452586bf604bf Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 23:14:16 +0800 Subject: [PATCH 36/52] support format markdown/json/js file from stdin --- prettier/README.md | 7 +- prettier/main.ts | 31 +++++--- prettier/main_test.ts | 162 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 185 insertions(+), 15 deletions(-) diff --git a/prettier/README.md b/prettier/README.md index 2fd7f26f17a3..6485d2da21e5 100644 --- a/prettier/README.md +++ b/prettier/README.md @@ -22,12 +22,13 @@ You can format files on specific directory by passing the directory's path. deno --allow-read --allow-write https://deno.land/std/prettier/main.ts path/to/script.ts ``` -You can format the input plain text stream. - -Because there is no way to identify which file type is from the stream, only formatted typescript is currently supported. +You can format the input plain text stream. default parse it as typescript code. ```bash cat path/to/script.ts | deno https://deno.land/std/prettier/main.ts +cat path/to/script.js | deno https://deno.land/std/prettier/main.ts --stdin-parser=babel +cat path/to/config.json | deno https://deno.land/std/prettier/main.ts --stdin-parser=json +cat path/to/README.md | deno https://deno.land/std/prettier/main.ts --stdin-parser=markdown ``` ## Use API diff --git a/prettier/main.ts b/prettier/main.ts index c7094292e9ff..b89942d3c41d 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -243,10 +243,14 @@ async function formatSourceFiles( /** * Format source code */ -function format(text: string, prettierOpts: PrettierOptions): string { +function format( + text: string, + parser: ParserLabel, + prettierOpts: PrettierOptions +): string { const formatted: string = prettier.format(text, { ...prettierOpts, - parser: selectParser("stdin.ts"), + parser: parser, plugins: prettierPlugins }); @@ -256,9 +260,16 @@ function format(text: string, prettierOpts: PrettierOptions): string { /** * Format code from stdin and output to stdout */ -async function formatFromStdin(prettierOpts: PrettierOptions): Promise { +async function formatFromStdin( + parser: ParserLabel, + prettierOpts: PrettierOptions +): Promise { const byte = await readAll(stdin); - const formattedCode = format(new TextDecoder().decode(byte), prettierOpts); + const formattedCode = format( + new TextDecoder().decode(byte), + parser, + prettierOpts + ); await stdout.write(new TextEncoder().encode(formattedCode)); } @@ -357,11 +368,11 @@ async function main(opts): Promise { const tty = Deno.isTTY(); const shouldReadFromStdin = - (!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["from-stdin"]; + (!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["stdin"]; try { if (shouldReadFromStdin) { - await formatFromStdin(prettierOpts); + await formatFromStdin(opts["stdin-parser"], prettierOpts); } else if (check) { await checkSourceFiles(files, prettierOpts); } else { @@ -382,7 +393,8 @@ main( "trailing-comma", "arrow-parens", "prose-wrap", - "end-of-line" + "end-of-line", + "stdin-parser" ], boolean: [ "check", @@ -392,7 +404,7 @@ main( "single-quote", "bracket-spacing", "write", - "from-stdin" + "stdin" ], default: { ignore: [], @@ -407,7 +419,8 @@ main( "prose-wrap": "preserve", "end-of-line": "auto", write: false, - "from-stdin": false + stdin: false, + "stdin-parser": "typescript" }, alias: { H: "help" diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 5399a9278779..ce66ddd74bd5 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -234,7 +234,7 @@ test(async function testPrettierPrintToStdout(): Promise { emptyDir(tempDir); }); -test(async function testPrettierReadFromStdin(): Promise { +test(async function testPrettierReadFromStdinFormatTS(): Promise { const inputCode = `console.log("abc" )`; const p1 = Deno.run({ args: ["echo", `${inputCode}`], @@ -242,7 +242,7 @@ test(async function testPrettierReadFromStdin(): Promise { }); const p2 = Deno.run({ - args: ["deno", "run", "./prettier/main.ts", "--from-stdin"], + args: ["deno", "run", "./prettier/main.ts", "--stdin"], stdin: "piped", stdout: "piped" }); @@ -280,6 +280,162 @@ test(async function testPrettierReadFromStdin(): Promise { p1.close(); }); +test(async function testPrettierReadFromStdinFormatJS(): Promise { + const inputCode = `console.log("abc" )`; + const p1 = Deno.run({ + args: ["echo", `${inputCode}`], + stdout: "piped" + }); + + const p2 = Deno.run({ + args: [ + "deno", + "run", + "./prettier/main.ts", + "--stdin", + "--stdin-parser=babel" + ], + stdin: "piped", + stdout: "piped" + }); + + if (!p2.stdin) { + assert(false, "There should be stdin here."); + return; + } + + if (!p2.stdout) { + assert(false, "There should be stdout here."); + return; + } + + if (!p1.stdout) { + assert(false, "There should be stdout here."); + return; + } + + const streamed = Deno.copy(p2.stdin, p1.stdout); + const n = await streamed; + console.log("bytes copied:", n); + + const status1 = await p1.status(); + assertEquals(status1.code, 0); + assertEquals(status1.success, true); + p2.stdin.close(); + const status2 = await p2.status(); + assertEquals(status2.code, 0); + assertEquals(status2.success, true); + const stdout = await Deno.readAll(p2.stdout); + const formattedCode = new TextDecoder("utf-8").decode(stdout); + assertEquals(formattedCode, `console.log("abc");` + "\n"); + p2.close(); + p1.close(); +}); + +test(async function testPrettierReadFromStdinFormatJSON(): Promise { + const inputCode = `{"a":"b"}`; + const p1 = Deno.run({ + args: ["echo", `${inputCode}`], + stdout: "piped" + }); + + const p2 = Deno.run({ + args: [ + "deno", + "run", + "./prettier/main.ts", + "--stdin", + "--stdin-parser=json" + ], + stdin: "piped", + stdout: "piped" + }); + + if (!p2.stdin) { + assert(false, "There should be stdin here."); + return; + } + + if (!p2.stdout) { + assert(false, "There should be stdout here."); + return; + } + + if (!p1.stdout) { + assert(false, "There should be stdout here."); + return; + } + + const streamed = Deno.copy(p2.stdin, p1.stdout); + const n = await streamed; + console.log("bytes copied:", n); + + const status1 = await p1.status(); + assertEquals(status1.code, 0); + assertEquals(status1.success, true); + p2.stdin.close(); + const status2 = await p2.status(); + assertEquals(status2.code, 0); + assertEquals(status2.success, true); + const stdout = await Deno.readAll(p2.stdout); + const formattedCode = new TextDecoder("utf-8").decode(stdout); + assertEquals(formattedCode, `{ "a": "b" }` + "\n"); + p2.close(); + p1.close(); +}); + +test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { + const inputCode = `## test`; + const p1 = Deno.run({ + args: ["echo", `${inputCode}`], + stdout: "piped" + }); + + const p2 = Deno.run({ + args: [ + "deno", + "run", + "./prettier/main.ts", + "--stdin", + "--stdin-parser=markdown" + ], + stdin: "piped", + stdout: "piped" + }); + + if (!p2.stdin) { + assert(false, "There should be stdin here."); + return; + } + + if (!p2.stdout) { + assert(false, "There should be stdout here."); + return; + } + + if (!p1.stdout) { + assert(false, "There should be stdout here."); + return; + } + + const streamed = Deno.copy(p2.stdin, p1.stdout); + const n = await streamed; + console.log("bytes copied:", n); + + const status1 = await p1.status(); + assertEquals(status1.code, 0); + assertEquals(status1.success, true); + p2.stdin.close(); + const status2 = await p2.status(); + assertEquals(status2.code, 0); + assertEquals(status2.success, true); + const stdout = await Deno.readAll(p2.stdout); + const formattedCode = new TextDecoder("utf-8").decode(stdout); + assertEquals(formattedCode, `## test` + "\n"); + p2.close(); + p1.close(); +}); + test(async function testPrettierReadInvalidCodeFromStdin(): Promise { const inputCode = `InvalidTypescriptCode##@@!!`; const p1 = Deno.run({ @@ -288,7 +444,7 @@ test(async function testPrettierReadInvalidCodeFromStdin(): Promise { }); const p2 = Deno.run({ - args: ["deno", "run", "./prettier/main.ts", "--from-stdin"], + args: ["deno", "run", "./prettier/main.ts", "--stdin"], stdin: "piped", stdout: "piped", stderr: "piped" From a6a6c5e9588d888cdacc532c3a59f49a7eb5b5b7 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 23:26:46 +0800 Subject: [PATCH 37/52] fix eol --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index ce66ddd74bd5..278549e43259 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -379,7 +379,7 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { assertEquals(status2.success, true); const stdout = await Deno.readAll(p2.stdout); const formattedCode = new TextDecoder("utf-8").decode(stdout); - assertEquals(formattedCode, `{ "a": "b" }` + "\n"); + assertEquals(formattedCode, `{ "a": "b" }` + EOL); p2.close(); p1.close(); }); From 1c07d7556c9a94555aca9319649f63e336b60f82 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 23:37:40 +0800 Subject: [PATCH 38/52] try fix in windows --- prettier/main_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 278549e43259..5b0f084283d4 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -333,7 +333,7 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { }); test(async function testPrettierReadFromStdinFormatJSON(): Promise { - const inputCode = `{"a":"b"}`; + const inputCode = `{\"a\":\"b\"}`; const p1 = Deno.run({ args: ["echo", `${inputCode}`], stdout: "piped" @@ -379,7 +379,7 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { assertEquals(status2.success, true); const stdout = await Deno.readAll(p2.stdout); const formattedCode = new TextDecoder("utf-8").decode(stdout); - assertEquals(formattedCode, `{ "a": "b" }` + EOL); + assertEquals(formattedCode, `{ "a": "b" }` + "\n"); p2.close(); p1.close(); }); From 93e944a57041d8f0fd48fb71f5282a70fe5173ae Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 23:49:31 +0800 Subject: [PATCH 39/52] try fix in windows --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 5b0f084283d4..2955566edb24 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -333,7 +333,7 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { }); test(async function testPrettierReadFromStdinFormatJSON(): Promise { - const inputCode = `{\"a\":\"b\"}`; + const inputCode = `"{\"a\":\"b\"}"`; const p1 = Deno.run({ args: ["echo", `${inputCode}`], stdout: "piped" From aeea8347494c42898107187f6317ab96db986833 Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 24 Jun 2019 23:54:28 +0800 Subject: [PATCH 40/52] update --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 2955566edb24..5b0f084283d4 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -333,7 +333,7 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { }); test(async function testPrettierReadFromStdinFormatJSON(): Promise { - const inputCode = `"{\"a\":\"b\"}"`; + const inputCode = `{\"a\":\"b\"}`; const p1 = Deno.run({ args: ["echo", `${inputCode}`], stdout: "piped" From 151bb91416369a4de2d1f492f94704a0eb978674 Mon Sep 17 00:00:00 2001 From: axetroy Date: Tue, 25 Jun 2019 09:52:44 +0800 Subject: [PATCH 41/52] replace echo with echox --- prettier/main_test.ts | 10 +++++----- prettier/testdata/echox.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 prettier/testdata/echox.ts diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 5b0f084283d4..00e7b272cc18 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -237,7 +237,7 @@ test(async function testPrettierPrintToStdout(): Promise { test(async function testPrettierReadFromStdinFormatTS(): Promise { const inputCode = `console.log("abc" )`; const p1 = Deno.run({ - args: ["echo", `${inputCode}`], + args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); @@ -283,7 +283,7 @@ test(async function testPrettierReadFromStdinFormatTS(): Promise { test(async function testPrettierReadFromStdinFormatJS(): Promise { const inputCode = `console.log("abc" )`; const p1 = Deno.run({ - args: ["echo", `${inputCode}`], + args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); @@ -335,7 +335,7 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { test(async function testPrettierReadFromStdinFormatJSON(): Promise { const inputCode = `{\"a\":\"b\"}`; const p1 = Deno.run({ - args: ["echo", `${inputCode}`], + args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); @@ -387,7 +387,7 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { const inputCode = `## test`; const p1 = Deno.run({ - args: ["echo", `${inputCode}`], + args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); @@ -439,7 +439,7 @@ test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { test(async function testPrettierReadInvalidCodeFromStdin(): Promise { const inputCode = `InvalidTypescriptCode##@@!!`; const p1 = Deno.run({ - args: ["echo", `${inputCode}`], + args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); diff --git a/prettier/testdata/echox.ts b/prettier/testdata/echox.ts new file mode 100644 index 000000000000..68c54b9995f5 --- /dev/null +++ b/prettier/testdata/echox.ts @@ -0,0 +1,8 @@ +async function echox(args: string[]) { + for (const arg of args) { + await Deno.stdout.write(new TextEncoder().encode(arg)); + } + Deno.exit(0); +} + +echox(Deno.args.slice(1)); From 3398246a2e0487aff65d7ab761b5243586bff72f Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 16:48:56 +0800 Subject: [PATCH 42/52] remove main function's error handler --- prettier/main.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index b89942d3c41d..195a5baaa40a 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -426,9 +426,4 @@ main( H: "help" } }) -).catch( - (err): void => { - console.error(err); - exit(1); - } -); +) From 0f74198b449351f77d03a128ff52a9a65bf0dd33 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 16:59:14 +0800 Subject: [PATCH 43/52] improve command help infomation --- prettier/main.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/prettier/main.ts b/prettier/main.ts index 195a5baaa40a..2b649ece47a5 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -41,6 +41,14 @@ Options: it will output to stdout, Defaults to false. --ignore Ignore the given path(s). + --stdin Specifies to read the code from stdin. + If run the command in a pipe in the + terminal, you do not need to specify + this flag. + --stdin-parser If set --stdin flag, then need specify a + parser for stdin. available parser: + typescript/babel/markdown/json. Defaults + to typescript. JS/TS Styling Options: --print-width The line length where Prettier will try @@ -76,8 +84,13 @@ Example: deno run prettier/main.ts script1.ts Print the formatted code to stdout + cat script1.ts | deno run prettier/main.ts - Read the Typescript code from stdin and + Read the typescript code from stdin and + output formatted code to stdout. + + cat config.json | deno run prettier/main.ts --stdin-parser=json + Read the JSON string from stdin and output formatted code to stdout. `; From f5f2f8b9dae8ca2dbc4f35eebd92e2e5c9ebc490 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 17:04:22 +0800 Subject: [PATCH 44/52] format code --- prettier/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main.ts b/prettier/main.ts index 2b649ece47a5..5275b16f6791 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -439,4 +439,4 @@ main( H: "help" } }) -) +); From 3c0097f2509df6211a13367e1cac0238cf897be0 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 19:51:09 +0800 Subject: [PATCH 45/52] remove unnecessary assertions --- prettier/main_test.ts | 65 ------------------------------------------- 1 file changed, 65 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 00e7b272cc18..1600b3cf4b59 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -299,21 +299,6 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { stdout: "piped" }); - if (!p2.stdin) { - assert(false, "There should be stdin here."); - return; - } - - if (!p2.stdout) { - assert(false, "There should be stdout here."); - return; - } - - if (!p1.stdout) { - assert(false, "There should be stdout here."); - return; - } - const streamed = Deno.copy(p2.stdin, p1.stdout); const n = await streamed; console.log("bytes copied:", n); @@ -351,21 +336,6 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { stdout: "piped" }); - if (!p2.stdin) { - assert(false, "There should be stdin here."); - return; - } - - if (!p2.stdout) { - assert(false, "There should be stdout here."); - return; - } - - if (!p1.stdout) { - assert(false, "There should be stdout here."); - return; - } - const streamed = Deno.copy(p2.stdin, p1.stdout); const n = await streamed; console.log("bytes copied:", n); @@ -403,21 +373,6 @@ test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { stdout: "piped" }); - if (!p2.stdin) { - assert(false, "There should be stdin here."); - return; - } - - if (!p2.stdout) { - assert(false, "There should be stdout here."); - return; - } - - if (!p1.stdout) { - assert(false, "There should be stdout here."); - return; - } - const streamed = Deno.copy(p2.stdin, p1.stdout); const n = await streamed; console.log("bytes copied:", n); @@ -450,26 +405,6 @@ test(async function testPrettierReadInvalidCodeFromStdin(): Promise { stderr: "piped" }); - if (!p2.stdin) { - assert(false, "There should be stdin here."); - return; - } - - if (!p2.stdout) { - assert(false, "There should be stdout here."); - return; - } - - if (!p2.stderr) { - assert(false, "There should be stderr here."); - return; - } - - if (!p1.stdout) { - assert(false, "There should be stdout here."); - return; - } - const streamed = Deno.copy(p2.stdin, p1.stdout); const n = await streamed; console.log("bytes copied:", n); From 27121fefd423d61f11e9f87d4f2799f1802daa17 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 19:53:48 +0800 Subject: [PATCH 46/52] improve test --- prettier/main_test.ts | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 1600b3cf4b59..7384f834062a 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -262,8 +262,7 @@ test(async function testPrettierReadFromStdinFormatTS(): Promise { return; } - const streamed = Deno.copy(p2.stdin, p1.stdout); - const n = await streamed; + const n = await Deno.copy(p2.stdin, p1.stdout); console.log("bytes copied:", n); const status1 = await p1.status(); @@ -299,8 +298,7 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { stdout: "piped" }); - const streamed = Deno.copy(p2.stdin, p1.stdout); - const n = await streamed; + const n = await Deno.copy(p2.stdin, p1.stdout); console.log("bytes copied:", n); const status1 = await p1.status(); @@ -320,13 +318,13 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { test(async function testPrettierReadFromStdinFormatJSON(): Promise { const inputCode = `{\"a\":\"b\"}`; const p1 = Deno.run({ - args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); const p2 = Deno.run({ args: [ - "deno", + execPath, "run", "./prettier/main.ts", "--stdin", @@ -336,8 +334,7 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { stdout: "piped" }); - const streamed = Deno.copy(p2.stdin, p1.stdout); - const n = await streamed; + const n = await Deno.copy(p2.stdin, p1.stdout); console.log("bytes copied:", n); const status1 = await p1.status(); @@ -357,13 +354,13 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { const inputCode = `## test`; const p1 = Deno.run({ - args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); const p2 = Deno.run({ args: [ - "deno", + execPath, "run", "./prettier/main.ts", "--stdin", @@ -373,8 +370,7 @@ test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { stdout: "piped" }); - const streamed = Deno.copy(p2.stdin, p1.stdout); - const n = await streamed; + const n = await Deno.copy(p2.stdin, p1.stdout); console.log("bytes copied:", n); const status1 = await p1.status(); @@ -394,19 +390,18 @@ test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { test(async function testPrettierReadInvalidCodeFromStdin(): Promise { const inputCode = `InvalidTypescriptCode##@@!!`; const p1 = Deno.run({ - args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); const p2 = Deno.run({ - args: ["deno", "run", "./prettier/main.ts", "--stdin"], + args: [execPath, "run", "./prettier/main.ts", "--stdin"], stdin: "piped", stdout: "piped", stderr: "piped" }); - const streamed = Deno.copy(p2.stdin, p1.stdout); - const n = await streamed; + const n = await Deno.copy(p2.stdin, p1.stdout); console.log("bytes copied:", n); const status1 = await p1.status(); From e0255d3a2fc018bac045b7ca5c4279ed0c582dbd Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 20:07:53 +0800 Subject: [PATCH 47/52] improve test --- prettier/main_test.ts | 57 ++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 7384f834062a..812cf2551222 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -1,7 +1,7 @@ // 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, assert } from "../testing/asserts.ts"; +import { assertEquals } from "../testing/asserts.ts"; import { test, runIfMain } from "../testing/mod.ts"; import { xrun } from "./util.ts"; import { copy, emptyDir } from "../fs/mod.ts"; @@ -237,42 +237,27 @@ test(async function testPrettierPrintToStdout(): Promise { test(async function testPrettierReadFromStdinFormatTS(): Promise { const inputCode = `console.log("abc" )`; const p1 = Deno.run({ - args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); const p2 = Deno.run({ - args: ["deno", "run", "./prettier/main.ts", "--stdin"], + args: [execPath, "run", "./prettier/main.ts", "--stdin"], stdin: "piped", stdout: "piped" }); - if (!p2.stdin) { - assert(false, "There should be stdin here."); - return; - } - - if (!p2.stdout) { - assert(false, "There should be stdout here."); - return; - } - - if (!p1.stdout) { - assert(false, "There should be stdout here."); - return; - } - - const n = await Deno.copy(p2.stdin, p1.stdout); + const n = await Deno.copy(p2.stdin!, p1.stdout!); console.log("bytes copied:", n); const status1 = await p1.status(); assertEquals(status1.code, 0); assertEquals(status1.success, true); - p2.stdin.close(); + p2.stdin!.close(); const status2 = await p2.status(); assertEquals(status2.code, 0); assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout); + const stdout = await Deno.readAll(p2.stdout!); const formattedCode = new TextDecoder("utf-8").decode(stdout); assertEquals(formattedCode, `console.log("abc");` + "\n"); p2.close(); @@ -282,13 +267,13 @@ test(async function testPrettierReadFromStdinFormatTS(): Promise { test(async function testPrettierReadFromStdinFormatJS(): Promise { const inputCode = `console.log("abc" )`; const p1 = Deno.run({ - args: ["deno", "./prettier/testdata/echox.ts", `${inputCode}`], + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], stdout: "piped" }); const p2 = Deno.run({ args: [ - "deno", + execPath, "run", "./prettier/main.ts", "--stdin", @@ -298,17 +283,17 @@ test(async function testPrettierReadFromStdinFormatJS(): Promise { stdout: "piped" }); - const n = await Deno.copy(p2.stdin, p1.stdout); + const n = await Deno.copy(p2.stdin!, p1.stdout!); console.log("bytes copied:", n); const status1 = await p1.status(); assertEquals(status1.code, 0); assertEquals(status1.success, true); - p2.stdin.close(); + p2.stdin!.close(); const status2 = await p2.status(); assertEquals(status2.code, 0); assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout); + const stdout = await Deno.readAll(p2.stdout!); const formattedCode = new TextDecoder("utf-8").decode(stdout); assertEquals(formattedCode, `console.log("abc");` + "\n"); p2.close(); @@ -334,17 +319,17 @@ test(async function testPrettierReadFromStdinFormatJSON(): Promise { stdout: "piped" }); - const n = await Deno.copy(p2.stdin, p1.stdout); + const n = await Deno.copy(p2.stdin!, p1.stdout!); console.log("bytes copied:", n); const status1 = await p1.status(); assertEquals(status1.code, 0); assertEquals(status1.success, true); - p2.stdin.close(); + p2.stdin!.close(); const status2 = await p2.status(); assertEquals(status2.code, 0); assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout); + const stdout = await Deno.readAll(p2.stdout!); const formattedCode = new TextDecoder("utf-8").decode(stdout); assertEquals(formattedCode, `{ "a": "b" }` + "\n"); p2.close(); @@ -370,17 +355,17 @@ test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { stdout: "piped" }); - const n = await Deno.copy(p2.stdin, p1.stdout); + const n = await Deno.copy(p2.stdin!, p1.stdout!); console.log("bytes copied:", n); const status1 = await p1.status(); assertEquals(status1.code, 0); assertEquals(status1.success, true); - p2.stdin.close(); + p2.stdin!.close(); const status2 = await p2.status(); assertEquals(status2.code, 0); assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout); + const stdout = await Deno.readAll(p2.stdout!); const formattedCode = new TextDecoder("utf-8").decode(stdout); assertEquals(formattedCode, `## test` + "\n"); p2.close(); @@ -401,21 +386,21 @@ test(async function testPrettierReadInvalidCodeFromStdin(): Promise { stderr: "piped" }); - const n = await Deno.copy(p2.stdin, p1.stdout); + const n = await Deno.copy(p2.stdin!, p1.stdout!); console.log("bytes copied:", n); const status1 = await p1.status(); assertEquals(status1.code, 0); assertEquals(status1.success, true); - p2.stdin.close(); + p2.stdin!.close(); const status2 = await p2.status(); assertEquals(status2.code, 1); assertEquals(status2.success, false); const stdoutOutput = new TextDecoder("utf-8").decode( - await Deno.readAll(p2.stdout) + await Deno.readAll(p2.stdout!) ); const stderrOutput = new TextDecoder("utf-8").decode( - await Deno.readAll(p2.stderr) + await Deno.readAll(p2.stderr!) ); assertEquals(stdoutOutput, ""); assertEquals( From 3a304de60b14ab1ff50aeb0d5123a002f8567a76 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 20:37:34 +0800 Subject: [PATCH 48/52] refactor main test. make it clear --- prettier/main_test.ts | 282 ++++++++++++++++-------------------------- 1 file changed, 108 insertions(+), 174 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 812cf2551222..fb0e3d9e0ad0 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -234,181 +234,115 @@ test(async function testPrettierPrintToStdout(): Promise { emptyDir(tempDir); }); -test(async function testPrettierReadFromStdinFormatTS(): Promise { - const inputCode = `console.log("abc" )`; - const p1 = Deno.run({ - args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], - stdout: "piped" - }); - - const p2 = Deno.run({ - args: [execPath, "run", "./prettier/main.ts", "--stdin"], - stdin: "piped", - stdout: "piped" - }); - - const n = await Deno.copy(p2.stdin!, p1.stdout!); - console.log("bytes copied:", n); - - const status1 = await p1.status(); - assertEquals(status1.code, 0); - assertEquals(status1.success, true); - p2.stdin!.close(); - const status2 = await p2.status(); - assertEquals(status2.code, 0); - assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout!); - const formattedCode = new TextDecoder("utf-8").decode(stdout); - assertEquals(formattedCode, `console.log("abc");` + "\n"); - p2.close(); - p1.close(); -}); - -test(async function testPrettierReadFromStdinFormatJS(): Promise { - const inputCode = `console.log("abc" )`; - const p1 = Deno.run({ - args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], - stdout: "piped" - }); - - const p2 = Deno.run({ - args: [ - execPath, - "run", - "./prettier/main.ts", - "--stdin", - "--stdin-parser=babel" - ], - stdin: "piped", - stdout: "piped" - }); - - const n = await Deno.copy(p2.stdin!, p1.stdout!); - console.log("bytes copied:", n); - - const status1 = await p1.status(); - assertEquals(status1.code, 0); - assertEquals(status1.success, true); - p2.stdin!.close(); - const status2 = await p2.status(); - assertEquals(status2.code, 0); - assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout!); - const formattedCode = new TextDecoder("utf-8").decode(stdout); - assertEquals(formattedCode, `console.log("abc");` + "\n"); - p2.close(); - p1.close(); -}); - -test(async function testPrettierReadFromStdinFormatJSON(): Promise { - const inputCode = `{\"a\":\"b\"}`; - const p1 = Deno.run({ - args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], - stdout: "piped" - }); - - const p2 = Deno.run({ - args: [ - execPath, - "run", - "./prettier/main.ts", - "--stdin", - "--stdin-parser=json" - ], - stdin: "piped", - stdout: "piped" - }); - - const n = await Deno.copy(p2.stdin!, p1.stdout!); - console.log("bytes copied:", n); - - const status1 = await p1.status(); - assertEquals(status1.code, 0); - assertEquals(status1.success, true); - p2.stdin!.close(); - const status2 = await p2.status(); - assertEquals(status2.code, 0); - assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout!); - const formattedCode = new TextDecoder("utf-8").decode(stdout); - assertEquals(formattedCode, `{ "a": "b" }` + "\n"); - p2.close(); - p1.close(); -}); - -test(async function testPrettierReadFromStdinFormatMarkdown(): Promise { - const inputCode = `## test`; - const p1 = Deno.run({ - args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], - stdout: "piped" - }); - - const p2 = Deno.run({ - args: [ - execPath, - "run", - "./prettier/main.ts", - "--stdin", - "--stdin-parser=markdown" - ], - stdin: "piped", - stdout: "piped" - }); - - const n = await Deno.copy(p2.stdin!, p1.stdout!); - console.log("bytes copied:", n); - - const status1 = await p1.status(); - assertEquals(status1.code, 0); - assertEquals(status1.success, true); - p2.stdin!.close(); - const status2 = await p2.status(); - assertEquals(status2.code, 0); - assertEquals(status2.success, true); - const stdout = await Deno.readAll(p2.stdout!); - const formattedCode = new TextDecoder("utf-8").decode(stdout); - assertEquals(formattedCode, `## test` + "\n"); - p2.close(); - p1.close(); -}); - -test(async function testPrettierReadInvalidCodeFromStdin(): Promise { - const inputCode = `InvalidTypescriptCode##@@!!`; - const p1 = Deno.run({ - args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], - stdout: "piped" - }); - - const p2 = Deno.run({ - args: [execPath, "run", "./prettier/main.ts", "--stdin"], - stdin: "piped", - stdout: "piped", - stderr: "piped" +function testReadFromStdin( + name: string, + stdin: string, + expectedStdout: string, + expectedStderr: string, + expectedCode: number, + expectedSuccess: boolean, + parser?: string +): void { + test({ + name: name, + fn: async function testPrettierReadFromStdinFormatTS(): Promise { + const inputCode = stdin; + const p1 = Deno.run({ + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], + stdout: "piped" + }); + + const p2 = Deno.run({ + args: [ + execPath, + "run", + "./prettier/main.ts", + "--stdin", + parser ? `--stdin-parser=${parser}` : "" + ], + stdin: "piped", + stdout: "piped", + stderr: "piped" + }); + + const n = await Deno.copy(p2.stdin!, p1.stdout!); + assertEquals(n, new TextEncoder().encode(stdin).length); + + const status1 = await p1.status(); + assertEquals(status1.code, 0); + assertEquals(status1.success, true); + p2.stdin!.close(); + const status2 = await p2.status(); + assertEquals(status2.code, expectedCode); + assertEquals(status2.success, expectedSuccess); + const stdout = await Deno.readAll(p2.stdout!); + const stderr = await Deno.readAll(p2.stderr!); + const decoder = new TextDecoder("utf-8"); + assertEquals(decoder.decode(stdout), expectedStdout); + assertEquals(decoder.decode(stderr).split(EOL)[0], expectedStderr); + p2.close(); + p1.close(); + } }); +} - const n = await Deno.copy(p2.stdin!, p1.stdout!); - console.log("bytes copied:", n); - - const status1 = await p1.status(); - assertEquals(status1.code, 0); - assertEquals(status1.success, true); - p2.stdin!.close(); - const status2 = await p2.status(); - assertEquals(status2.code, 1); - assertEquals(status2.success, false); - const stdoutOutput = new TextDecoder("utf-8").decode( - await Deno.readAll(p2.stdout!) - ); - const stderrOutput = new TextDecoder("utf-8").decode( - await Deno.readAll(p2.stderr!) - ); - assertEquals(stdoutOutput, ""); - assertEquals( - stderrOutput.split(EOL)[0], - "SyntaxError: Invalid character. (1:22)" - ); - p2.close(); - p1.close(); -}); +testReadFromStdin( + "read from stdin and format typescript code", + `console.log("abc" )`, + `console.log("abc");` + "\n", + "", + 0, + true +); + +testReadFromStdin( + "read from stdin and format javascript code", + `console.log("abc" )`, + `console.log("abc");` + "\n", + "", + 0, + true, + "babel" +); + +testReadFromStdin( + "read from stdin and format JSON code", + `{\"a\":\"b\"}`, + `{ "a": "b" }` + "\n", + "", + 0, + true, + "json" +); + +testReadFromStdin( + "read from stdin and format markdown code", + `## test`, + `## test` + "\n", + "", + 0, + true, + "markdown" +); + +testReadFromStdin( + "read from stdin and format invalid typescript code", + `invalid typescript code##!!@@`, + "", + "SyntaxError: ';' expected. (1:9)", + 1, + false +); + +testReadFromStdin( + "read from stdin and format typescript code with invalid parser", + `console.log("foo");`, + "", + 'Error: Couldn\'t resolve parser "invalid_parser". ' + + "Parsers must be explicitly added to the standalone bundle.", + 1, + false, + "invalid_parser" +); runIfMain(import.meta); From e8d3ae73db54964e2b5e2eb70b8f1d8fdf8de298 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 20:53:29 +0800 Subject: [PATCH 49/52] improve main test --- prettier/main_test.ts | 228 ++++++++++++++++++++++-------------------- 1 file changed, 120 insertions(+), 108 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index fb0e3d9e0ad0..59a3d8ac39ee 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -234,115 +234,127 @@ test(async function testPrettierPrintToStdout(): Promise { emptyDir(tempDir); }); -function testReadFromStdin( - name: string, - stdin: string, - expectedStdout: string, - expectedStderr: string, - expectedCode: number, - expectedSuccess: boolean, - parser?: string -): void { - test({ - name: name, - fn: async function testPrettierReadFromStdinFormatTS(): Promise { - const inputCode = stdin; - const p1 = Deno.run({ - args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], - stdout: "piped" - }); - - const p2 = Deno.run({ - args: [ - execPath, - "run", - "./prettier/main.ts", - "--stdin", - parser ? `--stdin-parser=${parser}` : "" - ], - stdin: "piped", - stdout: "piped", - stderr: "piped" - }); - - const n = await Deno.copy(p2.stdin!, p1.stdout!); - assertEquals(n, new TextEncoder().encode(stdin).length); - - const status1 = await p1.status(); - assertEquals(status1.code, 0); - assertEquals(status1.success, true); - p2.stdin!.close(); - const status2 = await p2.status(); - assertEquals(status2.code, expectedCode); - assertEquals(status2.success, expectedSuccess); - const stdout = await Deno.readAll(p2.stdout!); - const stderr = await Deno.readAll(p2.stderr!); - const decoder = new TextDecoder("utf-8"); - assertEquals(decoder.decode(stdout), expectedStdout); - assertEquals(decoder.decode(stderr).split(EOL)[0], expectedStderr); - p2.close(); - p1.close(); +test(async function testPrettierReadFromStdin(): Promise { + interface testcase { + stdin: string; + stdout: string; + stderr: string; + code: number; + success: boolean; + parser?: string; + } + + async function readFromStdinAssertion( + stdin: string, + expectedStdout: string, + expectedStderr: string, + expectedCode: number, + expectedSuccess: boolean, + parser?: string + ): Promise { + const inputCode = stdin; + const p1 = Deno.run({ + args: [execPath, "./prettier/testdata/echox.ts", `${inputCode}`], + stdout: "piped" + }); + + const p2 = Deno.run({ + args: [ + execPath, + "run", + "./prettier/main.ts", + "--stdin", + parser ? `--stdin-parser=${parser}` : "" + ], + stdin: "piped", + stdout: "piped", + stderr: "piped" + }); + + const n = await Deno.copy(p2.stdin!, p1.stdout!); + assertEquals(n, new TextEncoder().encode(stdin).length); + + const status1 = await p1.status(); + assertEquals(status1.code, 0); + assertEquals(status1.success, true); + p2.stdin!.close(); + const status2 = await p2.status(); + assertEquals(status2.code, expectedCode); + assertEquals(status2.success, expectedSuccess); + const decoder = new TextDecoder("utf-8"); + assertEquals( + decoder.decode(await Deno.readAll(p2.stdout!)), + expectedStdout + ); + assertEquals( + decoder.decode(await Deno.readAll(p2.stderr!)).split(EOL)[0], + expectedStderr + ); + p2.close(); + p1.close(); + } + + const testcases: testcase[] = [ + { + stdin: `console.log("abc" )`, + stdout: `console.log("abc");\n`, + stderr: ``, + code: 0, + success: true + }, + { + stdin: `console.log("abc" )`, + stdout: `console.log("abc");\n`, + stderr: ``, + code: 0, + success: true, + parser: "babel" + }, + { + stdin: `{\"a\":\"b\"}`, + stdout: `{ "a": "b" }\n`, + stderr: ``, + code: 0, + success: true, + parser: "json" + }, + { + stdin: `## test`, + stdout: `## test\n`, + stderr: ``, + code: 0, + success: true, + parser: "markdown" + }, + { + stdin: `invalid typescript code##!!@@`, + stdout: ``, + stderr: `SyntaxError: ';' expected. (1:9)`, + code: 1, + success: false + }, + { + stdin: `console.log("foo");`, + stdout: ``, + stderr: + 'Error: Couldn\'t resolve parser "invalid_parser". ' + + "Parsers must be explicitly added to the standalone bundle.", + code: 1, + success: false, + parser: "invalid_parser" } - }); -} + ]; -testReadFromStdin( - "read from stdin and format typescript code", - `console.log("abc" )`, - `console.log("abc");` + "\n", - "", - 0, - true -); - -testReadFromStdin( - "read from stdin and format javascript code", - `console.log("abc" )`, - `console.log("abc");` + "\n", - "", - 0, - true, - "babel" -); - -testReadFromStdin( - "read from stdin and format JSON code", - `{\"a\":\"b\"}`, - `{ "a": "b" }` + "\n", - "", - 0, - true, - "json" -); - -testReadFromStdin( - "read from stdin and format markdown code", - `## test`, - `## test` + "\n", - "", - 0, - true, - "markdown" -); - -testReadFromStdin( - "read from stdin and format invalid typescript code", - `invalid typescript code##!!@@`, - "", - "SyntaxError: ';' expected. (1:9)", - 1, - false -); - -testReadFromStdin( - "read from stdin and format typescript code with invalid parser", - `console.log("foo");`, - "", - 'Error: Couldn\'t resolve parser "invalid_parser". ' + - "Parsers must be explicitly added to the standalone bundle.", - 1, - false, - "invalid_parser" -); + for (const t of testcases) { + await readFromStdinAssertion( + t.stdin, + t.stdout, + t.stderr, + t.code, + t.success, + t.parser + ); + } +}); runIfMain(import.meta); From 24b415bf78ae04ae996078f037dc55477959422b Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 20:57:57 +0800 Subject: [PATCH 50/52] fix eslint --- prettier/main_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 59a3d8ac39ee..4e4904db6394 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -235,7 +235,7 @@ test(async function testPrettierPrintToStdout(): Promise { }); test(async function testPrettierReadFromStdin(): Promise { - interface testcase { + interface TestCase { stdin: string; stdout: string; stderr: string; @@ -294,7 +294,7 @@ test(async function testPrettierReadFromStdin(): Promise { p1.close(); } - const testcases: testcase[] = [ + const testCases: TestCase[] = [ { stdin: `console.log("abc" )`, stdout: `console.log("abc");\n`, @@ -345,7 +345,7 @@ test(async function testPrettierReadFromStdin(): Promise { } ]; - for (const t of testcases) { + for (const t of testCases) { await readFromStdinAssertion( t.stdin, t.stdout, From 51368cee0dd144fef584a2ad21c719c86cdcdf65 Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 21:08:00 +0800 Subject: [PATCH 51/52] improve help message --- prettier/main.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/prettier/main.ts b/prettier/main.ts index 5275b16f6791..03fe4605cd07 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -42,10 +42,11 @@ Options: false. --ignore Ignore the given path(s). --stdin Specifies to read the code from stdin. - If run the command in a pipe in the - terminal, you do not need to specify - this flag. - --stdin-parser If set --stdin flag, then need specify a + If run the command in a pipe, you do not + need to specify this flag. + Defaults to false. + --stdin-parser + If set --stdin flag, then need specify a parser for stdin. available parser: typescript/babel/markdown/json. Defaults to typescript. From fc60af1b2ec4c0e1f20fcddc5c8fc6ecf1dc63fe Mon Sep 17 00:00:00 2001 From: axetroy Date: Sun, 30 Jun 2019 21:15:30 +0800 Subject: [PATCH 52/52] improve main test --- prettier/main_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 4e4904db6394..cb23079d2ceb 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -264,7 +264,7 @@ test(async function testPrettierReadFromStdin(): Promise { "run", "./prettier/main.ts", "--stdin", - parser ? `--stdin-parser=${parser}` : "" + ...(parser ? ["--stdin-parser", parser] : []) ], stdin: "piped", stdout: "piped",