@hg-ts/execution-mode 0.5.17 → 0.5.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hg-ts/execution-mode",
3
- "version": "0.5.17",
3
+ "version": "0.5.18",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,9 +16,9 @@
16
16
  "lint:ts:fix": "lint-ts --fix"
17
17
  },
18
18
  "devDependencies": {
19
- "@hg-ts-config/typescript": "0.5.17",
20
- "@hg-ts/linter": "0.5.17",
21
- "@hg-ts/types": "0.5.17",
19
+ "@hg-ts-config/typescript": "0.5.18",
20
+ "@hg-ts/linter": "0.5.18",
21
+ "@hg-ts/types": "0.5.18",
22
22
  "@nestjs/common": "11.1.0",
23
23
  "@types/node": "22.19.1",
24
24
  "eslint": "9.18.0",
@@ -0,0 +1,18 @@
1
+ import {
2
+ Global,
3
+ Module,
4
+ } from '@nestjs/common';
5
+ import { ExecutionMode } from './execution-mode.js';
6
+ import { HgExecutionMode } from './hg.execution-mode.js';
7
+
8
+ @Global()
9
+ @Module({
10
+ providers: [
11
+ {
12
+ provide: ExecutionMode,
13
+ useClass: HgExecutionMode,
14
+ },
15
+ ],
16
+ exports: [ExecutionMode],
17
+ })
18
+ export class ExecutionModeModule {}
@@ -0,0 +1,78 @@
1
+ export enum ExecutionModeVariants {
2
+ DEBUG = 'debug',
3
+ DEV = 'dev',
4
+ TEST = 'test',
5
+ QA = 'qa',
6
+ DEMO = 'demo',
7
+ PROD = 'prod',
8
+ }
9
+
10
+ export class ExecutionMode<ValueType extends ExecutionModeVariants = ExecutionModeVariants> {
11
+ private value: ValueType;
12
+ private readonly projectName: string;
13
+ private readonly defaultValue: ValueType;
14
+
15
+ public constructor(projectName: string, defaultValue: ValueType) {
16
+ this.projectName = projectName.toUpperCase();
17
+ this.defaultValue = defaultValue;
18
+ this.initValue();
19
+ }
20
+
21
+ public isDebug(): boolean {
22
+ return this.value === ExecutionModeVariants.DEBUG;
23
+ }
24
+
25
+ public isDev(): boolean {
26
+ return this.value === ExecutionModeVariants.DEV;
27
+ }
28
+
29
+ public isTest(): boolean {
30
+ return this.value === ExecutionModeVariants.TEST;
31
+ }
32
+
33
+ public isQa(): boolean {
34
+ return this.value === ExecutionModeVariants.QA;
35
+ }
36
+
37
+ public isDemo(): boolean {
38
+ return this.value === ExecutionModeVariants.DEMO;
39
+ }
40
+
41
+ public isProd(): boolean {
42
+ return this.value === ExecutionModeVariants.PROD;
43
+ }
44
+
45
+ public getValue(): ValueType {
46
+ return this.value;
47
+ }
48
+
49
+ public isValueIn(variants: ValueType[]): boolean {
50
+ return variants.includes(this.value);
51
+ }
52
+
53
+ public is(expected: ValueType): boolean {
54
+ return expected === this.value;
55
+ }
56
+
57
+ protected getEnvName(): string {
58
+ return `${this.projectName}_ENV`;
59
+ }
60
+
61
+ protected isValueValid(value?: string): value is ValueType {
62
+ return Object.values(ExecutionModeVariants).includes(value as any);
63
+ }
64
+
65
+ protected getRawValue(): string | undefined {
66
+ return process.env[this.getEnvName()];
67
+ }
68
+
69
+ private initValue(): void {
70
+ const value = this.getRawValue();
71
+
72
+ if (this.isValueValid(value)) {
73
+ this.value = value;
74
+ } else {
75
+ this.value = this.defaultValue;
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,10 @@
1
+ import {
2
+ ExecutionMode,
3
+ ExecutionModeVariants,
4
+ } from './execution-mode.js';
5
+
6
+ export class HgExecutionMode extends ExecutionMode {
7
+ public constructor() {
8
+ super('HG', ExecutionModeVariants.PROD);
9
+ }
10
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './execution-mode.js';
2
+ export * from './hg.execution-mode.js';
3
+ export * from './mock.execution-mode.js';
4
+ export * from './execution-mode.module.js';
@@ -0,0 +1,11 @@
1
+ import {
2
+ ExecutionMode,
3
+ ExecutionModeVariants,
4
+ } from './execution-mode.js';
5
+
6
+ export class MockExecutionMode<ValueType extends ExecutionModeVariants = ExecutionModeVariants>
7
+ extends ExecutionMode<ValueType> {
8
+ public constructor(value: ValueType) {
9
+ super('NEVER_USED', value);
10
+ }
11
+ }