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

pmcelhaney / counterfact / 9307669503

30 May 2024 06:49PM UTC coverage: 87.241% (-0.8%) from 87.991%
9307669503

push

github

web-flow
Merge pull request #919 from pmcelhaney/types-directory

directory structure change

984 of 1094 branches covered (89.95%)

Branch coverage included in aggregate %.

18 of 56 new or added lines in 9 files covered. (32.14%)

2 existing lines in 1 file now uncovered.

3228 of 3734 relevant lines covered (86.45%)

44.33 hits per line

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

92.86
/src/typescript-generator/repository.js
1
/* eslint-disable max-statements */
2✔
2
import { existsSync } from "node:fs";
2✔
3
import fs from "node:fs/promises";
2✔
4
import nodePath, { dirname } from "node:path";
2✔
5
import { fileURLToPath } from "node:url";
2✔
6

2✔
7
import createDebug from "debug";
2✔
8

2✔
9
import { ensureDirectoryExists } from "../util/ensure-directory-exists.js";
2✔
10
import { CONTEXT_FILE_TOKEN } from "./context-file-token.js";
2✔
11
import { Script } from "./script.js";
2✔
12

2✔
13
const debug = createDebug("counterfact:server:repository");
2✔
14

2✔
15
// eslint-disable-next-line no-underscore-dangle
2✔
16
const __dirname = dirname(fileURLToPath(import.meta.url)).replaceAll("\\", "/");
2✔
17

2✔
18
debug("dirname is %s", __dirname);
2✔
19

2✔
20
export class Repository {
2✔
21
  constructor() {
2✔
22
    this.scripts = new Map();
46✔
23
  }
46✔
24

2✔
25
  get(path) {
2✔
26
    debug("getting script at %s", path);
186✔
27

186✔
28
    if (this.scripts.has(path)) {
186✔
29
      debug("already have script %s, returning it", path);
60✔
30

60✔
31
      return this.scripts.get(path);
60✔
32
    }
60✔
33

126✔
34
    debug("don't have %s, creating it", path);
126✔
35

126✔
36
    const script = new Script(this, path);
126✔
37

126✔
38
    this.scripts.set(path, script);
126✔
39

126✔
40
    return script;
126✔
41
  }
126✔
42

2✔
43
  async finished() {
2✔
44
    while (
16✔
45
      Array.from(this.scripts.values()).some((script) => script.isInProgress())
16✔
46
    ) {
16✔
47
      debug("waiting for %i scripts to finish", this.scripts.size);
10✔
48
      // eslint-disable-next-line no-await-in-loop
10✔
49
      await Promise.all(
10✔
50
        Array.from(this.scripts.values(), (script) => script.finished()),
10✔
51
      );
10✔
52
    }
10✔
53
  }
16✔
54

2✔
55
  async copyCoreFiles(destination) {
2✔
56
    const sourcePath = nodePath.join(__dirname, "../../dist/server/types.d.ts");
14✔
57
    const destinationPath = nodePath.join(destination, "types.d.ts");
14✔
58

14✔
59
    if (!existsSync(sourcePath)) {
14✔
60
      return false;
14✔
61
    }
14!
62

×
63
    await ensureDirectoryExists(destination);
×
64

×
65
    return fs.copyFile(sourcePath, destinationPath);
×
66
  }
×
67

2✔
68
  async writeFiles(destination, { routes, types }) {
2✔
69
    debug(
14✔
70
      "waiting for %i or more scripts to finish before writing files",
14✔
71
      this.scripts.size,
14✔
72
    );
14✔
73
    await this.finished();
14✔
74
    debug("all %i scripts are finished", this.scripts.size);
14✔
75

14✔
76
    const writeFiles = Array.from(
14✔
77
      this.scripts.entries(),
14✔
78

14✔
79
      async ([path, script]) => {
14✔
80
        const contents = await script.contents();
24✔
81

24✔
82
        const fullPath = nodePath.join(destination, path).replaceAll("\\", "/");
24✔
83

24✔
84
        await ensureDirectoryExists(fullPath);
24✔
85

24✔
86
        const shouldWriteRoutes = routes && path.startsWith("routes");
24✔
87
        const shouldWriteTypes = types && !path.startsWith("routes");
24✔
88

24✔
89
        if (
24✔
90
          shouldWriteRoutes &&
24✔
91
          (await fs
8✔
92
            .stat(fullPath)
8✔
93
            .then((stat) => stat.isFile())
8✔
94
            .catch(() => false))
8✔
95
        ) {
24!
96
          debug(`not overwriting ${fullPath}\n`);
×
97

×
98
          return;
×
99
        }
×
100

24✔
101
        if (shouldWriteRoutes || shouldWriteTypes) {
24✔
102
          debug("about to write", fullPath);
16✔
103
          await fs.writeFile(
16✔
104
            fullPath,
16✔
105
            contents.replaceAll(
16✔
106
              CONTEXT_FILE_TOKEN,
16✔
107
              this.findContextPath(destination, path),
16✔
108
            ),
16✔
109
          );
16✔
110
          debug("did write", fullPath);
16✔
111
        }
16✔
112
      },
24✔
113
    );
14✔
114

14✔
115
    await Promise.all(writeFiles);
14✔
116

14✔
117
    await this.copyCoreFiles(destination);
14✔
118

14✔
119
    if (routes) {
14✔
120
      await this.createDefaultContextFile(destination);
12✔
121
    }
12✔
122
  }
14✔
123

2✔
124
  async createDefaultContextFile(destination) {
2✔
125
    const contextFilePath = nodePath.join(
12✔
126
      destination,
12✔
127
      "routes",
12✔
128
      "_.context.ts",
12✔
129
    );
12✔
130

12✔
131
    if (existsSync(contextFilePath)) {
12!
UNCOV
132
      return;
×
UNCOV
133
    }
×
134

12✔
135
    await ensureDirectoryExists(contextFilePath);
12✔
136

12✔
137
    await fs.writeFile(
12✔
138
      contextFilePath,
12✔
139
      `/**
12✔
140
* This is the default context for Counterfact.
12✔
141
*
12✔
142
* It defines the context object in the REPL 
12✔
143
* and the $.context object in the code.
12✔
144
*
12✔
145
* Add properties and methods to suit your needs.
12✔
146
* 
12✔
147
* See https://counterfact.dev/docs/usage.html#working-with-state-the-codecontextcode-object-and-codecontexttscode
12✔
148
*/
12✔
149
export class Context {
12✔
150

12✔
151
}
12✔
152
`,
12✔
153
    );
12✔
154
  }
12✔
155

2✔
156
  findContextPath(destination, path) {
2✔
157
    return nodePath
24✔
158
      .relative(
24✔
159
        nodePath.join(destination, nodePath.dirname(path)),
24✔
160
        this.nearestContextFile(destination, path),
24✔
161
      )
24✔
162
      .replaceAll("\\", "/");
24✔
163
  }
24✔
164

2✔
165
  nearestContextFile(destination, path) {
2✔
166
    const directory = nodePath
56✔
167
      .dirname(path)
56✔
168
      .replaceAll("\\", "/")
56✔
169
      .replace("types/paths", "routes");
56✔
170

56✔
171
    const candidate = nodePath.join(destination, directory, "_.context.ts");
56✔
172

56✔
173
    if (directory.length <= 1) {
56✔
174
      // No _context.ts was found so import the one that should be in the root
16✔
175
      return nodePath.join(destination, "routes", "_.context.ts");
16✔
176
    }
16✔
177

40✔
178
    if (existsSync(candidate)) {
56✔
179
      return candidate;
8✔
180
    }
8✔
181

32✔
182
    return this.nearestContextFile(destination, nodePath.join(path, ".."));
32✔
183
  }
32✔
184
}
2✔
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

© 2026 Coveralls, Inc