aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-ecosystem/src/runner.ts
blob: 8b4e42aa7f86952a6e5bc799065c9c8ae23878ed (plain) (blame)
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
import type { Package } from "./packages";
import { packages } from "./packages";
import { existsSync, copyFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { globby } from "globby";

for (const pkg of packages) {
  try {
    await loadPackage(pkg, "tmp");
  } catch (error) {
    console.error(pkg.name, error);
  }
}

async function loadPackage(pkg: Package, cwd?: string): Promise<void> {
  await gitClone({
    cwd,
    repository: pkg.repository,
    name: pkg.name,
  });
  const dir = join(cwd ?? "", pkg.name, pkg.cwd ?? "");
  await spawn({
    cwd: dir,
    cmd: ["bun", "install"],
  });
  if (!pkg.tests || pkg.tests.style !== "jest") {
    return;
  }
  const files = await globby(pkg.tests.include, {
    cwd: dir,
    ignore: pkg.tests.exclude ?? [crypto.randomUUID()],
    onlyFiles: true,
    caseSensitiveMatch: false,
  });
  if (!files.length) {
    throw new Error("No tests found");
  }
  for (const file of files) {
    let path = file;
    if (!file.includes(".test.")) {
      const ext = path.lastIndexOf(".");
      path = file.substring(0, ext) + ".test" + file.substring(ext);
      copyFileSync(join(dir, file), join(dir, path));
    }
    await spawn({
      cwd: dir,
      cmd: ["bun", "wiptest", path],
    });
  }
}

type GitCloneOptions = {
  repository: string;
  cwd?: string;
  name?: string;
};

async function gitClone(options: GitCloneOptions): Promise<void> {
  const name = options.name ?? dirname(options.repository);
  const cwd = options.cwd ?? process.cwd();
  const path = join(cwd, name);
  if (existsSync(path)) {
    await spawn({
      cwd: path,
      cmd: ["git", "pull"],
    });
  } else {
    const url = `${options.repository}`;
    await spawn({
      cwd,
      cmd: ["git", "clone", "--single-branch", "--depth", "1", url, name],
    });
  }
}

type SpawnOptions = {
  cwd: string;
  cmd: string[];
};

async function spawn({ cwd, cmd }: SpawnOptions) {
  const { exited } = await Bun.spawn({
    cwd,
    cmd,
    stdout: "inherit",
    stderr: "inherit",
  });
  const exitCode = await exited;
  if (exitCode !== 0) {
    throw new Error(`"${cmd.join(" ")}" exited with ${exitCode}`);
  }
}