diff --git a/.changeset/shared-ignores-apply.md b/.changeset/shared-ignores-apply.md new file mode 100644 index 0000000..d98cbda --- /dev/null +++ b/.changeset/shared-ignores-apply.md @@ -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`. diff --git a/src/commands/format.test.ts b/src/commands/format.test.ts new file mode 100644 index 0000000..87f34bc --- /dev/null +++ b/src/commands/format.test.ts @@ -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']); + }); +}); diff --git a/src/commands/format.ts b/src/commands/format.ts index 7cd6baa..7d66929 100644 --- a/src/commands/format.ts +++ b/src/commands/format.ts @@ -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'; @@ -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);