Skip to content

Commit

Permalink
fix: validation works when generating on same (input) file #219 (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvillaren committed Apr 10, 2024
1 parent 180a1c6 commit c146e83
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chokidar from "chokidar";
import { existsSync, outputFile, readFile } from "fs-extra";
import inquirer from "inquirer";
import ora from "ora";
import { join, parse, relative } from "path";
import { join, normalize, parse, relative } from "path";
import prettier from "prettier";
import slash from "slash";
import ts from "typescript";
Expand All @@ -15,7 +15,10 @@ import {
} from "./config.zod";
import { GenerateProps, generate } from "./core/generate";
import { createConfig } from "./createConfig";
import { getImportPath } from "./utils/getImportPath";
import {
areImportPathsEqualIgnoringExtension,
getImportPath,
} from "./utils/getImportPath";
import * as worker from "./worker";

let config: TsToZodConfig | undefined;
Expand Down Expand Up @@ -334,6 +337,16 @@ See more help with --help`,
}
}

let outputForValidation = output || "";

// If we're generating over the same file, we need to set a fake output path for validation
if (!output || areImportPathsEqualIgnoringExtension(input, output)) {
const outputFileName = "source.zod.ts";
const { dir } = parse(normalize(input));

outputForValidation = join(dir, outputFileName);
}

const generationErrors = await worker.validateGeneratedTypesInWorker({
sourceTypes: {
sourceText: transformedSourceText,
Expand All @@ -342,13 +355,15 @@ See more help with --help`,
integrationTests: {
sourceText: getIntegrationTestFile(
getImportPath("./source.integration.ts", input),
getImportPath("./source.integration.ts", output || input)
getImportPath("./source.integration.ts", outputForValidation)
),
relativePath: "./source.integration.ts",
},
zodSchemas: {
sourceText: getZodSchemasFile(getImportPath(output || input, input)),
relativePath: output || input,
sourceText: getZodSchemasFile(
getImportPath(outputForValidation, input)
),
relativePath: outputForValidation,
},
skipParseJSDoc: Boolean(generateOptions.skipParseJSDoc),
extraFiles,
Expand Down

0 comments on commit c146e83

Please sign in to comment.