1
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
2
import path from 'path' ;
3
- import { DEFAULT_RUNTIME_LOAD_PATH } from './constants' ;
4
3
import { ModelMeta , PolicyDef , ZodSchemas } from './enhancements' ;
5
4
6
5
/**
@@ -10,15 +9,18 @@ import { ModelMeta, PolicyDef, ZodSchemas } from './enhancements';
10
9
* will use default load path.
11
10
*/
12
11
export function getDefaultModelMeta ( loadPath : string | undefined ) : ModelMeta {
13
- const toLoad = loadPath ? path . resolve ( loadPath , 'model-meta' ) : `${ DEFAULT_RUNTIME_LOAD_PATH } /model-meta` ;
14
12
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
+ }
17
19
} catch {
18
- if ( process . env . ZENSTACK_TEST === '1' && ! path . isAbsolute ( toLoad ) ) {
20
+ if ( process . env . ZENSTACK_TEST === '1' && ! loadPath ) {
19
21
try {
20
22
// 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 ;
22
24
} catch {
23
25
throw new Error ( 'Model meta cannot be loaded. Please make sure "zenstack generate" has been run.' ) ;
24
26
}
@@ -34,14 +36,18 @@ export function getDefaultModelMeta(loadPath: string | undefined): ModelMeta {
34
36
* will use default load path.
35
37
*/
36
38
export function getDefaultPolicy ( loadPath : string | undefined ) : PolicyDef {
37
- const toLoad = loadPath ? path . resolve ( loadPath , 'policy' ) : `${ DEFAULT_RUNTIME_LOAD_PATH } /policy` ;
38
39
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
+ }
40
46
} catch {
41
- if ( process . env . ZENSTACK_TEST === '1' && ! path . isAbsolute ( toLoad ) ) {
47
+ if ( process . env . ZENSTACK_TEST === '1' && ! loadPath ) {
42
48
try {
43
49
// 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 ;
45
51
} catch {
46
52
throw new Error (
47
53
'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 {
61
67
* will use default load path.
62
68
*/
63
69
export function getDefaultZodSchemas ( loadPath : string | undefined ) : ZodSchemas | undefined {
64
- const toLoad = loadPath ? path . resolve ( loadPath , 'zod' ) : `${ DEFAULT_RUNTIME_LOAD_PATH } /zod` ;
65
70
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
+ }
67
77
} catch {
68
- if ( process . env . ZENSTACK_TEST === '1' && ! path . isAbsolute ( toLoad ) ) {
78
+ if ( process . env . ZENSTACK_TEST === '1' && ! loadPath ) {
69
79
try {
70
80
// 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' ) ) ;
72
82
} catch {
73
83
return undefined ;
74
84
}
0 commit comments