• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

homer0 / packages / 4422859008

pending completion
4422859008

push

github

homer0
fix(eslint-plugin): use project as an array in the preset

720 of 720 branches covered (100.0%)

Branch coverage included in aggregate %.

2027 of 2027 relevant lines covered (100.0%)

45.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/packages/public/env-utils/src/index.ts
1
/* eslint-disable no-process-env */
2
import { providerCreator } from '@homer0/jimple';
1✔
3

4
/**
5
 * A simple service to avoid calling `process.env` on multiples places of an app.
6
 */
7
export class EnvUtils {
1✔
8
  /**
9
   * The current `NODE_ENV`. If the variable is empty, the value will be `development`.
10
   */
11
  readonly env: string;
11✔
12
  /**
13
   * Whether or not the environment is production.
14
   */
15
  readonly production: boolean;
11✔
16
  constructor() {
17
    this.env = this.get('NODE_ENV', 'development');
11✔
18
    this.production = this.env === 'production';
11✔
19
  }
20
  /**
21
   * Checks whether an environment variable exists or not.
22
   *
23
   * @param name  The name of the variable.
24
   */
25
  exists(name: string): boolean {
26
    return typeof process.env[name] !== 'undefined';
20✔
27
  }
28
  /**
29
   * Gets the value of an environment variable.
30
   *
31
   * @param name          The name of the variable.
32
   * @param defaultValue  A fallback value in case the variable is `undefined`.
33
   * @param required      If the variable is required and `undefined`, it will throw an
34
   *                      error.
35
   * @throws If `required` is set to `true` and the variable is `undefined`.
36
   */
37
  get(name: string, defaultValue: string = '', required: boolean = false): string {
21✔
38
    if (this.exists(name)) {
17✔
39
      return process.env[name]!;
5✔
40
    }
41

42
    if (required) {
12✔
43
      throw new Error(`The following environment variable is missing: '${name}'`);
1✔
44
    }
45

46
    return defaultValue;
11✔
47
  }
48
  /**
49
   * Sets the value of an environment variable.
50
   *
51
   * @param name       The name of the variable.
52
   * @param value      The value of the variable.
53
   * @param overwrite  If the variable already exists, the method won't overwrite it,
54
   *                   unless you set this parameter to `true`.
55
   * @returns Whether or not the variable was set.
56
   */
57
  set(name: string, value: unknown, overwrite: boolean = false): boolean {
2✔
58
    if (!this.exists(name) || overwrite) {
3✔
59
      process.env[name] = String(value);
2✔
60
      return true;
2✔
61
    }
62

63
    return false;
1✔
64
  }
65
  /**
66
   * Whether or not the environment is for development.
67
   */
68
  get development(): boolean {
69
    return !this.production;
2✔
70
  }
71
}
72

73
/**
74
 * Shorthand for `new EnvUtils()`.
75
 *
76
 * @param args  The same parameters as the {@link EnvUtils} constructor.
77
 * @returns A new instance of {@link EnvUtils}.
78
 */
79
export const envUtils = (...args: ConstructorParameters<typeof EnvUtils>): EnvUtils =>
1✔
80
  new EnvUtils(...args);
1✔
81

82
/**
83
 * The options for the {@link EnvUtils} Jimple's provider creator.
84
 */
85
export type EnvUtilsProviderOptions = {
86
  /**
87
   * The name that will be used to register the service.
88
   *
89
   * @default 'envUtils'
90
   */
91
  serviceName?: string;
92
};
93
/**
94
 * A provider creator to register {@link PathUtils} in a Jimple container.
95
 */
96
export const envUtilsProvider = providerCreator(
1✔
97
  ({ serviceName = 'envUtils' }: EnvUtilsProviderOptions = {}) =>
2✔
98
    (container) => {
2✔
99
      container.set(serviceName, () => new EnvUtils());
2✔
100
    },
101
);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc