(
key: T,
value: ConfigSchemas.Config[T],
options?: {
nosave?: boolean;
partOfFullConfigChange?: boolean;
},
)
| 16 | import { FunboxName } from "@monkeytype/schemas/configs"; |
| 17 | |
| 18 | export function setConfig<T extends keyof ConfigSchemas.Config>( |
| 19 | key: T, |
| 20 | value: ConfigSchemas.Config[T], |
| 21 | options?: { |
| 22 | nosave?: boolean; |
| 23 | partOfFullConfigChange?: boolean; |
| 24 | }, |
| 25 | ): boolean { |
| 26 | const metadata = configMetadata[key] as ConfigMetadataObject[T]; |
| 27 | if (metadata === undefined) { |
| 28 | throw new Error(`Config metadata for key "${key}" is not defined.`); |
| 29 | } |
| 30 | |
| 31 | if (metadata.overrideValue) { |
| 32 | value = metadata.overrideValue({ |
| 33 | value, |
| 34 | currentValue: Config[key], |
| 35 | currentConfig: Config, |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | const previousValue = Config[key]; |
| 40 | |
| 41 | if ( |
| 42 | metadata.changeRequiresRestart && |
| 43 | TestState.isActive && |
| 44 | Config.funbox.includes("no_quit") |
| 45 | ) { |
| 46 | showNoticeNotification( |
| 47 | "No quit funbox is active. Please finish the test.", |
| 48 | { |
| 49 | important: true, |
| 50 | }, |
| 51 | ); |
| 52 | console.warn( |
| 53 | `Could not set config key "${key}" with value "${JSON.stringify( |
| 54 | value, |
| 55 | )}" - no quit funbox active.`, |
| 56 | ); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if (metadata.isBlocked?.({ value, currentConfig: Config })) { |
| 61 | console.warn( |
| 62 | `Could not set config key "${key}" with value "${JSON.stringify( |
| 63 | value, |
| 64 | )}" - blocked.`, |
| 65 | ); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | const schema = ConfigSchemas.ConfigSchema.shape[key] as ZodSchema; |
| 70 | |
| 71 | if (!isConfigValueValid(metadata.displayString ?? key, value, schema)) { |
| 72 | console.warn( |
| 73 | `Could not set config key "${key}" with value "${JSON.stringify( |
| 74 | value, |
| 75 | )}" - invalid value.`, |
no test coverage detected