feat: minor changes

This commit is contained in:
Javier Segarra 2025-01-17 23:34:57 +01:00
parent b53b03b81c
commit b96ec7ff71
3 changed files with 101 additions and 39 deletions

1
config/salix.local.yml Normal file
View File

@ -0,0 +1 @@
salixVersion: 24.40.0

View File

@ -18,24 +18,49 @@ module.exports = class Salix {
this._logInfo = []; this._logInfo = [];
this._salixVersion = null; this._salixVersion = null;
this._salixConfig = null; this._salixConfig = null;
this._token = null;
} }
emit() {} emit() {}
subscribe(externalListenerFunction) { subscribe(externalListenerFunction) {
this.emit = externalListenerFunction; this.emit = externalListenerFunction;
} }
async getToken() {
const { url: path, user, password } = this.conf.salix.api.login;
let response;
try {
response = await fetch(`${this.conf.salix.url}/${path}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
user,
password,
}),
});
response = await response.json();
} catch (error) {
console.error(error);
}
this.token = response.token;
}
async init(logger) { async init(logger) {
this.conf = logger.conf; this.conf = logger.conf;
const salixFileConfig = await this.loadConfig(); const salixFileConfig = await this.loadConfig();
try { try {
// No exite fichero almacenado. Solicito config y version // No exite fichero almacenado. Solicito config y version
// else {
this._salixConfig = salixFileConfig;
//Obtengo la version
await this.getSalixVersion(false);
this.salixVersion && (await this.handleVersion(salixFileConfig));
if (!salixFileConfig) await this.salixLogInfo(); if (!salixFileConfig) await this.salixLogInfo();
else { this._logInfo = salixFileConfig.log;
this._salixConfig = salixFileConfig; // }
//Obtengo la version
await this.getSalixVersion(false);
this.salixVersion && this.handleVersion(salixFileConfig);
this._logInfo = salixFileConfig.log;
}
} catch (error) {} } catch (error) {}
setInterval( setInterval(
@ -49,19 +74,24 @@ module.exports = class Salix {
return `${configDir}${CONFIG_FILENAME}`; return `${configDir}${CONFIG_FILENAME}`;
} }
async loadConfig() { async loadConfig() {
try { // try {
if (fs.existsSync(this.filePath)) { if (fs.existsSync(this.filePath)) {
const contenidoYAML = require(this.filePath); const contenidoYAML = require(this.filePath);
return Object.assign({}, contenidoYAML); return Object.assign({}, contenidoYAML);
} } else {
} catch (error) { new Error(`No existe el archivo de configuración`);
if (error.code === "ENOENT") { this.log("Creando archivo de configuración");
console.error(i18n.erroFs.noExists); fs.writeFileSync(this.filePath, require("js-yaml").dump({}), "utf-8");
} else { this.log("Archivo de configuración creado");
console.error(i18n.erroFs.noRead, error);
}
return null;
} }
// } catch (error) {
// if (error.code === "ENOENT") {
// console.error(i18n.erroFs.noExists);
// } else {
// console.error(i18n.erroFs.noRead, error);
// }
// return null;
// }
} }
async saveConfig(configValue) { async saveConfig(configValue) {
@ -123,21 +153,33 @@ module.exports = class Salix {
async fetch(path) { async fetch(path) {
try { try {
let salixCall = await fetch(`${this.conf.salix.url}/${path}`, { // Configuración de la llamada fetch
const fetchConfig = {
method: "GET", method: "GET",
}); headers: {
const response = JSON.parse(await salixCall.text()); "Content-Type": "application/json",
Authorization: `${this.token}`,
},
};
const salixCall = await fetch(
`${this.conf.salix.url}/${path}`,
fetchConfig
);
if (salixCall.status !== 200) { if (salixCall.status !== 200) {
console.error(response.error); throw new Error(response);
throw new Error(response.error.message);
} }
const responseText = await salixCall.text();
const response = JSON.parse(responseText);
const newVersion = salixCall.headers.get(SALIX_VERSION_HEADER); const newVersion = salixCall.headers.get(SALIX_VERSION_HEADER);
if(!this.salixVersion) this.salixVersion = newVersion; if (!this.salixVersion) {
if (this.salixVersion!==newVersion) { this.salixVersion = newVersion;
const isDeprecated = this.compareVersion( this.saveConfig();
newVersion, await this.salixLogInfo();
this.salixVersion }
); if (this.salixVersion !== newVersion) {
const isDeprecated = this.compareVersion(newVersion, this.salixVersion);
if (isDeprecated) { if (isDeprecated) {
this.salixVersion = newVersion; this.salixVersion = newVersion;
console.info(i18n.versionChanged); console.info(i18n.versionChanged);
@ -145,9 +187,9 @@ module.exports = class Salix {
} }
} }
return response; return response;
} catch ({ message }) { } catch (response) {
console.error(message); const message = response?.error?.message ?? response.message;
if (!this._salixConfig) throw new Error(message); this.log(message, 'error');
} }
} }
@ -161,6 +203,7 @@ module.exports = class Salix {
async salixLogInfo() { async salixLogInfo() {
this.log("REQUEST LOGINFO"); this.log("REQUEST LOGINFO");
await this.getToken();
let salixLogConfig = await this.fetch(this.conf.salix.api.logInfo); let salixLogConfig = await this.fetch(this.conf.salix.api.logInfo);
this._logInfo = salixLogConfig; this._logInfo = salixLogConfig;
@ -168,19 +211,31 @@ module.exports = class Salix {
this.log("LOGINFO REQUESTED"); this.log("LOGINFO REQUESTED");
} }
log(param) { log(param, type = 'log') {
console.log(`${Salix.name} - ${param}`); const colors = {
log: '\x1b[37m',
error: '\x1b[31m',
warn: '\x1b[33m' ,
}
console[type](colors[type], `${Salix.name} - ${type} - ${param}`);
} }
get logInfo() { get logInfo() {
return this._logInfo; return this._logInfo;
} }
get token() {
return this._token;
}
set token(token) {
this._token = token;
}
get salixVersion() { get salixVersion() {
return this._salixVersion; return this._salixVersion;
} }
set salixVersion(newVersion) { set salixVersion(newVersion) {
if(this._salixVersion && (this._salixVersion!== newVersion)) this.emit(newVersion); if (this._salixVersion && this._salixVersion !== newVersion)
this.emit(newVersion);
this._salixVersion = newVersion; this._salixVersion = newVersion;
} }

View File

@ -50,8 +50,14 @@ module.exports = class MyLogger {
console.log('Test mode enabled, just logging queries to console.'); console.log('Test mode enabled, just logging queries to console.');
console.log('Starting process.'); console.log('Starting process.');
await this.init(); try {
console.log('Process started.');
await this.init();
console.log('Process started.');
} catch (error) {
console.error(error);
}
} }
async stop() { async stop() {