()
| 2 | import path from 'path' |
| 3 | |
| 4 | async function copyLastLogLines() { |
| 5 | const projectRoot = path.join(__dirname, '..') |
| 6 | const debugLogPath = path.join(projectRoot, 'backend', 'src', 'debug.log') |
| 7 | const subsetLogPath = path.join(projectRoot, 'backend', 'debug-subset.log') |
| 8 | const linesToCopy = 100 |
| 9 | |
| 10 | try { |
| 11 | // Check if debug.log exists |
| 12 | if (!fs.existsSync(debugLogPath)) { |
| 13 | console.error(`Error: Input file not found at ${debugLogPath}`) |
| 14 | process.exit(1) |
| 15 | } |
| 16 | |
| 17 | // Read the debug log file |
| 18 | const content = await fs.promises.readFile(debugLogPath, 'utf-8') |
| 19 | const lines = content.split('\n') |
| 20 | |
| 21 | // Get the last N lines (handle files with fewer lines) |
| 22 | const startLine = Math.max(0, lines.length - linesToCopy) |
| 23 | // If the last line is empty, we might want to exclude it unless it's the only line |
| 24 | const relevantLines = lines.slice(startLine, lines.length) |
| 25 | // Filter out potentially empty last line if file ends with newline and has more than one line |
| 26 | const lastLines = |
| 27 | lines.length > 1 && lines[lines.length - 1] === '' |
| 28 | ? relevantLines.slice(0, -1) |
| 29 | : relevantLines |
| 30 | |
| 31 | const subsetContent = lastLines.join('\n') |
| 32 | |
| 33 | // Write the subset to the new file |
| 34 | await fs.promises.writeFile(subsetLogPath, subsetContent, 'utf-8') |
| 35 | |
| 36 | console.log( |
| 37 | `Successfully copied the last ${lastLines.length} lines from ${debugLogPath} to ${subsetLogPath}`, |
| 38 | ) |
| 39 | } catch (error) { |
| 40 | console.error('An error occurred:', error) |
| 41 | process.exit(1) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | copyLastLogLines() |
no test coverage detected