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

pmcelhaney / counterfact / 5941980772

22 Aug 2023 05:15PM UTC coverage: 87.563% (+0.2%) from 87.318%
5941980772

Pull #377

github

pmcelhaney
maybe readFileSync will work?
Pull Request #377: run CI on Windows as well as Linux

430 of 457 branches covered (94.09%)

Branch coverage included in aggregate %.

206 of 206 new or added lines in 13 files covered. (100.0%)

1999 of 2317 relevant lines covered (86.28%)

21.17 hits per line

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

92.75
/src/server/transpiler.js
1
// Stryker disable all
1✔
2

1✔
3
import fs from "node:fs/promises";
1✔
4
import nodePath from "node:path";
1✔
5
import { existsSync, constants as fsConstants } from "node:fs";
1✔
6
import { once } from "node:events";
1✔
7

1✔
8
import ts from "typescript";
1✔
9
import chokidar from "chokidar";
1✔
10

1✔
11
import { CHOKIDAR_OPTIONS } from "./constants.js";
1✔
12

1✔
13
function log(...strings) {
60✔
14
  process.stdout.write(`[transpiler] ${strings.join("\t")}\n`);
60✔
15
}
60✔
16

1✔
17
async function ensureDirectoryExists(filePath) {
6✔
18
  const directory = nodePath.dirname(filePath);
6✔
19

6✔
20
  try {
6✔
21
    await fs.access(directory, fsConstants.W_OK);
6✔
22
  } catch {
6✔
23
    await fs.mkdir(directory, { recursive: true });
4✔
24
  }
4✔
25
}
6✔
26

1✔
27
export class Transpiler extends EventTarget {
1✔
28
  constructor(sourcePath, destinationPath) {
1✔
29
    super();
4✔
30
    this.sourcePath = sourcePath;
4✔
31
    this.destinationPath = destinationPath;
4✔
32
  }
4✔
33

1✔
34
  async watch() {
1✔
35
    this.watcher = chokidar.watch(`${this.sourcePath}/**/*.{ts,mts,js,mjs}`, {
4✔
36
      ignored: `${this.sourcePath}/js`,
4✔
37
      CHOKIDAR_OPTIONS,
4✔
38
    });
4✔
39

4✔
40
    const transpiles = [];
4✔
41

4✔
42
    // eslint-disable-next-line max-statements
4✔
43
    this.watcher.on("all", async (eventName, sourcePathOriginal) => {
4✔
44
      const sourcePath = sourcePathOriginal.replaceAll("\\", "/");
7✔
45

7✔
46
      const destinationPath = sourcePath
7✔
47
        .replace(this.sourcePath, this.destinationPath)
7✔
48
        .replaceAll("\\", "/")
7✔
49
        .replace(".ts", ".js");
7✔
50

7✔
51
      log("chokidar", eventName, sourcePath, destinationPath);
7✔
52

7✔
53
      if (["add", "change"].includes(eventName)) {
7✔
54
        transpiles.push(
6✔
55
          this.transpileFile(eventName, sourcePath, destinationPath)
6✔
56
        );
6✔
57
      }
6✔
58

7✔
59
      if (eventName === "unlink") {
7✔
60
        try {
1✔
61
          log("removing", destinationPath);
1✔
62
          await fs.rm(destinationPath);
1✔
63
        } catch (error) {
1✔
64
          if (error.code !== "ENOENT") {
1!
65
            log("ERROR: could not remove", destinationPath, error);
×
66

×
67
            throw error;
×
68
          }
×
69
        }
1✔
70

1✔
71
        this.dispatchEvent(new Event("delete"));
1✔
72
      }
1✔
73
    });
7✔
74

4✔
75
    log("waiting for watcher to be ready");
4✔
76
    await once(this.watcher, "ready");
4✔
77
    log("watcher is ready");
4✔
78

4✔
79
    log("waiting until", transpiles.length, "files are transpiled");
4✔
80

4✔
81
    await Promise.all(transpiles);
4✔
82

4✔
83
    log("done transpiling", transpiles.length, "files");
4✔
84
  }
4✔
85

1✔
86
  async stopWatching() {
1✔
87
    await this.watcher?.close();
8✔
88
  }
8✔
89

1✔
90
  // eslint-disable-next-line max-statements
1✔
91
  async transpileFile(eventName, sourcePath, destinationPath) {
1✔
92
    log("transpiling", sourcePath, "to", destinationPath, "because", eventName);
6✔
93
    log("first make sure the directory exists for", destinationPath);
6✔
94
    await ensureDirectoryExists(destinationPath);
6✔
95
    log("the directory does exist for", destinationPath);
6✔
96

6✔
97
    const source = await fs.readFile(sourcePath, "utf8");
6✔
98

6✔
99
    const result = ts.transpileModule(source, {
6✔
100
      compilerOptions: { module: ts.ModuleKind.ES2022 },
6✔
101
    }).outputText;
6✔
102

6✔
103
    const fullDestination = nodePath
6✔
104
      .join(
6✔
105
        sourcePath
6✔
106
          .replace(this.sourcePath, this.destinationPath)
6✔
107
          .replace(".ts", ".mjs")
6✔
108
      )
6✔
109
      .replaceAll("\\", "/");
6✔
110

6✔
111
    log("starting transpilation for", fullDestination);
6✔
112

6✔
113
    try {
6✔
114
      await fs.writeFile(fullDestination, result);
6✔
115
    } catch (error) {
6!
116
      log("ERROR: could not transpile", fullDestination, error);
×
117

×
118
      throw new Error("could not transpile");
×
119
    }
×
120

6✔
121
    log("finished transpilation for", fullDestination);
6✔
122

6✔
123
    log("is the file there?", existsSync(fullDestination));
6✔
124
    this.dispatchEvent(new Event("write"));
6✔
125
  }
6✔
126
}
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