Skip to content

快速入门

🌐 Quickstart

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

🌐 Recommended setup and common workflows.

安装

🌐 Install

oxfmt 安装为开发依赖:

🌐 Install oxfmt as a dev dependency:

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

package.json 添加脚本:

🌐 Add scripts to package.json:

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

格式化文件:

🌐 Format files:

sh
pnpm run fmt

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

🌐 Check formatting without writing files:

sh
pnpm run fmt:check

用法

🌐 Usage

sh
oxfmt [OPTIONS] [PATH]...

在不带参数的情况下运行 oxfmt 会格式化当前目录(相当于 prettier --write .)。

🌐 Running oxfmt without arguments formats the current directory (equivalent to prettier --write .).

不支持类似 --no-semi 的命令行选项。请改用配置文件,以确保命令行和编辑器集成中的设置一致。

🌐 CLI options like --no-semi are not supported. Use the configuration file instead to ensure consistent settings across CLI and editor integrations.

位置路径中的通配符不会被展开(依赖你的 shell)。但是,以 ! 开头的排除路径支持通配符展开。

🌐 Globs in positional paths are not expanded (rely on your shell). However, !-prefixed exclude paths support glob expansion.

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

🌐 For the complete list of options, see the CLI reference.

常见工作流程

🌐 Common workflows

使用 lint-staged 的预提交

🌐 Pre-commit with lint-staged

package.json
json
{
  "lint-staged": {
    "*": "oxfmt --no-error-on-unmatched-pattern"
  }
}

--no-error-on-unmatched-pattern 可以在没有文件匹配该模式时防止错误。

创建配置文件

🌐 Create a config file

用默认值初始化 .oxfmtrc.json

🌐 Initialize .oxfmtrc.json with defaults:

sh
oxfmt --init

从 Prettier 迁移

🌐 Migrate from Prettier

sh
oxfmt --migrate prettier

详情请参阅 从 Prettier 迁移

🌐 See migrate from prettier for details.

列出不同的文件

🌐 List files that differ

sh
oxfmt --list-different

这对于配置要忽略的文件很有用。

🌐 This is useful for configuring files to ignore.

传递文件内容

🌐 Piping file contents

sh
echo 'const   x   =   1' | oxfmt --stdin-filepath test.ts

打印 const x = 1;

🌐 Prints const x = 1;

Node.js 接口

🌐 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"

下一步

🌐 Next steps