mono/packages/commons/src/config.ts
2025-01-24 12:29:57 +01:00

90 lines
3.7 KiB
TypeScript

import * as path from 'path'
import pkg from 'env-var';
const { get } = pkg;
import { sync as read } from '@polymech/fs/read'
import { sync as exists } from '@polymech/fs/exists'
import { isString, isObject } from '@polymech/core/primitives'
import { API_PREFIX, API_NAMESPACE, API_PREFIX_NEXT } from './constants.js'
export const HOME = (sub = '') => path.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], sub)
export const get_var = (key: string = '') => get(key).asString() || get(key.replace(/-/g, '_')).asString() || get(key.replace(/_/g, '-')).asString()
export const OSR_ROOT = (key: string = 'OSR-ROOT') => get_var(key) || path.join(HOME('desktop'), API_PREFIX)
export const OSR_SUB_DEFAULT = (key: string = '') => get_var(key) || path.join(OSR_ROOT(), key)
export const CONFIG_DEFAULT_PATH = (key: string = 'OSR-CONFIG') => get_var(key) || path.join(HOME(`${API_PREFIX}`), '.config.json')
export const OSR_TEMP = (key: string = 'OSR-TEMP') => get_var(key) || OSR_SUB_DEFAULT(`.${API_PREFIX}/temp`)
export const OSR_CACHE = (key: string = 'OSR-CACHE') => get_var(key) || OSR_SUB_DEFAULT(`.${API_PREFIX}/cache`)
export const OSR_PRIVATE = (key: string = 'OSR-PRIVATE') => get_var(key)
export const KB_ROOT = (key: string = 'OSR-KB') => get_var(key)
export const OSR_LIBRARY = (key: string = 'OSR-LIBRARY') => get_var(key)
export const OSR_LIBRARY_MACHINES = (key: string = 'OSR-LIBRARY-MACHINES') => get_var(key)
export const OSR_LIBRARY_DIRECTORY = (key: string = 'OSR-LIBRARY-DIRECTORY') => get_var(key)
export const PRODUCT_ROOT = (key: string = 'PRODUCT-ROOT') => get_var(key)
export const OSR_CUSTOMER_DRIVE = (key: string = 'OSR-CUSTOMER-DRIVE') => get_var(key)
export const OA_ROOT = (key: string = 'OA-ROOT') => get_var(key)
export const OSR_USER_ASSETS = (key: string = 'OSR-USER-ASSETS') => get_var(key)
export const POLYMECH_ROOT = (key: string = 'POLYMECH-ROOT') => get_var(key) || path.join(HOME('desktop'), API_PREFIX_NEXT)
export const DEFAULT_ROOTS = {
OSR_ROOT: OSR_ROOT(),
OSR_TEMP: OSR_TEMP(),
PRODUCT_ROOT: PRODUCT_ROOT(),
OA_ROOT: OA_ROOT(),
KB_ROOT: KB_ROOT(),
OSR_CACHE: OSR_CACHE(),
OSR_LIBRARY: OSR_LIBRARY(),
OSR_LIBRARY_MACHINES: OSR_LIBRARY_MACHINES(),
OSR_LIBRARY_DIRECTORY: OSR_LIBRARY_DIRECTORY(),
OSR_USER_ASSETS: OSR_USER_ASSETS(),
OSR_PRIVATE: OSR_PRIVATE(),
OSR_TEMPLATES: path.join(OSR_SUB_DEFAULT('osr-templates')),
OSR_CONTENT: path.join(OSR_SUB_DEFAULT('osr-content')),
OSR_PROFILES: path.join(OSR_SUB_DEFAULT('osr-profiles')),
OSR_CUSTOMER_DRIVE: OSR_CUSTOMER_DRIVE(),
POLYMECH_ROOT: POLYMECH_ROOT()
}
export const CONFIG_DEFAULT = (key: string = 'OSR-CONFIG') => {
const cPath = path.resolve(CONFIG_DEFAULT_PATH(key));
if (exists(cPath)) {
return read(cPath, 'json');
}
return false;
}
import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package'
import { IComponentConfig } from './component.js';
//////////////////////////////////////////////////////
//
// NPM related
export const readNPMMeta = (_path: string) => read(_path, 'json') as JSONSchemaForNPMPackageJsonFiles || {}
export const readPackage = (val): JSONSchemaForNPMPackageJsonFiles => {
if (isString(val)) {
return readNPMMeta(val)
} else if (isObject(val)) {
return val
}
return {}
}
//////////////////////////////////////////////////////
//
// OSR related
export const readOSRMeta = (_path: string) => read(_path, 'json') as IComponentConfig
export const readOSRConfig = (val) => {
if (isString(val)) {
return readOSRMeta(val)
} else if (isObject(val)) {
return val
}
return null
}