UNPKG

counterfact

Version:

a library for building a fake REST API for testing

38 lines (37 loc) 1.27 kB
import { watch } from "chokidar"; import { CHOKIDAR_OPTIONS } from "../server/constants.js"; import { waitForEvent } from "../util/wait-for-event.js"; import { generate } from "./generate.js"; export class CodeGenerator extends EventTarget { openapiPath; destination; generateOptions; watcher; constructor(openApiPath, destination, generateOptions) { super(); this.openapiPath = openApiPath; this.destination = destination; this.generateOptions = generateOptions; } async generate() { await generate(this.openapiPath, this.destination, this.generateOptions); } async watch() { if (this.openapiPath.startsWith("http")) { return; } this.watcher = watch(this.openapiPath, CHOKIDAR_OPTIONS).on("change", () => { void generate(this.openapiPath, this.destination, this.generateOptions).then(() => { this.dispatchEvent(new Event("generate")); return true; }, () => { this.dispatchEvent(new Event("failed")); return false; }); }); await waitForEvent(this.watcher, "ready"); } async stopWatching() { await this.watcher?.close(); } }