1- import { findUp } from '@zenstackhq/common-helpers' ;
21import { loadDocument } from '@zenstackhq/language' ;
32import { PrismaSchemaGenerator } from '@zenstackhq/sdk' ;
43import colors from 'colors' ;
@@ -53,10 +52,13 @@ export function handleSubProcessError(err: unknown) {
5352 }
5453}
5554
56- export async function generateTempPrismaSchema ( zmodelPath : string ) {
55+ export async function generateTempPrismaSchema ( zmodelPath : string , folder ?: string ) {
5756 const model = await loadSchemaDocument ( zmodelPath ) ;
5857 const prismaSchema = await new PrismaSchemaGenerator ( model ) . generate ( ) ;
59- const prismaSchemaFile = path . resolve ( path . dirname ( zmodelPath ) , '~schema.prisma' ) ;
58+ if ( ! folder ) {
59+ folder = path . dirname ( zmodelPath ) ;
60+ }
61+ const prismaSchemaFile = path . resolve ( folder , '~schema.prisma' ) ;
6062 fs . writeFileSync ( prismaSchemaFile , prismaSchema ) ;
6163 return prismaSchemaFile ;
6264}
@@ -83,3 +85,28 @@ export function getPkgJsonConfig(startPath: string) {
8385
8486 return result ;
8587}
88+
89+ type FindUpResult < Multiple extends boolean > = Multiple extends true ? string [ ] | undefined : string | undefined ;
90+
91+ function findUp < Multiple extends boolean = false > (
92+ names : string [ ] ,
93+ cwd : string = process . cwd ( ) ,
94+ multiple : Multiple = false as Multiple ,
95+ result : string [ ] = [ ] ,
96+ ) : FindUpResult < Multiple > {
97+ if ( ! names . some ( ( name ) => ! ! name ) ) {
98+ return undefined ;
99+ }
100+ const target = names . find ( ( name ) => fs . existsSync ( path . join ( cwd , name ) ) ) ;
101+ if ( multiple === false && target ) {
102+ return path . join ( cwd , target ) as FindUpResult < Multiple > ;
103+ }
104+ if ( target ) {
105+ result . push ( path . join ( cwd , target ) ) ;
106+ }
107+ const up = path . resolve ( cwd , '..' ) ;
108+ if ( up === cwd ) {
109+ return ( multiple && result . length > 0 ? result : undefined ) as FindUpResult < Multiple > ;
110+ }
111+ return findUp ( names , up , multiple , result ) ;
112+ }
0 commit comments