Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-steaks-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/format": patch
---

Allow `format()` to pass even if the patterns didn't match any files
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Options can be passed to the first parameter:

### `format()`

Format the given patterns with a specified or auto-detected formatter. It returns `true` if a formatter is found and the formatting process passes, otherwise returns `false`.
Format the given patterns with a specified or auto-detected formatter. It returns `true` if a formatter is found and the formatting process passes (even if the patterns didn't match any files), otherwise returns `false`.

<!-- NOTE: Docs should match src/format.ts -->

Expand Down
3 changes: 2 additions & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export interface FormatOptions {

/**
* Format the given patterns with a specified or auto-detected formatter. It returns `true` if a
* formatter is found and the formatting process passes, otherwise returns `false`.
* formatter is found and the formatting process passes (even if the patterns didn't match any
* files), otherwise returns `false`.
*/
export async function format(patterns: string[], options: FormatOptions = {}): Promise<boolean> {
const formatterName =
Expand Down
22 changes: 17 additions & 5 deletions src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const prettier: Formatter = {
],
async format(files, ctx) {
const pm = await ctx.getPackageManager();
await packageManagerExecute(pm, ["prettier", "--write", ...files], ctx.cwd);
await packageManagerExecute(
pm,
["prettier", "--write", "--no-error-on-unmatched-pattern", ...files],
ctx.cwd,
);
},
};

Expand All @@ -69,7 +73,11 @@ const biome: Formatter = {
configFiles: ["biome.json", "biome.jsonc", ".biome.json", ".biome.jsonc"],
async format(files, ctx) {
const pm = await ctx.getPackageManager();
await packageManagerExecute(pm, ["@biomejs/biome", "format", "--write", ...files], ctx.cwd);
await packageManagerExecute(
pm,
["@biomejs/biome", "format", "--write", "--no-errors-on-unmatched", ...files],
ctx.cwd,
);
},
};

Expand All @@ -80,7 +88,11 @@ const oxfmt: Formatter = {
configFiles: [".oxfmtrc.json", ".oxfmtrc.jsonc", "oxfmt.config.ts", "oxfmt.config.mts"],
async format(files, ctx) {
const pm = await ctx.getPackageManager();
await packageManagerExecute(pm, ["oxfmt", "--write", ...files], ctx.cwd);
await packageManagerExecute(
pm,
["oxfmt", "--write", "--no-error-on-unmatched-pattern", ...files],
ctx.cwd,
);
},
};

Expand All @@ -89,7 +101,7 @@ const deno: Formatter = {
// https://docs.deno.com/runtime/reference/cli/fmt/#configuring-the-formatter
configFiles: ["deno.json", "deno.jsonc", { file: "deno.json", key: "fmt" }],
async format(files, ctx) {
await spawnProcess("deno", ["fmt", ...files], ctx.cwd);
await spawnProcess("deno", ["fmt", "--permit-no-files", ...files], ctx.cwd);
},
};

Expand All @@ -102,7 +114,7 @@ const dprint: Formatter = {
// NOTE: dprint could be installed in many ways globally, but for all the tools here, we assume
// that they're installed locally in node_modules for now.
const pm = await ctx.getPackageManager();
await packageManagerExecute(pm, ["dprint", "fmt", ...files], ctx.cwd);
await packageManagerExecute(pm, ["dprint", "fmt", "--allow-no-files", ...files], ctx.cwd);
},
};

Expand Down
17 changes: 4 additions & 13 deletions tests/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ function canRunDeno() {
});
}

// dprint requires an internet connection to download plugins (on the first run)
async function canRunDprint() {
try {
await dns.lookup("plugins.dprint.dev");
return true;
} catch {
return false;
}
}
Comment on lines -16 to -24

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed anymore since #21 as the plugin is installed locally with npm


const cases: FormatterName[] = ["biome", "deno", "dprint", "oxfmt", "prettier"];

test.for(cases)("detect and format %s fixture", { timeout: 10000 }, async (name, ctx) => {
Expand All @@ -32,9 +22,6 @@ test.for(cases)("detect and format %s fixture", { timeout: 10000 }, async (name,
if (name === "deno" && !(await canRunDeno())) {
ctx.skip("deno is not installed");
}
if (name === "dprint" && !(await canRunDprint())) {
ctx.skip("no internet connection for dprint plugin download");
}
}

const fixtureTemplateDir = path.join(import.meta.dirname, "fixtures", name);
Expand All @@ -47,4 +34,8 @@ test.for(cases)("detect and format %s fixture", { timeout: 10000 }, async (name,
const formatResult = await format(["file.ts"], { cwd: fixture.path, formatter: name });
expect(formatResult).toBe(true);
expect(await fixture.readFile("file.ts", "utf-8")).toMatchSnapshot();

// Formatting a missing file should be fine
const formatMissingResult = await format(["missing.ts"], { cwd: fixture.path, formatter: name });
expect(formatMissingResult).toBe(true);
});
29 changes: 24 additions & 5 deletions tests/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ const mocks = vi.hoisted(() => ({
vi.mock(import("tinyexec"), () => ({ exec: mocks.exec }));

const cases: { formatter: FormatterName; expectedCommand: string[] }[] = [
{ formatter: "prettier", expectedCommand: ["npx", "prettier", "--write", "file.ts"] },
{
formatter: "prettier",
expectedCommand: ["npx", "prettier", "--write", "--no-error-on-unmatched-pattern", "file.ts"],
},
{
formatter: "biome",
expectedCommand: ["npx", "@biomejs/biome", "format", "--write", "file.ts"],
expectedCommand: [
"npx",
"@biomejs/biome",
"format",
"--write",
"--no-errors-on-unmatched",
"file.ts",
],
},
{
formatter: "oxfmt",
expectedCommand: ["npx", "oxfmt", "--write", "--no-error-on-unmatched-pattern", "file.ts"],
},
{
formatter: "deno",
expectedCommand: ["deno", "fmt", "--permit-no-files", "file.ts"],
},
{
formatter: "dprint",
expectedCommand: ["npx", "dprint", "fmt", "--allow-no-files", "file.ts"],
},
{ formatter: "oxfmt", expectedCommand: ["npx", "oxfmt", "--write", "file.ts"] },
{ formatter: "deno", expectedCommand: ["deno", "fmt", "file.ts"] },
{ formatter: "dprint", expectedCommand: ["npx", "dprint", "fmt", "file.ts"] },
];

test.for(cases)("executes the correct command for $formatter", async (c) => {
Expand Down