All files / src cli.ts

0% Statements 0/52
0% Branches 0/6
0% Functions 0/6
0% Lines 0/42

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                                                                                                                   
import colors from "colors";
import {ReferenceStore, ReferenceConfiguration, AddReferenceConfiguration} from "./store";
import {ElectroInstance, InstanceReader} from "./instance";
import generate from "./generate";
import httpServer from "./serve";
 
export function typeDef(filepath: string, output?: string): string | undefined {
  return generate(filepath, output);
}
 
export function add(configurationLocation: string, filePath: string, {label, overwrite, table, endpoint, region}: AddReferenceConfiguration = {}): string {
  const store = new ReferenceStore(configurationLocation);
  const config = new ReferenceConfiguration(store);
  let instanceReader = new InstanceReader(filePath);
  let [, instance] = instanceReader.get({table, endpoint, region});
  let electro = new ElectroInstance(instance);
  return config.add(filePath, electro, label, {overwrite, table, endpoint, region});
}
 
export function remove(configurationLocation: string, service: string): string {
  const store = new ReferenceStore(configurationLocation);
  const config = new ReferenceConfiguration(store);
  return config.remove(service);
}
 
export function list(configurationLocation: string): string {
  const store = new ReferenceStore(configurationLocation);
  const config = new ReferenceConfiguration(store);
  return config.list();
}
 
export function serve(configurationLocation: string, port: number) {
  let services = getElectroInstances(configurationLocation);
  httpServer(port, services);
}
 
export function getElectroInstances(location: string): ElectroInstance[] {
  const store = new ReferenceStore(location);
  let services = store.get();
  let instances: ElectroInstance[] = [];
  for (let name of Object.keys(services)) {
    let reader;
    let instance;
    try {
      reader = new InstanceReader(services[name].filePath);
      [,instance] = reader.get(services[name]);
    } catch(err) {
      console.log(colors.red(`Error loading service "${name}": ${err.message} - Remove this entity using 'remove' command or use the 'add' command with the --force flag to update the file path.`))
    }
    if (instance === undefined || reader === undefined) {
      continue;
    }
    let e = new ElectroInstance(instance);
    e.setName(name);
    instances.push(e);
  }
  return instances;
}