Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/typescript-generator/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,33 +223,35 @@ export class CodeGenerator extends EventTarget {
}

/**
* Starts watching the OpenAPI document for changes.
* Starts watching the OpenAPI document and any overlay files for changes.
*
* Has no effect when `openApiPath` is a URL (HTTP sources are not watched).
* Has no effect when neither source is watchable (for example, an HTTP
* OpenAPI source with no local overlays).
* Resolves once the watcher is ready.
*/
public async watch() {
if (this.openapiPath.startsWith("http")) {
const watchablePaths = this.openapiPath.startsWith("http")
? [...this.overlays]
: [this.openapiPath, ...this.overlays];

if (watchablePaths.length === 0) {
return;
}

this.watcher = watch(this.openapiPath, CHOKIDAR_OPTIONS).on(
"change",
() => {
void this.generate().then(
() => {
this.dispatchEvent(new Event("generate"));

return true;
},
() => {
this.dispatchEvent(new Event("failed"));

return false;
},
);
},
);
this.watcher = watch(watchablePaths, CHOKIDAR_OPTIONS).on("change", () => {
void this.generate().then(
() => {
this.dispatchEvent(new Event("generate"));

return true;
},
() => {
this.dispatchEvent(new Event("failed"));

return false;
},
);
});

await waitForEvent(this.watcher, "ready");
}
Expand Down
41 changes: 41 additions & 0 deletions test/server/code-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ const OPENAPI = {
},
};

const overlayForType = (type: string) =>
[
"overlay: 1.0.0",
"info:",
" title: Example overlay",
" version: 1.0.0",
"actions:",
" - target: $.components.schemas.Example",
" update:",
` type: ${type}`,
].join("\n");

describe("a CodeGenerator", () => {
if (process.platform === "win32") {
// Not sure why these tests are failing on Windows
Expand Down Expand Up @@ -109,6 +121,35 @@ describe("a CodeGenerator", () => {
});
});

it("updates the code when an overlay file changes", async () => {
await usingTemporaryFiles(async ({ add, path, read }) => {
await add("openapi.json", JSON.stringify(OPENAPI));
await add("overlay.yaml", overlayForType("string"));

const generator = new CodeGenerator(
path("./openapi.json"),
path("./out"),
{ routes: true, types: true },
"",
[path("./overlay.yaml")],
);

await generator.watch();

await add("overlay.yaml", overlayForType("integer"));

await waitForEvent(generator, "generate");

const exampleComponent = await read(
"./out/types/components/schemas/Example.ts",
);

await generator.stopWatching();

expect(exampleComponent).toContain("export type Example = number;\n");
});
});

it("does not update the code when the spec changes if watch is false", async () => {
await usingTemporaryFiles(async ({ add, path, read }) => {
await add("openapi.json", JSON.stringify(OPENAPI));
Expand Down
Loading