Skip to content

Commit

Permalink
hide it under a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
asarazan committed Apr 29, 2024
1 parent b4a2e96 commit 785cf42
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "jest",
"special": "ts-node src/index.ts '../../personal/Tangent-Mobile/api/entities/*.d.ts' -i -t -o ./schema/Tangent",
"special2": "ts-node src/index.ts 'tests/comparisons/**/*.d.ts' -i -t -o ./schema/Tests/",
"zod": "ts-node src/index.ts 'tests/comparisons/single/zodStuff.ts' -i -t -o ./schema/Tests/ZodStuff.kt",
"zod": "ts-node src/index.ts 'tests/comparisons/single/zodStuff.ts' -i -t -z -o ./schema/Tests/ZodStuff.kt",
"lint": "eslint . --ext .ts"
},
"author": "aaron@sarazan.net",
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ const args = yargs
default: false,
describe: "Just throw in an import kotlinx.serialization.*",
})
.option("experimentalZodSupport", {
alias: "z",
type: "boolean",
default: false,
describe:
"WIP support for Zod transpilations. Need to manually infer and expand",
})
.showHelpOnFail(true)
.help()
.strict()
Expand All @@ -64,6 +71,7 @@ export type TranspileSingleArgs = {
snakeToCamelCase: boolean;
annotationNewLines: boolean;
importStar: boolean;
experimentalZodSupport: boolean;
};

async function transpile(args: TranspileSingleArgs) {
Expand All @@ -74,6 +82,7 @@ async function transpile(args: TranspileSingleArgs) {
snakeToCamelCase,
annotationNewLines,
importStar,
experimentalZodSupport,
} = args;
const config: MartokConfig = {
files: resolve.files,
Expand All @@ -84,6 +93,7 @@ async function transpile(args: TranspileSingleArgs) {
snakeToCamelCase,
annotationNewLines,
importStar,
experimentalZodSupport,
},
};
const martok = new Martok(config);
Expand Down
5 changes: 4 additions & 1 deletion src/martok/Martok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class Martok {

public readonly imports;

public readonly zodProcessor: ZodProcessor;

public get checker(): TypeChecker {
return this.program.getTypeChecker();
}
Expand Down Expand Up @@ -73,6 +75,7 @@ export class Martok {
this.program = this.compiler.compileFiles(fsMap);
this.program = new TypeExpander(this).expand();
this.imports = new ImportGenerator(this);
this.zodProcessor = new ZodProcessor(this);

this.declarations = new DeclarationGenerator(this);
this.storage = new AsyncLocalStorage<MartokState>();
Expand Down Expand Up @@ -196,7 +199,7 @@ export class Martok {
let relativePath = path.resolve(path.dirname(file.fileName));
if (relativePath.startsWith(this.config.sourceRoot)) {
relativePath = relativePath.slice(this.config.sourceRoot.length);
} else if (!ZodProcessor.allowImportThrough(file)) {
} else if (!this.zodProcessor.allowImportThrough(file)) {
throw new Error(
`${file.fileName} is not within the given source root, it can't be included in this project.`
);
Expand Down
1 change: 1 addition & 0 deletions src/martok/MartokOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type MartokOptions = {
snakeToCamelCase?: boolean;
annotationNewLines?: boolean;
importStar?: boolean;
experimentalZodSupport?: boolean;
};
5 changes: 4 additions & 1 deletion src/martok/processing/ZodProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import ts from "typescript";
import { Martok } from "../Martok";

export class ZodProcessor {
public static allowImportThrough(file: ts.SourceFile): boolean {
public constructor(private readonly martok: Martok) {}
public allowImportThrough(file: ts.SourceFile): boolean {
if (!this.martok.config.options?.experimentalZodSupport) return false;

Check warning on line 7 in src/martok/processing/ZodProcessor.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return file.fileName.includes("/martok/node_modules/zod/lib/");
}
}

0 comments on commit 785cf42

Please sign in to comment.