Skip to content

Commit

Permalink
feat: node_modules generation configuration, support for nested packages
Browse files Browse the repository at this point in the history
  • Loading branch information
DJankauskas committed Jun 23, 2023
1 parent 87af04c commit 9229f41
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 118 deletions.
44 changes: 44 additions & 0 deletions packages/ts-to-zod/src/__tests__/generate-in-node-modules.test.ts
@@ -0,0 +1,44 @@
import { multiFileTestCase } from "./multiFileTestCase";

import { AddrIface, Enum } from "./common";
import { StringSchema } from "../types";
import pkgUp from "pkg-up";
import path from "path";

type T = {
datetime: StringSchema<{ max: 20; datetime: true }>;
addresses: AddrIface[];
enum: Enum;
};

it(`generating in node_modules`, async () =>
expect(
await multiFileTestCase({
__filename,
genOptions: {
genLocation: {
type: "node_modules",
genPath: "zodgen/"
},
rootPath: await (async () => {
const rootPackageJson = await pkgUp({
cwd: __dirname
});
if (!rootPackageJson) {
throw new Error("test must run within npm package");
}
return path.dirname(rootPackageJson);
})()
}
})
).toMatchInlineSnapshot(`
{
"node_modules/zodgen/src/__tests__/common.ts": "import { Enum as __enum_Enum } from "../../../../src/__tests__/common.ts";
export const AddressIface = z.object({ street: z.string(), city: z.string(), state: z.string(), postalCode: z.string() });
export const Enum = z.nativeEnum(__enum_Enum);
",
"node_modules/zodgen/src/__tests__/generate-in-node-modules.test.ts": "import { AddressIface as AddrIface, Enum } from "./common.ts";
const T = z.object({ datetime: z.string().max(20).datetime(), addresses: z.array(z.lazy(() => AddrIface)), enum: z.lazy(() => Enum) });
",
}
`));
12 changes: 8 additions & 4 deletions packages/ts-to-zod/src/__tests__/multiFileTestCase.ts
@@ -1,14 +1,15 @@
import * as tm from "ts-morph";
import { SchemaGenContext, convertSymbol } from "../convertType";
import { testProject } from "./testProject";
import { generateFiles } from "../generateFiles";
import { GenOptions, generateFiles } from "../generateFiles";
import * as path from "path";
import pkgUp from "pkg-up";

export const multiFileTestCase = async (options: {
__filename: string;
getNode?: (file: tm.SourceFile) => tm.Node | null | undefined;
getSymbol?: (file: tm.SourceFile) => tm.Symbol | null | undefined;
genOptions?: GenOptions;
}) => {
const sourceFile = testProject.getSourceFile(options.__filename);
if (!sourceFile) {
Expand All @@ -30,19 +31,22 @@ export const multiFileTestCase = async (options: {
}
const ctx = new SchemaGenContext(testProject);
convertSymbol(ctx, symbol);
const rootPackageJson = await pkgUp();
const rootPackageJson = await pkgUp({
cwd: __dirname
});
if (!rootPackageJson) {
throw new Error("test must run within npm package");
}
const rootPath = path.dirname(rootPackageJson);
const result: Record<string, string> = {};
for (const [file, sourceFile] of generateFiles(ctx, {
const genOptions = options.genOptions || {
genLocation: {
type: "alongside",
dependencyGenPath: "./dependency-schemas/",
},
rootPath,
})) {
};
for (const [file, sourceFile] of generateFiles(ctx, genOptions)) {
const relativeFile = path.relative(rootPath, file);
result[relativeFile] = tm.ts.createPrinter().printFile(sourceFile);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/ts-to-zod/src/generateFiles.ts
Expand Up @@ -6,12 +6,12 @@ import * as Path from "path";

const DEFAULT_ALONGSIDE_SUFFIX = "codegen";

type GenLocationOptions =
export type GenLocationOptions =
| { type: "alongside"; dependencyGenPath: string; suffix?: string }
| { type: "folder"; genPath: string }
| { type: "node_modules" };
| { type: "node_modules"; genPath: string };

interface Options {
export interface GenOptions {
genLocation: GenLocationOptions;
/**
* the project root (where package.json resides) from which generation
Expand All @@ -22,7 +22,7 @@ interface Options {

export function generateFiles(
ctx: SchemaGenContext,
options: Options
options: GenOptions
): Map<string, ts.SourceFile> {
let basePath: string;
let baseDependenciesPath: string;
Expand All @@ -45,7 +45,7 @@ export function generateFiles(
basePath = Path.join(
options.rootPath,
"node_modules",
"path/to/gen/in/TODO"
options.genLocation.genPath
);
baseDependenciesPath = Path.join(basePath, "zod_schema_node_modules");
break;
Expand Down

0 comments on commit 9229f41

Please sign in to comment.