Skip to content

Commit 1d0e1b2

Browse files
committed
Move env var getters out of util.ts
1 parent 4c868b3 commit 1d0e1b2

3 files changed

Lines changed: 90 additions & 82 deletions

File tree

lib/entry-points.js

Lines changed: 32 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/environment.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,60 @@ export enum EnvVar {
164164
RISK_ASSESSMENT_ID = "CODEQL_ACTION_RISK_ASSESSMENT_ID",
165165
}
166166

167+
/**
168+
* Gets an environment variable, but throws an error if it is not set.
169+
*/
170+
export function getRequiredEnvVar(
171+
env: NodeJS.ProcessEnv,
172+
paramName: string,
173+
): string {
174+
const value = env[paramName];
175+
if (value === undefined || value.length === 0) {
176+
throw new Error(`${paramName} environment variable must be set`);
177+
}
178+
return value;
179+
}
180+
181+
/**
182+
* Get an environment parameter, but throw an error if it is not set.
183+
*/
184+
export function getRequiredEnvParam(paramName: string): string {
185+
return getRequiredEnvVar(process.env, paramName);
186+
}
187+
188+
/**
189+
* Gets an environment variable, but returns `undefined` if it is not set or empty.
190+
*/
191+
export function getOptionalEnvVarFrom(
192+
env: NodeJS.ProcessEnv,
193+
paramName: string,
194+
): string | undefined {
195+
const value = env[paramName];
196+
if (value?.trim().length === 0) {
197+
return undefined;
198+
}
199+
return value;
200+
}
201+
202+
/**
203+
* Get an environment variable, but return `undefined` if it is not set or empty.
204+
*/
205+
export function getOptionalEnvVar(paramName: string): string | undefined {
206+
return getOptionalEnvVarFrom(process.env, paramName);
207+
}
208+
209+
/** Gets an `Env` instance for `env`, which is `process.env` by default. */
210+
export function getEnv(env: NodeJS.ProcessEnv = process.env): Env {
211+
return {
212+
getRequired: (name) => getRequiredEnvVar(env, name),
213+
getOptional: (name) => getOptionalEnvVarFrom(env, name),
214+
entries: () => Object.entries(env),
215+
set: (name, value) => {
216+
env[name] = value;
217+
},
218+
};
219+
}
220+
167221
/** A wrapper around an environment, to allow abstracting away from `process.env` in tests. */
168222
export interface Env {
169223
/** Tries to get the value for `name` and throws if there isn't one. */

src/util.ts

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import * as apiCompatibility from "./api-compatibility.json";
1313
import type { CodeQL, VersionInfo } from "./codeql";
1414
import type { Pack } from "./config/db-config";
1515
import type { Config } from "./config-utils";
16-
import { Env, EnvVar } from "./environment";
16+
import { EnvVar, getRequiredEnvParam } from "./environment";
1717
import * as json from "./json";
1818
import { Language } from "./languages";
1919
import { Logger } from "./logging";
2020

21+
// Re-export for backwards compatibility to avoid updating a lot of imports elsewhere.
22+
export { getRequiredEnvParam, getOptionalEnvVar, getEnv } from "./environment";
23+
2124
/**
2225
* The name of the file containing the base database OIDs, as stored in the
2326
* root of the database location.
@@ -566,60 +569,6 @@ export function initializeEnvironment(version: string) {
566569
core.exportVariable(EnvVar.VERSION, version);
567570
}
568571

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-
entries: () => Object.entries(env),
575-
set: (name, value) => {
576-
env[name] = value;
577-
},
578-
};
579-
}
580-
581-
/**
582-
* Gets an environment variable, but throws an error if it is not set.
583-
*/
584-
export function getRequiredEnvVar(
585-
env: NodeJS.ProcessEnv,
586-
paramName: string,
587-
): string {
588-
const value = env[paramName];
589-
if (value === undefined || value.length === 0) {
590-
throw new Error(`${paramName} environment variable must be set`);
591-
}
592-
return value;
593-
}
594-
595-
/**
596-
* Get an environment parameter, but throw an error if it is not set.
597-
*/
598-
export function getRequiredEnvParam(paramName: string): string {
599-
return getRequiredEnvVar(process.env, paramName);
600-
}
601-
602-
/**
603-
* Gets an environment variable, but returns `undefined` if it is not set or empty.
604-
*/
605-
export function getOptionalEnvVarFrom(
606-
env: NodeJS.ProcessEnv,
607-
paramName: string,
608-
): string | undefined {
609-
const value = env[paramName];
610-
if (value?.trim().length === 0) {
611-
return undefined;
612-
}
613-
return value;
614-
}
615-
616-
/**
617-
* Get an environment variable, but return `undefined` if it is not set or empty.
618-
*/
619-
export function getOptionalEnvVar(paramName: string): string | undefined {
620-
return getOptionalEnvVarFrom(process.env, paramName);
621-
}
622-
623572
export class HTTPError extends Error {
624573
public status: number;
625574

0 commit comments

Comments
 (0)