MCPcopy Index your code
hub / github.com/zenstackhq/zenstack / loadDocument

Function loadDocument

packages/language/src/document.ts:32–156  ·  view source on GitHub ↗
(
    fileName: string,
    additionalModelFiles: string[] = [],
    mergeImports: boolean = true,
    returnPartialModelForError: boolean = false,
)

Source from the content-addressed store, hash-verified

30 * files if given.
31 */
32export 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,

Callers 9

expectLoadedFunction · 0.90
expectLoadedFunction · 0.90
loadSchemaFunction · 0.90
loadSchemaWithErrorFunction · 0.90
loadSchemaDocumentFunction · 0.90
updateSchemaFunction · 0.90
loadDocumentWithPluginsFunction · 0.90
issue-509.test.tsFile · 0.90
issue-392.test.tsFile · 0.90

Calls 6

createZModelServicesFunction · 0.90
loadImportsFunction · 0.85
mergeImportsDeclarationsFunction · 0.85
resolveMethod · 0.80
pushMethod · 0.80

Tested by 2

expectLoadedFunction · 0.72
expectLoadedFunction · 0.72