-
-
Notifications
You must be signed in to change notification settings - Fork 857
refactor(oxfmt): Rewrite --init mode in JS
#16769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graphite-app
merged 1 commit into
main
from
12-12-refactor_oxfmt_rewrite_--init_mode_in_js
Dec 12, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,20 @@ | ||
| import { format } from "./bindings.js"; | ||
| import { setupConfig, formatEmbeddedCode, formatFile } from "./prettier-proxy.js"; | ||
| import { runInit } from "./migration/init.js"; | ||
|
|
||
| const args = process.argv.slice(2); | ||
| void (async () => { | ||
| const args = process.argv.slice(2); | ||
|
|
||
| // Call the Rust formatter with our JS callback | ||
| const success = await format(args, setupConfig, formatEmbeddedCode, formatFile); | ||
| // Handle `--init` command in JS | ||
| if (args.includes("--init")) { | ||
| return await runInit(); | ||
| } | ||
|
|
||
| // NOTE: It's recommended to set `process.exitCode` instead of calling `process.exit()`. | ||
| // `process.exit()` kills the process immediately and `stdout` may not be flushed before process dies. | ||
| // https://nodejs.org/api/process.html#processexitcode | ||
| if (!success) process.exitCode = 1; | ||
| // Call the Rust formatter with our JS callback | ||
| const success = await format(args, setupConfig, formatEmbeddedCode, formatFile); | ||
|
|
||
| // NOTE: It's recommended to set `process.exitCode` instead of calling `process.exit()`. | ||
| // `process.exit()` kills the process immediately and `stdout` may not be flushed before process dies. | ||
| // https://nodejs.org/api/process.html#processexitcode | ||
| if (!success) process.exitCode = 1; | ||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* oxlint-disable no-console */ | ||
|
|
||
| import { stat, writeFile } from "node:fs/promises"; | ||
|
|
||
| async function isFile(path: string) { | ||
| try { | ||
| const stats = await stat(path); | ||
| return stats.isFile(); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run the `--init` command to scaffold a default `.oxfmtrc.json` file. | ||
| */ | ||
| export async function runInit() { | ||
| // Check if config file already exists | ||
| if ((await isFile(".oxfmtrc.json")) || (await isFile(".oxfmtrc.jsonc"))) { | ||
| console.error("Configuration file already exists."); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| // Build config object | ||
| const schemaPath = "./node_modules/oxfmt/configuration_schema.json"; | ||
|
|
||
| const config: Record<string, unknown> = { | ||
| // Add `$schema` field at the top if schema file exists in `node_modules` | ||
| $schema: schemaPath, | ||
| // `ignorePatterns` is included to make visible and preferred over `.prettierignore` | ||
| ignorePatterns: [], | ||
| }; | ||
|
|
||
| // Remove if this command is run with e.g. `npx` | ||
| // NOTE: To keep `$schema` field at the top, we delete it here instead of defining conditionally above | ||
| if (!(await isFile(schemaPath))) { | ||
| delete config.$schema; | ||
| } | ||
leaysgur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| const jsonStr = JSON.stringify(config, null, 2); | ||
|
|
||
| // TODO: Call napi `validateConfig()` to ensure validity | ||
|
|
||
| await writeFile(".oxfmtrc.json", jsonStr + "\n"); | ||
| console.log("Created `.oxfmtrc.json`."); | ||
| } catch { | ||
| console.error("Failed to write `.oxfmtrc.json`."); | ||
| process.exitCode = 1; | ||
| } | ||
leaysgur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { join } from "node:path"; | ||
| import { tmpdir } from "node:os"; | ||
| import fs from "node:fs/promises"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { runCli } from "./utils"; | ||
|
|
||
| describe("init", () => { | ||
| it("should create .oxfmtrc.json", async () => { | ||
| const tempDir = await fs.mkdtemp(join(tmpdir(), "oxfmt-init-test-")); | ||
|
|
||
| try { | ||
| const result = await runCli(tempDir, ["--init"]); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout).toContain("Created `.oxfmtrc.json`."); | ||
|
|
||
| const configPath = join(tempDir, ".oxfmtrc.json"); | ||
| const content = await fs.readFile(configPath, "utf8"); | ||
| const config = JSON.parse(content); | ||
|
|
||
| expect(config.ignorePatterns).toEqual([]); | ||
| } finally { | ||
| await fs.rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it("should add $schema when node_modules/oxfmt exists", async () => { | ||
| const tempDir = await fs.mkdtemp(join(tmpdir(), "oxfmt-init-test-")); | ||
|
|
||
| try { | ||
| // Create fake node_modules/oxfmt/configuration_schema.json | ||
| const schemaDir = join(tempDir, "node_modules", "oxfmt"); | ||
| await fs.mkdir(schemaDir, { recursive: true }); | ||
| await fs.writeFile(join(schemaDir, "configuration_schema.json"), "{}"); | ||
|
|
||
| const result = await runCli(tempDir, ["--init"]); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
|
|
||
| const configPath = join(tempDir, ".oxfmtrc.json"); | ||
| const content = await fs.readFile(configPath, "utf8"); | ||
| const config = JSON.parse(content); | ||
|
|
||
| expect(config.$schema).toBe("./node_modules/oxfmt/configuration_schema.json"); | ||
| expect(Object.keys(config)[0]).toBe("$schema"); // $schema should be first | ||
| } finally { | ||
| await fs.rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it("should abort if .oxfmtrc.json already exists", async () => { | ||
| const tempDir = await fs.mkdtemp(join(tmpdir(), "oxfmt-init-test-")); | ||
|
|
||
| try { | ||
| // Create existing config file | ||
| await fs.writeFile(join(tempDir, ".oxfmtrc.json"), "{}"); | ||
|
|
||
| const result = await runCli(tempDir, ["--init"]); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stderr).toContain("Configuration file already exists."); | ||
| } finally { | ||
| await fs.rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it("should abort if .oxfmtrc.jsonc already exists", async () => { | ||
| const tempDir = await fs.mkdtemp(join(tmpdir(), "oxfmt-init-test-")); | ||
|
|
||
| try { | ||
| // Create existing config file | ||
| await fs.writeFile(join(tempDir, ".oxfmtrc.jsonc"), "{}"); | ||
|
|
||
| const result = await runCli(tempDir, ["--init"]); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stderr).toContain("Configuration file already exists."); | ||
| } finally { | ||
| await fs.rm(tempDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.