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

Function getPluginDocuments

packages/language/src/utils.ts:716–774  ·  view source on GitHub ↗
(model: Model, schemaPath: string)

Source from the content-addressed store, hash-verified

714 * Gets the list of plugin documents from the given model.
715 */
716export function getPluginDocuments(model: Model, schemaPath: string): string[] {
717 // traverse plugins and collect "plugin.zmodel" documents
718 const result: string[] = [];
719 for (const decl of model.declarations.filter(isPlugin)) {
720 const providerField = decl.fields.find((f) => f.name === 'provider');
721 if (!providerField) {
722 continue;
723 }
724
725 const provider = getLiteral<string>(providerField.value);
726 if (!provider) {
727 continue;
728 }
729
730 let pluginModelFile: string | undefined;
731
732 // first try to treat provider as a path
733 let providerPath = path.resolve(path.dirname(schemaPath), provider);
734 if (fs.existsSync(providerPath)) {
735 if (fs.statSync(providerPath).isDirectory()) {
736 providerPath = path.join(providerPath, 'index.js');
737 }
738
739 // try plugin.zmodel next to the provider file
740 pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
741 if (!fs.existsSync(pluginModelFile)) {
742 // try to find upwards
743 pluginModelFile = findUp([PLUGIN_MODULE_NAME], path.dirname(providerPath));
744 }
745 }
746
747 if (!pluginModelFile) {
748 if (typeof import.meta.resolve === 'function') {
749 try {
750 // try loading as a ESM module
751 const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
752 pluginModelFile = fileURLToPath(resolvedUrl);
753 } catch {
754 // noop
755 }
756 }
757 }
758
759 if (!pluginModelFile) {
760 // try loading as a CJS module
761 try {
762 const require = createRequire(pathToFileURL(schemaPath));
763 pluginModelFile = require.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
764 } catch {
765 // noop
766 }
767 }
768
769 if (pluginModelFile && fs.existsSync(pluginModelFile)) {
770 result.push(pluginModelFile);
771 }
772 }
773 return result;

Callers 1

Calls 5

getLiteralFunction · 0.85
findMethod · 0.80
resolveMethod · 0.80
pushMethod · 0.80
findUpFunction · 0.70

Tested by

no test coverage detected