mirror of
https://github.com/enso-org/enso.git
synced 2024-11-22 21:51:05 +03:00
9b7e3d0f16
- Closes #8179 # Important Notes - ⚠️ These tests are currently *not run* on any CI workflow. - There is some unused code for mocking the PM. This has been intentionally kept, as this may be useful in the future. Note that this may be useful for testing the dashboard, however the dashboard is currently only tested in cloud mode - that is, without the backend switcher, and with only the remote backend available. As such, currently it uses HTTP API mocks, and no PM mock.
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
/** @file Shared utility functions. */
|
|
import * as fs from 'node:fs'
|
|
import * as path from 'node:path'
|
|
import process from 'node:process'
|
|
|
|
// =================
|
|
// === Constants ===
|
|
// =================
|
|
|
|
/** Indent size for outputting JSON. */
|
|
export const INDENT_SIZE = 4
|
|
|
|
// ===================
|
|
// === Environment ===
|
|
// ===================
|
|
|
|
/** 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.`)
|
|
}
|
|
}
|
|
|
|
// ======================
|
|
// === String Helpers ===
|
|
// ======================
|
|
|
|
/** Get the common prefix of the two strings. */
|
|
export function getCommonPrefix(a: string, b: string): string {
|
|
let i = 0
|
|
while (i < a.length && i < b.length && a[i] === b[i]) {
|
|
i++
|
|
}
|
|
return a.slice(0, i)
|
|
}
|