@@ -11,6 +11,18 @@ import tmp from 'tmp';
11
11
import { loadDocument } from 'zenstack/cli/cli-util' ;
12
12
import prismaPlugin from 'zenstack/plugins/prisma' ;
13
13
14
+ /**
15
+ * Use it to represent multiple files in a single string like this
16
+ `schema.zmodel
17
+ import "user"
18
+ ${FILE_SPLITTER}user.zmodel
19
+ import "schema"
20
+ model User {
21
+ ...
22
+ }`
23
+ */
24
+ export const FILE_SPLITTER = '#FILE_SPLITTER#' ;
25
+
14
26
export type WeakDbOperations = {
15
27
[ key in keyof DbOperations ] : ( ...args : any [ ] ) => Promise < any > ;
16
28
} ;
@@ -112,17 +124,42 @@ export async function loadSchema(
112
124
console . log ( 'Workdir:' , projectRoot ) ;
113
125
process . chdir ( projectRoot ) ;
114
126
115
- schema = schema . replaceAll ( '$projectRoot' , projectRoot ) ;
116
-
117
127
let zmodelPath = path . join ( projectRoot , 'schema.zmodel' ) ;
118
- const content = addPrelude ? `${ MODEL_PRELUDE } \n${ schema } ` : schema ;
119
- if ( customSchemaFilePath ) {
120
- zmodelPath = path . join ( projectRoot , customSchemaFilePath ) ;
121
- fs . mkdirSync ( path . dirname ( zmodelPath ) , { recursive : true } ) ;
122
- fs . writeFileSync ( zmodelPath , content ) ;
128
+
129
+ const files = schema . split ( FILE_SPLITTER ) ;
130
+
131
+ if ( files . length > 1 ) {
132
+ // multiple files
133
+ files . forEach ( ( file , index ) => {
134
+ //first line is the file name
135
+ const firstLine = file . indexOf ( '\n' ) ;
136
+ const fileName = file . substring ( 0 , firstLine ) . trim ( ) ;
137
+ let fileContent = file . substring ( firstLine + 1 ) ;
138
+ if ( index === 0 ) {
139
+ // The first file is the main schema file
140
+ zmodelPath = path . join ( projectRoot , fileName ) ;
141
+ if ( addPrelude ) {
142
+ // plugin need to be added after import statement
143
+ fileContent = `${ fileContent } \n${ MODEL_PRELUDE } ` ;
144
+ }
145
+ }
146
+
147
+ fileContent = fileContent . replaceAll ( '$projectRoot' , projectRoot ) ;
148
+ const filePath = path . join ( projectRoot , fileName ) ;
149
+ fs . writeFileSync ( filePath , fileContent ) ;
150
+ } ) ;
123
151
} else {
124
- fs . writeFileSync ( 'schema.zmodel' , content ) ;
152
+ schema = schema . replaceAll ( '$projectRoot' , projectRoot ) ;
153
+ const content = addPrelude ? `${ MODEL_PRELUDE } \n${ schema } ` : schema ;
154
+ if ( customSchemaFilePath ) {
155
+ zmodelPath = path . join ( projectRoot , customSchemaFilePath ) ;
156
+ fs . mkdirSync ( path . dirname ( zmodelPath ) , { recursive : true } ) ;
157
+ fs . writeFileSync ( zmodelPath , content ) ;
158
+ } else {
159
+ fs . writeFileSync ( 'schema.zmodel' , content ) ;
160
+ }
125
161
}
162
+
126
163
run ( 'npm install' ) ;
127
164
128
165
if ( customSchemaFilePath ) {
0 commit comments