ngx-i18n-combine
Version:
Merge i18n files from components and merge to common locale files
89 lines • 3.64 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parser = void 0;
const picocolors_1 = __importDefault(require("picocolors"));
const glob = __importStar(require("glob"));
const fs = __importStar(require("fs"));
const root = __importStar(require("app-root-path"));
const path = __importStar(require("path"));
class Parser {
constructor(options) {
this.options = options;
this.locales = {};
}
parse() {
this.log(picocolors_1.default.bold('Parse files...'));
this.locales = {};
this.options.input.forEach((filesPath) => {
const files = this.getFiles(filesPath);
if (!files.length) {
this.log(picocolors_1.default.red(picocolors_1.default.bold('ERROR: files not found')));
}
else {
files.forEach((file) => this.setLocale(file));
}
});
return this.locales;
}
setLocale(file) {
const fileName = path.basename(file).split('.');
const contents = fs.readFileSync(file, 'utf-8');
const ifEsFormat = ['js', 'ts', 'tsx', 'jsx'].includes(fileName[1]);
const parseObject = JSON.parse((ifEsFormat ? contents.split('=')[1].replace(';', '') : contents));
if (!Object.keys(parseObject).length) {
this.log(picocolors_1.default.red(picocolors_1.default.bold(`ERROR: File is empty: ${file}`)));
}
else {
this.isUnique(fileName[0], parseObject);
this.locales[fileName[0]] = this.locales[fileName[0]] ? Object.assign(Object.assign({}, this.locales[fileName[0]]), parseObject) : parseObject;
if (this.options.verbose) {
this.log(picocolors_1.default.gray('- %s'), file);
}
}
}
getFiles(filePath) {
const pattern = '/**/*.json';
const fileName = !!(path.extname(filePath));
const pathWp = fileName ? filePath : filePath + pattern;
return glob.sync(pathWp, { root: root.path })
.filter(file => fs.statSync(file).isFile());
}
isUnique(localeName, contents) {
if (contents) {
const keys = Object.keys(contents);
const locale = this.locales[localeName];
for (const key in locale) {
if (keys.includes(key)) {
this.log(picocolors_1.default.red(picocolors_1.default.bold('String it was replaced: %s')), key);
}
}
}
}
log(...args) {
console.log.apply(this, args);
}
}
exports.Parser = Parser;
//# sourceMappingURL=parser.class.js.map