| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import { |
| | | 3 | | compareEnvFileKeys, |
| | | 4 | | type EnvFileKeyComparison, |
| | | 5 | | } from '../../domain/EnvFileComparison.js'; |
| | | 6 | | import { EnvironmentFileError } from '../../domain/errors/DomainErrors.js'; |
| | | 7 | | import type { IVariableStore } from '../../domain/ports/IVariableStore.js'; |
| | | 8 | | import { TYPES } from '../../types.js'; |
| | | 9 | | import type { ValidateEnvFileKeysCommand } from './ValidateEnvFileKeysCommand.js'; |
| | | 10 | | |
| | | 11 | | /** |
| | | 12 | | * Compares the keys of a local env file with the variables a map file |
| | | 13 | | * declares, without calling any provider. Only keys are compared: the values |
| | | 14 | | * the env file holds are read by the parser and then dropped. Run it on a map |
| | | 15 | | * file that passed offline validation. |
| | | 16 | | */ |
| | | 17 | | @injectable() |
| | 9 | 18 | | export class ValidateEnvFileKeysCommandHandler { |
| | | 19 | | constructor( |
| | | 20 | | @inject(TYPES.IVariableStore) |
| | 16 | 21 | | private readonly variableStore: IVariableStore, |
| | | 22 | | ) {} |
| | | 23 | | |
| | | 24 | | async handle( |
| | | 25 | | command: ValidateEnvFileKeysCommand, |
| | | 26 | | ): Promise<EnvFileKeyComparison> { |
| | 16 | 27 | | const mapping = await this.variableStore.getMapping(command.mapPath); |
| | 16 | 28 | | const entries = await this.variableStore.readEnvironmentEntries( |
| | | 29 | | command.envFilePath, |
| | | 30 | | ); |
| | 16 | 31 | | if (entries === null) { |
| | 2 | 32 | | throw new EnvironmentFileError( |
| | | 33 | | `Env file not found: ${command.envFilePath}`, |
| | | 34 | | ); |
| | | 35 | | } |
| | 14 | 36 | | return compareEnvFileKeys(Object.keys(mapping), entries); |
| | | 37 | | } |
| | | 38 | | } |