Store and Load Config File automatically.
This commit is contained in:
parent
fe42a85204
commit
18f484348b
@ -8,11 +8,18 @@
|
||||
|
||||
/* eslint-disable no-fallthrough */
|
||||
|
||||
import { readFile } from "fs/promises";
|
||||
import { injectable } from "inversify";
|
||||
import { join } from "path";
|
||||
import {
|
||||
exportMethod,
|
||||
exportProperty
|
||||
} from "../../../lib/decorators/moduleDecorators";
|
||||
import {
|
||||
createFile,
|
||||
createPath,
|
||||
exists
|
||||
} from "../../../lib/helpers/fileMethods";
|
||||
import { getNopeLogger } from "../../../lib/logger/getLogger";
|
||||
import { InjectableNopeBaseModule } from "../../../lib/module/BaseModule.injectable";
|
||||
import { NopeObservable } from "../../../lib/observables/nopeObservable";
|
||||
@ -53,6 +60,7 @@ export class WaMOCarrierMapper
|
||||
})
|
||||
public carriers = new NopeObservable<string[]>();
|
||||
protected _carriers = new Set<string>();
|
||||
protected _file: string;
|
||||
|
||||
/**
|
||||
* The Logger of the Module.
|
||||
@ -77,11 +85,60 @@ export class WaMOCarrierMapper
|
||||
};
|
||||
|
||||
this._logger = getNopeLogger("wamo-carrier-mapper-" + this.identifier);
|
||||
|
||||
// Create a temporary File to store the Elements
|
||||
const _path = join(process.cwd(), "temp");
|
||||
await createPath(_path);
|
||||
|
||||
// Define the File Name. It is based on the identifier.
|
||||
this._file = join(_path, this.identifier + "_file.json");
|
||||
|
||||
// Now Test if the File exists:
|
||||
if (!(await exists(this._file))) {
|
||||
|
||||
this._logger.warn("No configuration File defined. Creating a new one under:",this._file);
|
||||
|
||||
this.carriers.setContent([]);
|
||||
this.mapping.setContent({});
|
||||
|
||||
// Create an Empty File.
|
||||
await this.storeConfig();
|
||||
}
|
||||
|
||||
// We now try to load the configuration
|
||||
const loadedConfig = JSON.parse(
|
||||
await readFile(this._file, {
|
||||
encoding: "utf-8"
|
||||
})
|
||||
);
|
||||
|
||||
// Load the Mapping and register the Carriers.
|
||||
for (const carrier in loadedConfig.carriers){
|
||||
await this.registerCarrier(carrier);
|
||||
}
|
||||
// Add the Products to the Carrier.
|
||||
for (const carrier in loadedConfig.mapping){
|
||||
await this.addProductToCarrier(carrier, loadedConfig.mapping[carrier]);
|
||||
}
|
||||
|
||||
await super.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function, wich will
|
||||
*
|
||||
* @memberof WaMOCarrierMapper
|
||||
*/
|
||||
@exportMethod({
|
||||
paramsHasNoCallback: true
|
||||
})
|
||||
public async storeConfig(){
|
||||
await createFile(this._file, JSON.stringify({
|
||||
carriers: this.carriers.getContent(),
|
||||
mapping: this.mapping.getContent()
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper Function to Store new Carriers. If the carrier
|
||||
* is already present => Nothing happens
|
||||
@ -93,16 +150,33 @@ export class WaMOCarrierMapper
|
||||
@exportMethod({
|
||||
paramsHasNoCallback: true
|
||||
})
|
||||
public async registerCarrier(carrier: string) {
|
||||
public async registerCarrier(carrier: string): Promise<void> {
|
||||
// Add the Carrier if required:
|
||||
if (!this._carriers.has(carrier)) {
|
||||
this._carriers.add(carrier);
|
||||
this.carriers.setContent(Array.from(this._carriers));
|
||||
|
||||
this._logger.info(`New Carrier with ID "${carrier}" has been detected`);
|
||||
|
||||
// Update the Config.
|
||||
await this.storeConfig();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function, to check if there exists a mapping for the carrier or not.
|
||||
*
|
||||
* @param {string} carrier
|
||||
* @return {*} {Promise<boolean>}
|
||||
* @memberof WaMOCarrierMapper
|
||||
*/
|
||||
@exportMethod({
|
||||
paramsHasNoCallback: true
|
||||
})
|
||||
public async isProductLinkedToCarrier(carrier: string): Promise<boolean> {
|
||||
return this.mapping.getContent()[carrier] !== undefined;
|
||||
}
|
||||
|
||||
@exportMethod({
|
||||
paramsHasNoCallback: true
|
||||
})
|
||||
@ -112,6 +186,9 @@ export class WaMOCarrierMapper
|
||||
// Delete all Carriers
|
||||
this._carriers.clear();
|
||||
this.carriers.setContent([]);
|
||||
|
||||
// Update the Config.
|
||||
await this.storeConfig();
|
||||
}
|
||||
|
||||
@exportMethod({
|
||||
@ -132,6 +209,9 @@ export class WaMOCarrierMapper
|
||||
|
||||
// Add the Carrier
|
||||
await this.registerCarrier(carrier);
|
||||
|
||||
// Update the Config.
|
||||
await this.storeConfig();
|
||||
}
|
||||
|
||||
@exportMethod({
|
||||
@ -146,6 +226,9 @@ export class WaMOCarrierMapper
|
||||
|
||||
// Add the Carrier
|
||||
await this.registerCarrier(carrier);
|
||||
|
||||
// Update the Config.
|
||||
await this.storeConfig();
|
||||
}
|
||||
|
||||
@exportMethod({
|
||||
|
Loading…
Reference in New Issue
Block a user