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

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

Merged
merged 14 commits into from
May 21, 2019
3 changes: 2 additions & 1 deletion format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async function main(opts): Promise<void> {
"--ignore",
"testdata",
"--ignore",
"vendor"
"vendor",
"--write"
];

if (opts.check) {
Expand Down
35 changes: 24 additions & 11 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 } = Deno;
const { args, exit, readFile, writeFile, stdout } = Deno;
import { glob } from "../fs/glob.ts";
import { walk, WalkInfo } from "../fs/walk.ts";
import { parse } from "../flags/mod.ts";
Expand All @@ -25,6 +25,7 @@ Usage: deno prettier/main.ts [options] [files...]
Options:
-H, --help Show this help message and exit.
--check Check if the source files are formatted.
--write Whether to write to the file, otherwise it will output to stdout, Defaults to false.
--ignore <path> Ignore the given path(s).

JS/TS Styling Options:
Expand Down Expand Up @@ -54,14 +55,17 @@ Markdown Styling Options:
Defaults to preserve.

Example:
deno prettier/main.ts script1.ts script2.js
deno run prettier/main.ts --write script1.ts script2.js
Formats the files

deno prettier/main.ts --check script1.ts script2.js
deno run prettier/main.ts --check script1.ts script2.js
Checks if the files are formatted

deno prettier/main.ts
deno run prettier/main.ts --write
Formats the all files in the repository

deno run prettier/main.ts script1.ts
Print the formatted code to stdout
`;

// Available parsers
Expand All @@ -78,6 +82,7 @@ interface PrettierOptions {
arrowParens: string;
proseWrap: string;
endOfLine: string;
write: boolean;
}

const encoder = new TextEncoder();
Expand Down Expand Up @@ -139,15 +144,20 @@ async function formatFile(
return;
}

const formatted = prettier.format(text, {
const formatted: string = prettier.format(text, {
...prettierOpts,
parser,
plugins: prettierPlugins
});

if (text !== formatted) {
console.log(`Formatting ${filename}`);
await writeFile(filename, encoder.encode(formatted));
const fileUnit8 = encoder.encode(formatted);
if (prettierOpts.write) {
if (text !== formatted) {
console.log(`Formatting ${filename}`);
await writeFile(filename, fileUnit8);
}
} else {
await stdout.write(fileUnit8);
}
}

Expand Down Expand Up @@ -230,7 +240,8 @@ async function main(opts): Promise<void> {
bracketSpacing: Boolean(opts["bracket-spacing"]),
arrowParens: opts["arrow-parens"],
proseWrap: opts["prose-wrap"],
endOfLine: opts["end-of-line"]
endOfLine: opts["end-of-line"],
write: opts["write"]
};

if (help) {
Expand Down Expand Up @@ -275,7 +286,8 @@ main(
"semi",
"use-tabs",
"single-quote",
"bracket-spacing"
"bracket-spacing",
"write"
],
default: {
ignore: [],
Expand All @@ -288,7 +300,8 @@ main(
"bracket-spacing": true,
"arrow-parens": "avoid",
"prose-wrap": "preserve",
"end-of-line": "auto"
"end-of-line": "auto",
write: false
},
alias: {
H: "help"
Expand Down
63 changes: 52 additions & 11 deletions prettier/main_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { join } from "../fs/path.ts";
import { EOL } from "../fs/path/constants.ts";
import { assertEquals } from "../testing/asserts.ts";
import { test } from "../testing/mod.ts";
import { xrun } from "./util.ts";
Expand Down Expand Up @@ -59,7 +60,7 @@ test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
assertEquals(code, 1);
assertEquals(normalizeOutput(stdout), "Some files are not formatted");

var { code, stdout } = await run([...cmd, ...files]);
var { code, stdout } = await run([...cmd, "--write", ...files]);
assertEquals(code, 0);
assertEquals(
normalizeOutput(stdout),
Expand All @@ -83,7 +84,7 @@ test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
assertEquals(code, 1);
assertEquals(normalizeOutput(stdout), "Some files are not formatted");

var { code, stdout } = await run([...cmd, ...dirs]);
var { code, stdout } = await run([...cmd, "--write", ...dirs]);
assertEquals(code, 0);
assertEquals(
normalizeOutput(stdout),
Expand Down Expand Up @@ -111,15 +112,23 @@ test(async function testPrettierOptions(): Promise<void> {
const getSourceCode = async (f: string): Promise<string> =>
decoder.decode(await Deno.readFile(f));

await run([...cmd, "--no-semi", file0]);
await run([...cmd, "--no-semi", "--write", file0]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0)
console.log([function foo() {}, function baz() {}, a => {}])
`
);

await run([...cmd, "--print-width", "30", "--tab-width", "4", file0]);
await run([
...cmd,
"--print-width",
"30",
"--tab-width",
"4",
"--write",
file0
]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
Expand All @@ -131,7 +140,7 @@ console.log([
`
);

await run([...cmd, "--print-width", "30", "--use-tabs", file0]);
await run([...cmd, "--print-width", "30", "--use-tabs", "--write", file0]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
Expand All @@ -143,14 +152,22 @@ console.log([
`
);

await run([...cmd, "--single-quote", file1]);
await run([...cmd, "--single-quote", "--write", file1]);
assertEquals(
normalizeSourceCode(await getSourceCode(file1)),
`console.log('1');
`
);

await run([...cmd, "--print-width", "30", "--trailing-comma", "all", file0]);
await run([
...cmd,
"--print-width",
"30",
"--trailing-comma",
"all",
"--write",
file0
]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
Expand All @@ -162,31 +179,55 @@ console.log([
`
);

await run([...cmd, "--no-bracket-spacing", file2]);
await run([...cmd, "--no-bracket-spacing", "--write", file2]);
assertEquals(
normalizeSourceCode(await getSourceCode(file2)),
`console.log({a: 1});
`
);

await run([...cmd, "--arrow-parens", "always", file0]);
await run([...cmd, "--arrow-parens", "always", "--write", file0]);
assertEquals(
normalizeSourceCode(await getSourceCode(file0)),
`console.log(0);
console.log([function foo() {}, function baz() {}, (a) => {}]);
`
);

await run([...cmd, "--prose-wrap", "always", file3]);
await run([...cmd, "--prose-wrap", "always", "--write", file3]);
assertEquals(
normalizeSourceCode(await getSourceCode(file3)),
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
`
);

await run([...cmd, "--end-of-line", "crlf", file2]);
await run([...cmd, "--end-of-line", "crlf", "--write", file2]);
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");

await clearTestdataChanges();
});

test(async function testPrettierPrintToStdout(): Promise<void> {
await clearTestdataChanges();

const file0 = join(testdata, "0.ts");
const file1 = join(testdata, "formatted.ts");

const getSourceCode = async (f: string): Promise<string> =>
decoder.decode(await Deno.readFile(f));

const { stdout } = await run([...cmd, file0]);
// The source file will not change without `--write` flags.
assertEquals(await getSourceCode(file0), "console.log (0)" + EOL);
// The output should be formatted code.
assertEquals(stdout, "console.log(0);" + EOL);

const { stdout: formattedCode } = await run([...cmd, file1]);
// The source file will not change without `--write` flags.
assertEquals(await getSourceCode(file1), "console.log(0);" + EOL);
// The output will be formatted code even it is the same as the source file's content.
assertEquals(formattedCode, "console.log(0);" + EOL);

await clearTestdataChanges();
});
1 change: 1 addition & 0 deletions prettier/testdata/formatted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(0);