counterfact 0.25.0 → 0.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,5 +35,5 @@ jobs:
35
35
  - name: Install Packages
36
36
  run: yarn install --frozen-lockfile --network-timeout 100000
37
37
  - name: Try running the petstore
38
- run: npx . https://petstore3.swagger.io/api/v3/openapi.json out
38
+ run: npx . ./openapi-example.yaml out
39
39
  timeout-minutes: 2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # counterfact
2
2
 
3
+ ## 0.25.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 226ce37: fixed a couple more issues in Windows
8
+
3
9
  ## 0.25.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "counterfact",
3
- "version": "0.25.0",
3
+ "version": "0.25.1",
4
4
  "description": "a library for building a fake REST API for testing",
5
5
  "type": "module",
6
6
  "main": "./src/server/counterfact.js",
@@ -2,12 +2,16 @@ import fs from "node:fs/promises";
2
2
  import { existsSync } from "node:fs";
3
3
  import nodePath from "node:path";
4
4
  import { once } from "node:events";
5
+ import { pathToFileURL } from "node:url";
5
6
 
6
7
  import chokidar from "chokidar";
8
+ import createDebug from "debug";
7
9
 
8
10
  import { ContextRegistry } from "./context-registry.js";
9
11
  import { CHOKIDAR_OPTIONS } from "./constants.js";
10
12
 
13
+ const debug = createDebug("counterfact:typescript-generator:module-loader");
14
+
11
15
  export class ModuleLoader extends EventTarget {
12
16
  basePath;
13
17
 
@@ -28,6 +32,7 @@ export class ModuleLoader extends EventTarget {
28
32
  this.watcher = chokidar
29
33
  .watch(`${this.basePath}/**/*.{js,mjs,ts,mts}`, CHOKIDAR_OPTIONS)
30
34
 
35
+ // eslint-disable-next-line max-statements
31
36
  .on("all", (eventName, pathNameOriginal) => {
32
37
  const pathName = pathNameOriginal.replaceAll("\\", "/");
33
38
 
@@ -47,10 +52,15 @@ export class ModuleLoader extends EventTarget {
47
52
  return;
48
53
  }
49
54
 
55
+ const fileUrl = `${pathToFileURL(pathName)}?cacheBust=${Date.now()}`;
56
+
57
+ debug("importing module: %s", fileUrl);
58
+
50
59
  // eslint-disable-next-line import/no-dynamic-require, no-unsanitized/method
51
- import(`${pathName}?cacheBust=${Date.now()}`)
60
+ import(fileUrl)
52
61
  // eslint-disable-next-line promise/prefer-await-to-then
53
62
  .then((endpoint) => {
63
+ debug("imported module: %s", fileUrl);
54
64
  this.dispatchEvent(new Event(eventName), pathName);
55
65
 
56
66
  if (pathName.includes("$.context")) {
@@ -65,7 +75,7 @@ export class ModuleLoader extends EventTarget {
65
75
  })
66
76
  // eslint-disable-next-line promise/prefer-await-to-then
67
77
  .catch((error) => {
68
- process.stdout.write(`\nError loading ${pathName}:\n${error}\n`);
78
+ process.stdout.write(`\nError loading ${fileUrl}:\n${error}\n`);
69
79
  });
70
80
  });
71
81
 
@@ -111,8 +121,14 @@ export class ModuleLoader extends EventTarget {
111
121
  .replaceAll("\\", "/");
112
122
 
113
123
  try {
114
- // eslint-disable-next-line import/no-dynamic-require, no-unsanitized/method
115
- const endpoint = await import(fullPath);
124
+ const fileUrl = `${pathToFileURL(fullPath)}?cacheBust=${Date.now()}`;
125
+
126
+ debug("* importing module: %s", fileUrl);
127
+
128
+ // eslint-disable-next-line import/no-dynamic-require, no-unsanitized/method
129
+ const endpoint = await import(fileUrl);
130
+
131
+ debug("* imported module: %s", fileUrl);
116
132
 
117
133
  if (file.name.includes("$.context")) {
118
134
  this.contextRegistry.add(`/${directory}`, endpoint.default);
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable max-statements */
2
- import nodePath from "node:path";
3
- import { pathToFileURL } from "node:url";
2
+ import nodePath, { dirname } from "node:path";
3
+ import { pathToFileURL, fileURLToPath } from "node:url";
4
4
 
5
5
  import yaml from "js-yaml";
6
6
  import Koa from "koa";
@@ -13,7 +13,7 @@ import { readFile } from "../util/read-file.js";
13
13
  import { counterfact } from "./counterfact.js";
14
14
 
15
15
  // eslint-disable-next-line no-underscore-dangle
16
- const __dirname = nodePath.dirname(new URL(import.meta.url).pathname);
16
+ const __dirname = dirname(fileURLToPath(import.meta.url));
17
17
 
18
18
  const DEFAULT_PORT = 3100;
19
19
 
@@ -41,11 +41,7 @@ class Context {
41
41
  return "new Context()";
42
42
  }
43
43
 
44
- const parentPath = nodePath.normalize(
45
- nodePath.join(script.path, "../../$.context.ts").replaceAll("\\", "/")
46
- );
47
-
48
- script.repository.get(parentPath).exportDefault(this);
44
+ script.repository.get(script.path).exportDefault(this);
49
45
 
50
46
  return { raw: 'export { default } from "../$.context.mjs"' };
51
47
  }
@@ -6,6 +6,7 @@ import { OperationCoder } from "./operation-coder.js";
6
6
 
7
7
  const debug = createDebug("counterfact:typescript-generator:generate");
8
8
 
9
+ // eslint-disable-next-line max-statements
9
10
  export async function generate(
10
11
  source,
11
12
  destination,
@@ -23,7 +24,7 @@ export async function generate(
23
24
 
24
25
  const paths = await specification.requirementAt("#/paths");
25
26
 
26
- debug("got %n paths", paths.size);
27
+ debug("got %i paths", paths.size);
27
28
 
28
29
  paths.forEach((pathDefinition, key) => {
29
30
  debug("processing path %s", key);
@@ -35,4 +36,6 @@ export async function generate(
35
36
  debug("telling the repository to write the files to %s", destination);
36
37
 
37
38
  await repository.writeFiles(destination);
39
+
40
+ debug("finished writing the files");
38
41
  }
@@ -1,13 +1,19 @@
1
- import nodePath from "node:path";
1
+ import nodePath, { dirname } from "node:path";
2
2
  import fs from "node:fs/promises";
3
3
  import { constants as fsConstants } from "node:fs";
4
+ import { fileURLToPath } from "node:url";
4
5
 
5
6
  import prettier from "prettier";
7
+ import createDebug from "debug";
6
8
 
7
9
  import { Script } from "./script.js";
8
10
 
11
+ const debug = createDebug("counterfact:typescript-generator:repository");
12
+
9
13
  // eslint-disable-next-line no-underscore-dangle
10
- const __dirname = nodePath.dirname(new URL(import.meta.url).pathname);
14
+ const __dirname = dirname(fileURLToPath(import.meta.url));
15
+
16
+ debug("dirname is %s", __dirname);
11
17
 
12
18
  async function ensureDirectoryExists(filePath) {
13
19
  const directory = nodePath.dirname(filePath);
@@ -25,10 +31,16 @@ export class Repository {
25
31
  }
26
32
 
27
33
  get(path) {
34
+ debug("getting script at %s", path);
35
+
28
36
  if (this.scripts.has(path)) {
37
+ debug("already have script %s, returning it", path);
38
+
29
39
  return this.scripts.get(path);
30
40
  }
31
41
 
42
+ debug("don't have %s, creating it", path);
43
+
32
44
  const script = new Script(this, path);
33
45
 
34
46
  this.scripts.set(path, script);
@@ -40,6 +52,7 @@ export class Repository {
40
52
  while (
41
53
  Array.from(this.scripts.values()).some((script) => script.isInProgress())
42
54
  ) {
55
+ debug("waiting for %i scripts to finish", this.scripts.size);
43
56
  // eslint-disable-next-line no-await-in-loop
44
57
  await Promise.all(
45
58
  Array.from(this.scripts.values(), (script) => script.finished())
@@ -48,7 +61,7 @@ export class Repository {
48
61
  }
49
62
 
50
63
  copyCoreFiles(destination) {
51
- const files = ["package.json", "response-builder-factory.ts"];
64
+ const files = ["response-builder-factory.ts"];
52
65
 
53
66
  return files.map((file) => {
54
67
  const path = nodePath.join(destination, file).replaceAll("\\", "/");
@@ -65,7 +78,12 @@ export class Repository {
65
78
  }
66
79
 
67
80
  async writeFiles(destination) {
81
+ debug(
82
+ "waiting for %i or more scripts to finish before writing files",
83
+ this.scripts.size
84
+ );
68
85
  await this.finished();
86
+ debug("all %i scripts are finished", this.scripts.size);
69
87
 
70
88
  const writeFiles = Array.from(
71
89
  this.scripts.entries(),
@@ -90,7 +108,9 @@ export class Repository {
90
108
  return;
91
109
  }
92
110
 
111
+ debug("about to write", fullPath);
93
112
  await fs.writeFile(fullPath, contents);
113
+ debug("did write", fullPath);
94
114
 
95
115
  process.stdout.write(`writing ${fullPath}\n`);
96
116
  }
@@ -1,6 +1,9 @@
1
1
  import nodePath from "node:path";
2
2
 
3
3
  import prettier from "prettier";
4
+ import createDebugger from "debug";
5
+
6
+ const debug = createDebugger("counterfact:typescript-generator:script");
4
7
 
5
8
  export class Script {
6
9
  constructor(repository, path) {
@@ -73,15 +76,24 @@ export class Script {
73
76
  this.export(coder, isType, true);
74
77
  }
75
78
 
79
+ // eslint-disable-next-line max-statements
76
80
  import(coder, isType = false, isDefault = false) {
81
+ debug("import coder: %s", coder.id);
82
+
77
83
  const modulePath = coder.modulePath();
78
84
 
79
85
  const cacheKey = `${coder.id}@${modulePath}:${isType}:${isDefault}`;
80
86
 
87
+ debug("cache key: %s", cacheKey);
88
+
81
89
  if (this.cache.has(cacheKey)) {
90
+ debug("cache hit: %s", cacheKey);
91
+
82
92
  return this.cache.get(cacheKey);
83
93
  }
84
94
 
95
+ debug("cache miss: %s", cacheKey);
96
+
85
97
  const name = this.firstUniqueName(coder);
86
98
 
87
99
  this.cache.set(cacheKey, name);
@@ -1,4 +0,0 @@
1
- {
2
- "description": "This file ensures the .ts files herein are treated as ESM modules. If you have another package.json with type: module in a parent directory, you can delete this file.",
3
- "type": "module"
4
- }