|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import program from 'commander' |
| 4 | +import fs from 'fs' |
| 5 | +import languages from '../../lib/languages.js' |
| 6 | + |
| 7 | +const defaultWorkflowUrl = [ |
| 8 | + process.env.GITHUB_SERVER_URL, |
| 9 | + process.env.GITHUB_REPOSITORY, |
| 10 | + 'actions/runs', |
| 11 | + process.env.GITHUB_RUN_ID, |
| 12 | +].join('/') |
| 13 | + |
| 14 | +const reportTypes = { |
| 15 | + 'pull-request-body': pullRequestBodyReport, |
| 16 | + csv: csvReport, |
| 17 | +} |
| 18 | + |
| 19 | +program |
| 20 | + .description('Reads a translation batch log and generates a report') |
| 21 | + .requiredOption('--language <language>', 'The language to compare') |
| 22 | + .requiredOption('--log-file <log-file>', 'The batch log file') |
| 23 | + .requiredOption( |
| 24 | + '--report-type <report-type>', |
| 25 | + 'The batch log file, I.E: ' + Object.keys(reportTypes).join(', ') |
| 26 | + ) |
| 27 | + .option('--workflow-url <workflow-url>', 'The workflow url', defaultWorkflowUrl) |
| 28 | + .parse(process.argv) |
| 29 | + |
| 30 | +const options = program.opts() |
| 31 | +const language = languages[options.language] |
| 32 | +const { logFile, workflowUrl, reportType } = options |
| 33 | + |
| 34 | +if (!Object.keys(reportTypes).includes(reportType)) { |
| 35 | + throw new Error(`Invalid report type: ${reportType}`) |
| 36 | +} |
| 37 | + |
| 38 | +const logFileContents = fs.readFileSync(logFile, 'utf8') |
| 39 | + |
| 40 | +const revertLines = logFileContents |
| 41 | + .split('\n') |
| 42 | + .filter((line) => line.match(/^-> reverted to English/)) |
| 43 | + .filter((line) => line.match(language.dir)) |
| 44 | + |
| 45 | +const reportEntries = revertLines.sort().map((line) => { |
| 46 | + const [, file, reason] = line.match(/^-> reverted to English: (.*) Reason: (.*)$/) |
| 47 | + return { file, reason } |
| 48 | +}) |
| 49 | + |
| 50 | +function pullRequestBodyReport() { |
| 51 | + const body = [ |
| 52 | + `New translation batch for ${language.name}. Product of [this workflow](${workflowUrl}).`, |
| 53 | + '\n', |
| 54 | + `## ${reportEntries.length} files reverted.`, |
| 55 | + ] |
| 56 | + |
| 57 | + const filesByReason = {} |
| 58 | + |
| 59 | + reportEntries.forEach(({ file, reason }) => { |
| 60 | + filesByReason[reason] = filesByReason[reason] || [] |
| 61 | + filesByReason[reason].push(file) |
| 62 | + }) |
| 63 | + |
| 64 | + Object.keys(filesByReason) |
| 65 | + .sort() |
| 66 | + .forEach((reason) => { |
| 67 | + const files = filesByReason[reason] |
| 68 | + body.push(`\n### ${reason}`) |
| 69 | + body.push(`\n${files.length} files:\n`) |
| 70 | + const checkBoxes = files.map((file) => `- [ ] ${file}`) |
| 71 | + body.push(checkBoxes) |
| 72 | + }) |
| 73 | + |
| 74 | + return body.join('\n') |
| 75 | +} |
| 76 | + |
| 77 | +function csvReport() { |
| 78 | + const lines = reportEntries.map(({ file, reason }) => { |
| 79 | + return [file, reason].join(',') |
| 80 | + }) |
| 81 | + |
| 82 | + return ['file,reason', lines].flat().join('\n') |
| 83 | +} |
| 84 | + |
| 85 | +console.log(reportTypes[reportType]()) |
0 commit comments