summaryrefslogtreecommitdiffstats
path: root/src/utils.ts
blob: fd8a26d766daff7d5e64b7b7c5ee971c8eb99d39 (plain)
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
import { exec } from "child_process";

export const asyncExec = (command: string) => {
  return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
        console.error(`exec error: ${error}`);
        reject(error);
      } else {
        resolve({ stdout, stderr });
      }
    });
  });
};

export const wait = (ms: number) =>
  new Promise((resolve) => setTimeout(resolve, ms));

export const runWithRetries = async <T>(
  fn: () => Promise<T>,
  retries: number,
  backoff: number = 2000,
): Promise<T> => {
  let attempts = 0;
  while (true) {
    await wait(attempts * backoff);
    try {
      return await fn();
    } catch (e) {
      if (attempts >= retries) {
        throw e;
      }

      console.error("Error:", e);
      attempts++;
    }
  }
};