Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prettier: support read code from stdin and output to stdout #498

Merged
merged 54 commits into from
Jun 30, 2019
Merged
Changes from 2 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
54e7755
prettier: support read code from stdin and output to stdout
axetroy Jun 17, 2019
d829718
add example for usage
axetroy Jun 17, 2019
ffdd17f
merge master
axetroy Jun 20, 2019
e447763
remove from-stdin
axetroy Jun 20, 2019
9d905a0
add test case for reading code from stdin and format
axetroy Jun 20, 2019
787d3d5
update
axetroy Jun 20, 2019
f5484f4
update
axetroy Jun 21, 2019
5bb6001
update
axetroy Jun 21, 2019
6c787cf
update
axetroy Jun 21, 2019
ffcc04b
update
axetroy Jun 23, 2019
876de56
update
axetroy Jun 23, 2019
af51bd5
update
axetroy Jun 23, 2019
f1c63a9
update
axetroy Jun 23, 2019
7275713
update
axetroy Jun 23, 2019
db677fe
update
axetroy Jun 23, 2019
7a3b205
update
axetroy Jun 23, 2019
0c1f4d7
update
axetroy Jun 23, 2019
c2813a8
update
axetroy Jun 23, 2019
077ba77
update
axetroy Jun 24, 2019
2b254c2
update
axetroy Jun 24, 2019
6fc1c7f
Merge branch 'master' into prettier_stdin
axetroy Jun 24, 2019
c9302d9
update
axetroy Jun 24, 2019
2d17da7
update
axetroy Jun 24, 2019
34d9420
update
axetroy Jun 24, 2019
b6ab458
update
axetroy Jun 24, 2019
7a80229
update
axetroy Jun 24, 2019
7e5940d
update
axetroy Jun 24, 2019
426cc3e
update
axetroy Jun 24, 2019
f35958e
update
axetroy Jun 24, 2019
3905810
update
axetroy Jun 24, 2019
78351a9
update
axetroy Jun 24, 2019
e6bf9a8
update
axetroy Jun 24, 2019
2632e69
update useage
axetroy Jun 24, 2019
bcddbd1
refactor
axetroy Jun 24, 2019
a252c2f
fix eslint
axetroy Jun 24, 2019
b986390
add test case when stdin is invalid typescript code
axetroy Jun 24, 2019
972eeab
fix
axetroy Jun 24, 2019
a692dd1
support format markdown/json/js file from stdin
axetroy Jun 24, 2019
a6a6c5e
fix eol
axetroy Jun 24, 2019
1c07d75
try fix in windows
axetroy Jun 24, 2019
93e944a
try fix in windows
axetroy Jun 24, 2019
aeea834
update
axetroy Jun 24, 2019
151bb91
replace echo with echox
axetroy Jun 25, 2019
3398246
remove main function's error handler
axetroy Jun 30, 2019
0f74198
improve command help infomation
axetroy Jun 30, 2019
f5f2f8b
format code
axetroy Jun 30, 2019
3c0097f
remove unnecessary assertions
axetroy Jun 30, 2019
27121fe
improve test
axetroy Jun 30, 2019
e0255d3
improve test
axetroy Jun 30, 2019
3a304de
refactor main test. make it clear
axetroy Jun 30, 2019
e8d3ae7
improve main test
axetroy Jun 30, 2019
24b415b
fix eslint
axetroy Jun 30, 2019
51368ce
improve help message
axetroy Jun 30, 2019
fc60af1
improve main test
axetroy Jun 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
axetroy marked this conversation as resolved.
Show resolved Hide resolved
--ignore <path> Ignore the given path(s).

JS/TS Styling Options:
Expand Down Expand Up @@ -66,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
Expand Down Expand Up @@ -227,6 +231,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.
Expand Down Expand Up @@ -306,6 +323,15 @@ async function main(opts): Promise<void> {
write: opts["write"]
};

const isReadFromStdin: boolean = opts["from-stdin"];

if (isReadFromStdin) {
const byte = await readAll(stdin);
axetroy marked this conversation as resolved.
Show resolved Hide resolved
const formattedCode = format(new TextDecoder().decode(byte), prettierOpts);
stdout.write(new TextEncoder().encode(formattedCode));
return;
}

if (help) {
console.log(HELP_MESSAGE);
exit(0);
Expand Down Expand Up @@ -348,7 +374,8 @@ main(
"use-tabs",
"single-quote",
"bracket-spacing",
"write"
"write",
"from-stdin"
],
default: {
ignore: [],
Expand All @@ -362,7 +389,8 @@ main(
"arrow-parens": "avoid",
"prose-wrap": "preserve",
"end-of-line": "auto",
write: false
write: false,
"from-stdin": false
},
alias: {
H: "help"
Expand Down