Skip to content

快速入门

推荐的设置和常用工作流程。

安装

oxfmt 安装为开发依赖项:

sh
$ npm add -D oxfmt
sh
$ pnpm add -D oxfmt
sh
$ yarn add -D oxfmt
sh
$ bun add -D oxfmt

package.json 添加脚本:

package.json
json
{
  "scripts": {
    "fmt": "oxfmt",
    "fmt:check": "oxfmt --check"
  }
}

格式化文件:

sh
npm run fmt
sh
pnpm run fmt
sh
yarn run fmt
sh
bun run fmt

在不写入文件的情况下检查格式:

sh
npm run fmt:check
sh
pnpm run fmt:check
sh
yarn run fmt:check
sh
bun run fmt:check

使用方法

sh
oxfmt [OPTIONS] [PATH]...

不带参数运行 oxfmt 会格式化当前目录(等同于 prettier --write .)。

不支持 CLI 选项如 --no-semi。请使用配置文件以确保命令行和编辑器集成之间的一致性设置。

在位置路径中使用通配符模式时,请务必加上引号。否则,是否展开取决于您的环境。

有关完整选项列表,请参阅 CLI 参考

常用工作流程

在 pre-commit 中使用 lint-staged

package.json
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"

下一步