Skip to content

Commit

Permalink
fix: issue with locating .zenstack package (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Mar 19, 2024
1 parent aa8b182 commit 13f8c73
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
29 changes: 9 additions & 20 deletions packages/schema/src/plugins/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { PluginGlobalOptions } from '@zenstackhq/sdk';
import fs from 'fs';
import path from 'path';
import { PluginRunnerOptions } from '../cli/plugin-runner';
import { getPackageManager } from '../utils/pkg-utils';

export const ALL_OPERATION_KINDS: PolicyOperationKind[] = ['create', 'update', 'postUpdate', 'read', 'delete'];

Expand Down Expand Up @@ -82,27 +81,17 @@ export function getDefaultOutputFolder(globalOptions?: PluginGlobalOptions) {
return path.join(process.cwd(), 'node_modules', DEFAULT_RUNTIME_LOAD_PATH);
}

const { projectRoot } = getPackageManager(__dirname);
if (fs.existsSync(path.join(projectRoot, 'node_modules'))) {
// use the located node_modules folder
return path.join(projectRoot, 'node_modules', DEFAULT_RUNTIME_LOAD_PATH);
} else {
// unable to locate a node_modules folder, fallback to where the runtime
// package resides

// find the real runtime module path, it might be a symlink in pnpm
let runtimeModulePath = require.resolve('@zenstackhq/runtime');
// find the real runtime module path, it might be a symlink in pnpm
let runtimeModulePath = require.resolve('@zenstackhq/runtime');

if (runtimeModulePath) {
// start with the parent folder of @zenstackhq, supposed to be a node_modules folder
while (!runtimeModulePath.endsWith('@zenstackhq') && runtimeModulePath !== '/') {
runtimeModulePath = path.join(runtimeModulePath, '..');
}
runtimeModulePath = path.join(runtimeModulePath, '..');
}
const modulesFolder = getNodeModulesFolder(runtimeModulePath);
return modulesFolder ? path.join(modulesFolder, DEFAULT_RUNTIME_LOAD_PATH) : undefined;
// start with the parent folder of @zenstackhq, supposed to be a node_modules folder
while (!runtimeModulePath.endsWith('@zenstackhq') && runtimeModulePath !== '/') {
runtimeModulePath = path.join(runtimeModulePath, '..');
}
runtimeModulePath = path.join(runtimeModulePath, '..');

const modulesFolder = getNodeModulesFolder(runtimeModulePath);
return modulesFolder ? path.join(modulesFolder, DEFAULT_RUNTIME_LOAD_PATH) : undefined;
}

/**
Expand Down
18 changes: 15 additions & 3 deletions packages/server/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export function getDefaultModelMeta(loadPath: string | undefined): ModelMeta {
const toLoad = path.resolve(loadPath, 'model-meta');
return require(toLoad).default;
} else {
return require('.zenstack/model-meta').default;
// model-meta should be resolved relative to the runtime
const metaPath = require.resolve('.zenstack/model-meta', {
paths: [require.resolve('@zenstackhq/runtime')],
});
return require(metaPath).default;
}
} catch {
if (process.env.ZENSTACK_TEST === '1' && !loadPath) {
Expand Down Expand Up @@ -61,7 +65,11 @@ export function getDefaultPolicy(loadPath: string | undefined): PolicyDef {
const toLoad = path.resolve(loadPath, 'policy');
return require(toLoad).default;
} else {
return require('.zenstack/policy').default;
// policy should be resolved relative to the runtime
const policyPath = require.resolve('.zenstack/policy', {
paths: [require.resolve('@zenstackhq/runtime')],
});
return require(policyPath).default;
}
} catch {
if (process.env.ZENSTACK_TEST === '1' && !loadPath) {
Expand Down Expand Up @@ -92,7 +100,11 @@ export function getDefaultZodSchemas(loadPath: string | undefined): ZodSchemas |
const toLoad = path.resolve(loadPath, 'zod');
return require(toLoad);
} else {
return require('.zenstack/zod');
// policy should be resolved relative to the runtime
const zodPath = require.resolve('.zenstack/zod', {
paths: [require.resolve('@zenstackhq/runtime')],
});
return require(zodPath);
}
} catch {
if (process.env.ZENSTACK_TEST === '1' && !loadPath) {
Expand Down

0 comments on commit 13f8c73

Please sign in to comment.