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
Show file tree
Hide file tree
Changes from 46 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
15 changes: 12 additions & 3 deletions prettier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ 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. 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

You can use APIs of prettier as the following:
Expand Down
72 changes: 66 additions & 6 deletions prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = 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 @@ -41,6 +41,14 @@ Options:
it will output to stdout, Defaults to
false.
--ignore <path> 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 <int> The line length where Prettier will try
Expand Down Expand Up @@ -76,6 +84,14 @@ 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.

cat config.json | deno run prettier/main.ts --stdin-parser=json
Read the JSON string from stdin and
output formatted code to stdout.
`;

// Available parsers
Expand Down Expand Up @@ -237,6 +253,39 @@ async function formatSourceFiles(
exit(0);
}

/**
* Format source code
*/
function format(
text: string,
parser: ParserLabel,
prettierOpts: PrettierOptions
): string {
const formatted: string = prettier.format(text, {
...prettierOpts,
parser: parser,
plugins: prettierPlugins
});

return formatted;
}

/**
* Format code from stdin and output to stdout
*/
async function formatFromStdin(
parser: ParserLabel,
prettierOpts: PrettierOptions
): Promise<void> {
const byte = await readAll(stdin);
const formattedCode = format(
new TextDecoder().decode(byte),
parser,
prettierOpts
);
await stdout.write(new TextEncoder().encode(formattedCode));
}

/**
* Get the files to format.
* @param selectors The glob patterns to select the files.
Expand Down Expand Up @@ -329,14 +378,21 @@ async function main(opts): Promise<void> {
options
);

const tty = Deno.isTTY();

const shouldReadFromStdin =
(!tty.stdin && (tty.stdout || tty.stderr)) || !!opts["stdin"];

try {
if (check) {
if (shouldReadFromStdin) {
await formatFromStdin(opts["stdin-parser"], prettierOpts);
} else if (check) {
await checkSourceFiles(files, prettierOpts);
} else {
await formatSourceFiles(files, prettierOpts);
}
} catch (e) {
console.log(e);
console.error(e);
exit(1);
}
}
Expand All @@ -350,7 +406,8 @@ main(
"trailing-comma",
"arrow-parens",
"prose-wrap",
"end-of-line"
"end-of-line",
"stdin-parser"
],
boolean: [
"check",
Expand All @@ -359,7 +416,8 @@ main(
"use-tabs",
"single-quote",
"bracket-spacing",
"write"
"write",
"stdin"
],
default: {
ignore: [],
Expand All @@ -373,7 +431,9 @@ main(
"arrow-parens": "avoid",
"prose-wrap": "preserve",
"end-of-line": "auto",
write: false
write: false,
stdin: false,
"stdin-parser": "typescript"
},
alias: {
H: "help"
Expand Down
Loading