Formats your code with whatever formatter your project is already using. ๐งผ
npm install formatly
Formats your code with whatever formatter your project is already using.
๐งผ
formatly can automatically detect and format with:
- Biome
- deno fmt
- dprint
- Prettier
See Formatter Detection for details on how they are detected.
``shell`
npx formatly
formatly takes in any number of glob patterns.
It will then:
1. Detect which supported formatter is configured in the repository
2. Pass those glob patterns directly to the formatter
For example, to match all directories and folders in the current directory:
`shell`
npx formatly *
To match only .ts files in src/:
`shell`
npx formatly "src/*/.ts"
`shell`
npm i formatly
The formatly package exports the functions used by the formatly CLI.
#### formatly
Runs formatting on any number of glob pattern strings.
`ts
import { formatly } from "formatly";
await formatly(["*"]);
`
Parameters:
1. patterns: string[] _(required)_: any number of glob patternsoptions: FormatlyOptions
2. _(optional)_:cwd: string
- _(optional)_: working directory, if not "."formatter: FormatterName
- _(optional)_: explicit formatter to use instead of detecting one, supports "biome", "deno", "dprint", and "prettier"
Resolves with a FormatlyReport, which is either:
- FormatlyReportError if a formatter could not be determined, which an object containing:ran: false
- FormatlyReportResult
- if a formatter could be determined, which is an object containing:formatter: Formatter
- : as resolved by resolveFormatterran: true
- result: FormatlyReportChildProcessResult
- :code: number | null
- : exit code of the child processsignal: NodeJS.Signal | null
- : signal that terminated the child process
For example, to run formatting on TypeScript source files in a child directory and check the result:
`ts
import { formatly } from "formatly";
const report = await formatly(["src/*/.ts"], { cwd: "path/to/project" });
if (!report.ran) {
console.error("Could not determine formatter.");
return;
}
const { formatter, result } = report;
if (result.code) {
console.error(Error running ${formatter.runner}:, result.stderr);Formatted with ${formatter.name}! ๐งผ
} else {
console.log();`
}
#### resolveFormatter
Detects which of the supported formatters to use for a directory.
`ts
import { resolveFormatter } from "formatly";
const formatter = await resolveFormatter();
// {
// name: "Prettier",
// runner: "npx prettier --write",
// testers: { ... }
// }
console.log(formatter);
`
Parameters:
1. cwd: string _(optional)_: working directory, if not "."
Resolves with either:
- undefined if a formatter could not be detectedFormatter
- if one can be found, which is an object containing:name: string
- : English name of the formatterrunner: string
- : the shell command used to run the formattertesters: object
- : strings and regular expressions used to test for the formatter
Formatters are detected based on the first match from, in order:
1. Existence of the formatter's default supported config file name
2. The formatter's name in a package.json fmt or format scriptpackage.json
3. Well-known root-level key
| Formatter | Config File | Package Key | Script |
| ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------ | ---------- |
| Biome | Configure Biome | | biome |deno
| deno fmt | Deno Configuration > Formatting | | |dprint
| dprint | dprint setup | | |"prettier"
| Prettier | Prettier Configuration File | | prettier |
> Want support for a formatter not mentioned here?
> Great!
> Please file a feature request GitHub issue.
> ๐
Formatly is a tool for any developer tool that creates files for users.
If your tool creates, say, a config file that users are meant to check into their repository, you probably want that file to be formatted per the user's preference.
But there are several popular formatters in use today: it's not enough to just call to prettier.format.
Formatly takes away the burden of
- Detecting which formatter -if any- a userland project is using
- Calling to that formatter's API(s) to format the file
No.
Formatly is a detection + wrapping layer around formatters such as Prettier.
Userland projects still need to configure a formatter themselves.
See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md.
Thanks! ๐งผ
Alec Larson ๐ค ๐ป | Bjorn Lu ๐ค ๐ป ๐ | Josh Goldberg โจ ๐ป ๐ ๐ค ๐ ๐ง ๐ ๐ง ๐ ๐ | Lars Kappert ๐ ๐ป | rubiesonthesky ๐ |
> ๐ This package was templated with create-typescript-app` using the Bingo framework.