import { spawnSync } from 'node:child_process' import { statSync } from 'node:fs' import { fileURLToPath } from 'node:url' import prettier from 'prettier/package.json' with { type: 'json' } import { applyLintFixes } from './lint-fix.mjs' const [directory = '.', ...extra] = process.argv.slice( process.argv[2] === '--' ? 3 : 2, ) if ( extra.length || !statSync(directory, { throwIfNoEntry: false })?.isDirectory() ) { console.error('Usage: pnpm fix [directory]') process.exit(1) } const lintCode = await applyLintFixes([directory]) const prettierBin = new URL( prettier.bin, import.meta.resolve('prettier/package.json'), ) const { error, status } = spawnSync( process.execPath, [fileURLToPath(prettierBin), '--write', '--', directory], { stdio: 'inherit' }, ) if (error) throw error process.exitCode = (status ?? 1) || lintCode