3
1

util.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Options, Proposals } from "../../types"
  2. const moment = require("moment")
  3. export const parseArgs = (args: string[]): Options => {
  4. const inArgs = (term: string): boolean => {
  5. return args.find(a => a.search(term) > -1) ? true : false;
  6. };
  7. const options: Options = {
  8. verbose: inArgs("--verbose") ? 2 : inArgs("--quiet") ? 0 : 1,
  9. channel: inArgs("--channel"),
  10. council: inArgs("--council"),
  11. forum: inArgs("--forum"),
  12. proposals: inArgs("--proposals")
  13. };
  14. if (options.verbose > 1) console.debug("args", args, "\noptions", options);
  15. return options;
  16. };
  17. export const printStatus = (
  18. opts: Options,
  19. data: {
  20. block: number;
  21. cats: number[];
  22. chain: string;
  23. posts: number[];
  24. proposals: Proposals;
  25. threads: number[];
  26. }
  27. ): void => {
  28. if (opts.verbose < 1) return;
  29. const { block, chain, proposals, cats, posts, threads } = data;
  30. const date = formatTime();
  31. let message = `[${date}] Chain:${chain} Block:${block} `;
  32. if (opts.forum)
  33. message += `Post:${posts[1]} Cat:${cats[1]} Thread:${threads[1]} `;
  34. if (opts.proposals)
  35. message += `Proposals:${proposals.current} (Active:${proposals.active.length} Pending:${proposals.executing.length}) `;
  36. console.log(message);
  37. };
  38. // time
  39. export const formatTime = (time?: any): string =>
  40. moment(time).format("H:mm:ss");
  41. export const passedTime = (start: number, now: number): string =>
  42. formatTime(moment.utc(moment(now).diff(moment(start))));
  43. export const exit = (log: (s: string) => void) => {
  44. log("\nNo connection, exiting.\n");
  45. process.exit();
  46. };