| 30 | * files if given. |
| 31 | */ |
| 32 | export async function loadDocument( |
| 33 | fileName: string, |
| 34 | additionalModelFiles: string[] = [], |
| 35 | mergeImports: boolean = true, |
| 36 | returnPartialModelForError: boolean = false, |
| 37 | ): Promise< |
| 38 | | { success: true; model: Model; warnings: string[]; services: ZModelServices } |
| 39 | | { success: false; model?: Model; errors: string[]; warnings: string[] } |
| 40 | > { |
| 41 | const { ZModelLanguage: services } = createZModelServices(false); |
| 42 | const extensions = services.LanguageMetaData.fileExtensions; |
| 43 | if (!extensions.includes(path.extname(fileName))) { |
| 44 | return { |
| 45 | success: false, |
| 46 | errors: ['invalid schema file extension'], |
| 47 | warnings: [], |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | if (!fs.existsSync(fileName)) { |
| 52 | return { |
| 53 | success: false, |
| 54 | errors: ['schema file does not exist'], |
| 55 | warnings: [], |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | // load standard library |
| 60 | |
| 61 | // isomorphic __dirname |
| 62 | const _dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(fileURLToPath(import.meta.url)); |
| 63 | const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument( |
| 64 | URI.file(path.resolve(path.join(_dirname, '../res', STD_LIB_MODULE_NAME))), |
| 65 | ); |
| 66 | |
| 67 | // load the document |
| 68 | const langiumDocuments = services.shared.workspace.LangiumDocuments; |
| 69 | const document = await langiumDocuments.getOrCreateDocument(URI.file(path.resolve(fileName))); |
| 70 | |
| 71 | // load imports |
| 72 | const importedURIs = await loadImports(document, langiumDocuments); |
| 73 | const importedDocuments: LangiumDocument[] = []; |
| 74 | for (const uri of importedURIs) { |
| 75 | importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri)); |
| 76 | } |
| 77 | |
| 78 | // build the document together with standard library, additional modules, and imported documents |
| 79 | |
| 80 | // load additional model files |
| 81 | const additionalDocs = await Promise.all( |
| 82 | additionalModelFiles.map((file) => |
| 83 | services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(path.resolve(file))), |
| 84 | ), |
| 85 | ); |
| 86 | |
| 87 | await services.shared.workspace.DocumentBuilder.build([stdLib, ...additionalDocs, document, ...importedDocuments], { |
| 88 | validation: { |
| 89 | stopAfterLexingErrors: true, |