Skip to content

Commit

Permalink
Merge branch 'main' into change-context-update-algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Feb 10, 2024
2 parents f1c360e + 8fb4e4f commit 539f77c
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 1,545 deletions.
8 changes: 8 additions & 0 deletions .changeset/spicy-mice-smell.md
@@ -0,0 +1,8 @@
---
"counterfact": patch
---

Fixed and simplified the way `\_.context.ts` files work.

- it's no longer necessary to have a `_.context.ts` file in every directory, only the ones where you want to establish a new context
- removed the need for `export type ContextType`
48 changes: 0 additions & 48 deletions src/typescript-generator/context-coder.js

This file was deleted.

1 change: 1 addition & 0 deletions src/typescript-generator/context-file-token.js
@@ -0,0 +1 @@
export const CONTEXT_FILE_TOKEN = "@@CONTEXT_FILE_TOKEN@@";
44 changes: 0 additions & 44 deletions src/typescript-generator/context-type-coder.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/typescript-generator/operation-type-coder.js
@@ -1,7 +1,7 @@
import nodePath from "node:path";

import { Coder } from "./coder.js";
import { ContextTypeCoder } from "./context-type-coder.js";
import { CONTEXT_FILE_TOKEN } from "./context-file-token.js";
import { ParametersTypeCoder } from "./parameters-type-coder.js";
import { ResponseTypeCoder } from "./response-type-coder.js";
import { SchemaTypeCoder } from "./schema-type-coder.js";
Expand Down Expand Up @@ -68,8 +68,9 @@ export class OperationTypeCoder extends Coder {
}

write(script) {
const contextTypeImportName = script.importType(
new ContextTypeCoder(this.requirement),
const contextTypeImportName = script.importExternalType(
"Context",
CONTEXT_FILE_TOKEN,
);

const parameters = this.requirement.get("parameters");
Expand Down
63 changes: 62 additions & 1 deletion src/typescript-generator/repository.js
Expand Up @@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url";
import createDebug from "debug";

import { ensureDirectoryExists } from "../util/ensure-directory-exists.js";
import { CONTEXT_FILE_TOKEN } from "./context-file-token.js";
import { Script } from "./script.js";

const debug = createDebug("counterfact:server:repository");
Expand Down Expand Up @@ -103,13 +104,73 @@ export class Repository {
}

debug("about to write", fullPath);
await fs.writeFile(fullPath, contents);
await fs.writeFile(
fullPath,
contents.replaceAll(
CONTEXT_FILE_TOKEN,
this.findContextPath(destination, path),
),
);
debug("did write", fullPath);
},
);

await Promise.all(writeFiles);

await this.copyCoreFiles(destination);

await this.createDefaultContextFile(destination);
}

async createDefaultContextFile(destination) {
const contextFilePath = nodePath.join(destination, "paths", "_.context.ts");

if (existsSync(contextFilePath)) {
return;
}

await ensureDirectoryExists(contextFilePath);

await fs.writeFile(
contextFilePath,
`/**
* This is the default context for Counterfact.
*
* It defines the context object in the REPL
* and the $.context object in the code.
*
* Add properties and methods to suit your needs.
*
* See https://counterfact.dev/docs/usage.html#working-with-state-the-codecontextcode-object-and-codecontexttscode
*/
export class Context {
}
`,
);
}

findContextPath(destination, path) {
return nodePath.relative(
nodePath.join(destination, nodePath.dirname(path)),
this.nearestContextFile(destination, path),
);
}

nearestContextFile(destination, path) {
const directory = nodePath.dirname(path).replace("path-types", "paths");

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

if (directory.length <= 1) {
// No _context.ts was found so import the one that should be in the root
return nodePath.join(destination, "paths", "_.context.ts");
}

if (existsSync(candidate)) {
return candidate;
}

return this.nearestContextFile(destination, nodePath.join(path, ".."));
}
}

0 comments on commit 539f77c

Please sign in to comment.