enso/app/ide-desktop/utils.ts
somebody1234 fa23e800e5
Finish enabling ts-eslint (#5944)
- prefer `null`, `!= null` and `== null` instead of `undefined`
- disallow `as`, add comments for the existing usages of `as`
- make `tsconfig.json` a bit stricter
- minor fixes to other files that were missed

# Important Notes
N/A
2023-03-20 09:35:16 +00:00

48 lines
1.3 KiB
TypeScript

/** @file Shared utility functions. */
import * as fs from 'node:fs'
import * as path from 'node:path'
import process from 'node:process'
/**
* Get the environment variable value.
*
* @param name - The name of the environment variable.
* @returns The value of the environment variable.
* @throws {Error} If the environment variable is not set.
*/
export function requireEnv(name: string) {
return (
process.env[name] ??
(() => {
throw Error(`Missing ${name} environment variable.`)
})()
)
}
/**
* Read the path from environment variable and resolve it.
*
* @param name - The name of the environment variable.
* @returns The resolved path.
* @throws {Error} If the environment variable is not set.
*/
export function requireEnvResolvedPath(name: string) {
return path.resolve(requireEnv(name))
}
/**
* Read the path from environment variable and resolve it. Verify that it exists.
*
* @param name - The name of the environment variable.
* @returns The resolved path.
* @throws {Error} If the environment variable is not set or path does not exist.
*/
export function requireEnvPathExist(name: string) {
const value = requireEnv(name)
if (fs.existsSync(value)) {
return value
} else {
throw Error(`File with path ${value} read from environment variable ${name} is missing.`)
}
}