@adaas/a-utils 0.0.9 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +21 -15
- package/dist/index.js +53 -22
- package/dist/index.js.map +1 -1
- package/dist/src/constants/errors.constants.d.ts +0 -65
- package/dist/src/constants/errors.constants.js +0 -72
- package/dist/src/constants/errors.constants.js.map +1 -1
- package/dist/src/lib/A-Channel/A-Channel.component.d.ts +3 -0
- package/dist/src/lib/A-Channel/A-Channel.component.js +8 -0
- package/dist/src/lib/A-Channel/A-Channel.component.js.map +1 -0
- package/dist/src/lib/A-Channel/A-Channel.error.d.ts +3 -0
- package/dist/src/lib/A-Channel/A-Channel.error.js +8 -0
- package/dist/src/lib/A-Channel/A-Channel.error.js.map +1 -0
- package/dist/src/lib/A-Channel/A-Channel.types.d.ts +0 -0
- package/dist/src/lib/A-Channel/A-Channel.types.js +2 -0
- package/dist/src/lib/A-Channel/A-Channel.types.js.map +1 -0
- package/dist/src/lib/A-Command/A-Command.constants.d.ts +22 -0
- package/dist/src/lib/A-Command/A-Command.constants.js +28 -0
- package/dist/src/lib/A-Command/A-Command.constants.js.map +1 -0
- package/dist/src/lib/A-Command/A-Command.entity.d.ts +133 -0
- package/dist/src/lib/A-Command/A-Command.entity.js +273 -0
- package/dist/src/lib/A-Command/A-Command.entity.js.map +1 -0
- package/dist/src/lib/A-Command/A-Command.error.d.ts +3 -0
- package/dist/src/lib/A-Command/A-Command.error.js +8 -0
- package/dist/src/lib/A-Command/A-Command.error.js.map +1 -0
- package/dist/src/lib/A-Command/A-Command.types.d.ts +73 -0
- package/dist/src/lib/A-Command/A-Command.types.js +4 -0
- package/dist/src/lib/A-Command/A-Command.types.js.map +1 -0
- package/dist/src/lib/A-Config/A-Config.constants.d.ts +3 -0
- package/dist/src/lib/A-Config/A-Config.constants.js +6 -0
- package/dist/src/lib/A-Config/A-Config.constants.js.map +1 -0
- package/dist/src/lib/A-Config/A-Config.container.d.ts +8 -0
- package/dist/src/lib/A-Config/A-Config.container.js +75 -0
- package/dist/src/lib/A-Config/A-Config.container.js.map +1 -0
- package/dist/src/lib/A-Config/A-Config.context.d.ts +29 -0
- package/dist/src/lib/A-Config/A-Config.context.js +63 -0
- package/dist/src/lib/A-Config/A-Config.context.js.map +1 -0
- package/dist/src/lib/A-Config/A-Config.error.d.ts +4 -0
- package/dist/src/lib/A-Config/A-Config.error.js +9 -0
- package/dist/src/lib/A-Config/A-Config.error.js.map +1 -0
- package/dist/src/lib/A-Config/A-Config.types.d.ts +19 -0
- package/dist/src/lib/A-Config/A-Config.types.js +7 -0
- package/dist/src/lib/A-Config/A-Config.types.js.map +1 -0
- package/dist/src/lib/A-Config/components/ConfigReader.component.d.ts +30 -0
- package/dist/src/lib/A-Config/components/ConfigReader.component.js +85 -0
- package/dist/src/lib/A-Config/components/ConfigReader.component.js.map +1 -0
- package/dist/src/lib/A-Config/components/ENVConfigReader.component.d.ts +10 -0
- package/dist/src/lib/A-Config/components/ENVConfigReader.component.js +37 -0
- package/dist/src/lib/A-Config/components/ENVConfigReader.component.js.map +1 -0
- package/dist/src/lib/A-Config/components/FileConfigReader.component.d.ts +11 -0
- package/dist/src/lib/A-Config/components/FileConfigReader.component.js +47 -0
- package/dist/src/lib/A-Config/components/FileConfigReader.component.js.map +1 -0
- package/dist/src/lib/A-Logger/A-Logger.component.d.ts +29 -0
- package/dist/src/lib/A-Logger/A-Logger.component.js +152 -0
- package/dist/src/lib/A-Logger/A-Logger.component.js.map +1 -0
- package/dist/src/lib/A-Logger/A-Logger.types.d.ts +0 -0
- package/dist/src/lib/A-Logger/A-Logger.types.js +2 -0
- package/dist/src/lib/A-Logger/A-Logger.types.js.map +1 -0
- package/dist/src/lib/A-Memory/A-Memory.context.d.ts +64 -0
- package/dist/src/lib/A-Memory/A-Memory.context.js +105 -0
- package/dist/src/lib/A-Memory/A-Memory.context.js.map +1 -0
- package/dist/src/lib/A-Polyfill/A-Polyfill.component.d.ts +20 -0
- package/dist/src/lib/A-Polyfill/A-Polyfill.component.js +53 -0
- package/dist/src/lib/A-Polyfill/A-Polyfill.component.js.map +1 -0
- package/dist/src/{global/A_Polyfills.d.ts → lib/A-Polyfill/A-Polyfill.types.d.ts} +1 -13
- package/dist/src/lib/A-Polyfill/A-Polyfill.types.js +2 -0
- package/dist/src/lib/A-Polyfill/A-Polyfill.types.js.map +1 -0
- package/dist/src/lib/A-Polyfill/A-Polyfills.class.d.ts +10 -0
- package/dist/src/{global/A_Polyfills.js → lib/A-Polyfill/A-Polyfills.class.js} +4 -4
- package/dist/src/lib/A-Polyfill/A-Polyfills.class.js.map +1 -0
- package/dist/src/{global/A_Deferred.class.d.ts → lib/A-Schedule/A-Deferred.class.d.ts} +4 -0
- package/dist/src/{global/A_Deferred.class.js → lib/A-Schedule/A-Deferred.class.js} +5 -1
- package/dist/src/lib/A-Schedule/A-Deferred.class.js.map +1 -0
- package/dist/src/lib/A-Schedule/A-Schedule.component.d.ts +57 -0
- package/dist/src/lib/A-Schedule/A-Schedule.component.js +49 -0
- package/dist/src/lib/A-Schedule/A-Schedule.component.js.map +1 -0
- package/dist/src/{types/A_ScheduleObject.types.d.ts → lib/A-Schedule/A-Schedule.types.d.ts} +2 -1
- package/dist/src/{types/ASEID.types.js → lib/A-Schedule/A-Schedule.types.js} +1 -3
- package/dist/src/lib/A-Schedule/A-Schedule.types.js.map +1 -0
- package/dist/src/lib/A-Schedule/A-ScheduleObject.class.d.ts +29 -0
- package/dist/src/{global/A_ScheduleObject.class.js → lib/A-Schedule/A-ScheduleObject.class.js} +25 -5
- package/dist/src/lib/A-Schedule/A-ScheduleObject.class.js.map +1 -0
- package/index.ts +33 -58
- package/jest.config.ts +3 -3
- package/package.json +6 -6
- package/src/constants/errors.constants.ts +0 -78
- package/src/lib/A-Channel/A-Channel.component.ts +8 -0
- package/src/lib/A-Channel/A-Channel.error.ts +6 -0
- package/src/lib/A-Channel/A-Channel.types.ts +0 -0
- package/src/lib/A-Command/A-Command.constants.ts +31 -0
- package/src/lib/A-Command/A-Command.entity.ts +327 -0
- package/src/lib/A-Command/A-Command.error.ts +6 -0
- package/src/lib/A-Command/A-Command.types.ts +100 -0
- package/src/lib/A-Config/A-Config.constants.ts +13 -0
- package/src/lib/A-Config/A-Config.container.ts +63 -0
- package/src/lib/A-Config/A-Config.context.ts +117 -0
- package/src/lib/A-Config/A-Config.error.ts +7 -0
- package/src/lib/A-Config/A-Config.types.ts +26 -0
- package/src/lib/A-Config/components/ConfigReader.component.ts +67 -0
- package/src/lib/A-Config/components/ENVConfigReader.component.ts +31 -0
- package/src/lib/A-Config/components/FileConfigReader.component.ts +42 -0
- package/src/lib/A-Logger/A-Logger.component.ts +190 -0
- package/src/lib/A-Logger/A-Logger.types.ts +0 -0
- package/src/lib/A-Memory/A-Memory.context.ts +115 -0
- package/src/lib/A-Polyfill/A-Polyfill.component.ts +45 -0
- package/src/lib/A-Polyfill/A-Polyfill.types.ts +10 -0
- package/src/{global/A_Polyfills.ts → lib/A-Polyfill/A-Polyfills.class.ts} +2 -16
- package/src/{global/A_Deferred.class.ts → lib/A-Schedule/A-Deferred.class.ts} +4 -0
- package/src/lib/A-Schedule/A-Schedule.component.ts +89 -0
- package/src/{types/A_ScheduleObject.types.ts → lib/A-Schedule/A-Schedule.types.ts} +7 -2
- package/src/{global/A_ScheduleObject.class.ts → lib/A-Schedule/A-ScheduleObject.class.ts} +25 -7
- package/tests/A-Channel.test.ts +16 -0
- package/tests/A-Command.test.ts +133 -0
- package/tests/A-Config.test.ts +185 -0
- package/tests/A-Polyfill.test.ts +67 -0
- package/tests/A-Schedule.test.ts +84 -0
- package/tests/jest.setup.ts +35 -0
- package/tsconfig.build.json +56 -0
- package/tsconfig.json +2 -2
- package/dist/src/global/ASEID.class.d.ts +0 -77
- package/dist/src/global/ASEID.class.js +0 -129
- package/dist/src/global/ASEID.class.js.map +0 -1
- package/dist/src/global/A_Deferred.class.js.map +0 -1
- package/dist/src/global/A_Entity.class.d.ts +0 -49
- package/dist/src/global/A_Entity.class.js +0 -94
- package/dist/src/global/A_Entity.class.js.map +0 -1
- package/dist/src/global/A_Error.class.d.ts +0 -13
- package/dist/src/global/A_Error.class.js +0 -63
- package/dist/src/global/A_Error.class.js.map +0 -1
- package/dist/src/global/A_Polyfills.js.map +0 -1
- package/dist/src/global/A_ScheduleObject.class.d.ts +0 -9
- package/dist/src/global/A_ScheduleObject.class.js.map +0 -1
- package/dist/src/global/A_ServerError.class.d.ts +0 -13
- package/dist/src/global/A_ServerError.class.js +0 -57
- package/dist/src/global/A_ServerError.class.js.map +0 -1
- package/dist/src/helpers/A_Common.helper.d.ts +0 -42
- package/dist/src/helpers/A_Common.helper.js +0 -214
- package/dist/src/helpers/A_Common.helper.js.map +0 -1
- package/dist/src/helpers/A_ID.helper.d.ts +0 -17
- package/dist/src/helpers/A_ID.helper.js +0 -26
- package/dist/src/helpers/A_ID.helper.js.map +0 -1
- package/dist/src/helpers/A_Schedule.helper.d.ts +0 -6
- package/dist/src/helpers/A_Schedule.helper.js +0 -21
- package/dist/src/helpers/A_Schedule.helper.js.map +0 -1
- package/dist/src/types/ASEID.types.d.ts +0 -65
- package/dist/src/types/ASEID.types.js.map +0 -1
- package/dist/src/types/A_Common.types.d.ts +0 -59
- package/dist/src/types/A_Common.types.js +0 -3
- package/dist/src/types/A_Common.types.js.map +0 -1
- package/dist/src/types/A_Entity.types.d.ts +0 -13
- package/dist/src/types/A_Entity.types.js +0 -4
- package/dist/src/types/A_Entity.types.js.map +0 -1
- package/dist/src/types/A_Error.type.d.ts +0 -7
- package/dist/src/types/A_Error.type.js +0 -3
- package/dist/src/types/A_Error.type.js.map +0 -1
- package/dist/src/types/A_Error.types.d.ts +0 -7
- package/dist/src/types/A_Error.types.js +0 -3
- package/dist/src/types/A_Error.types.js.map +0 -1
- package/dist/src/types/A_ScheduleObject.types.js +0 -3
- package/dist/src/types/A_ScheduleObject.types.js.map +0 -1
- package/dist/src/types/A_ServerError.types.d.ts +0 -4
- package/dist/src/types/A_ServerError.types.js +0 -3
- package/dist/src/types/A_ServerError.types.js.map +0 -1
- package/src/global/ASEID.class.ts +0 -208
- package/src/global/A_Error.class.ts +0 -88
- package/src/global/A_ServerError.class.ts +0 -70
- package/src/helpers/A_Common.helper.ts +0 -265
- package/src/helpers/A_ID.helper.ts +0 -32
- package/src/helpers/A_Schedule.helper.ts +0 -25
- package/src/types/ASEID.types.ts +0 -86
- package/src/types/A_Common.types.ts +0 -111
- package/src/types/A_Entity.types.ts +0 -17
- package/src/types/A_Error.types.ts +0 -10
- package/src/types/A_ServerError.types.ts +0 -7
- package/tests/default.test.ts +0 -173
- package/tests/polyfill.test.ts +0 -37
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { A_Component, A_Concept, A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY, A_Inject, A_Scope } from "@adaas/a-concept";
|
|
2
|
+
import { A_Config } from "../A-Config.context";
|
|
3
|
+
import { A_CONSTANTS__CONFIG_ENV_VARIABLES_ARRAY } from "../A-Config.constants";
|
|
4
|
+
import { A_Polyfill } from "../../A-Polyfill/A-Polyfill.component";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Config Reader
|
|
8
|
+
*/
|
|
9
|
+
export class ConfigReader extends A_Component {
|
|
10
|
+
|
|
11
|
+
constructor(
|
|
12
|
+
@A_Inject(A_Scope) protected scope: A_Scope,
|
|
13
|
+
@A_Inject(A_Polyfill) protected polyfill: A_Polyfill,
|
|
14
|
+
) {
|
|
15
|
+
super();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@A_Concept.Load()
|
|
20
|
+
async inject(
|
|
21
|
+
@A_Inject(A_Config) config: A_Config,
|
|
22
|
+
) {
|
|
23
|
+
|
|
24
|
+
const data = await this.read([
|
|
25
|
+
...config.CONFIG_PROPERTIES,
|
|
26
|
+
...A_CONSTANTS__DEFAULT_ENV_VARIABLES_ARRAY,
|
|
27
|
+
...A_CONSTANTS__CONFIG_ENV_VARIABLES_ARRAY
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
config.set(data);
|
|
31
|
+
|
|
32
|
+
const rootDir = await this.getProjectRoot();
|
|
33
|
+
|
|
34
|
+
config.set('A_CONCEPT_ROOT_FOLDER', rootDir);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Get the configuration property by Name
|
|
40
|
+
* @param property
|
|
41
|
+
*/
|
|
42
|
+
resolve<_ReturnType = any>(property: string): _ReturnType {
|
|
43
|
+
return property as _ReturnType;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* This method reads the configuration and sets the values to the context
|
|
48
|
+
*
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
async read<T extends string>(
|
|
52
|
+
variables: Array<T> = []
|
|
53
|
+
): Promise<Record<T, any>> {
|
|
54
|
+
return {} as Record<T, any>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Finds the root directory of the project by locating the folder containing package.json
|
|
60
|
+
*
|
|
61
|
+
* @param {string} startPath - The initial directory to start searching from (default is __dirname)
|
|
62
|
+
* @returns {string|null} - The path to the root directory or null if package.json is not found
|
|
63
|
+
*/
|
|
64
|
+
protected async getProjectRoot(startPath = __dirname) {
|
|
65
|
+
return process.cwd();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { A_FormatterHelper } from "@adaas/a-concept";
|
|
2
|
+
import { ConfigReader } from "./ConfigReader.component";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class ENVConfigReader extends ConfigReader {
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the configuration property Name
|
|
10
|
+
* @param property
|
|
11
|
+
*/
|
|
12
|
+
getConfigurationProperty_ENV_Alias(property: string): string {
|
|
13
|
+
return A_FormatterHelper.toUpperSnakeCase(property);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
resolve<_ReturnType = any>(property: string): _ReturnType {
|
|
18
|
+
return process.env[this.getConfigurationProperty_ENV_Alias(property)] as _ReturnType;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async read<T extends string>(variables: Array<T> = []): Promise<Record<T, any>> {
|
|
23
|
+
const config: Record<T, any> = {} as Record<T, any>;
|
|
24
|
+
|
|
25
|
+
variables.forEach(variable => {
|
|
26
|
+
config[variable] = this.resolve(variable);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return config;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { A_Context, A_FormatterHelper } from "@adaas/a-concept";
|
|
2
|
+
import { ConfigReader } from "./ConfigReader.component";
|
|
3
|
+
|
|
4
|
+
export class FileConfigReader extends ConfigReader {
|
|
5
|
+
|
|
6
|
+
private FileData: Map<string, any> = new Map<string, any>();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the configuration property Name
|
|
10
|
+
* @param property
|
|
11
|
+
*/
|
|
12
|
+
getConfigurationProperty_File_Alias(property: string): string {
|
|
13
|
+
return A_FormatterHelper.toCamelCase(property);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
resolve<_ReturnType = any>(property: string): _ReturnType {
|
|
18
|
+
return this.FileData.get(this.getConfigurationProperty_File_Alias(property)) as _ReturnType;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async read<T extends string>(
|
|
23
|
+
variables?: Array<T>
|
|
24
|
+
): Promise<Record<T, any>> {
|
|
25
|
+
|
|
26
|
+
const fs = await this.polyfill.fs();
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const data = fs.readFileSync(`${A_Context.concept}.conf.json`, 'utf8');
|
|
30
|
+
|
|
31
|
+
const config: Record<T, any> = JSON.parse(data);
|
|
32
|
+
|
|
33
|
+
this.FileData = new Map(Object.entries(config));
|
|
34
|
+
|
|
35
|
+
return config;
|
|
36
|
+
|
|
37
|
+
} catch (error) {
|
|
38
|
+
// this.context.Logger.error(error);
|
|
39
|
+
return {} as Record<T, any>;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { A_Component, A_Error, A_Inject, A_Scope } from "@adaas/a-concept";
|
|
2
|
+
import { A_Config } from "../A-Config/A-Config.context";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class A_Logger extends A_Component {
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
@A_Inject(A_Scope) protected scope: A_Scope,
|
|
10
|
+
@A_Inject(A_Config) protected config: A_Config
|
|
11
|
+
) {
|
|
12
|
+
super();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
readonly colors = {
|
|
16
|
+
green: '32',
|
|
17
|
+
blue: '34',
|
|
18
|
+
red: '31',
|
|
19
|
+
yellow: '33',
|
|
20
|
+
gray: '90',
|
|
21
|
+
magenta: '35',
|
|
22
|
+
cyan: '36',
|
|
23
|
+
white: '37',
|
|
24
|
+
pink: '95',
|
|
25
|
+
} as const
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
get scopeLength() {
|
|
29
|
+
return this.scope.name.length;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
compile(
|
|
34
|
+
color: keyof typeof this.colors,
|
|
35
|
+
...args: any[]
|
|
36
|
+
): Array<string> {
|
|
37
|
+
|
|
38
|
+
return [
|
|
39
|
+
`\x1b[${this.colors[color]}m[${this.scope.name}] |${this.getTime()}|`,
|
|
40
|
+
(
|
|
41
|
+
args.length > 1
|
|
42
|
+
? '\n' + `${' '.repeat(this.scopeLength + 3)}|-------------------------------`
|
|
43
|
+
: ''
|
|
44
|
+
|
|
45
|
+
),
|
|
46
|
+
...(args
|
|
47
|
+
.map((arg, i) => {
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
switch (true) {
|
|
51
|
+
case arg instanceof A_Error:
|
|
52
|
+
return this.compile_A_Error(arg);
|
|
53
|
+
|
|
54
|
+
case arg instanceof Error:
|
|
55
|
+
return this.compile_Error(arg);
|
|
56
|
+
|
|
57
|
+
case typeof arg === 'object':
|
|
58
|
+
return JSON.stringify(arg, null, 2)
|
|
59
|
+
.replace(/\n/g, '\n' + `${' '.repeat(this.scopeLength + 3)}| `);
|
|
60
|
+
|
|
61
|
+
default:
|
|
62
|
+
return String(
|
|
63
|
+
((i > 0 || args.length > 1) ? '\n' : '')
|
|
64
|
+
+ arg)
|
|
65
|
+
.replace(/\n/g, '\n' + `${' '.repeat(this.scopeLength + 3)}| `)
|
|
66
|
+
}
|
|
67
|
+
})),
|
|
68
|
+
(
|
|
69
|
+
args.length > 1
|
|
70
|
+
? '\n' + `${' '.repeat(this.scopeLength + 3)}|-------------------------------\x1b[0m`
|
|
71
|
+
: '\x1b[0m'
|
|
72
|
+
)
|
|
73
|
+
]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
protected get allowedToLog() {
|
|
77
|
+
return this.config.get('CONFIG_VERBOSE') !== undefined
|
|
78
|
+
&& this.config.get('CONFIG_VERBOSE') !== 'false'
|
|
79
|
+
&& this.config.get('CONFIG_VERBOSE') !== false
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
log(
|
|
84
|
+
color: keyof typeof this.colors,
|
|
85
|
+
...args: any[]
|
|
86
|
+
)
|
|
87
|
+
log(
|
|
88
|
+
...args: any[]
|
|
89
|
+
)
|
|
90
|
+
log(
|
|
91
|
+
param1: any,
|
|
92
|
+
...args: any[]
|
|
93
|
+
) {
|
|
94
|
+
if (!this.allowedToLog)
|
|
95
|
+
return;
|
|
96
|
+
|
|
97
|
+
if (typeof param1 === 'string' && this.colors[param1]) {
|
|
98
|
+
console.log(...this.compile(param1 as keyof typeof this.colors, ...args));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
console.log(...this.compile('blue', param1, ...args));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
warning(...args) {
|
|
107
|
+
if (!this.allowedToLog)
|
|
108
|
+
return;
|
|
109
|
+
|
|
110
|
+
console.log(...this.compile('yellow', ...args));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
error(...args) {
|
|
114
|
+
if (this.config.get('CONFIG_IGNORE_ERRORS'))
|
|
115
|
+
return;
|
|
116
|
+
|
|
117
|
+
return console.log(...this.compile('red', ...args));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
protected log_A_Error(error: A_Error) {
|
|
122
|
+
const time = this.getTime();
|
|
123
|
+
|
|
124
|
+
console.log(`\x1b[31m[${this.scope.name}] |${time}| ERROR ${error.code}
|
|
125
|
+
${' '.repeat(this.scopeLength + 3)}| ${error.message}
|
|
126
|
+
${' '.repeat(this.scopeLength + 3)}| ${error.description}
|
|
127
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
128
|
+
${' '.repeat(this.scopeLength + 3)}| ${error.stack?.split('\n').map((line, index) => index === 0 ? line : `${' '.repeat(this.scopeLength + 3)}| ${line}`).join('\n') || 'No stack trace'}
|
|
129
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
130
|
+
\x1b[0m`
|
|
131
|
+
+ (error.originalError ? `\x1b[31m${' '.repeat(this.scopeLength + 3)}| Wrapped From ${error.originalError.message}
|
|
132
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
133
|
+
${' '.repeat(this.scopeLength + 3)}| ${error.originalError.stack?.split('\n').map((line, index) => index === 0 ? line : `${' '.repeat(this.scopeLength + 3)}| ${line}`).join('\n') || 'No stack trace'}
|
|
134
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
135
|
+
\x1b[0m`: '')
|
|
136
|
+
+ (error.link ? `\x1b[31m${' '.repeat(this.scopeLength + 3)}| Read in docs: ${error.link}
|
|
137
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
138
|
+
\x1b[0m`: ''));
|
|
139
|
+
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
protected compile_A_Error(error: A_Error): string {
|
|
143
|
+
const time = this.getTime();
|
|
144
|
+
|
|
145
|
+
return '\n' +
|
|
146
|
+
|
|
147
|
+
`${' '.repeat(this.scopeLength + 3)}|-------------------------------` +
|
|
148
|
+
'\n' +
|
|
149
|
+
`${' '.repeat(this.scopeLength + 3)}| Error: | ${error.code}
|
|
150
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
151
|
+
${' '.repeat(this.scopeLength + 3)}|${' '.repeat(10)}| ${error.message}
|
|
152
|
+
${' '.repeat(this.scopeLength + 3)}|${' '.repeat(10)}| ${error.description}
|
|
153
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
154
|
+
${' '.repeat(this.scopeLength + 3)}| ${error.stack?.split('\n').map((line, index) => index === 0 ? line : `${' '.repeat(this.scopeLength + 3)}| ${line}`).join('\n') || 'No stack trace'}
|
|
155
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------`
|
|
156
|
+
+
|
|
157
|
+
(error.originalError ? `${' '.repeat(this.scopeLength + 3)}| Wrapped From ${error.originalError.message}
|
|
158
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------
|
|
159
|
+
${' '.repeat(this.scopeLength + 3)}| ${error.originalError.stack?.split('\n').map((line, index) => index === 0 ? line : `${' '.repeat(this.scopeLength + 3)}| ${line}`).join('\n') || 'No stack trace'}
|
|
160
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------` : '')
|
|
161
|
+
+
|
|
162
|
+
(error.link ? `${' '.repeat(this.scopeLength + 3)}| Read in docs: ${error.link}
|
|
163
|
+
${' '.repeat(this.scopeLength + 3)}|-------------------------------` : '');
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
protected compile_Error(error: Error): string {
|
|
169
|
+
return JSON.stringify({
|
|
170
|
+
name: error.name,
|
|
171
|
+
message: error.message,
|
|
172
|
+
stack: error.stack?.split('\n')
|
|
173
|
+
.map((line, index) => index === 0 ? line : `${' '.repeat(this.scopeLength + 3)}| ${line}`)
|
|
174
|
+
.join('\n')
|
|
175
|
+
|
|
176
|
+
}, null, 2)
|
|
177
|
+
.replace(/\n/g, '\n' + `${' '.repeat(this.scopeLength + 3)}| `)
|
|
178
|
+
.replace(/\\n/g, '\n')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
protected getTime() {
|
|
184
|
+
const now = new Date();
|
|
185
|
+
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
186
|
+
const seconds = String(now.getSeconds()).padStart(2, '0');
|
|
187
|
+
const milliseconds = String(now.getMilliseconds()).padStart(4, '0');
|
|
188
|
+
return `${minutes}:${seconds}:${milliseconds}`;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { A_Error, A_Fragment } from "@adaas/a-concept";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class A_Memory<
|
|
5
|
+
_MemoryType extends Record<string, any> = Record<string, any>,
|
|
6
|
+
_SerializedType extends Record<string, any> = Record<string, any>
|
|
7
|
+
> extends A_Fragment {
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Internal storage of all intermediate values
|
|
12
|
+
*/
|
|
13
|
+
protected _memory: Map<keyof _MemoryType, _MemoryType[keyof _MemoryType]>;
|
|
14
|
+
/**
|
|
15
|
+
* Errors encountered during the execution
|
|
16
|
+
*/
|
|
17
|
+
protected _errors: Set<A_Error>;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Memory object that allows to store intermediate values and errors
|
|
23
|
+
*
|
|
24
|
+
* @param initialValues
|
|
25
|
+
*/
|
|
26
|
+
constructor(initialValues?: _MemoryType) {
|
|
27
|
+
super();
|
|
28
|
+
this._memory = new Map(Object.entries(initialValues || {}));
|
|
29
|
+
this._errors = new Set();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
get Errors(): Set<A_Error> | undefined {
|
|
34
|
+
return this._errors.size > 0 ? this._errors : undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Verifies that all required keys are present in the proxy values
|
|
40
|
+
*
|
|
41
|
+
* @param requiredKeys
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
async verifyPrerequisites(
|
|
45
|
+
requiredKeys: Array<keyof _MemoryType>
|
|
46
|
+
): Promise<boolean> {
|
|
47
|
+
return requiredKeys.every(key => this._memory.has(key));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Adds an error to the context
|
|
52
|
+
*
|
|
53
|
+
* @param error
|
|
54
|
+
*/
|
|
55
|
+
async error(error: A_Error): Promise<void> {
|
|
56
|
+
this._errors.add(error);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Saves a value in the context memory
|
|
61
|
+
*
|
|
62
|
+
* @param key
|
|
63
|
+
* @param value
|
|
64
|
+
*/
|
|
65
|
+
async set<K extends keyof _MemoryType>(
|
|
66
|
+
/**
|
|
67
|
+
* Key to save the value under
|
|
68
|
+
*/
|
|
69
|
+
key: K,
|
|
70
|
+
/**
|
|
71
|
+
* Value to save
|
|
72
|
+
*/
|
|
73
|
+
value: _MemoryType[K]
|
|
74
|
+
): Promise<void> {
|
|
75
|
+
this._memory.set(key, value);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Removes a value from the context memory by key
|
|
81
|
+
*
|
|
82
|
+
* @param key
|
|
83
|
+
*/
|
|
84
|
+
async drop(key: keyof _MemoryType): Promise<void> {
|
|
85
|
+
this._memory.delete(key);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Clears all stored values in the context memory
|
|
90
|
+
*/
|
|
91
|
+
async clear(): Promise<void> {
|
|
92
|
+
this._memory.clear();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Converts all stored values to a plain object
|
|
98
|
+
*
|
|
99
|
+
* [!] By default uses all saved in memory values
|
|
100
|
+
*
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
toJSON(): _SerializedType {
|
|
104
|
+
const obj: Record<string, any> = {};
|
|
105
|
+
|
|
106
|
+
this._memory.forEach((value, key) => {
|
|
107
|
+
obj[key as string] =
|
|
108
|
+
typeof value === 'object' && value !== null && 'toJSON' in value && typeof value.toJSON === 'function'
|
|
109
|
+
? value.toJSON()
|
|
110
|
+
: value;
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return obj as _SerializedType;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { A_Component } from "@adaas/a-concept";
|
|
2
|
+
import { A_PolyfillClass } from "./A-Polyfills.class";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class A_Polyfill extends A_Component {
|
|
7
|
+
|
|
8
|
+
protected _polyfill!: A_PolyfillClass;
|
|
9
|
+
|
|
10
|
+
async load() {
|
|
11
|
+
this._polyfill = new A_PolyfillClass();
|
|
12
|
+
|
|
13
|
+
await this._polyfill.fs();
|
|
14
|
+
await this._polyfill.crypto();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Allows to use the 'fs' polyfill methods regardless of the environment
|
|
20
|
+
* This method loads the 'fs' polyfill and returns its instance
|
|
21
|
+
*
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
async fs() {
|
|
25
|
+
if (!this._polyfill) {
|
|
26
|
+
await this.load();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return await this._polyfill.fs();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Allows to use the 'crypto' polyfill methods regardless of the environment
|
|
34
|
+
* This method loads the 'crypto' polyfill and returns its instance
|
|
35
|
+
*
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
async crypto() {
|
|
39
|
+
if (!this._polyfill) {
|
|
40
|
+
await this.load();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return await this._polyfill.crypto();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface Ifspolyfill {
|
|
2
|
+
readFileSync: (path: string, encoding: string) => string;
|
|
3
|
+
existsSync: (path: string) => boolean;
|
|
4
|
+
createReadStream: (path: string, options?: BufferEncoding) => any
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface IcryptoInterface {
|
|
8
|
+
createTextHash(text: string, algorithm: string): Promise<string>
|
|
9
|
+
createFileHash(filePath: string, algorithm: string): Promise<string>
|
|
10
|
+
}
|
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
interface Ifspolyfill {
|
|
2
|
-
readFileSync: (path: string, encoding: string) => string;
|
|
3
|
-
existsSync: (path: string) => boolean;
|
|
4
|
-
createReadStream: (path: string, options?: BufferEncoding) => any
|
|
5
|
-
}
|
|
6
1
|
|
|
7
|
-
interface ICryptoInterface {
|
|
8
|
-
createTextHash(text: string, algorithm: string): Promise<string>
|
|
9
|
-
createFileHash(filePath: string, algorithm: string): Promise<string>
|
|
10
|
-
}
|
|
11
2
|
|
|
12
3
|
|
|
13
|
-
class
|
|
4
|
+
export class A_PolyfillClass {
|
|
14
5
|
|
|
15
6
|
private _fs!: Ifspolyfill;
|
|
16
|
-
private _crypto!:
|
|
7
|
+
private _crypto!: IcryptoInterface;
|
|
17
8
|
|
|
18
9
|
// eslint-disable-next-line no-use-before-define
|
|
19
10
|
private fsName = 'fs'
|
|
@@ -104,9 +95,4 @@ class A_PolyfillsClass {
|
|
|
104
95
|
};
|
|
105
96
|
}
|
|
106
97
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
98
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
export const A_Polyfills = new A_PolyfillsClass();
|
|
@@ -3,6 +3,10 @@ export class A_Deferred<T> {
|
|
|
3
3
|
private resolveFn!: (value: T | PromiseLike<T>) => void;
|
|
4
4
|
private rejectFn!: (reason?: any) => void;
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Creates a deferred promise
|
|
8
|
+
* @returns A promise that can be resolved or rejected later
|
|
9
|
+
*/
|
|
6
10
|
constructor() {
|
|
7
11
|
this.promise = new Promise<T>((resolve, reject) => {
|
|
8
12
|
this.resolveFn = resolve;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { A_Component, A_TypeGuards } from "@adaas/a-concept";
|
|
2
|
+
import { A_ScheduleObject } from "./A-ScheduleObject.class";
|
|
3
|
+
import { A_UTILS_TYPES__ScheduleObjectCallback, A_UTILS_TYPES__ScheduleObjectConfig } from "./A-Schedule.types";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class A_Schedule extends A_Component {
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Allows to schedule a callback for particular time in the future
|
|
11
|
+
*
|
|
12
|
+
* @param timestamp - Unix timestamp in milliseconds
|
|
13
|
+
* @param callback - The callback to execute
|
|
14
|
+
* @returns A promise that resolves to the schedule object
|
|
15
|
+
*/
|
|
16
|
+
async schedule<T extends any = any>(
|
|
17
|
+
/**
|
|
18
|
+
* Unix timestamp in milliseconds
|
|
19
|
+
*/
|
|
20
|
+
timestamp: number,
|
|
21
|
+
/**
|
|
22
|
+
* The callback to execute
|
|
23
|
+
*/
|
|
24
|
+
callback: A_UTILS_TYPES__ScheduleObjectCallback<T>,
|
|
25
|
+
/**
|
|
26
|
+
* Configuration options for the schedule object
|
|
27
|
+
*/
|
|
28
|
+
config?: A_UTILS_TYPES__ScheduleObjectConfig
|
|
29
|
+
): Promise<A_ScheduleObject<T>>
|
|
30
|
+
async schedule<T extends any = any>(
|
|
31
|
+
/**
|
|
32
|
+
* ISO date string representing the date and time to schedule the callback for
|
|
33
|
+
*/
|
|
34
|
+
date: string,
|
|
35
|
+
/**
|
|
36
|
+
* The callback to execute
|
|
37
|
+
*/
|
|
38
|
+
callback: A_UTILS_TYPES__ScheduleObjectCallback<T>,
|
|
39
|
+
/**
|
|
40
|
+
* Configuration options for the schedule object
|
|
41
|
+
*/
|
|
42
|
+
config?: A_UTILS_TYPES__ScheduleObjectConfig
|
|
43
|
+
): Promise<A_ScheduleObject<T>>
|
|
44
|
+
async schedule<T extends any = any>(
|
|
45
|
+
date: string | number,
|
|
46
|
+
callback: A_UTILS_TYPES__ScheduleObjectCallback<T>,
|
|
47
|
+
config?: A_UTILS_TYPES__ScheduleObjectConfig
|
|
48
|
+
): Promise<A_ScheduleObject<T>> {
|
|
49
|
+
|
|
50
|
+
const timestamp = A_TypeGuards.isString(date)
|
|
51
|
+
? (new Date(date)).getTime()
|
|
52
|
+
: date;
|
|
53
|
+
|
|
54
|
+
return new A_ScheduleObject<T>(
|
|
55
|
+
timestamp - Date.now(),
|
|
56
|
+
callback,
|
|
57
|
+
config
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Allows to execute callback after particular delay in milliseconds
|
|
65
|
+
* So the callback will be executed after the specified delay
|
|
66
|
+
*
|
|
67
|
+
* @param ms
|
|
68
|
+
*/
|
|
69
|
+
async delay<T extends any = any>(
|
|
70
|
+
/**
|
|
71
|
+
* Delay in milliseconds
|
|
72
|
+
*/
|
|
73
|
+
ms: number,
|
|
74
|
+
/**
|
|
75
|
+
* The callback to execute after the delay
|
|
76
|
+
*/
|
|
77
|
+
callback: A_UTILS_TYPES__ScheduleObjectCallback<T>,
|
|
78
|
+
/**
|
|
79
|
+
* Configuration options for the schedule object
|
|
80
|
+
*/
|
|
81
|
+
config?: A_UTILS_TYPES__ScheduleObjectConfig
|
|
82
|
+
): Promise<A_ScheduleObject<T>> {
|
|
83
|
+
return new A_ScheduleObject<T>(
|
|
84
|
+
ms,
|
|
85
|
+
callback,
|
|
86
|
+
config
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export type A_UTILS_TYPES__ScheduleObjectConfig = {
|
|
2
4
|
/**
|
|
3
5
|
* If the timeout is cleared, should the promise resolve or reject?
|
|
4
6
|
* BY Default it rejects
|
|
@@ -6,4 +8,7 @@ export type A_TYPES__ScheduleObjectConfig = {
|
|
|
6
8
|
* !!!NOTE: If the property is set to true, the promise will resolve with undefined
|
|
7
9
|
*/
|
|
8
10
|
resolveOnClear: boolean;
|
|
9
|
-
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export type A_UTILS_TYPES__ScheduleObjectCallback<T> = () => Promise<T>;
|