Allow --debug-check to work with stdin (#2013)

* Allow --debug-check to work with stdin

This makes it easier to quickly test formatting code snippets with e.g.

    pbpaste | prettier --debug-check

Before, the following would happen:

    $ pbpaste | prettier --debug-check
    stdin: TypeError: Invalid data, chunk must be a string or buffer, not undefined
        at WriteStream.Socket.write (net.js:693:11)
        at writeOutput (/Users/josephfrazier/workspace/prettier/bin/prettier.js:375:18)
        at getStdin.then.input (/Users/josephfrazier/workspace/prettier/bin/prettier.js:275:7)
        at <anonymous>
        at process._tickCallback (internal/process/next_tick.js:169:7)
    $

Now, it looks like this:

    $ pbpaste | prettier --debug-check
    (stdin)
    $

* Test that --debug-check still prints filenames

See https://github.com/prettier/prettier/pull/2013#discussion_r120499306
master
Joseph Frazier 2017-06-06 19:10:24 -04:00 committed by Christopher Chedeau
parent b20437ad13
commit 552ceb30e8
3 changed files with 18 additions and 5 deletions

View File

@ -202,7 +202,7 @@ function format(input, opt) {
diff(input, pp);
}
}
return {};
return { formatted: opt.filepath || "(stdin)\n" };
}
return prettier.formatWithCursor(input, opt);
@ -280,7 +280,7 @@ if (stdin) {
});
} else {
eachFilename(filepatterns, filename => {
if (write || argv["debug-check"]) {
if (write) {
// Don't use `console.log` here since we need to replace this line.
process.stdout.write(filename);
}
@ -358,7 +358,6 @@ if (stdin) {
}
}
} else if (argv["debug-check"]) {
process.stdout.write("\n");
if (output) {
console.log(output);
} else {

View File

@ -8,5 +8,15 @@ test("doesn't crash when --debug-check is passed", () => {
"--debug-check"
]);
expect(result.stdout).toEqual("issue1890.js\n");
expect(result.stderr).toEqual("");
});
test("checks stdin with --debug-check", () => {
const result = runPrettier("cli/with-shebang", ["--debug-check"], {
input: "0"
});
expect(result.stdout).toEqual("(stdin)\n");
expect(result.stderr).toEqual("");
});

View File

@ -12,14 +12,18 @@ const PRETTIER_PATH = path.resolve(__dirname, "../bin/prettier.js");
// return the result of the spawned process:
// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
// 'envPairs', 'options', 'args', 'file' ]
function runPrettier(dir, args) {
function runPrettier(dir, args, options) {
const isRelative = dir[0] !== "/";
if (isRelative) {
dir = path.resolve(__dirname, dir);
}
const result = spawnSync(PRETTIER_PATH, args || [], { cwd: dir });
const result = spawnSync(
PRETTIER_PATH,
args || [],
Object.assign({}, options, { cwd: dir })
);
result.stdout = result.stdout && result.stdout.toString();
result.stderr = result.stderr && result.stderr.toString();