angular-translation-checker
Version:
A comprehensive tool for analyzing translation keys in Angular projects using ngx-translate
153 lines • 5.51 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.NodeFileSystemAdapter = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const util_1 = require("util");
// Promisify fs methods
const readFileAsync = (0, util_1.promisify)(fs.readFile);
const writeFileAsync = (0, util_1.promisify)(fs.writeFile);
const readdirAsync = (0, util_1.promisify)(fs.readdir);
const statAsync = (0, util_1.promisify)(fs.stat);
class NodeFileSystemAdapter {
async readFile(filePath) {
try {
return await readFileAsync(filePath, 'utf8');
}
catch (error) {
throw new Error(`Failed to read file ${filePath}: ${error}`);
}
}
async writeFile(filePath, content) {
try {
// Ensure directory exists
const dir = path.dirname(filePath);
if (!await this.exists(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
await writeFileAsync(filePath, content, 'utf8');
}
catch (error) {
throw new Error(`Failed to write file ${filePath}: ${error}`);
}
}
async readdir(dirPath) {
try {
return await readdirAsync(dirPath);
}
catch (error) {
throw new Error(`Failed to read directory ${dirPath}: ${error}`);
}
}
async exists(filePath) {
try {
await statAsync(filePath);
return true;
}
catch {
return false;
}
}
async isDirectory(filePath) {
try {
const stats = await statAsync(filePath);
return stats.isDirectory();
}
catch {
return false;
}
}
async glob(pattern, options = {}) {
try {
// Try to use the modern glob API with dynamic import
const globModule = await Promise.resolve().then(() => __importStar(require('glob')));
const globFunc = globModule.glob || globModule.default || globModule;
if (typeof globFunc === 'function') {
const results = await globFunc(pattern, options);
return Array.isArray(results) ? results : [];
}
}
catch (error) {
// Fall back to basic implementation
}
return this.basicGlob(pattern, options.cwd || process.cwd());
}
async basicGlob(pattern, basePath) {
const results = [];
try {
await this.walkDirectory(basePath, (filePath) => {
const relativePath = path.relative(basePath, filePath);
if (this.matchesPattern(relativePath, pattern)) {
results.push(filePath);
}
});
}
catch (error) {
throw new Error(`Glob operation failed for pattern ${pattern}: ${error}`);
}
return results;
}
async walkDirectory(dirPath, callback) {
const entries = await this.readdir(dirPath);
for (const entry of entries) {
const fullPath = path.join(dirPath, entry);
try {
if (await this.isDirectory(fullPath)) {
await this.walkDirectory(fullPath, callback);
}
else {
callback(fullPath);
}
}
catch (error) {
// Skip files that can't be accessed
continue;
}
}
}
matchesPattern(filePath, pattern) {
// Convert glob pattern to regex
let regexPattern = pattern
.replace(/\*\*/g, '.*') // ** matches any number of directories
.replace(/\*/g, '[^/]*') // * matches any characters except path separator
.replace(/\?/g, '.') // ? matches single character
.replace(/\./g, '\\.'); // Escape dots
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(filePath.replace(/\\/g, '/'));
}
}
exports.NodeFileSystemAdapter = NodeFileSystemAdapter;
//# sourceMappingURL=filesystem.js.map
;