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

pmcelhaney / counterfact / 5837079355

11 Aug 2023 09:10PM UTC coverage: 87.031%. Remained the same
5837079355

push

github

web-flow
Merge pull request #494 from pmcelhaney/shebangs-shebangs

fix an issue where ts-node was not being used

407 of 434 branches covered (93.78%)

Branch coverage included in aggregate %.

1821 of 2126 relevant lines covered (85.65%)

21.22 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

8✔
13
  registry;
8✔
14

8✔
15
  watcher;
8✔
16

8✔
17
  contextRegistry;
1✔
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

11✔
97
      try {
11✔
98
        // eslint-disable-next-line  import/no-dynamic-require, no-unsanitized/method
11✔
99
        const endpoint = await import(fullPath);
11✔
100

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

12✔
114
    await Promise.all(imports);
12✔
115
  }
12✔
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