Extract @ipp/core npm workspace package

This commit is contained in:
Alan Grainger
2026-10-05 15:55:18 +13:00
parent 54c0f91c4e
commit d03a77f691
65 changed files with 1056 additions and 746 deletions
+28
View File
@@ -0,0 +1,28 @@
import { getCurrentConfig } from './loader'
/**
* Read a configuration option using dotted notation. Returns `defaultOption`
* when the path doesn't resolve to a value.
*
* @example
* getConfigOption('ipp.gallery.singleImage', false)
*/
export function getConfigOption (path: string, defaultOption?: unknown) {
const value = path.split('.').reduce(
(obj: { [key: string]: unknown }, key) => (obj || {})[key] as { [key: string]: unknown },
getCurrentConfig() as { [key: string]: unknown }
)
if (value === undefined) {
return defaultOption
}
return value
}
/**
* Read a configuration option that must be a finite number. Non-numeric
* values fall back to `defaultOption` instead of propagating `NaN`.
*/
export function getNumericConfigOption (path: string, defaultOption: number): number {
const value = Number(getConfigOption(path, defaultOption))
return Number.isFinite(value) ? value : defaultOption
}
+62
View File
@@ -0,0 +1,62 @@
import { readFileSync } from 'fs'
import { resolve } from 'path'
export type Config = Record<string, unknown>
/** Where each app keeps its config file, and how it upgrades legacy config shapes. */
export interface LoadConfigOptions {
/** Absolute path of the bundled config file, used when `IPP_CONFIG` is unset. */
defaultPath?: string
/** Rewrites legacy keys in place before the config is cached. */
migrate?: (config: Config) => void
}
// Module-level cache populated by `loadConfig()`. Access through
// `getCurrentConfig()` rather than importing directly; that gives
// `config/access.ts` a stable read path even before `loadConfig()` has
// been called (returning an empty object so calls fall back to defaults).
let currentConfig: Config = {}
/**
* Read the runtime configuration from `process.env.CONFIG` (an inline JSON
* string, typically set in docker-compose) or from the config file. Applies
* the app's `migrate` hook, caches the result, and returns it.
*
* The default path comes from the app, not from this module's location:
* core is compiled into its own package, so a path relative to `__dirname`
* would point inside `@ipp/core` instead of at the app's bundled file.
*
* Called once from each app's `index.ts` at startup. Safe to call again in
* tests with a fresh env to reset state.
*/
export function loadConfig ({ defaultPath, migrate }: LoadConfigOptions = {}): Config {
let config: Config = {}
try {
if (process.env.CONFIG) {
// Attempt to parse docker-compose config string into JSON (if specified)
config = JSON.parse(process.env.CONFIG)
} else {
// IPP_CONFIG (if set) is taken as-is for absolute paths, or resolved
// against the current working directory for relative paths.
const configPath = process.env.IPP_CONFIG ? resolve(process.env.IPP_CONFIG) : defaultPath
if (configPath) {
const configJson = JSON.parse(readFileSync(configPath, 'utf8'))
if (typeof configJson === 'object') config = configJson
}
}
} catch (e) {
console.log(e)
}
migrate?.(config)
currentConfig = config
return config
}
/**
* Return the most recently loaded config, or an empty object if
* `loadConfig()` hasn't been called yet. Used by `getConfigOption`.
*/
export function getCurrentConfig (): Config {
return currentConfig
}