快速开始
推荐的设置和常用工作流程。
安装
将 oxfmt 安装为开发依赖:
sh
$ npm add -D oxfmtsh
$ pnpm add -D oxfmtsh
$ yarn add -D oxfmtsh
$ bun add -D oxfmt在 package.json 中添加脚本:
json
{
"scripts": {
"fmt": "oxfmt",
"fmt:check": "oxfmt --check"
}
}格式化文件:
sh
npm run fmtsh
pnpm run fmtsh
yarn run fmtsh
bun run fmt检查格式化但不写入文件:
sh
npm run fmt:checksh
pnpm run fmt:checksh
yarn run fmt:checksh
bun run fmt:check使用方法
sh
oxfmt [OPTIONS] [PATH]...不带参数运行 oxfmt 会格式化当前目录(相当于 prettier --write .)。
不支持 --no-semi 等 CLI 选项。请使用配置文件来确保 CLI 和编辑器集成之间的设置一致。
要在位置参数路径中使用 glob 模式,请确保用引号括起来。否则,它们可能会根据您的环境被展开或不展开。
完整的选项列表,请参阅 CLI 参考。
常用工作流程
配合 lint-staged 使用 pre-commit
json
{
"lint-staged": {
"*": "oxfmt --no-error-on-unmatched-pattern"
}
}--no-error-on-unmatched-pattern 可防止没有文件匹配模式时出错。
创建配置文件
使用默认值初始化 .oxfmtrc.json:
sh
oxfmt --init从 Prettier 迁移
sh
oxfmt --migrate prettier详见 从 Prettier 迁移。
列出有差异的文件
sh
oxfmt --list-different这对于配置要忽略的文件很有用。
管道文件内容
sh
echo 'const x = 1' | oxfmt --stdin-filepath test.ts输出 const x = 1;
Node.js API
ts
import { format, type FormatOptions } from "oxfmt";
const input = `let a=42;`;
const options: FormatOptions = {
semi: false,
};
const { code } = await format("a.js", input, options);
console.log(code); // "let a = 42"