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/shared-ignores-apply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bomb.sh/tools": patch
---

`format` CLI command applies the shared `ignorePatterns` to the project being formatted instead of only to files inside `@bomb.sh/tools`.
27 changes: 27 additions & 0 deletions src/commands/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fileURLToPath } from 'node:url';
import { x } from 'tinyexec';
import { describe, it, expect } from 'vitest';
import { createFixture } from '../test-utils/index.ts';

const bin = fileURLToPath(new URL('../bin.ts', import.meta.url));

describe('format command', () => {
it('applies the shared ignore patterns to the project being formatted', async () => {
const fixture = await createFixture({
src: { 'a.ts': 'const a={b:1}\n' },
'data.json': '{"a":1}',
'config.jsonc': '{"a":1}',
'README.md': '* item\n',
docs: { 'nested.md': '* item\n' },
'.github/workflows/ci.yml': 'a: 1\n',
});

const result = await x(
process.execPath,
['--experimental-strip-types', '--no-warnings', bin, 'format', '--list-different'],
{ nodeOptions: { cwd: fileURLToPath(fixture.root) }, throwOnError: false },
);

expect(result.stdout.trim().split('\n').toSorted()).toEqual(['src/a.ts']);
});
});
9 changes: 8 additions & 1 deletion src/commands/format.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { x } from 'tinyexec';
import type { CommandContext } from '../context.ts';
Expand All @@ -6,7 +7,13 @@ import { local } from '../utils.ts';
const config = fileURLToPath(new URL('../../oxfmtrc.json', import.meta.url));

export async function format(ctx: CommandContext) {
const result = x(local('oxfmt'), ['-c', config, ...ctx.args]);
// oxfmt resolves `ignorePatterns` relative to the config file, which lives in this
// package, so pass them as excludes to apply them to the project being formatted.
const { ignorePatterns = [] }: { ignorePatterns?: string[] } = JSON.parse(
await readFile(config, 'utf-8'),
);
const excludes = ignorePatterns.map((pattern) => `!${pattern}`);
const result = x(local('oxfmt'), ['-c', config, ...ctx.args, ...excludes]);

for await (const line of result) {
console.info(line);
Expand Down
Loading