angular-translation-checker
Version:
A comprehensive tool for analyzing translation keys in Angular projects using ngx-translate
145 lines (143 loc) • 6.23 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileReporter = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
class FileReporter {
constructor() {
this.name = 'file-reporter';
this.version = '1.0.0';
this.description = 'Saves analysis reports to files';
this.outputDir = './reports';
}
async initialize(context) {
this.logger = context.logger;
this.outputDir = context.config.outputDir || './reports';
this.logger.debug('File reporter initialized');
}
async report(result, output) {
try {
// Ensure output directory exists
await fs_1.promises.mkdir(this.outputDir, { recursive: true });
// Generate filename with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const format = this.detectFormat(output);
const filename = `translation-analysis-${timestamp}.${format}`;
const filepath = path.join(this.outputDir, filename);
// Write the report
await fs_1.promises.writeFile(filepath, output, 'utf-8');
this.logger.info(`Report saved to: ${filepath}`);
// Also create a latest report link
const latestPath = path.join(this.outputDir, `latest.${format}`);
await fs_1.promises.writeFile(latestPath, output, 'utf-8');
// Generate index file for multiple reports
await this.generateIndexFile(result);
}
catch (error) {
this.logger.error(`Failed to save report: ${error}`);
throw error;
}
}
detectFormat(output) {
if (output.trim().startsWith('<!DOCTYPE html')) {
return 'html';
}
else if (output.trim().startsWith('{')) {
return 'json';
}
else {
return 'txt';
}
}
async generateIndexFile(result) {
try {
const files = await fs_1.promises.readdir(this.outputDir);
const reportFiles = files.filter(f => f.startsWith('translation-analysis-') && f !== 'index.html');
const indexHtml = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Translation Analysis Reports</title>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 40px; background: #f5f5f5; }
.container { max-width: 800px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h1 { color: #007acc; border-bottom: 3px solid #007acc; padding-bottom: 10px; }
.report-list { list-style: none; padding: 0; }
.report-item { background: #f8f9fa; margin: 10px 0; padding: 15px; border-radius: 6px; border-left: 4px solid #007acc; }
.report-item a { text-decoration: none; color: #007acc; font-weight: bold; }
.report-item a:hover { text-decoration: underline; }
.report-meta { color: #666; font-size: 0.9em; margin-top: 5px; }
.latest-badge { background: #4caf50; color: white; padding: 2px 8px; border-radius: 12px; font-size: 0.8em; margin-left: 10px; }
</style>
</head>
<body>
<div class="container">
<h1>📊 Translation Analysis Reports</h1>
<p>Generated reports for project: <strong>${result.config.srcPath}</strong></p>
<ul class="report-list">
${reportFiles.reverse().map((file, index) => {
const timestamp = file.match(/translation-analysis-(.+)\./)?.[1]?.replace(/-/g, ':') || '';
const date = new Date(timestamp).toLocaleString();
const isLatest = index === 0;
return `
<li class="report-item">
<a href="${file}">${file}</a>
${isLatest ? '<span class="latest-badge">Latest</span>' : ''}
<div class="report-meta">Generated: ${date}</div>
</li>
`;
}).join('')}
</ul>
<div style="margin-top: 30px; padding: 20px; background: #e3f2fd; border-radius: 6px;">
<h3>Quick Links</h3>
<p><a href="latest.html">📄 Latest HTML Report</a></p>
<p><a href="latest.json">📊 Latest JSON Report</a></p>
<p><a href="latest.txt">📝 Latest Text Report</a></p>
</div>
</div>
</body>
</html>`;
await fs_1.promises.writeFile(path.join(this.outputDir, 'index.html'), indexHtml, 'utf-8');
}
catch (error) {
this.logger.warn(`Failed to generate index file: ${error}`);
}
}
}
exports.FileReporter = FileReporter;
//# sourceMappingURL=file-reporter.js.map
;