diff --git a/README.md b/README.md index 2bc6bdadc..d6bf6928d 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,20 @@ If you prefer to run off the fully compiled release version of your current code > Note: Running any of the `build:` prefixed commands will not publish anything to npm. They simply populate the `dist/` directory with the compiled code. You can run these commands safely as often as you wish. +## Updating instrumentation log formatting + +The script `prettier-ignore-log-lines.js` automatically adds `/* prettier-ignore */` +comments to long instrumentation log statements. Run it whenever you add or modify +logging counters so that Prettier doesn't wrap these lines: + +```sh +node prettier-ignore-log-lines.js +``` + +Execute the command from the repository root (or pass a working directory containing +a `src/` folder). The script scans `src/**/*.ts` files and only updates lines that +match known instrumentation patterns and exceed the configured print width. + ## Releasing If you're a core developer on this project and need to cut a release, simply run: diff --git a/test/unit/scripts/prettier-ignore-log-lines.test.ts b/test/unit/scripts/prettier-ignore-log-lines.test.ts new file mode 100644 index 000000000..05226f723 --- /dev/null +++ b/test/unit/scripts/prettier-ignore-log-lines.test.ts @@ -0,0 +1,50 @@ +import fs from 'fs' +import os from 'os' +import path from 'path' +import { spawnSync } from 'child_process' + +const scriptPath = path.resolve(__dirname, '../../../prettier-ignore-log-lines.js') + +function runScriptWithFile(content: string): string { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prettier-test-')) + const srcDir = path.join(tmpDir, 'src') + fs.mkdirSync(srcDir) + const filePath = path.join(srcDir, 'file.ts') + fs.writeFileSync(filePath, content + '\n') + const result = spawnSync('node', [scriptPath], { cwd: tmpDir }) + if (result.status !== 0) { + throw new Error(result.stderr.toString()) + } + const output = fs.readFileSync(filePath, 'utf8') + fs.rmSync(tmpDir, { recursive: true, force: true }) + return output.trimEnd() +} + +describe('prettier-ignore-log-lines script', () => { + test('inserts comment for long instrumentation lines', () => { + const content = [ + ` nestedCountersInstance.countEvent('cat', '${'x'.repeat(150)}')`, + ` nestedCountersInstance.countRareEvent('cat', '${'x'.repeat(150)}')`, + ` stateManager.statemanager_fatal('fatal', '${'x'.repeat(150)}')`, + ` if (logFlags.verbose) console.log('${'x'.repeat(150)}')`, + ].join('\n') + + const result = runScriptWithFile(content) + const lines = result.split(/\r?\n/) + for (const line of lines) { + expect(line.startsWith(' /* prettier-ignore */ ')).toBe(true) + } + }) + + test('keeps short and unrelated lines unchanged', () => { + const originalLines = [ + ` nestedCountersInstance.countEvent('cat', 'short')`, + ` console.log('hello')`, + ] + const result = runScriptWithFile(originalLines.join('\n')) + const lines = result.split(/\r?\n/) + lines.forEach((line, idx) => { + expect(line).toBe(originalLines[idx]) + }) + }) +})