Formats code with whatever formatter a project is already using. 🧼
TypeScript
47
656 commits
updated Oct 12, 2026
Formats your code with whatever formatter your project is already using. 🧼
formatly can automatically detect and format with:
See Formatter Detection for details on how they are detected.
npx formatly <files>
formatly takes in any number of glob patterns.
It will then:
For example, to match all directories and folders in the current directory:
npx formatly *
To match only .ts files in src/:
npx formatly "src/**/*.ts"
--dry-runPass --dry-run to report which formatter was detected and the command that would run, without formatting anything:
npx formatly --dry-run "src/**/*.ts"
Detected prettier. 🔍
Would run: prettier --write --ignore-unknown src/**/*.ts
--help and --versionPass --help (-h) to print usage information, or --version (-v) to print formatly's version.
npm i formatly
The formatly package exports the functions used by the formatly CLI, as well as a format function for formatting text in memory and a formatFiles function for formatting or checking files on disk.
formatFormats a string of text as if it were a file at a given path, without writing to disk.
import { format } from "formatly";
import fs from "node:fs/promises";
const report = await format("console.log(123)", { filePath: "src/index.ts" });
if (report.ran && !report.error) {
await fs.writeFile("src/index.ts", report.formatted);
}
Parameters:
text: string (required): the text to formatoptions: FormatOptions (required):
filePath: string (required): path the text should be treated as being at, relative to cwd; formatters use it to infer the parser and apply any per-path config overridescwd: string (optional): working directory, if not "."formatter: FormatterName (optional): explicit formatter to use instead of detecting one, as in formatlyorder: FormatterName[] (optional): formatters to detect first, in order, as used by resolveFormatterstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, as used by resolveFormatterResolves with a FormatReport, which is one of:
FormatlyReportError if a formatter could not be determined, which is an object containing:
message: stringran: falseFormatReportFailure if the formatter ran but failed, such as on a syntax error, which is an object containing:
error: Error: the failure, including any output the formatter printed to stderrformatter: Formatter: as resolved by resolveFormatterran: trueFormatReportResult if the formatter succeeded, which is an object containing:
formatted: string: the formatted textformatter: Formatter: as resolved by resolveFormatterran: trueText is formatted by piping it through the formatter's CLI, so the formatter must be installed in the project.
Prettier is instead run in memory using the project's own installed prettier package when it can be resolved from cwd.
Biome is instead run in memory when the project installs @biomejs/js-api along with a @biomejs/wasm-nodejs matching its @biomejs/biome version, unless its config uses extends, is a nested ("root": false) config, or enables vcs.useIgnoreFile.
dprint is instead run through one long-lived dprint editor-service process per cwd, which doesn't keep the Node.js process alive while idle.
Note:
deno fmtcan only be told the file's extension, not its path, so per-path config overrides indeno.jsondon't apply toformat.fmt.excludeis honored by asking Deno about the file on disk, so it only applies to files that already exist.
formatFilesFormats, or checks the formatting of, any number of files on disk in a single formatter invocation.
import { formatFiles } from "formatly";
const report = await formatFiles(["src/a.ts", "src/b.ts"], { check: true });
if (report.ran && !report.error) {
console.log("Unformatted files:", report.changed);
}
Parameters:
filePaths: string[] (required): paths of the files to format, relative to cwdoptions: FormatFilesOptions (optional):
check: boolean (optional): whether to report which files aren't formatted instead of formatting themcwd: string (optional): working directory, if not "."formatter: FormatterName (optional): explicit formatter to use instead of detecting one, as in formatlyorder: FormatterName[] (optional): formatters to detect first, in order, as used by resolveFormatterstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, as used by resolveFormatterResolves with a FormatFilesReport, which is one of:
FormatlyReportError if a formatter could not be determined, which is an object containing:
message: stringran: falseFormatFilesReportFailure if the formatter ran but failed, which is an object containing:
error: Error: the failure, including any output the formatter printed to stderr when checkingformatter: Formatter: as resolved by resolveFormatterran: trueFormatFilesReportResult if the formatter succeeded, which is an object containing:
changed: string[]: absolute paths of the files that were changed, or with check, that would be changedformatter: Formatter: as resolved by resolveFormatterran: trueNote: Deno fails when none of the given files can be formatted without
check, such as when they're all excluded.
formatlyRuns formatting on any number of glob pattern strings.
import { formatly } from "formatly";
await formatly(["*"]);
Parameters:
patterns: string[] (required): any number of glob patternsoptions: FormatlyOptions (optional):
cwd: string (optional): working directory, if not "."dryRun: boolean (optional): whether to resolve the command that would run without running itformatter: FormatterName (optional): explicit formatter to use instead of detecting one, supports "biome", "deno", "dprint", "oxfmt", and "prettier"order: FormatterName[] (optional): formatters to detect first, in order, as used by resolveFormatterstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, as used by resolveFormatterResolves with a FormatlyReport, which is either:
FormatlyReportError if a formatter could not be determined, which an object containing:
ran: falseFormatlyReportResult if a formatter could be determined, which is an object containing:
formatter: Formatter: as resolved by resolveFormatterran: trueresult, one of:
FormatlyReportChildProcessResult if the formatter was spawned as a child process:
code: number | null: exit code of the child processsignal: NodeJS.Signal | null: signal that terminated the child processFormatlyReportDryRunResult if dryRun was passed:
args: string[]: arguments of the command that would have runcommand: string: the command that would have runFormatlyReportVirtualResult if the formatter was run in memory:
code: number: exit code the formatter would have exited with as a child processSpawned formatters' error output is passed through to the process's stderr, while their other output is ignored.
For example, to run formatting on TypeScript source files in a child directory and check the result:
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 ("code" in result && result.code) {
console.error(`${formatter.name} exited with code ${result.code}.`);
} else {
console.log(`Formatted with ${formatter.name}! 🧼`);
}
resolveFormatterDetects which of the supported formatters to use for a directory.
import { resolveFormatter } from "formatly";
const formatter = await resolveFormatter();
// {
// checker: [Function],
// formatText: [Function],
// name: "prettier",
// runner: [Function],
// testers: { ... }
// }
console.log(formatter);
Parameters:
cwd: string (optional): working directory, if not "."options: ResolveFormatterOptions (optional):
order: FormatterName[] (optional): formatters to detect first, in orderstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, either a string path or a (currentDirectory: string) => boolean functionBy default, only the working directory is searched for a config file.
Passing stopDirectory also searches each parent directory in turn, stopping once the directory matching stopDirectory has been searched.
A string is matched as a directory path, while a function is called with each directory and may return true to indicate the last directory to search.
Reaching the file system root without a match throws an error, as that indicates a stopDirectory that isn't a parent of the working directory.
For example, to search up to whichever parent directory contains a .git directory:
import { resolveFormatter } from "formatly";
import { existsSync } from "node:fs";
import * as path from "node:path";
const formatter = await resolveFormatter("path/to/project", {
stopDirectory: (currentDirectory) =>
existsSync(path.join(currentDirectory, ".git")),
});
console.log(formatter);
By default, formatters are detected in their documented order.
Passing order tries the named formatters first, in the order given; any formatter not named is tried afterwards in the default order.
Duplicate names, and names that aren't supported formatters, throw an error.
For example, to detect Biome before Prettier:
import { resolveFormatter } from "formatly";
const formatter = await resolveFormatter("path/to/project", {
order: ["biome", "prettier"],
});
console.log(formatter);
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 formatterchecker: FormatterChecker: the function used to list which files aren't formattedformatText: FormatterTextRunner: the function used to format text in memoryrunner: FormatterRunner: the function used to run the formatter on filestesters: object: strings and regular expressions used to test for the formatterFormatters are detected based on the first match from, in order:
package.json script running the formatter's format command, such as deno fmtpackage.json script mentioning the formatter's name as a whole word, such as deno task buildpackage.json keyConfig files are only searched for in the working directory unless a stopDirectory is provided.
package.json is always searched for in the working directory and its parent directories.
| Formatter | Config File | Package Key | Format Command |
|---|---|---|---|
| Biome | Configure Biome | biome check, biome ci, biome format | |
| deno fmt | Deno Configuration > Formatting | deno fmt | |
| dprint | dprint setup | dprint check, dprint fmt | |
| Oxfmt | Oxfmt Configuration | oxfmt | |
| Prettier | Prettier Configuration File | "prettier" | 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
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! 🧼
💝 This package was templated with
create-typescript-appusing the Bingo framework.
865 followers · starred Jan 2025
55 followers · starred Aug 2026
122 followers · starred Jun 2025
Formats code with whatever formatter a project is already using. 🧼
TypeScript
47
656 commits
updated Oct 12, 2026
Formats your code with whatever formatter your project is already using. 🧼
formatly can automatically detect and format with:
See Formatter Detection for details on how they are detected.
npx formatly <files>
formatly takes in any number of glob patterns.
It will then:
For example, to match all directories and folders in the current directory:
npx formatly *
To match only .ts files in src/:
npx formatly "src/**/*.ts"
--dry-runPass --dry-run to report which formatter was detected and the command that would run, without formatting anything:
npx formatly --dry-run "src/**/*.ts"
Detected prettier. 🔍
Would run: prettier --write --ignore-unknown src/**/*.ts
--help and --versionPass --help (-h) to print usage information, or --version (-v) to print formatly's version.
npm i formatly
The formatly package exports the functions used by the formatly CLI, as well as a format function for formatting text in memory and a formatFiles function for formatting or checking files on disk.
formatFormats a string of text as if it were a file at a given path, without writing to disk.
import { format } from "formatly";
import fs from "node:fs/promises";
const report = await format("console.log(123)", { filePath: "src/index.ts" });
if (report.ran && !report.error) {
await fs.writeFile("src/index.ts", report.formatted);
}
Parameters:
text: string (required): the text to formatoptions: FormatOptions (required):
filePath: string (required): path the text should be treated as being at, relative to cwd; formatters use it to infer the parser and apply any per-path config overridescwd: string (optional): working directory, if not "."formatter: FormatterName (optional): explicit formatter to use instead of detecting one, as in formatlyorder: FormatterName[] (optional): formatters to detect first, in order, as used by resolveFormatterstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, as used by resolveFormatterResolves with a FormatReport, which is one of:
FormatlyReportError if a formatter could not be determined, which is an object containing:
message: stringran: falseFormatReportFailure if the formatter ran but failed, such as on a syntax error, which is an object containing:
error: Error: the failure, including any output the formatter printed to stderrformatter: Formatter: as resolved by resolveFormatterran: trueFormatReportResult if the formatter succeeded, which is an object containing:
formatted: string: the formatted textformatter: Formatter: as resolved by resolveFormatterran: trueText is formatted by piping it through the formatter's CLI, so the formatter must be installed in the project.
Prettier is instead run in memory using the project's own installed prettier package when it can be resolved from cwd.
Biome is instead run in memory when the project installs @biomejs/js-api along with a @biomejs/wasm-nodejs matching its @biomejs/biome version, unless its config uses extends, is a nested ("root": false) config, or enables vcs.useIgnoreFile.
dprint is instead run through one long-lived dprint editor-service process per cwd, which doesn't keep the Node.js process alive while idle.
Note:
deno fmtcan only be told the file's extension, not its path, so per-path config overrides indeno.jsondon't apply toformat.fmt.excludeis honored by asking Deno about the file on disk, so it only applies to files that already exist.
formatFilesFormats, or checks the formatting of, any number of files on disk in a single formatter invocation.
import { formatFiles } from "formatly";
const report = await formatFiles(["src/a.ts", "src/b.ts"], { check: true });
if (report.ran && !report.error) {
console.log("Unformatted files:", report.changed);
}
Parameters:
filePaths: string[] (required): paths of the files to format, relative to cwdoptions: FormatFilesOptions (optional):
check: boolean (optional): whether to report which files aren't formatted instead of formatting themcwd: string (optional): working directory, if not "."formatter: FormatterName (optional): explicit formatter to use instead of detecting one, as in formatlyorder: FormatterName[] (optional): formatters to detect first, in order, as used by resolveFormatterstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, as used by resolveFormatterResolves with a FormatFilesReport, which is one of:
FormatlyReportError if a formatter could not be determined, which is an object containing:
message: stringran: falseFormatFilesReportFailure if the formatter ran but failed, which is an object containing:
error: Error: the failure, including any output the formatter printed to stderr when checkingformatter: Formatter: as resolved by resolveFormatterran: trueFormatFilesReportResult if the formatter succeeded, which is an object containing:
changed: string[]: absolute paths of the files that were changed, or with check, that would be changedformatter: Formatter: as resolved by resolveFormatterran: trueNote: Deno fails when none of the given files can be formatted without
check, such as when they're all excluded.
formatlyRuns formatting on any number of glob pattern strings.
import { formatly } from "formatly";
await formatly(["*"]);
Parameters:
patterns: string[] (required): any number of glob patternsoptions: FormatlyOptions (optional):
cwd: string (optional): working directory, if not "."dryRun: boolean (optional): whether to resolve the command that would run without running itformatter: FormatterName (optional): explicit formatter to use instead of detecting one, supports "biome", "deno", "dprint", "oxfmt", and "prettier"order: FormatterName[] (optional): formatters to detect first, in order, as used by resolveFormatterstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, as used by resolveFormatterResolves with a FormatlyReport, which is either:
FormatlyReportError if a formatter could not be determined, which an object containing:
ran: falseFormatlyReportResult if a formatter could be determined, which is an object containing:
formatter: Formatter: as resolved by resolveFormatterran: trueresult, one of:
FormatlyReportChildProcessResult if the formatter was spawned as a child process:
code: number | null: exit code of the child processsignal: NodeJS.Signal | null: signal that terminated the child processFormatlyReportDryRunResult if dryRun was passed:
args: string[]: arguments of the command that would have runcommand: string: the command that would have runFormatlyReportVirtualResult if the formatter was run in memory:
code: number: exit code the formatter would have exited with as a child processSpawned formatters' error output is passed through to the process's stderr, while their other output is ignored.
For example, to run formatting on TypeScript source files in a child directory and check the result:
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 ("code" in result && result.code) {
console.error(`${formatter.name} exited with code ${result.code}.`);
} else {
console.log(`Formatted with ${formatter.name}! 🧼`);
}
resolveFormatterDetects which of the supported formatters to use for a directory.
import { resolveFormatter } from "formatly";
const formatter = await resolveFormatter();
// {
// checker: [Function],
// formatText: [Function],
// name: "prettier",
// runner: [Function],
// testers: { ... }
// }
console.log(formatter);
Parameters:
cwd: string (optional): working directory, if not "."options: ResolveFormatterOptions (optional):
order: FormatterName[] (optional): formatters to detect first, in orderstopDirectory: StopDirectory (optional): directory to stop searching parent directories for a config file at, either a string path or a (currentDirectory: string) => boolean functionBy default, only the working directory is searched for a config file.
Passing stopDirectory also searches each parent directory in turn, stopping once the directory matching stopDirectory has been searched.
A string is matched as a directory path, while a function is called with each directory and may return true to indicate the last directory to search.
Reaching the file system root without a match throws an error, as that indicates a stopDirectory that isn't a parent of the working directory.
For example, to search up to whichever parent directory contains a .git directory:
import { resolveFormatter } from "formatly";
import { existsSync } from "node:fs";
import * as path from "node:path";
const formatter = await resolveFormatter("path/to/project", {
stopDirectory: (currentDirectory) =>
existsSync(path.join(currentDirectory, ".git")),
});
console.log(formatter);
By default, formatters are detected in their documented order.
Passing order tries the named formatters first, in the order given; any formatter not named is tried afterwards in the default order.
Duplicate names, and names that aren't supported formatters, throw an error.
For example, to detect Biome before Prettier:
import { resolveFormatter } from "formatly";
const formatter = await resolveFormatter("path/to/project", {
order: ["biome", "prettier"],
});
console.log(formatter);
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 formatterchecker: FormatterChecker: the function used to list which files aren't formattedformatText: FormatterTextRunner: the function used to format text in memoryrunner: FormatterRunner: the function used to run the formatter on filestesters: object: strings and regular expressions used to test for the formatterFormatters are detected based on the first match from, in order:
package.json script running the formatter's format command, such as deno fmtpackage.json script mentioning the formatter's name as a whole word, such as deno task buildpackage.json keyConfig files are only searched for in the working directory unless a stopDirectory is provided.
package.json is always searched for in the working directory and its parent directories.
| Formatter | Config File | Package Key | Format Command |
|---|---|---|---|
| Biome | Configure Biome | biome check, biome ci, biome format | |
| deno fmt | Deno Configuration > Formatting | deno fmt | |
| dprint | dprint setup | dprint check, dprint fmt | |
| Oxfmt | Oxfmt Configuration | oxfmt | |
| Prettier | Prettier Configuration File | "prettier" | 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
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! 🧼
💝 This package was templated with
create-typescript-appusing the Bingo framework.
865 followers · starred Jan 2025
55 followers · starred Aug 2026
122 followers · starred Jun 2025