11/* eslint-disable @typescript-eslint/no-var-requires */
22import path from 'path' ;
3- import { DEFAULT_RUNTIME_LOAD_PATH } from './constants' ;
43import { ModelMeta , PolicyDef , ZodSchemas } from './enhancements' ;
54
65/**
@@ -10,15 +9,18 @@ import { ModelMeta, PolicyDef, ZodSchemas } from './enhancements';
109 * will use default load path.
1110 */
1211export function getDefaultModelMeta ( loadPath : string | undefined ) : ModelMeta {
13- const toLoad = loadPath ? path . resolve ( loadPath , 'model-meta' ) : `${ DEFAULT_RUNTIME_LOAD_PATH } /model-meta` ;
1412 try {
15- // normal load
16- return require ( toLoad ) . default ;
13+ if ( loadPath ) {
14+ const toLoad = path . resolve ( loadPath , 'model-meta' ) ;
15+ return require ( toLoad ) . default ;
16+ } else {
17+ return require ( '.zenstack/model-meta' ) . default ;
18+ }
1719 } catch {
18- if ( process . env . ZENSTACK_TEST === '1' && ! path . isAbsolute ( toLoad ) ) {
20+ if ( process . env . ZENSTACK_TEST === '1' && ! loadPath ) {
1921 try {
2022 // special handling for running as tests, try resolving relative to CWD
21- return require ( path . join ( process . cwd ( ) , 'node_modules' , toLoad ) ) . default ;
23+ return require ( path . join ( process . cwd ( ) , 'node_modules' , '.zenstack' , 'model-meta' ) ) . default ;
2224 } catch {
2325 throw new Error ( 'Model meta cannot be loaded. Please make sure "zenstack generate" has been run.' ) ;
2426 }
@@ -34,14 +36,18 @@ export function getDefaultModelMeta(loadPath: string | undefined): ModelMeta {
3436 * will use default load path.
3537 */
3638export function getDefaultPolicy ( loadPath : string | undefined ) : PolicyDef {
37- const toLoad = loadPath ? path . resolve ( loadPath , 'policy' ) : `${ DEFAULT_RUNTIME_LOAD_PATH } /policy` ;
3839 try {
39- return require ( toLoad ) . default ;
40+ if ( loadPath ) {
41+ const toLoad = path . resolve ( loadPath , 'policy' ) ;
42+ return require ( toLoad ) . default ;
43+ } else {
44+ return require ( '.zenstack/policy' ) . default ;
45+ }
4046 } catch {
41- if ( process . env . ZENSTACK_TEST === '1' && ! path . isAbsolute ( toLoad ) ) {
47+ if ( process . env . ZENSTACK_TEST === '1' && ! loadPath ) {
4248 try {
4349 // special handling for running as tests, try resolving relative to CWD
44- return require ( path . join ( process . cwd ( ) , 'node_modules' , toLoad ) ) . default ;
50+ return require ( path . join ( process . cwd ( ) , 'node_modules' , '.zenstack' , 'policy' ) ) . default ;
4551 } catch {
4652 throw new Error (
4753 'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.'
@@ -61,14 +67,18 @@ export function getDefaultPolicy(loadPath: string | undefined): PolicyDef {
6167 * will use default load path.
6268 */
6369export function getDefaultZodSchemas ( loadPath : string | undefined ) : ZodSchemas | undefined {
64- const toLoad = loadPath ? path . resolve ( loadPath , 'zod' ) : `${ DEFAULT_RUNTIME_LOAD_PATH } /zod` ;
6570 try {
66- return require ( toLoad ) ;
71+ if ( loadPath ) {
72+ const toLoad = path . resolve ( loadPath , 'zod' ) ;
73+ return require ( toLoad ) ;
74+ } else {
75+ return require ( '.zenstack/zod' ) ;
76+ }
6777 } catch {
68- if ( process . env . ZENSTACK_TEST === '1' && ! path . isAbsolute ( toLoad ) ) {
78+ if ( process . env . ZENSTACK_TEST === '1' && ! loadPath ) {
6979 try {
7080 // special handling for running as tests, try resolving relative to CWD
71- return require ( path . join ( process . cwd ( ) , 'node_modules' , toLoad ) ) ;
81+ return require ( path . join ( process . cwd ( ) , 'node_modules' , '.zenstack' , 'zod' ) ) ;
7282 } catch {
7383 return undefined ;
7484 }
0 commit comments