All files / core/src/modules source.ts

25% Statements 9/36
20% Branches 2/10
50% Functions 3/6
22.85% Lines 8/35

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 97 98 99 100 101 102 103 104 105 106 107 108          5x             9x 9x               2x   2x 2x 1x           1x                                                                                                                                                        
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
import { RibbonLogger } from '@ribbon-studios/logger';
import { KeyInfo } from '../types/key-info';
import { TargetModule } from './target';
 
const logger = new RibbonLogger('@refreshly/core');
 
export abstract class SourceModule {
  #targets: TargetModule[];
  #prefix?: string;
 
  constructor({ prefix, targets }: SourceModule.Options) {
    this.#targets = targets;
    this.#prefix = prefix;
  }
 
  abstract get name(): string;
 
  abstract get originalKeyInfos(): KeyInfo[];
 
  async prefix(keyInfosPromise: Promise<KeyInfo[]>): Promise<KeyInfo[]> {
    const keyInfos = await keyInfosPromise;
 
    const prefix = this.#prefix;
    if (prefix) {
      return keyInfos.map((keyInfo) => ({
        name: prefix.concat(keyInfo.name),
        value: keyInfo.value,
      }));
    }
 
    return keyInfos;
  }
 
  async exec(): Promise<void> {
    try {
      logger.silly(`(${this.name}) Getting the new value`);
      const keyInfos = await this.prefix(this.source());
      logger.info(`(${this.name}) Successfully retrieved new value!`);
 
      if (this.#targets.length === 0) {
        logger.error('Please provide a list of targets');
 
        if (this.revert) {
          await this.revert();
        }
 
        return;
      }
 
      await Promise.all(
        this.#targets.map(async (target) => {
          logger.silly(`(${target.name}) Updating...`);
          await target.target(target.prefix(keyInfos));
          logger.info(`(${target.name}) Successfully updated!`);
        })
      );
 
      logger.silly('Successfully updated targets!');
 
      if (this.cleanup) {
        await this.cleanup();
      }
    } catch (error) {
      logger.error('Error detected, reverting to previous state...', error);
 
      await Promise.all(
        this.#targets.map(async (target) => {
          logger.silly(`(${target.name}) Reverting...`);
          await target.target(target.prefix(this.originalKeyInfos));
          logger.silly(`(${target.name}) Successfully reverted!`);
        })
      );
 
      logger.info('Successfully reverted targets!');
 
      if (this.revert) {
        logger.silly(`(${this.name}) Reverting...`);
        await this.revert();
        logger.info(`(${this.name}) Successfully reverted!`);
      }
 
      throw error;
    }
  }
 
  abstract source(): Promise<KeyInfo[]>;
}
 
export interface SourceModule {
  /**
   * Revert any changes we did and put everything back the way it was
   */
  revert?(): Promise<void>;
 
  /**
   * Nothing went wrong, we just need to cleanup any old information!
   */
  cleanup?(): Promise<void>;
}
 
export namespace SourceModule {
  export type Options = {
    targets: TargetModule[];
    prefix?: string;
  };
}