|
| 1 | +import fs from 'node:fs'; |
| 2 | +import settings from '../../resources/settings'; |
| 3 | + |
| 4 | +export interface OpenComponentsConfig { |
| 5 | + /** JSON schema specification reference */ |
| 6 | + $schema?: string | null; |
| 7 | + /** List of registry URLs where components will be published */ |
| 8 | + registries?: string[]; |
| 9 | + /** Development-specific configuration settings */ |
| 10 | + development?: { |
| 11 | + /** Fallback configuration for when components cannot be found locally */ |
| 12 | + fallback?: { |
| 13 | + /** URL of the fallback registry to use when components cannot be found locally */ |
| 14 | + url: string; |
| 15 | + /** Whether to use the fallback registry's oc-client-browser.js for previewing components */ |
| 16 | + client?: boolean; |
| 17 | + }; |
| 18 | + |
| 19 | + /** Plugin mocking configuration for development */ |
| 20 | + plugins?: { |
| 21 | + /** Dynamic plugin mocks - file paths to JavaScript modules */ |
| 22 | + dynamic?: Record<string, string>; |
| 23 | + /** Static plugin mocks - static values that will always be returned */ |
| 24 | + static?: Record<string, string>; |
| 25 | + }; |
| 26 | + }; |
| 27 | + /** @deprecated Use development.plugins instead */ |
| 28 | + mocks?: { |
| 29 | + /** @deprecated Use development.plugins instead */ |
| 30 | + plugins?: { |
| 31 | + /** @deprecated Use development.plugins.dynamic instead */ |
| 32 | + dynamic?: Record<string, string>; |
| 33 | + /** @deprecated Use development.plugins.static instead */ |
| 34 | + static?: Record<string, string>; |
| 35 | + }; |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +type ParsedConfig = { |
| 40 | + registries: string[]; |
| 41 | + development: { |
| 42 | + plugins: { |
| 43 | + dynamic?: Record<string, string>; |
| 44 | + static?: Record<string, string>; |
| 45 | + }; |
| 46 | + fallback?: { |
| 47 | + url: string; |
| 48 | + client?: boolean; |
| 49 | + }; |
| 50 | + }; |
| 51 | +}; |
| 52 | + |
| 53 | +function parseConfig(config: OpenComponentsConfig): ParsedConfig { |
| 54 | + const plugins = { |
| 55 | + ...(config.mocks?.plugins || {}), |
| 56 | + ...(config.development?.plugins || {}) |
| 57 | + }; |
| 58 | + |
| 59 | + const parsedConfig: ParsedConfig = { |
| 60 | + ...config, |
| 61 | + registries: config.registries || [], |
| 62 | + development: { |
| 63 | + plugins, |
| 64 | + fallback: config.development?.fallback |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + return parsedConfig; |
| 69 | +} |
| 70 | + |
| 71 | +export function getOcConfig(path?: string): ParsedConfig { |
| 72 | + try { |
| 73 | + const config = JSON.parse( |
| 74 | + fs.readFileSync(path || settings.configFile.src, 'utf8') |
| 75 | + ); |
| 76 | + return parseConfig(config); |
| 77 | + } catch { |
| 78 | + return { |
| 79 | + registries: [], |
| 80 | + development: { |
| 81 | + plugins: {} |
| 82 | + } |
| 83 | + }; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export function setOcConfig(config: ParsedConfig, path?: string) { |
| 88 | + fs.writeFileSync( |
| 89 | + path || settings.configFile.src, |
| 90 | + JSON.stringify(parseConfig(config), null, 2) |
| 91 | + ); |
| 92 | +} |
0 commit comments