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
Prev Previous commit
Next Next commit
always print formatted code to stdout if no --write flag
  • Loading branch information
axetroy committed May 21, 2019
commit 964a787d3d4e4dc493f7e1d27f5e5d0221e750c3
10 changes: 5 additions & 5 deletions prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ async function formatFile(
plugins: prettierPlugins
});

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

Expand Down
10 changes: 5 additions & 5 deletions prettier/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ test(async function testPrettierPrintToStdout(): Promise<void> {
decoder.decode(await Deno.readFile(f));

const { stdout } = await run([...cmd, file0]);
// the source file will not change without `--write` flags
// The source file will not change without `--write` flags.
assertEquals(await getSourceCode(file0), "console.log (0)" + EOL);
// the output will be formatted code
// 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
// The source file will not change without `--write` flags.
assertEquals(await getSourceCode(file1), "console.log(0);" + EOL);
// the output will be empty string. because the formatted content is same with before
assertEquals(formattedCode, "");
// 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();
});