Skip to content

Commit

Permalink
chore: unify the logic of saving ts files (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 18, 2024
1 parent 9b56b1c commit 768d94c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
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

0 comments on commit 768d94c

Please sign in to comment.