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

format-file

v0.1.2

Published

Utility API to format files via biome

Downloads

519

Readme

format-file

Format files based on existing formatter configs like Biome.

npm package License

Table of Contents

Introduction

Code and config generation is hard enough already. Some of this code should be checked into version control, and should therefore abide by the repositories code formatting rules.

format-file is a package which will return a formatted version of generated JSON, typescript, and javascript files by inferring the formatting rules at runtime.

Under the hood, it uses either the user's configured biome or prettier setup to format files on their behalf.

Installation

npm i format-file

Format-file is an ESM package. It must be imported.

Due to peer dependencies on biome + prettier, it is recommended to install at least one of them as well (or set up a similar peer dependency).

Example

// generated.ts
export    const  foo={
abc   : 123,
      efg:456  ,
} ;

// index.ts
import { formatFile, formatText } from 'format-file';

await formatFile('./generated.ts');
readFileSync('/generated.ts', 'utf8');
/**
 * export const foo = {
 *   abc: 123,
 *   efg: 456
 * };
 */

await formatText(`
{     "abc":123   ,
     "efg":
              456}
`, { ext: '.json' });
/**
 * {
 *   "abc": 123,
 *   "efg": 456
 * }
 */

Usage

File formatting is best effort and is not necessarily guaranteed to succeed. Primarily if the repo does not have biome or prettier installed.

It is recommended to create reasonable files before formatting, such as generating JSON files using JSON.stringify(content, null, 2) as a reasonable default.

Both biome and prettier have been declared as optional peerDependencies in this package. If using this library as an internal functionality, it is recommended to keep biome and prettier as optional peer dependencies to maintain your package's dependency.

API

formatFile(filePath, options?)

Formats a file in-place.

The first parameter is the file path. If the path is relative, will be resolved from working directory just like regular fs methods.

The second parameter is an optional object with a formatter property. While the default behavior is 'inherit' which will iterate over viable formatters based on support. It may be overridden with 'biome' or 'parser'.

Returns a promise which when resolved, means the file is formatted according user settings.

formatFiles(filePath[], options?)

Formats a set of files in-place.

First parameter is a list of file paths. Any paths that are relative will be resolved from working directory just like regular fs methods.

Second parameter is an optional object the same as formatFile

Returns a promise which when resolved, means the files are formatted according user settings.

formatText(content, options?)

Formats an inline text body as if it were a file.

Parameter is the raw text body.

Second parameter is options which include a ext, which defaults to .js. It also support sthe formatter option that formatFile has.

Returns a promise which resolves to a formatted version of the text.