@criticalmanufacturing/dev-i18n-transform
Version:
i18n <--> gettext transform
199 lines (198 loc) • 10.4 kB
JavaScript
;
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 mssql = require("mssql");
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
//#region Constants
exports.connectionToDatabase = {
server: config.database.server,
user: config.database.user.name,
password: config.database.user.password,
database: config.database.name,
connectionTimeout: 30000,
driver: "tedious",
stream: false
};
/** SQL Queries*/
/**
* Query to get i18n localized messages
*/
exports.SQL_Geti18nLocalizedMessages = "select l.name as LocalizedMessageName, c.name as CultureName,\n l.MessageText as LocalizedMessageText from T_LocalizedMessage L\n INNER JOIN T_Culture C ON (l.CultureId = c.CultureId)\n WHERE l.name LIKE ";
/**
* Query to get resources localized messages
*/
exports.SQL_GetResourcesLocalizedMessages = "select l.name as LocalizedMessageName, c.name as CultureName,\n l.MessageText as LocalizedMessageText from T_LocalizedMessage L\n INNER JOIN T_Culture C ON (l.CultureId = c.CultureId)\n WHERE l.name NOT LIKE ";
/**
* Query to delete localized messages using their name
*/
exports.SQL_DeleteLocalizedMessage = "delete from T_LocalizedMessage where name=";
//#endregion
var DatabaseManagement = /** @class */ (function () {
function DatabaseManagement() {
}
//#endregion
//#region Public Methods
/**
* Using the constant "connection_database", this method returns a connection to specified database
* If occurs an error, a message is displayed in console and a new attempt to connect is made
* @param connectionToDatabase (constant with information to access database)
*/
DatabaseManagement.prototype.getConnection = function (connectionToDatabase) {
return __awaiter(this, void 0, void 0, function () {
var _connectionPoll, err_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 4]);
return [4 /*yield*/, new mssql.ConnectionPool(connectionToDatabase).connect()];
case 1:
_connectionPoll = _b.sent();
return [2 /*return*/, _connectionPoll];
case 2:
err_1 = _b.sent();
console.log("Database Connection Failed! Bad Config: ", err_1);
_a = this;
return [4 /*yield*/, new mssql.ConnectionPool(connectionToDatabase).connect()];
case 3:
_a._connectionPoll = _b.sent();
return [2 /*return*/, this._connectionPoll];
case 4: return [2 /*return*/];
}
});
});
};
/**
* Get all localized messages in database and
* returns a array of type IData with all
* @param connectionToDatabase (constant with information to access database)
* @param sqlCommand (query to get all localized messages in database)
* @param packages (packages in config.packages.i18n)
*/
DatabaseManagement.prototype.getLocalizedMessages = function (connectionToDatabase, sqlCommand, packages) {
return __awaiter(this, void 0, void 0, function () {
var arrayTyped, filteredSqlCommand, connected, _a, _b, _i, index, result, _c, _d, entry;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
arrayTyped = [];
filteredSqlCommand = sqlCommand;
return [4 /*yield*/, this.getConnection(connectionToDatabase)];
case 1:
connected = _e.sent();
_a = [];
for (_b in packages)
_a.push(_b);
_i = 0;
_e.label = 2;
case 2:
if (!(_i < _a.length)) return [3 /*break*/, 5];
index = _a[_i];
filteredSqlCommand = sqlCommand + ("'" + packages[index] + ".%'");
return [4 /*yield*/, connected.request().query(filteredSqlCommand)];
case 3:
result = _e.sent();
if (result.recordset.length > 0) {
// Put each localized messaged received from database into an array
for (_c = 0, _d = result.recordset; _c < _d.length; _c++) {
entry = _d[_c];
arrayTyped.push({ cultureName: entry["CultureName"],
localizedMessageName: entry["LocalizedMessageName"],
localizedMessageText: entry["LocalizedMessageText"]
});
}
}
filteredSqlCommand = sqlCommand;
_e.label = 4;
case 4:
_i++;
return [3 /*break*/, 2];
case 5:
// Close connection to database
connected.close();
return [2 /*return*/, Promise.resolve(arrayTyped)];
}
});
});
};
/**
* Delete localized messages from database using their name
* @param connectionToDatabase (constant with information to access database)
* @param sqlCommand (query to delete localized messages using name)
* @param LocalizedMessages (array with all localized messages treated)
*/
DatabaseManagement.prototype.deleteLocalizedMessages = function (connectionToDatabase, sqlCommand, localizedMessages) {
return __awaiter(this, void 0, void 0, function () {
var connected, i, sqlDeleteCommand;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getConnection(connectionToDatabase)];
case 1:
connected = _a.sent();
i = 0;
_a.label = 2;
case 2:
if (!(i < localizedMessages.length)) return [3 /*break*/, 5];
sqlDeleteCommand = sqlCommand + " '" + localizedMessages[i].localizedMessageName + "'";
return [4 /*yield*/, connected.request().query(sqlDeleteCommand)];
case 3:
_a.sent();
_a.label = 4;
case 4:
i++;
return [3 /*break*/, 2];
case 5:
// Close connection to database
connected.close();
return [2 /*return*/];
}
});
});
};
return DatabaseManagement;
}());
exports.DatabaseManagement = DatabaseManagement;