mirror of
https://github.com/denoland/deno.git
synced 2024-12-27 01:29:14 -05:00
feat(prettier): output to stdout instead of write file by default unless specified --write flag (#332)
This commit is contained in:
parent
47134db9f2
commit
434007b8ab
4 changed files with 79 additions and 23 deletions
|
@ -16,7 +16,8 @@ async function main(opts): Promise<void> {
|
||||||
"--ignore",
|
"--ignore",
|
||||||
"testdata",
|
"testdata",
|
||||||
"--ignore",
|
"--ignore",
|
||||||
"vendor"
|
"vendor",
|
||||||
|
"--write"
|
||||||
];
|
];
|
||||||
|
|
||||||
if (opts.check) {
|
if (opts.check) {
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
// This script formats the given source files. If the files are omitted, it
|
// This script formats the given source files. If the files are omitted, it
|
||||||
// formats the all files in the repository.
|
// 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 { glob } from "../fs/glob.ts";
|
||||||
import { walk, WalkInfo } from "../fs/walk.ts";
|
import { walk, WalkInfo } from "../fs/walk.ts";
|
||||||
import { parse } from "../flags/mod.ts";
|
import { parse } from "../flags/mod.ts";
|
||||||
|
@ -25,6 +25,7 @@ Usage: deno prettier/main.ts [options] [files...]
|
||||||
Options:
|
Options:
|
||||||
-H, --help Show this help message and exit.
|
-H, --help Show this help message and exit.
|
||||||
--check Check if the source files are formatted.
|
--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).
|
--ignore <path> Ignore the given path(s).
|
||||||
|
|
||||||
JS/TS Styling Options:
|
JS/TS Styling Options:
|
||||||
|
@ -54,14 +55,17 @@ Markdown Styling Options:
|
||||||
Defaults to preserve.
|
Defaults to preserve.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
deno prettier/main.ts script1.ts script2.js
|
deno run prettier/main.ts --write script1.ts script2.js
|
||||||
Formats the files
|
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
|
Checks if the files are formatted
|
||||||
|
|
||||||
deno prettier/main.ts
|
deno run prettier/main.ts --write
|
||||||
Formats the all files in the repository
|
Formats the all files in the repository
|
||||||
|
|
||||||
|
deno run prettier/main.ts script1.ts
|
||||||
|
Print the formatted code to stdout
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Available parsers
|
// Available parsers
|
||||||
|
@ -78,6 +82,7 @@ interface PrettierOptions {
|
||||||
arrowParens: string;
|
arrowParens: string;
|
||||||
proseWrap: string;
|
proseWrap: string;
|
||||||
endOfLine: string;
|
endOfLine: string;
|
||||||
|
write: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
@ -139,15 +144,20 @@ async function formatFile(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatted = prettier.format(text, {
|
const formatted: string = prettier.format(text, {
|
||||||
...prettierOpts,
|
...prettierOpts,
|
||||||
parser,
|
parser,
|
||||||
plugins: prettierPlugins
|
plugins: prettierPlugins
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const fileUnit8 = encoder.encode(formatted);
|
||||||
|
if (prettierOpts.write) {
|
||||||
if (text !== formatted) {
|
if (text !== formatted) {
|
||||||
console.log(`Formatting ${filename}`);
|
console.log(`Formatting ${filename}`);
|
||||||
await writeFile(filename, encoder.encode(formatted));
|
await writeFile(filename, fileUnit8);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await stdout.write(fileUnit8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,7 +240,8 @@ async function main(opts): Promise<void> {
|
||||||
bracketSpacing: Boolean(opts["bracket-spacing"]),
|
bracketSpacing: Boolean(opts["bracket-spacing"]),
|
||||||
arrowParens: opts["arrow-parens"],
|
arrowParens: opts["arrow-parens"],
|
||||||
proseWrap: opts["prose-wrap"],
|
proseWrap: opts["prose-wrap"],
|
||||||
endOfLine: opts["end-of-line"]
|
endOfLine: opts["end-of-line"],
|
||||||
|
write: opts["write"]
|
||||||
};
|
};
|
||||||
|
|
||||||
if (help) {
|
if (help) {
|
||||||
|
@ -275,7 +286,8 @@ main(
|
||||||
"semi",
|
"semi",
|
||||||
"use-tabs",
|
"use-tabs",
|
||||||
"single-quote",
|
"single-quote",
|
||||||
"bracket-spacing"
|
"bracket-spacing",
|
||||||
|
"write"
|
||||||
],
|
],
|
||||||
default: {
|
default: {
|
||||||
ignore: [],
|
ignore: [],
|
||||||
|
@ -288,7 +300,8 @@ main(
|
||||||
"bracket-spacing": true,
|
"bracket-spacing": true,
|
||||||
"arrow-parens": "avoid",
|
"arrow-parens": "avoid",
|
||||||
"prose-wrap": "preserve",
|
"prose-wrap": "preserve",
|
||||||
"end-of-line": "auto"
|
"end-of-line": "auto",
|
||||||
|
write: false
|
||||||
},
|
},
|
||||||
alias: {
|
alias: {
|
||||||
H: "help"
|
H: "help"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
import { join } from "../fs/path.ts";
|
import { join } from "../fs/path.ts";
|
||||||
|
import { EOL } from "../fs/path/constants.ts";
|
||||||
import { assertEquals } from "../testing/asserts.ts";
|
import { assertEquals } from "../testing/asserts.ts";
|
||||||
import { test } from "../testing/mod.ts";
|
import { test } from "../testing/mod.ts";
|
||||||
import { xrun } from "./util.ts";
|
import { xrun } from "./util.ts";
|
||||||
|
@ -59,7 +60,7 @@ test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
|
||||||
assertEquals(code, 1);
|
assertEquals(code, 1);
|
||||||
assertEquals(normalizeOutput(stdout), "Some files are not formatted");
|
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(code, 0);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeOutput(stdout),
|
normalizeOutput(stdout),
|
||||||
|
@ -83,7 +84,7 @@ test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
|
||||||
assertEquals(code, 1);
|
assertEquals(code, 1);
|
||||||
assertEquals(normalizeOutput(stdout), "Some files are not formatted");
|
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(code, 0);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeOutput(stdout),
|
normalizeOutput(stdout),
|
||||||
|
@ -111,7 +112,7 @@ test(async function testPrettierOptions(): Promise<void> {
|
||||||
const getSourceCode = async (f: string): Promise<string> =>
|
const getSourceCode = async (f: string): Promise<string> =>
|
||||||
decoder.decode(await Deno.readFile(f));
|
decoder.decode(await Deno.readFile(f));
|
||||||
|
|
||||||
await run([...cmd, "--no-semi", file0]);
|
await run([...cmd, "--no-semi", "--write", file0]);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file0)),
|
normalizeSourceCode(await getSourceCode(file0)),
|
||||||
`console.log(0)
|
`console.log(0)
|
||||||
|
@ -119,7 +120,15 @@ 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(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file0)),
|
normalizeSourceCode(await getSourceCode(file0)),
|
||||||
`console.log(0);
|
`console.log(0);
|
||||||
|
@ -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(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file0)),
|
normalizeSourceCode(await getSourceCode(file0)),
|
||||||
`console.log(0);
|
`console.log(0);
|
||||||
|
@ -143,14 +152,22 @@ console.log([
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
||||||
await run([...cmd, "--single-quote", file1]);
|
await run([...cmd, "--single-quote", "--write", file1]);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file1)),
|
normalizeSourceCode(await getSourceCode(file1)),
|
||||||
`console.log('1');
|
`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(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file0)),
|
normalizeSourceCode(await getSourceCode(file0)),
|
||||||
`console.log(0);
|
`console.log(0);
|
||||||
|
@ -162,14 +179,14 @@ console.log([
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
||||||
await run([...cmd, "--no-bracket-spacing", file2]);
|
await run([...cmd, "--no-bracket-spacing", "--write", file2]);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file2)),
|
normalizeSourceCode(await getSourceCode(file2)),
|
||||||
`console.log({a: 1});
|
`console.log({a: 1});
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
||||||
await run([...cmd, "--arrow-parens", "always", file0]);
|
await run([...cmd, "--arrow-parens", "always", "--write", file0]);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file0)),
|
normalizeSourceCode(await getSourceCode(file0)),
|
||||||
`console.log(0);
|
`console.log(0);
|
||||||
|
@ -177,7 +194,7 @@ console.log([function foo() {}, function baz() {}, (a) => {}]);
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
||||||
await run([...cmd, "--prose-wrap", "always", file3]);
|
await run([...cmd, "--prose-wrap", "always", "--write", file3]);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeSourceCode(await getSourceCode(file3)),
|
normalizeSourceCode(await getSourceCode(file3)),
|
||||||
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
|
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
|
||||||
|
@ -185,8 +202,32 @@ 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");
|
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");
|
||||||
|
|
||||||
await clearTestdataChanges();
|
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
prettier/testdata/formatted.ts
vendored
Normal file
1
prettier/testdata/formatted.ts
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
console.log(0);
|
Loading…
Reference in a new issue