formatly v0.1.0
Usage
formatly can automatically detect and format with:
See Formatter Detection for details on how they are detected.
CLI
npx formatly <files>formatly takes in any number of glob patterns
It will then:
- Detect which supported formatter is configured in the repository
- Pass those glob patterns directly to the formatter
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"Node.js API
npm i formatlyThe formatly package exports the functions used by the formatly CLI.
formatly
Runs 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"."
Resolves with a FormatlyReport, which is either:
FormatlyReportErrorif a formatter could not be determined, which an object containing:ran: false
FormatlyReportResultif a formatter could be determined, which is an object containing:formatter: Formatter: as resolved byresolveFormatterran: trueresult: Result, theResultfrom running the formatter withexeca
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 (result.code) {
console.error(`Error running ${formatter.runner}:`, result.stderr);
} else {
console.log(`Formatted with ${formatter.name}! ๐งผ`);
}resolveFormatter
Detects which of the supported formatters to use for a directory.
import { resolveFormatter } from "formatly";
const formatter = await resolveFormatter();
// {
// name: "Prettier",
// runner: "npx prettier --write",
// testers: { ... }
// }
console.log(formatter);Parameters:
cwd: string(optional): working directory, if not"."
Resolves with either:
undefinedif a formatter could not be detectedFormatterif 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
Formatter Detection
Formatters are detected based on the first match from, in order:
- Existence of the formatter's default supported config file name
- The formatter's name in a
package.jsonfmtorformatscript - Well-known root-level
package.jsonkey
Supported Formatters
| Formatter | Config File | Package Key | Script |
|---|---|---|---|
| Biome | Configure Biome | biome | |
| deno fmt | Deno Configuration > Formatting | deno | |
| dprint | dprint setup | dprint | |
| Prettier | Prettier Configuration File | "prettier" | prettier |
Want support for a formatter not mentioned here? Great! Please file a feature request GitHub issue. ๐
Development
See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md.
Thanks! ๐
Contributors
๐ This package was templated with
create-typescript-appusing thecreateengine.