| | | 1 | | import { Container } from 'inversify'; |
| | | 2 | | import { ExecWithSecretsCommandHandler } from '../../core/application/execWithSecrets/ExecWithSecretsCommandHandler.js'; |
| | | 3 | | import { ValidateMapFileCommandHandler } from '../../core/application/validateMapFile/ValidateMapFileCommandHandler.js'; |
| | | 4 | | import { ValidateRemoteKeysCommandHandler } from '../../core/application/validateRemoteKeys/ValidateRemoteKeysCommandHan |
| | | 5 | | import type { MapFileConfig } from '../../core/domain/MapFileConfig.js'; |
| | | 6 | | import type { ILogger } from '../../core/domain/ports/ILogger.js'; |
| | | 7 | | import type { IProcessContext } from '../../core/domain/ports/IProcessContext.js'; |
| | | 8 | | import type { IProcessRunner } from '../../core/domain/ports/IProcessRunner.js'; |
| | | 9 | | import type { ISecretMasker } from '../../core/domain/ports/ISecretMasker.js'; |
| | | 10 | | import type { IVariableStore } from '../../core/domain/ports/IVariableStore.js'; |
| | | 11 | | import { ConsoleLogger } from '../../core/infrastructure/logger/ConsoleLogger.js'; |
| | | 12 | | import { CrossSpawnProcessRunner } from '../../core/infrastructure/process/CrossSpawnProcessRunner.js'; |
| | | 13 | | import { NodeProcessContext } from '../../core/infrastructure/process/NodeProcessContext.js'; |
| | | 14 | | import { FileVariableStore } from '../../core/infrastructure/variableStore/FileVariableStore.js'; |
| | | 15 | | import { TYPES } from '../../core/types.js'; |
| | | 16 | | import { |
| | | 17 | | configureApplicationServices, |
| | | 18 | | configureInfrastructureServices, |
| | | 19 | | } from '../shared/ContainerConfiguration.js'; |
| | | 20 | | |
| | | 21 | | export class Startup { |
| | | 22 | | private readonly container: Container; |
| | | 23 | | |
| | | 24 | | constructor() { |
| | 120 | 25 | | this.container = new Container(); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | static build(): Startup { |
| | 120 | 29 | | return new Startup(); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | configureServices(): this { |
| | 79 | 33 | | configureApplicationServices(this.container); |
| | 79 | 34 | | return this; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /** |
| | | 38 | | * Wires the services specific to `envilder exec`: its handler, the process |
| | | 39 | | * runner, and the given logger and secret masker. The logger must keep |
| | | 40 | | * stdout free for the child. Call it before `configureInfrastructure`, |
| | | 41 | | * which only binds the defaults left unbound. |
| | | 42 | | */ |
| | | 43 | | configureExec(secretMasker: ISecretMasker, logger: ILogger): this { |
| | 38 | 44 | | this.container.bind<ILogger>(TYPES.ILogger).toConstantValue(logger); |
| | 38 | 45 | | this.container |
| | | 46 | | .bind<ISecretMasker>(TYPES.ISecretMasker) |
| | | 47 | | .toConstantValue(secretMasker); |
| | 38 | 48 | | this.container |
| | | 49 | | .bind<IProcessRunner>(TYPES.IProcessRunner) |
| | | 50 | | .to(CrossSpawnProcessRunner) |
| | | 51 | | .inSingletonScope(); |
| | 38 | 52 | | this.container |
| | | 53 | | .bind<IProcessContext>(TYPES.IProcessContext) |
| | | 54 | | .to(NodeProcessContext) |
| | | 55 | | .inSingletonScope(); |
| | 38 | 56 | | this.container |
| | | 57 | | .bind<ExecWithSecretsCommandHandler>(TYPES.ExecWithSecretsCommandHandler) |
| | | 58 | | .to(ExecWithSecretsCommandHandler) |
| | | 59 | | .inTransientScope(); |
| | 38 | 60 | | return this; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /** |
| | | 64 | | * Wires `envilder validate --offline`: the map file is read and checked |
| | | 65 | | * locally, so no secret provider is bound. Use it instead of |
| | | 66 | | * `configureServices` and `configureInfrastructure`. |
| | | 67 | | */ |
| | | 68 | | configureOfflineValidation(): this { |
| | 24 | 69 | | this.container |
| | | 70 | | .bind<ILogger>(TYPES.ILogger) |
| | | 71 | | .to(ConsoleLogger) |
| | | 72 | | .inSingletonScope(); |
| | 24 | 73 | | this.container |
| | | 74 | | .bind<IVariableStore>(TYPES.IVariableStore) |
| | | 75 | | .to(FileVariableStore) |
| | | 76 | | .inSingletonScope(); |
| | 24 | 77 | | this.container |
| | | 78 | | .bind<ValidateMapFileCommandHandler>(TYPES.ValidateMapFileCommandHandler) |
| | | 79 | | .to(ValidateMapFileCommandHandler) |
| | | 80 | | .inTransientScope(); |
| | 24 | 81 | | return this; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /** |
| | | 85 | | * Wires the remote part of `envilder validate`, which checks that each |
| | | 86 | | * declared secret exists. Call `configureInfrastructure` afterwards to bind |
| | | 87 | | * the secret provider, the logger and the variable store. |
| | | 88 | | */ |
| | | 89 | | configureRemoteValidation(): this { |
| | 13 | 90 | | this.container |
| | | 91 | | .bind<ValidateRemoteKeysCommandHandler>( |
| | | 92 | | TYPES.ValidateRemoteKeysCommandHandler, |
| | | 93 | | ) |
| | | 94 | | .to(ValidateRemoteKeysCommandHandler) |
| | | 95 | | .inTransientScope(); |
| | 13 | 96 | | return this; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | configureInfrastructure(config?: MapFileConfig): this { |
| | 91 | 100 | | configureInfrastructureServices(this.container, config); |
| | 91 | 101 | | return this; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | create(): Container { |
| | 113 | 105 | | return this.container; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | getServiceProvider(): Container { |
| | 2 | 109 | | return this.container; |
| | | 110 | | } |
| | | 111 | | } |