Skip to content

[RFC] Expose CLI programmatically - #2168

Closed
josephfrazier wants to merge 21 commits into
prettier:masterfrom
josephfrazier:export-cli
Closed

josephfrazier wants to merge 21 commits into
prettier:masterfrom
josephfrazier:export-cli

Conversation

@josephfrazier

@josephfrazier josephfrazier commented Jun 16, 2017 •

Copy link
Copy Markdown
Collaborator

(This diff is best viewed ignoring whitespace changes: https://github.com/prettier/prettier/pull/2168/files?w=true)

These changes make ./bin/prettier.js export an object with a cli
method that takes the following parameters:

  • an array of CLI arguments
  • stdin stream
  • stdout stream
  • stderr stream

and returns a Promise resolving to an object with an exitCode
property. The object may be an Error.

This allows prettier wrappers like prettier_d
to simply pass the arguments/streams to a preloaded prettier instance,
which handles all the CLI parsing and subsequent processing.

There is an example of using the new API in
tests/cli/jsfmt.spec.js.

@azz

azz commented Jun 17, 2017 •

Copy link
Copy Markdown
Member

I'm a bit confused, how does exposing the CLI as a node module offer performance gains versus just keeping a node.js process open and calling require("prettier").format("text", opts)?

The actual formatting doesn't support streaming output. I might be missing something.

@josephfrazier

Copy link
Copy Markdown
Collaborator Author

The original motivation for this was to help prettier_d support multiple files (see prettier_d's Limitations), without having to duplicate prettier's CLI processing. However, now that I've done this work and gotten a better idea of how things would fit together, I think it makes more sense for me to maintain a prettier fork with the relevant changes, rather than pushing them onto prettier, so I'll go ahead and close this.

josephfrazier added a commit to josephfrazier/prettier_d that referenced this pull request Jul 9, 2017
josephfrazier added a commit to josephfrazier/prettier that referenced this pull request Jul 9, 2017
@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 20, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants