npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pretty-maybe

v1.2.2

Published

Prepare a text file to be written to disc, applying prettier if it's configured

Downloads

1,777

Readme

pretty-maybe

NPM version Build Status Coverage Status Dependency Status

Serialize a JavaScript/CSS/markdown/... file, running it through prettier's programmatic interface if it is configured for the destination path. You have to tell it the path of the file that you're formatting. This is used to sniff the file type and to look up the prettier configuration and .prettierignore:

const prettyMaybe = require('pretty-maybe');

(async () => {
  const code = await prettyMaybe('/path/to/file.js', 'a=123');
  console.log(code); // a = 123;
})();

There is also a sync version:

const code = prettyMaybe.sync('/path/to/file.js', 'a=123');
console.log(code); // a = 123;

You can also tell it not to require a prettier configuration file by passing requireConfig: false in the 3rd parameter, which is an options object:

(async () => {
  const code = await prettyMaybe('/path/to/file.js', 'a=123', {
    requireConfig: false,
  });
})();

It will still require prettier to be require-able in your project, though.

Since you'll most often want to actually write the file to disc, there's also a writeFile function that does that:

(async () => {
  await prettyMaybe.writeFile('/path/to/file.js', 'a=123');
})();

As well as a sync version:

prettyMaybe.writeFileSync('/path/to/file.js', 'a=123');

Also available as:

prettyMaybe.writeFile.sync('/path/to/file.js', 'a=123');

Rationale

Why is it necessary to have a module like this when you can just use prettier's programmatic interface directly?

It's just for convenience. I found prettier's programmatic interface to be quite low level for this basic use case. You have to do several calls and find the .prettierignore file yourself.

License

3-clause BSD license -- see the LICENSE file for details.