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

Library code for "easy" external formatters #55

Open
lassik opened this issue Oct 11, 2018 · 4 comments
Open

Library code for "easy" external formatters #55

lassik opened this issue Oct 11, 2018 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@lassik
Copy link
Contributor

lassik commented Oct 11, 2018

Having added support for a few beautifiers now, it would be useful to have some library code to automate adding "easy" external formatters. They have the following properties:

  • Called on the command line as some-beautifier [arg ...].
  • The args (if any) in that command line are constant.
  • The beautifier writes all errors and warnings to stderr.
  • The beautifier exits zero on success, nonzero on error.
  • Code is read from stdin.
  • On success, the beautified code is written to stdout. Else nothing is written to stdout.

I copy-pasted this snippet to index.ts in all beautifier plugins I made:

    const executable = dependencies.get<ExecutableDependency>("foo");
    return executable
      .run({ args: [], stdin: text, options: {} })
      .then(({ exitCode, stderr, stdout }) => {
        if (exitCode) {
          return Promise.reject(stderr);
        }
        return Promise.resolve(stdout);
      });

In the format-all Emacs package I eventually made it have two APIs to call external formatters: an easy one and a hard one. The hard one is the same as the easy one but has some extra kludges for formatters that don't make proper use of their exit code.

@lassik
Copy link
Contributor Author

lassik commented Oct 11, 2018

It's worth noting that the easy beautifiers will eventually have to take some beautifier options from .unibeautifyrc so that has to be taken into account.

@stevenzeck
Copy link
Contributor

I think this would have to go with https://github.com/Unibeautify/create-unibeautify-beautifier. Right now, all the create-unibeautify-beautifier does is copies from the template and replaces everything in handlebars via egad. For this case, I'd want the create-unibeautify-beautifier to conditionally add/remove contents based on the options users select in the cli when scaffolding.

@lassik
Copy link
Contributor Author

lassik commented Oct 11, 2018

It might be a good idea to subclass Beautifier into EasyExecutableBeautifier. This way boilerplate code is reduced, and all beautifiers can benefit from fixes to the base class. (Is "subclass" and "base class" the correct terminology in TypeScript?)

@lassik
Copy link
Contributor Author

lassik commented Oct 11, 2018

For reference, format-all has 18 easy formatters and only 2 hard ones, so the statistics are very promising 😄

@Glavin001 Glavin001 added the enhancement New feature or request label Oct 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants