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.
- package/bin/counterfact.js +36 -1
- package/dist/app.js +1 -1
- package/dist/migrate/paths-to-routes.js +29 -0
- package/dist/typescript-generator/generate.js +1 -1
- package/dist/typescript-generator/operation-coder.js +1 -1
- package/dist/typescript-generator/operation-type-coder.js +1 -1
- package/dist/typescript-generator/repository.js +8 -5
- package/dist/typescript-generator/response-type-coder.js +1 -1
- package/dist/typescript-generator/schema-coder.js +1 -1
- package/dist/typescript-generator/schema-type-coder.js +1 -1
- package/package.json +1 -1
package/bin/counterfact.js
CHANGED
|
@@ -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, "
|
|
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(`
|
|
50
|
+
.get(`routes${key}.ts`)
|
|
51
51
|
.export(new OperationCoder(operation, requestMethod, securitySchemes));
|
|
52
52
|
});
|
|
53
53
|
});
|
|
@@ -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("
|
|
54
|
-
const shouldWriteTypes = types && !path.startsWith("
|
|
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, "
|
|
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
|
|
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, "
|
|
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 `
|
|
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 `
|
|
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 `
|
|
73
|
+
return `types/${this.requirement.data.$ref.replace(/^#\//u, "")}.ts`;
|
|
74
74
|
}
|
|
75
75
|
writeCode(script) {
|
|
76
76
|
// script.comments = READ_ONLY_COMMENTS;
|