Skip to content

Commit e371ec9

Browse files
authored
Merge pull request #521 from zenstackhq/dev
merge dev to main (v3.0.0-beta.34)
2 parents da6cf60 + 351780c commit e371ec9

File tree

26 files changed

+107
-42
lines changed

26 files changed

+107
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-v3",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"description": "ZenStack",
55
"packageManager": "pnpm@10.23.0",
66
"type": "module",

packages/auth-adapters/better-auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/better-auth",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"description": "ZenStack Better Auth Adapter. This adapter is modified from better-auth's Prisma adapter.",
55
"type": "module",
66
"scripts": {

packages/cli/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "zenstack",
44
"displayName": "ZenStack CLI",
55
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
6-
"version": "3.0.0-beta.33",
6+
"version": "3.0.0-beta.34",
77
"type": "module",
88
"author": {
99
"name": "ZenStack Team"
@@ -32,14 +32,15 @@
3232
"@zenstackhq/common-helpers": "workspace:*",
3333
"@zenstackhq/language": "workspace:*",
3434
"@zenstackhq/sdk": "workspace:*",
35-
"prisma": "catalog:",
3635
"colors": "1.4.0",
3736
"commander": "^8.3.0",
3837
"execa": "^9.6.0",
38+
"jiti": "^2.6.1",
3939
"langium": "catalog:",
4040
"mixpanel": "^0.18.1",
4141
"ora": "^5.4.1",
4242
"package-manager-detector": "^1.3.0",
43+
"prisma": "catalog:",
4344
"semver": "^7.7.2",
4445
"ts-pattern": "catalog:"
4546
},

packages/cli/src/actions/generate.ts

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { isPlugin, LiteralExpr, Plugin, type Model } from '@zenstackhq/language/
33
import { getLiteral, getLiteralArray } from '@zenstackhq/language/utils';
44
import { type CliPlugin } from '@zenstackhq/sdk';
55
import colors from 'colors';
6+
import { createJiti } from 'jiti';
7+
import fs from 'node:fs';
68
import path from 'node:path';
9+
import { pathToFileURL } from 'node:url';
710
import ora, { type Ora } from 'ora';
811
import { CliError } from '../cli-error';
912
import * as corePlugins from '../plugins';
@@ -73,16 +76,7 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
7376
throw new CliError(`Unknown core plugin: ${provider}`);
7477
}
7578
} else {
76-
let moduleSpec = provider;
77-
if (moduleSpec.startsWith('.')) {
78-
// relative to schema's path
79-
moduleSpec = path.resolve(path.dirname(schemaFile), moduleSpec);
80-
}
81-
try {
82-
cliPlugin = (await import(moduleSpec)).default as CliPlugin;
83-
} catch {
84-
// plugin may not export a generator so we simply ignore the error here
85-
}
79+
cliPlugin = await loadPluginModule(provider, path.dirname(schemaFile));
8680
}
8781

8882
if (cliPlugin) {
@@ -110,7 +104,8 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
110104
];
111105
defaultPlugins.forEach(({ plugin, options }) => {
112106
if (!processedPlugins.some((p) => p.cliPlugin === plugin)) {
113-
processedPlugins.push({ cliPlugin: plugin, pluginOptions: options });
107+
// default plugins are run before user plugins
108+
processedPlugins.unshift({ cliPlugin: plugin, pluginOptions: options });
114109
}
115110
});
116111

@@ -163,3 +158,69 @@ function getPluginOptions(plugin: Plugin): Record<string, unknown> {
163158
}
164159
return result;
165160
}
161+
162+
async function loadPluginModule(provider: string, basePath: string) {
163+
let moduleSpec = provider;
164+
if (moduleSpec.startsWith('.')) {
165+
// relative to schema's path
166+
moduleSpec = path.resolve(basePath, moduleSpec);
167+
}
168+
169+
const importAsEsm = async (spec: string) => {
170+
try {
171+
const result = (await import(spec)).default as CliPlugin;
172+
return result;
173+
} catch (err) {
174+
throw new CliError(`Failed to load plugin module from ${spec}: ${(err as Error).message}`);
175+
}
176+
};
177+
178+
const jiti = createJiti(pathToFileURL(basePath).toString());
179+
const importAsTs = async (spec: string) => {
180+
try {
181+
const result = (await jiti.import(spec, { default: true })) as CliPlugin;
182+
return result;
183+
} catch (err) {
184+
throw new CliError(`Failed to load plugin module from ${spec}: ${(err as Error).message}`);
185+
}
186+
};
187+
188+
const esmSuffixes = ['.js', '.mjs'];
189+
const tsSuffixes = ['.ts', '.mts'];
190+
191+
if (fs.existsSync(moduleSpec) && fs.statSync(moduleSpec).isFile()) {
192+
// try provider as ESM file
193+
if (esmSuffixes.some((suffix) => moduleSpec.endsWith(suffix))) {
194+
return await importAsEsm(pathToFileURL(moduleSpec).toString());
195+
}
196+
197+
// try provider as TS file
198+
if (tsSuffixes.some((suffix) => moduleSpec.endsWith(suffix))) {
199+
return await importAsTs(moduleSpec);
200+
}
201+
}
202+
203+
// try ESM index files in provider directory
204+
for (const suffix of esmSuffixes) {
205+
const indexPath = path.join(moduleSpec, `index${suffix}`);
206+
if (fs.existsSync(indexPath)) {
207+
return await importAsEsm(pathToFileURL(indexPath).toString());
208+
}
209+
}
210+
211+
// try TS index files in provider directory
212+
for (const suffix of tsSuffixes) {
213+
const indexPath = path.join(moduleSpec, `index${suffix}`);
214+
if (fs.existsSync(indexPath)) {
215+
return await importAsTs(indexPath);
216+
}
217+
}
218+
219+
// last resort, try to import as esm directly
220+
try {
221+
return (await import(moduleSpec)).default as CliPlugin;
222+
} catch {
223+
// plugin may not export a generator so we simply ignore the error here
224+
return undefined;
225+
}
226+
}

packages/clients/tanstack-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/tanstack-query",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"description": "TanStack Query Client for consuming ZenStack v3's CRUD service",
55
"main": "index.js",
66
"type": "module",

packages/common-helpers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/common-helpers",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"description": "ZenStack Common Helpers",
55
"type": "module",
66
"scripts": {

packages/config/eslint-config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/eslint-config",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"type": "module",
55
"private": true,
66
"license": "MIT"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/typescript-config",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"private": true,
55
"license": "MIT"
66
}

packages/config/vitest-config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/vitest-config",
33
"type": "module",
4-
"version": "3.0.0-beta.33",
4+
"version": "3.0.0-beta.34",
55
"private": true,
66
"license": "MIT",
77
"exports": {

packages/create-zenstack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-zenstack",
3-
"version": "3.0.0-beta.33",
3+
"version": "3.0.0-beta.34",
44
"description": "Create a new ZenStack project",
55
"type": "module",
66
"scripts": {

0 commit comments

Comments
 (0)