@@ -13,7 +13,7 @@ import * as apiCompatibility from "./api-compatibility.json";
1313import type { CodeQL , VersionInfo } from "./codeql" ;
1414import type { Pack } from "./config/db-config" ;
1515import type { Config } from "./config-utils" ;
16- import { EnvVar } from "./environment" ;
16+ import { Env , EnvVar } from "./environment" ;
1717import * as json from "./json" ;
1818import { Language } from "./languages" ;
1919import { Logger } from "./logging" ;
@@ -566,28 +566,56 @@ export function initializeEnvironment(version: string) {
566566 core . exportVariable ( EnvVar . VERSION , version ) ;
567567}
568568
569+ /** Gets an `Env` instance for `env`, which is `process.env` by default. */
570+ export function getEnv ( env : NodeJS . ProcessEnv = process . env ) : Env {
571+ return {
572+ getRequired : ( name ) => getRequiredEnvVar ( env , name ) ,
573+ getOptional : ( name ) => getOptionalEnvVarFrom ( env , name ) ,
574+ } ;
575+ }
576+
569577/**
570- * Get an environment parameter , but throw an error if it is not set.
578+ * Gets an environment variable , but throws an error if it is not set.
571579 */
572- export function getRequiredEnvParam ( paramName : string ) : string {
573- const value = process . env [ paramName ] ;
580+ export function getRequiredEnvVar (
581+ env : NodeJS . ProcessEnv ,
582+ paramName : string ,
583+ ) : string {
584+ const value = env [ paramName ] ;
574585 if ( value === undefined || value . length === 0 ) {
575586 throw new Error ( `${ paramName } environment variable must be set` ) ;
576587 }
577588 return value ;
578589}
579590
580591/**
581- * Get an environment variable , but return `undefined` if it is not set or empty .
592+ * Get an environment parameter , but throw an error if it is not set.
582593 */
583- export function getOptionalEnvVar ( paramName : string ) : string | undefined {
584- const value = process . env [ paramName ] ;
594+ export function getRequiredEnvParam ( paramName : string ) : string {
595+ return getRequiredEnvVar ( process . env , paramName ) ;
596+ }
597+
598+ /**
599+ * Gets an environment variable, but returns `undefined` if it is not set or empty.
600+ */
601+ export function getOptionalEnvVarFrom (
602+ env : NodeJS . ProcessEnv ,
603+ paramName : string ,
604+ ) : string | undefined {
605+ const value = env [ paramName ] ;
585606 if ( value ?. trim ( ) . length === 0 ) {
586607 return undefined ;
587608 }
588609 return value ;
589610}
590611
612+ /**
613+ * Get an environment variable, but return `undefined` if it is not set or empty.
614+ */
615+ export function getOptionalEnvVar ( paramName : string ) : string | undefined {
616+ return getOptionalEnvVarFrom ( process . env , paramName ) ;
617+ }
618+
591619export class HTTPError extends Error {
592620 public status : number ;
593621
0 commit comments