Skip to content

chore: unify the logic of saving ts files #1874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2024
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
7 changes: 4 additions & 3 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isDelegateModel,
isDiscriminatorField,
normalizedRelative,
saveSourceFile,
type PluginOptions,
} from '@zenstackhq/sdk';
import {
Expand Down Expand Up @@ -137,7 +138,7 @@ ${
{ overwrite: true }
);

await this.saveSourceFile(enhanceTs);
this.saveSourceFile(enhanceTs);
}

return {
Expand Down Expand Up @@ -779,9 +780,9 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
return isReferenceExpr(arg) ? (arg.target.ref as DataModelField) : undefined;
}

private async saveSourceFile(sf: SourceFile) {
private saveSourceFile(sf: SourceFile) {
if (this.options.preserveTsFiles) {
await sf.save();
saveSourceFile(sf);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
hasValidationAttributes,
isAuthInvocation,
isForeignKeyField,
saveSourceFile,
} from '@zenstackhq/sdk';
import { getPrismaClientImportSpec } from '@zenstackhq/sdk/prisma';
import { streamAst } from 'langium';
Expand Down Expand Up @@ -57,7 +58,7 @@ import {
export class PolicyGenerator {
constructor(private options: PluginOptions) {}

async generate(project: Project, model: Model, output: string) {
generate(project: Project, model: Model, output: string) {
const sf = project.createSourceFile(path.join(output, 'policy.ts'), undefined, { overwrite: true });

this.writeImports(model, output, sf);
Expand Down Expand Up @@ -86,7 +87,7 @@ export class PolicyGenerator {
// save ts files if requested explicitly or the user provided
const preserveTsFiles = this.options.preserveTsFiles === true || !!this.options.output;
if (preserveTsFiles) {
await sf.save();
saveSourceFile(sf);
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/model-meta-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
isForeignKeyField,
isIdField,
resolved,
saveSourceFile,
TypeScriptExpressionTransformer,
} from '.';

Expand Down Expand Up @@ -66,7 +67,7 @@ export type ModelMetaGeneratorOptions = {
shortNameMap?: Map<string, string>;
};

export async function generate(
export function generate(
project: Project,
models: DataModel[],
typeDefs: TypeDef[],
Expand All @@ -82,7 +83,7 @@ export async function generate(
sf.addStatements('export default metadata;');

if (options.preserveTsFiles) {
await sf.save();
saveSourceFile(sf);
}

return sf;
Expand Down
Loading