All files / apps/shared ContainerConfiguration.ts

100% Statements 20/20
100% Branches 14/14
100% Functions 4/4
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                      7x 22x 17x               42x 37x     42x 41x           42x   42x 1x 1x             42x 42x 3x           39x   39x           38x         38x         38x         38x          
import type { Container } from 'inversify';
 
import { DispatchActionCommandHandler } from '../../envilder/application/dispatch/DispatchActionCommandHandler.js';
import { PullSecretsToEnvCommandHandler } from '../../envilder/application/pullSecretsToEnv/PullSecretsToEnvCommandHandler.js';
import { PushEnvToSecretsCommandHandler } from '../../envilder/application/pushEnvToSecrets/PushEnvToSecretsCommandHandler.js';
import { PushSingleCommandHandler } from '../../envilder/application/pushSingle/PushSingleCommandHandler.js';
import { InvalidArgumentError } from '../../envilder/domain/errors/DomainErrors.js';
import type { MapFileConfig } from '../../envilder/domain/MapFileConfig.js';
import type { ILogger } from '../../envilder/domain/ports/ILogger.js';
import type { ISecretProvider } from '../../envilder/domain/ports/ISecretProvider.js';
import type { IVariableStore } from '../../envilder/domain/ports/IVariableStore.js';
import { createAwsSecretProvider } from '../../envilder/infrastructure/aws/AwsSecretProviderFactory.js';
import {
  type AzureProviderOptions,
  createAzureSecretProvider,
} from '../../envilder/infrastructure/azure/AzureSecretProviderFactory.js';
import { ConsoleLogger } from '../../envilder/infrastructure/logger/ConsoleLogger.js';
import { FileVariableStore } from '../../envilder/infrastructure/variableStore/FileVariableStore.js';
import { TYPES } from '../../envilder/types.js';
 
export type InfrastructureOptions = AzureProviderOptions;
 
type ProviderFactory = (
  config: MapFileConfig,
  options: InfrastructureOptions,
) => ISecretProvider;
 
const providerFactories: Record<string, ProviderFactory> = {
  aws: (config) => createAwsSecretProvider(config),
  azure: (config, options) => createAzureSecretProvider(config, options),
};
 
export function configureInfrastructureServices(
  container: Container,
  config: MapFileConfig = {},
  options: InfrastructureOptions = {},
): void {
  if (!container.isBound(TYPES.ILogger)) {
    container.bind<ILogger>(TYPES.ILogger).to(ConsoleLogger).inSingletonScope();
  }
 
  if (!container.isBound(TYPES.IVariableStore)) {
    container
      .bind<IVariableStore>(TYPES.IVariableStore)
      .to(FileVariableStore)
      .inSingletonScope();
  }
 
  const selectedProvider = config.provider?.toLowerCase() || 'aws';
 
  if (config.profile && selectedProvider !== 'aws') {
    const logger = container.get<ILogger>(TYPES.ILogger);
    logger.warn(
      `--profile is only supported with the aws provider` +
        ` and will be ignored` +
        ` (current provider: ${selectedProvider}).`,
    );
  }
 
  const factory = providerFactories[selectedProvider];
  if (!factory) {
    throw new InvalidArgumentError(
      `Unsupported provider: ${config.provider}.` +
        ` Supported providers:` +
        ` ${Object.keys(providerFactories).join(', ')}`,
    );
  }
  const secretProvider = factory(config, options);
 
  container
    .bind<ISecretProvider>(TYPES.ISecretProvider)
    .toConstantValue(secretProvider);
}
 
export function configureApplicationServices(container: Container): void {
  container
    .bind<PullSecretsToEnvCommandHandler>(TYPES.PullSecretsToEnvCommandHandler)
    .to(PullSecretsToEnvCommandHandler)
    .inTransientScope();
 
  container
    .bind<PushEnvToSecretsCommandHandler>(TYPES.PushEnvToSecretsCommandHandler)
    .to(PushEnvToSecretsCommandHandler)
    .inTransientScope();
 
  container
    .bind<PushSingleCommandHandler>(TYPES.PushSingleCommandHandler)
    .to(PushSingleCommandHandler)
    .inTransientScope();
 
  container
    .bind<DispatchActionCommandHandler>(TYPES.DispatchActionCommandHandler)
    .to(DispatchActionCommandHandler)
    .inTransientScope();
}