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