counterfact 0.43.2 → 0.44.0

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.
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /* eslint-disable complexity */
3
3
 
4
+ import fs from "node:fs";
4
5
  import { readFile } from "node:fs/promises";
5
6
  import nodePath from "node:path";
6
7
  import { fileURLToPath } from "node:url";
@@ -97,7 +98,7 @@ function createWatchMessage(config) {
97
98
  return watchMessage;
98
99
  }
99
100
 
100
- // eslint-disable-next-line max-statements
101
+ // eslint-disable-next-line max-statements, sonarjs/cognitive-complexity
101
102
  async function main(source, destination) {
102
103
  debug("executing the main function");
103
104
 
@@ -165,6 +166,23 @@ async function main(source, destination) {
165
166
 
166
167
  debug("loading counterfact (%o)", config);
167
168
 
169
+ let didMigrate = false;
170
+
171
+ // eslint-disable-next-line n/no-sync
172
+ if (fs.existsSync(nodePath.join(config.basePath, "paths"))) {
173
+ await fs.promises.rmdir(nodePath.join(config.basePath, "paths"), {
174
+ recursive: true,
175
+ });
176
+ await fs.promises.rmdir(nodePath.join(config.basePath, "path-types"), {
177
+ recursive: true,
178
+ });
179
+ await fs.promises.rmdir(nodePath.join(config.basePath, "components"), {
180
+ recursive: true,
181
+ });
182
+
183
+ didMigrate = true;
184
+ }
185
+
168
186
  const { start } = await counterfact(config);
169
187
 
170
188
  debug("loaded counterfact", config);
@@ -205,6 +223,23 @@ async function main(source, destination) {
205
223
  await open(guiUrl);
206
224
  debug("opened browser");
207
225
  }
226
+
227
+ if (didMigrate) {
228
+ process.stdout.write("\n\n\n*******************************\n");
229
+ process.stdout.write("MIGRATING TO NEW FILE STRUCTURE\n\n");
230
+ process.stdout.write(
231
+ "In preparation for version 1.0, Counterfact has migrated to a new file structure.\n",
232
+ );
233
+ process.stdout.write("- The paths directory has been renamed to routes.\n");
234
+ process.stdout.write(
235
+ "- The path-types and components directories are now stored under types.\n",
236
+ );
237
+ process.stdout.write("Your files have automatically been migrated.\n");
238
+ process.stdout.write(
239
+ "Please report any issues to https://github.com/pmcelhaney/counterfact/issues\n",
240
+ );
241
+ process.stdout.write("*******************************\n\n\n");
242
+ }
208
243
  }
209
244
 
210
245
  program
package/dist/app.js CHANGED
@@ -36,7 +36,7 @@ export async function counterfact(config) {
36
36
  const contextRegistry = new ContextRegistry();
37
37
  const codeGenerator = new CodeGenerator(config.openApiPath, config.basePath, config.generate);
38
38
  const dispatcher = new Dispatcher(registry, contextRegistry, await loadOpenApiDocument(config.openApiPath));
39
- const transpiler = new Transpiler(nodePath.join(modulesPath, "paths").replaceAll("\\", "/"), compiledPathsDirectory, "commonjs");
39
+ const transpiler = new Transpiler(nodePath.join(modulesPath, "routes").replaceAll("\\", "/"), compiledPathsDirectory, "commonjs");
40
40
  const moduleLoader = new ModuleLoader(compiledPathsDirectory, registry, contextRegistry);
41
41
  const middleware = koaMiddleware(dispatcher, config);
42
42
  const koaApp = createKoaApp(registry, middleware, config);
@@ -0,0 +1,29 @@
1
+ /* eslint-disable no-await-in-loop */
2
+ /* eslint-disable no-console */
3
+ import { promises as fs } from "node:fs";
4
+ import path from "node:path";
5
+ // eslint-disable-next-line max-statements
6
+ async function copyAndModifyFiles(sourceDirectory, destinationDirectory) {
7
+ try {
8
+ const entries = await fs.readdir(sourceDirectory, { withFileTypes: true });
9
+ await fs.mkdir(destinationDirectory, { recursive: true });
10
+ for (const entry of entries) {
11
+ const sourcePath = path.join(sourceDirectory, entry.name);
12
+ const destinationPath = path.join(destinationDirectory, entry.name);
13
+ if (entry.isDirectory()) {
14
+ await copyAndModifyFiles(sourcePath, destinationPath);
15
+ }
16
+ else {
17
+ let fileContent = await fs.readFile(sourcePath, "utf8");
18
+ fileContent = fileContent.replaceAll("path-types", "types/paths");
19
+ await fs.writeFile(destinationPath, fileContent);
20
+ }
21
+ }
22
+ }
23
+ catch (error) {
24
+ console.error("Error copying and modifying files:", error);
25
+ }
26
+ }
27
+ export async function pathsToRoutes(rootDirectory) {
28
+ await copyAndModifyFiles(path.join(rootDirectory, "paths"), path.join(rootDirectory, "routes"));
29
+ }
@@ -47,7 +47,7 @@ export async function generate(source, destination, generateOptions, repository
47
47
  debug("processing path %s", key);
48
48
  pathDefinition.forEach((operation, requestMethod) => {
49
49
  repository
50
- .get(`paths${key}.ts`)
50
+ .get(`routes${key}.ts`)
51
51
  .export(new OperationCoder(operation, requestMethod, securitySchemes));
52
52
  });
53
53
  });
@@ -37,7 +37,7 @@ export class OperationCoder extends Coder {
37
37
  .at(-2)
38
38
  .replaceAll("~1", "/");
39
39
  return `${nodePath
40
- .join("path", pathString)
40
+ .join("routes", pathString)
41
41
  .replaceAll("\\", "/")}.types.ts`;
42
42
  }
43
43
  }
@@ -54,7 +54,7 @@ export class OperationTypeCoder extends TypeCoder {
54
54
  .at(-2)
55
55
  .replaceAll("~1", "/");
56
56
  return `${nodePath
57
- .join("path-types", pathString)
57
+ .join("types/paths", pathString)
58
58
  .replaceAll("\\", "/")}.types.ts`;
59
59
  }
60
60
  userType() {
@@ -50,8 +50,8 @@ export class Repository {
50
50
  const contents = await script.contents();
51
51
  const fullPath = nodePath.join(destination, path).replaceAll("\\", "/");
52
52
  await ensureDirectoryExists(fullPath);
53
- const shouldWriteRoutes = routes && path.startsWith("paths");
54
- const shouldWriteTypes = types && !path.startsWith("paths");
53
+ const shouldWriteRoutes = routes && path.startsWith("routes");
54
+ const shouldWriteTypes = types && !path.startsWith("routes");
55
55
  if (shouldWriteRoutes &&
56
56
  (await fs
57
57
  .stat(fullPath)
@@ -73,7 +73,7 @@ export class Repository {
73
73
  }
74
74
  }
75
75
  async createDefaultContextFile(destination) {
76
- const contextFilePath = nodePath.join(destination, "paths", "_.context.ts");
76
+ const contextFilePath = nodePath.join(destination, "routes", "_.context.ts");
77
77
  if (existsSync(contextFilePath)) {
78
78
  return;
79
79
  }
@@ -99,11 +99,14 @@ export class Context {
99
99
  .replaceAll("\\", "/");
100
100
  }
101
101
  nearestContextFile(destination, path) {
102
- const directory = nodePath.dirname(path).replace("path-types", "paths");
102
+ const directory = nodePath
103
+ .dirname(path)
104
+ .replaceAll("\\", "/")
105
+ .replace("types/paths", "routes");
103
106
  const candidate = nodePath.join(destination, directory, "_.context.ts");
104
107
  if (directory.length <= 1) {
105
108
  // No _context.ts was found so import the one that should be in the root
106
- return nodePath.join(destination, "paths", "_.context.ts");
109
+ return nodePath.join(destination, "routes", "_.context.ts");
107
110
  }
108
111
  if (existsSync(candidate)) {
109
112
  return candidate;
@@ -53,7 +53,7 @@ export class ResponseTypeCoder extends TypeCoder {
53
53
  return requiredHeaders.length === 0 ? "never" : requiredHeaders.join(" | ");
54
54
  }
55
55
  modulePath() {
56
- return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
56
+ return `types/${this.requirement.data.$ref}.ts`;
57
57
  }
58
58
  writeCode(script) {
59
59
  return `{
@@ -34,7 +34,7 @@ export class SchemaCoder extends Coder {
34
34
  return script.importExternalType("JSONSchema6", "json-schema");
35
35
  }
36
36
  modulePath() {
37
- return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
37
+ return `types/${this.requirement.data.$ref}.ts`;
38
38
  }
39
39
  writeCode(script) {
40
40
  const { type } = this.requirement.data;
@@ -70,7 +70,7 @@ export class SchemaTypeCoder extends TypeCoder {
70
70
  .join(" | ");
71
71
  }
72
72
  modulePath() {
73
- return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
73
+ return `types/${this.requirement.data.$ref.replace(/^#\//u, "")}.ts`;
74
74
  }
75
75
  writeCode(script) {
76
76
  // script.comments = READ_ONLY_COMMENTS;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "counterfact",
3
- "version": "0.43.2",
3
+ "version": "0.44.0",
4
4
  "description": "a library for building a fake REST API for testing",
5
5
  "type": "module",
6
6
  "main": "./src/server/counterfact.js",