@criticalmanufacturing/dev-i18n-transform
Version:
i18n <--> gettext transform
299 lines (298 loc) • 18.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
//#region Imports
var database_1 = require("../model/database");
var sql_converter_1 = require("../converters/sql.converter");
var resources_converter_1 = require("../converters/resources.converter");
var azDevOps_1 = require("../azureDevOps/azDevOps");
var fs = require("fs");
var argv = require("minimist")(process.argv);
// Import config
var config;
if (argv["config"] === undefined || argv["config"] === null) {
config = require("../i18n-import.config.json");
}
else {
config = require(argv["config"]);
}
//#endregion
var Program = /** @class */ (function () {
function Program() {
//#region Private Properties
this.filesResourcesExtension = ".resx";
//#endregion
}
//#endregion
//#region Private Methods
/**
* Find all files in specific folder with specific extension
* @param path Path to get files
* @param extension Extension name, e.g: '.resx'
*/
Program.prototype.getFilesFromPath = function (path, extension) {
var dir = fs.readdirSync(path);
return dir.filter(function (elm) { return elm.match(new RegExp("^[A-z0-9]+." + extension + "$", "ig")); });
};
//#endregion
//#region Public Methods
Program.prototype.Main = function () {
return __awaiter(this, void 0, void 0, function () {
var db_1, tfs_1, i18nConverter_1, resourcesConverter, localized_1, userStory_1, packagesi18n_1, packagesResources, _a, _b, _i, index, filesForEachResourcesPath, contentForEachFile, i, packagesResourcesArray, i, _c, _d, iterator, file, content, file, content, filesChanged, connection, currentIteration, userStoryId, repository, commitedAndPushedBy, pullRequestId, err_1;
var _this = this;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
_e.trys.push([0, 16, , 17]);
db_1 = new database_1.DatabaseManagement();
tfs_1 = new azDevOps_1.AzDevOpsManagement();
i18nConverter_1 = new sql_converter_1.StructuredQueryLanguageConverter();
resourcesConverter = new resources_converter_1.ResourcesConverter();
localized_1 = [];
packagesi18n_1 = Object.keys(config.packages.i18n);
// Get i18n Localized Messages in database
return [4 /*yield*/, db_1.getLocalizedMessages(database_1.connectionToDatabase, database_1.SQL_Geti18nLocalizedMessages, packagesi18n_1).then(function (output) { return __awaiter(_this, void 0, void 0, function () {
var mapWithPathsChanged, _a, _b, _i, index, newArray, _c, _d, _e, i, pathToWrite, connection, currentIteration, userStoryId, _f, _g, _h, index, paths, repository, commitedAndPushedBy, pullRequestId;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
localized_1 = output;
if (localized_1.length === 0) {
return [2 /*return*/];
}
mapWithPathsChanged = new Map();
_a = [];
for (_b in packagesi18n_1)
_a.push(_b);
_i = 0;
_j.label = 1;
case 1:
if (!(_i < _a.length)) return [3 /*break*/, 7];
index = _a[_i];
newArray = [];
_c = [];
for (_d in localized_1)
_c.push(_d);
_e = 0;
_j.label = 2;
case 2:
if (!(_e < _c.length)) return [3 /*break*/, 5];
i = _c[_e];
if (!localized_1[i].localizedMessageName.match(packagesi18n_1[index])) return [3 /*break*/, 4];
return [4 /*yield*/, i18nConverter_1.writeToFile(localized_1[i], packagesi18n_1[index]).then(function (path) { return path; })];
case 3:
pathToWrite = _j.sent();
if (newArray.includes(pathToWrite) !== true) {
newArray.push(pathToWrite);
}
_j.label = 4;
case 4:
_e++;
return [3 /*break*/, 2];
case 5:
mapWithPathsChanged.set(packagesi18n_1[index], newArray);
_j.label = 6;
case 6:
_i++;
return [3 /*break*/, 1];
case 7: return [4 /*yield*/, tfs_1.getConnection().then(function (output) { return output; })];
case 8:
connection = _j.sent();
return [4 /*yield*/, tfs_1.getCurrentIteration(connection.iteration).then(function (output) { return output; })];
case 9:
currentIteration = _j.sent();
return [4 /*yield*/, tfs_1.checkIfUSAlreadyExist(connection.workItem, currentIteration.path)];
case 10:
userStoryId = _j.sent();
if (!(userStoryId === null)) return [3 /*break*/, 12];
return [4 /*yield*/, tfs_1.createUserStory(connection.workItem, currentIteration.path)];
case 11:
userStory_1 = _j.sent();
return [3 /*break*/, 13];
case 12:
userStory_1 = userStoryId;
_j.label = 13;
case 13:
_f = [];
for (_g in packagesi18n_1)
_f.push(_g);
_h = 0;
_j.label = 14;
case 14:
if (!(_h < _f.length)) return [3 /*break*/, 19];
index = _f[_h];
paths = mapWithPathsChanged.get(packagesi18n_1[index]);
if (!(paths.length > 0)) return [3 /*break*/, 18];
repository = config.packages.i18n[packagesi18n_1[index]].repository;
return [4 /*yield*/, tfs_1.createCommitsAndPush(connection.git, packagesi18n_1[index], paths, repository)];
case 15:
commitedAndPushedBy = _j.sent();
return [4 /*yield*/, tfs_1.createPR(connection.git, repository, userStory_1)];
case 16:
pullRequestId = _j.sent();
return [4 /*yield*/, tfs_1.updatePR(connection.git, pullRequestId, repository, commitedAndPushedBy)];
case 17:
_j.sent();
_j.label = 18;
case 18:
_h++;
return [3 /*break*/, 14];
case 19:
/*
* Delete Localized Messages already translated and written
*/
return [4 /*yield*/, db_1.deleteLocalizedMessages(database_1.connectionToDatabase, database_1.SQL_DeleteLocalizedMessage, localized_1)];
case 20:
/*
* Delete Localized Messages already translated and written
*/
_j.sent();
return [2 /*return*/];
}
});
}); })];
case 1:
// Get i18n Localized Messages in database
_e.sent();
return [4 /*yield*/, db_1.getLocalizedMessages(database_1.connectionToDatabase, database_1.SQL_GetResourcesLocalizedMessages, packagesi18n_1)];
case 2:
/**
* Get resources Localized Messages in database
* Ignore localized messages that match with packagesi18n
*/
localized_1 = _e.sent();
// If there are not localized messages, finish the process
if (localized_1.length === 0) {
process.exit();
}
packagesResources = Object.keys(config.packages.resources);
_a = [];
for (_b in packagesResources)
_a.push(_b);
_i = 0;
_e.label = 3;
case 3:
if (!(_i < _a.length)) return [3 /*break*/, 15];
index = _a[_i];
filesForEachResourcesPath = new Map();
contentForEachFile = new Map();
// Get files of packages in config.packages.resources[index]
if (config.packages.resources[packagesResources[index]].length > 0) {
for (i = 0; i < config.packages.resources[packagesResources[index]].length; i++) {
filesForEachResourcesPath.set(config.packages.resources[packagesResources[index]][i], this.getFilesFromPath(config.packages.resources[packagesResources[index]][i], this.filesResourcesExtension));
}
}
// Read all files in "filesForEachResourcesPath"
if (filesForEachResourcesPath.size > 0) {
packagesResourcesArray = Array.from(filesForEachResourcesPath.keys());
// Fill map "contentForEachFile" with filename as key and respective content as value
for (i = 0; i < packagesResourcesArray.length; i++) {
// If we have more than one resource file to the same path
if (filesForEachResourcesPath.get(packagesResourcesArray[i]).length > 1) {
for (_c = 0, _d = filesForEachResourcesPath.get(packagesResourcesArray[i]); _c < _d.length; _c++) {
iterator = _d[_c];
file = packagesResourcesArray[i] + "\\" + iterator;
content = fs.readFileSync(file).toString();
contentForEachFile.set(file, content);
}
}
else {
file = packagesResourcesArray[i] + "\\" + filesForEachResourcesPath.get(packagesResourcesArray[i]);
content = fs.readFileSync(file).toString();
contentForEachFile.set(file, content);
}
}
}
return [4 /*yield*/, resourcesConverter.writeToFile(localized_1, contentForEachFile, this.filesResourcesExtension)];
case 4:
filesChanged = _e.sent();
if (!(filesChanged !== undefined && filesChanged.length > 0)) return [3 /*break*/, 14];
return [4 /*yield*/, tfs_1.getConnection().then(function (output) { return output; })];
case 5:
connection = _e.sent();
return [4 /*yield*/, tfs_1.getCurrentIteration(connection.iteration).then(function (output) { return output; })];
case 6:
currentIteration = _e.sent();
return [4 /*yield*/, tfs_1.checkIfUSAlreadyExist(connection.workItem, currentIteration.path)];
case 7:
userStoryId = _e.sent();
if (!(userStoryId === null)) return [3 /*break*/, 9];
return [4 /*yield*/, tfs_1.createUserStory(connection.workItem, currentIteration.path)];
case 8:
userStory_1 = _e.sent();
return [3 /*break*/, 10];
case 9:
userStory_1 = userStoryId;
_e.label = 10;
case 10:
repository = packagesResources[index];
return [4 /*yield*/, tfs_1.createCommitsAndPush(connection.git, packagesResources[index], filesChanged, repository)];
case 11:
commitedAndPushedBy = _e.sent();
return [4 /*yield*/, tfs_1.createPR(connection.git, repository, userStory_1)];
case 12:
pullRequestId = _e.sent();
return [4 /*yield*/, tfs_1.updatePR(connection.git, pullRequestId, repository, commitedAndPushedBy)];
case 13:
_e.sent();
_e.label = 14;
case 14:
_i++;
return [3 /*break*/, 3];
case 15:
// Finish the process
process.exit();
return [3 /*break*/, 17];
case 16:
err_1 = _e.sent();
console.log(err_1);
return [3 /*break*/, 17];
case 17: return [2 /*return*/];
}
});
});
};
return Program;
}());
exports.Program = Program;
/*
* Call method "Main()"
*/
var program = new Program();
program.Main();