Skip to content

Commit 1a1a860

Browse files
committed
address pr comments
1 parent c2d9852 commit 1a1a860

File tree

5 files changed

+19
-73
lines changed

5 files changed

+19
-73
lines changed

packages/cli/src/actions/action-utils.ts

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { loadDocument, type ZModelServices } from '@zenstackhq/language';
2-
import { isDataSource, isPlugin, Model } from '@zenstackhq/language/ast';
3-
import { getLiteral } from '@zenstackhq/language/utils';
1+
import { loadDocument } from '@zenstackhq/language';
2+
import { isDataSource } from '@zenstackhq/language/ast';
43
import { PrismaSchemaGenerator } from '@zenstackhq/sdk';
54
import colors from 'colors';
65
import fs from 'node:fs';
76
import path from 'node:path';
8-
import { fileURLToPath } from 'node:url';
97
import { CliError } from '../cli-error';
10-
import { PLUGIN_MODULE_NAME } from '../constants';
118

129
export function getSchemaFile(file?: string) {
1310
if (file) {
@@ -50,63 +47,6 @@ export async function loadSchemaDocument(schemaFile: string) {
5047
return loadResult.model;
5148
}
5249

53-
export async function getPluginDocuments(services: ZModelServices, fileName: string): Promise<string[]> {
54-
// parse the user document (without validation)
55-
const parseResult = services.parser.LangiumParser.parse(fs.readFileSync(fileName, { encoding: 'utf-8' }));
56-
const parsed = parseResult.value as Model;
57-
58-
// balk if there are syntax errors
59-
if (parseResult.lexerErrors.length > 0 || parseResult.parserErrors.length > 0) {
60-
return [];
61-
}
62-
63-
// traverse plugins and collect "plugin.zmodel" documents
64-
const result: string[] = [];
65-
for (const decl of parsed.declarations.filter(isPlugin)) {
66-
const providerField = decl.fields.find((f) => f.name === 'provider');
67-
if (!providerField) {
68-
continue;
69-
}
70-
71-
const provider = getLiteral<string>(providerField.value);
72-
if (!provider) {
73-
continue;
74-
}
75-
76-
let pluginModelFile: string | undefined;
77-
78-
// first try to treat provider as a path
79-
let providerPath = path.resolve(path.dirname(fileName), provider);
80-
if (fs.existsSync(providerPath)) {
81-
if (fs.statSync(providerPath).isDirectory()) {
82-
providerPath = path.join(providerPath, 'index.js');
83-
}
84-
85-
// try plugin.zmodel next to the provider file
86-
pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
87-
if (!fs.existsSync(pluginModelFile)) {
88-
// try to find upwards
89-
pluginModelFile = findUp([PLUGIN_MODULE_NAME], path.dirname(providerPath));
90-
}
91-
}
92-
93-
if (!pluginModelFile) {
94-
// try loading it as a ESM module
95-
try {
96-
const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
97-
pluginModelFile = fileURLToPath(resolvedUrl);
98-
} catch {
99-
// noop
100-
}
101-
}
102-
103-
if (pluginModelFile && fs.existsSync(pluginModelFile)) {
104-
result.push(pluginModelFile);
105-
}
106-
}
107-
return result;
108-
}
109-
11050
export function handleSubProcessError(err: unknown) {
11151
if (err instanceof Error && 'status' in err && typeof err.status === 'number') {
11252
process.exit(err.status);

packages/language/src/document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isAstNode, URI, type AstNode, type LangiumDocument, type LangiumDocumen
22
import fs from 'node:fs';
33
import path from 'node:path';
44
import { fileURLToPath } from 'node:url';
5-
import { isDataSource, Model } from './ast';
5+
import { isDataSource, type Model } from './ast';
66
import { STD_LIB_MODULE_NAME } from './constants';
77
import { createZModelServices } from './module';
88
import { getDataModelAndTypeDefs, getDocument, hasAttribute, resolveImport, resolveTransitiveImports } from './utils';

packages/language/src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { NodeFileSystem } from 'langium/node';
1111
import path from 'node:path';
1212
import { fileURLToPath } from 'node:url';
13-
import { Model } from './ast';
13+
import type { Model } from './ast';
1414
import { ZModelGeneratedModule, ZModelGeneratedSharedModule, ZModelLanguageMetaData } from './generated/module';
1515
import { getPluginDocuments } from './utils';
1616
import { registerValidationChecks, ZModelValidator } from './validator';

packages/language/src/utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { AstUtils, URI, type AstNode, type LangiumDocument, type LangiumDocuments, type Reference } from 'langium';
22
import fs from 'node:fs';
3+
import { createRequire } from 'node:module';
34
import path from 'node:path';
4-
import { fileURLToPath } from 'node:url';
5+
import { fileURLToPath, pathToFileURL } from 'node:url';
56
import { PLUGIN_MODULE_NAME, STD_LIB_MODULE_NAME, type ExpressionContext } from './constants';
67
import {
78
isArrayExpr,
@@ -607,17 +608,22 @@ export function getPluginDocuments(model: Model, schemaPath: string): string[] {
607608
}
608609

609610
if (!pluginModelFile) {
610-
try {
611-
if (typeof import.meta.resolve === 'function') {
611+
if (typeof import.meta.resolve === 'function') {
612+
try {
612613
// try loading as a ESM module
613614
const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
614615
pluginModelFile = fileURLToPath(resolvedUrl);
615-
} else {
616-
// try loading as a CJS module
617-
pluginModelFile = require.resolve(`${provider}/${PLUGIN_MODULE_NAME}`, {
618-
paths: [path.dirname(schemaPath)],
619-
});
616+
} catch {
617+
// noop
620618
}
619+
}
620+
}
621+
622+
if (!pluginModelFile) {
623+
// try loading as a CJS module
624+
try {
625+
const require = createRequire(pathToFileURL(schemaPath));
626+
pluginModelFile = require.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
621627
} catch {
622628
// noop
623629
}

samples/blog/zenstack/schema.zmodel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum Role {
1010
}
1111

1212
plugin policy {
13-
// due to pnpm layout we can't directly usage package name here
13+
// due to pnpm layout we can't directly use package name here
1414
provider = '../node_modules/@zenstackhq/plugin-policy/dist/index.js'
1515
}
1616

0 commit comments

Comments
 (0)