• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmcelhaney / counterfact / 3919348865

pending completion
3919348865

push

github

GitHub
Update dependency eslint-config-hardcore to v26

398 of 420 branches covered (94.76%)

Branch coverage included in aggregate %.

1802 of 2023 relevant lines covered (89.08%)

22.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.15
/src/server/module-loader.js
1
import fs from "node:fs/promises";
1✔
2
import { existsSync } from "node:fs";
1✔
3
import nodePath from "node:path";
1✔
4
import { once } from "node:events";
1✔
5

1✔
6
import chokidar from "chokidar";
1✔
7

1✔
8
import { ContextRegistry } from "./context-registry.js";
1✔
9

1✔
10
export class ModuleLoader extends EventTarget {
1✔
11
  basePath;
1✔
12

7✔
13
  registry;
7✔
14

7✔
15
  watcher;
7✔
16

7✔
17
  contextRegistry;
1✔
18

1✔
19
  constructor(basePath, registry, contextRegistry = new ContextRegistry()) {
1✔
20
    super();
7✔
21
    this.basePath = basePath;
7✔
22
    this.registry = registry;
7✔
23
    this.contextRegistry = contextRegistry;
7✔
24
  }
7✔
25

1✔
26
  async watch() {
1✔
27
    this.watcher = chokidar
5✔
28
      .watch(`${this.basePath}/**/*.{js,mjs,ts,mts}`)
5✔
29
      .on("all", (eventName, pathName) => {
5✔
30
        if (!["add", "change", "unlink"].includes(eventName)) {
8!
31
          return;
×
32
        }
×
33

8✔
34
        const parts = nodePath.parse(pathName.replace(this.basePath, ""));
8✔
35
        const url = nodePath.normalize(
8✔
36
          `/${nodePath.join(parts.dir, parts.name)}`
8✔
37
        );
8✔
38

8✔
39
        if (eventName === "unlink") {
8✔
40
          this.registry.remove(url);
1✔
41
          this.dispatchEvent(new Event("remove"), pathName);
1✔
42
        }
1✔
43

8✔
44
        // eslint-disable-next-line node/no-unsupported-features/es-syntax, import/no-dynamic-require
8✔
45
        import(`${pathName}?cacheBust=${Date.now()}`)
8✔
46
          // eslint-disable-next-line promise/prefer-await-to-then
8✔
47
          .then((endpoint) => {
8✔
48
            this.dispatchEvent(new Event(eventName), pathName);
8✔
49

8✔
50
            if (pathName.includes("$.context")) {
8!
51
              this.contextRegistry.update(parts.dir, endpoint.default);
×
52

×
53
              return "context";
×
54
            }
×
55

8✔
56
            this.registry.add(url, endpoint);
8✔
57

8✔
58
            return "path";
8✔
59
          })
8✔
60
          // eslint-disable-next-line promise/prefer-await-to-then
8✔
61
          .catch((error) => {
8✔
62
            process.stdout.write(`\nError loading ${pathName}:\n${error}\n`);
×
63
          });
×
64
      });
8✔
65
    await once(this.watcher, "ready");
5✔
66
  }
5✔
67

1✔
68
  async stopWatching() {
1✔
69
    await this.watcher?.close();
5✔
70
  }
5✔
71

1✔
72
  async load(directory = "") {
1✔
73
    if (!existsSync(nodePath.join(this.basePath, directory))) {
11!
74
      throw new Error(`Directory does not exist ${this.basePath}`);
×
75
    }
×
76

11✔
77
    const files = await fs.readdir(nodePath.join(this.basePath, directory), {
11✔
78
      withFileTypes: true,
11✔
79
    });
11✔
80

11✔
81
    // eslint-disable-next-line max-statements
11✔
82
    const imports = files.flatMap(async (file) => {
11✔
83
      const extension = file.name.split(".").at(-1);
15✔
84

15✔
85
      if (file.isDirectory()) {
15✔
86
        await this.load(nodePath.join(directory, file.name));
4✔
87

4✔
88
        return;
4✔
89
      }
4✔
90

11✔
91
      if (!["js", "mjs", "ts", "mts"].includes(extension)) {
15✔
92
        return;
1✔
93
      }
1✔
94

10✔
95
      const fullPath = nodePath.join(this.basePath, directory, file.name);
10✔
96

10✔
97
      try {
10✔
98
        // eslint-disable-next-line node/no-unsupported-features/es-syntax, import/no-dynamic-require
10✔
99
        const endpoint = await import(fullPath);
10✔
100

10✔
101
        if (file.name.includes("$.context")) {
15✔
102
          this.contextRegistry.add(`/${directory}`, endpoint.default);
8✔
103
        } else {
8✔
104
          this.registry.add(
8✔
105
            `/${nodePath.join(directory, nodePath.parse(file.name).name)}`,
8✔
106
            endpoint
8✔
107
          );
8✔
108
        }
8✔
109
      } catch (error) {
15!
110
        process.stdout.write(`\nError loading ${fullPath}:\n${error}\n`);
×
111
      }
×
112
    });
15✔
113

11✔
114
    await Promise.all(imports);
11✔
115
  }
11✔
116
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc