Skip to content

Commit 815cf15

Browse files
fix(format): apply shared ignore patterns outside tools (#64)
* test: cover shared ignore patterns in format command * fix(format): apply shared ignore patterns outside tools --------- Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
1 parent df8a41e commit 815cf15

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎.changeset/shared-ignores-apply.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bomb.sh/tools": patch
3+
---
4+
5+
`format` CLI command applies the shared `ignorePatterns` to the project being formatted instead of only to files inside `@bomb.sh/tools`.

‎src/commands/format.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { x } from 'tinyexec';
3+
import { describe, it, expect } from 'vitest';
4+
import { createFixture } from '../test-utils/index.ts';
5+
6+
const bin = fileURLToPath(new URL('../bin.ts', import.meta.url));
7+
8+
describe('format command', () => {
9+
it('applies the shared ignore patterns to the project being formatted', async () => {
10+
const fixture = await createFixture({
11+
src: { 'a.ts': 'const a={b:1}\n' },
12+
'data.json': '{"a":1}',
13+
'config.jsonc': '{"a":1}',
14+
'README.md': '* item\n',
15+
docs: { 'nested.md': '* item\n' },
16+
'.github/workflows/ci.yml': 'a: 1\n',
17+
});
18+
19+
const result = await x(
20+
process.execPath,
21+
['--experimental-strip-types', '--no-warnings', bin, 'format', '--list-different'],
22+
{ nodeOptions: { cwd: fileURLToPath(fixture.root) }, throwOnError: false },
23+
);
24+
25+
expect(result.stdout.trim().split('\n').toSorted()).toEqual(['src/a.ts']);
26+
});
27+
});

‎src/commands/format.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFile } from 'node:fs/promises';
12
import { fileURLToPath } from 'node:url';
23
import { x } from 'tinyexec';
34
import type { CommandContext } from '../context.ts';
@@ -6,7 +7,13 @@ import { local } from '../utils.ts';
67
const config = fileURLToPath(new URL('../../oxfmtrc.json', import.meta.url));
78

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

1118
for await (const line of result) {
1219
console.info(line);

0 commit comments

Comments
 (0)