Skip to content

Commit 63f5cbc

Browse files
committed
fix: more robust calculation of default location for code generation
1 parent d3629be commit 63f5cbc

File tree

18 files changed

+72
-50
lines changed

18 files changed

+72
-50
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-monorepo",
3-
"version": "2.0.0-alpha.4",
3+
"version": "2.0.0-alpha.5",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r build",

packages/ide/jetbrains/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "dev.zenstack"
12-
version = "2.0.0-alpha.4"
12+
version = "2.0.0-alpha.5"
1313

1414
repositories {
1515
mavenCentral()

packages/ide/jetbrains/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jetbrains",
3-
"version": "2.0.0-alpha.4",
3+
"version": "2.0.0-alpha.5",
44
"displayName": "ZenStack JetBrains IDE Plugin",
55
"description": "ZenStack JetBrains IDE plugin",
66
"homepage": "https://zenstack.dev",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "2.0.0-alpha.4",
3+
"version": "2.0.0-alpha.5",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/misc/redwood/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/redwood",
33
"displayName": "ZenStack RedwoodJS Integration",
4-
"version": "2.0.0-alpha.4",
4+
"version": "2.0.0-alpha.5",
55
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
66
"repository": {
77
"type": "git",

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/openapi",
33
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
4-
"version": "2.0.0-alpha.4",
4+
"version": "2.0.0-alpha.5",
55
"description": "ZenStack plugin and runtime supporting OpenAPI",
66
"main": "index.js",
77
"repository": {

packages/plugins/swr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/swr",
33
"displayName": "ZenStack plugin for generating SWR hooks",
4-
"version": "2.0.0-alpha.4",
4+
"version": "2.0.0-alpha.5",
55
"description": "ZenStack plugin for generating SWR hooks",
66
"main": "index.js",
77
"repository": {

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/tanstack-query",
33
"displayName": "ZenStack plugin for generating tanstack-query hooks",
4-
"version": "2.0.0-alpha.4",
4+
"version": "2.0.0-alpha.5",
55
"description": "ZenStack plugin for generating tanstack-query hooks",
66
"main": "index.js",
77
"exports": {

packages/plugins/trpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/trpc",
33
"displayName": "ZenStack plugin for tRPC",
4-
"version": "2.0.0-alpha.4",
4+
"version": "2.0.0-alpha.5",
55
"description": "ZenStack plugin for tRPC",
66
"main": "index.js",
77
"repository": {

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/runtime",
33
"displayName": "ZenStack Runtime Library",
4-
"version": "2.0.0-alpha.4",
4+
"version": "2.0.0-alpha.5",
55
"description": "Runtime of ZenStack for both client-side and server-side environments.",
66
"repository": {
77
"type": "git",

packages/schema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "zenstack",
44
"displayName": "ZenStack Language Tools",
55
"description": "Build scalable web apps with minimum code by defining authorization and validation rules inside the data schema that closer to the database",
6-
"version": "2.0.0-alpha.4",
6+
"version": "2.0.0-alpha.5",
77
"author": {
88
"name": "ZenStack Team"
99
},

packages/schema/src/plugins/plugin-utils.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PluginGlobalOptions } from '@zenstackhq/sdk';
33
import fs from 'fs';
44
import path from 'path';
55
import { PluginRunnerOptions } from '../cli/plugin-runner';
6+
import { getPackageManager } from '../utils/pkg-utils';
67

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

@@ -76,23 +77,32 @@ export function getDefaultOutputFolder(globalOptions?: PluginGlobalOptions) {
7677
return path.resolve(globalOptions.output);
7778
}
7879

79-
// Find the real runtime module path, it might be a symlink in pnpm
80-
let runtimeModulePath = require.resolve('@zenstackhq/runtime');
81-
80+
// for testing, use the local node_modules
8281
if (process.env.ZENSTACK_TEST === '1') {
83-
// handle the case when running as tests, resolve relative to CWD
84-
runtimeModulePath = path.resolve(path.join(process.cwd(), 'node_modules', '@zenstackhq', 'runtime'));
82+
return path.join(process.cwd(), 'node_modules', DEFAULT_RUNTIME_LOAD_PATH);
8583
}
8684

87-
if (runtimeModulePath) {
88-
// start with the parent folder of @zenstackhq, supposed to be a node_modules folder
89-
while (!runtimeModulePath.endsWith('@zenstackhq') && runtimeModulePath !== '/') {
85+
const { projectRoot } = getPackageManager(__dirname);
86+
if (fs.existsSync(path.join(projectRoot, 'node_modules'))) {
87+
// use the located node_modules folder
88+
return path.join(projectRoot, 'node_modules', DEFAULT_RUNTIME_LOAD_PATH);
89+
} else {
90+
// unable to locate a node_modules folder, fallback to where the runtime
91+
// package resides
92+
93+
// find the real runtime module path, it might be a symlink in pnpm
94+
let runtimeModulePath = require.resolve('@zenstackhq/runtime');
95+
96+
if (runtimeModulePath) {
97+
// start with the parent folder of @zenstackhq, supposed to be a node_modules folder
98+
while (!runtimeModulePath.endsWith('@zenstackhq') && runtimeModulePath !== '/') {
99+
runtimeModulePath = path.join(runtimeModulePath, '..');
100+
}
90101
runtimeModulePath = path.join(runtimeModulePath, '..');
91102
}
92-
runtimeModulePath = path.join(runtimeModulePath, '..');
103+
const modulesFolder = getNodeModulesFolder(runtimeModulePath);
104+
return modulesFolder ? path.join(modulesFolder, DEFAULT_RUNTIME_LOAD_PATH) : undefined;
93105
}
94-
const modulesFolder = getNodeModulesFolder(runtimeModulePath);
95-
return modulesFolder ? path.join(modulesFolder, DEFAULT_RUNTIME_LOAD_PATH) : undefined;
96106
}
97107

98108
/**

packages/schema/src/utils/pkg-utils.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { execSync } from './exec-utils';
4+
import { match } from 'ts-pattern';
45

56
export type PackageManagers = 'npm' | 'yarn' | 'pnpm';
67

78
/**
8-
* A type named FindUp that takes a type parameter e which extends boolean.
9-
* If e extends true, it returns a union type of string[] or undefined.
9+
* A type named FindUp that takes a type parameter e which extends boolean.
10+
* If e extends true, it returns a union type of string[] or undefined.
1011
* If e does not extend true, it returns a union type of string or undefined.
1112
*
1213
* @export
1314
* @template e A type parameter that extends boolean
1415
*/
15-
export type FindUp<e extends boolean> = e extends true ? string[] | undefined : string | undefined
16+
export type FindUp<e extends boolean> = e extends true ? string[] | undefined : string | undefined;
1617
/**
17-
* Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path.
18-
* Optionally return a single path or multiple paths.
19-
* If multiple allowed, return all paths found.
18+
* Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path.
19+
* Optionally return a single path or multiple paths.
20+
* If multiple allowed, return all paths found.
2021
* If no paths are found, return undefined.
2122
*
2223
* @export
@@ -27,7 +28,12 @@ export type FindUp<e extends boolean> = e extends true ? string[] | undefined :
2728
* @param [result=[]] An array of strings representing the accumulated results used in multiple results
2829
* @returns Path(s) to a specific file or folder within the directory or parent directories
2930
*/
30-
export function findUp<e extends boolean = false>(names: string[], cwd: string = process.cwd(), multiple: e = false as e, result: string[] = []): FindUp<e> {
31+
export function findUp<e extends boolean = false>(
32+
names: string[],
33+
cwd: string = process.cwd(),
34+
multiple: e = false as e,
35+
result: string[] = []
36+
): FindUp<e> {
3137
if (!names.some((name) => !!name)) return undefined;
3238
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
3339
if (multiple == false && target) return path.join(cwd, target) as FindUp<e>;
@@ -37,23 +43,22 @@ export function findUp<e extends boolean = false>(names: string[], cwd: string =
3743
return findUp(names, up, multiple, result);
3844
}
3945

40-
function getPackageManager(projectPath = '.'): PackageManagers {
41-
const lockFile = findUp(['yarn.lock', 'pnpm-lock.yaml', 'package-lock.json'], projectPath);
46+
export function getPackageManager(searchStartPath = '.') {
47+
const lockFile = findUp(['yarn.lock', 'pnpm-lock.yaml', 'package-lock.json'], searchStartPath);
4248

4349
if (!lockFile) {
4450
// default use npm
45-
return 'npm';
51+
return { packageManager: 'npm', lockFile: undefined, projectRoot: searchStartPath };
4652
}
4753

48-
switch (path.basename(lockFile)) {
49-
case 'yarn.lock':
50-
return 'yarn';
51-
case 'pnpm-lock.yaml':
52-
return 'pnpm';
53-
default:
54-
return 'npm';
55-
}
54+
const packageManager = match(path.basename(lockFile))
55+
.with('yarn.lock', () => 'yarn')
56+
.with('pnpm-lock.yaml', () => 'pnpm')
57+
.otherwise(() => 'npm');
58+
59+
return { packageManager, lockFile, projectRoot: path.dirname(lockFile) };
5660
}
61+
5762
export function installPackage(
5863
pkg: string,
5964
dev: boolean,
@@ -106,7 +111,7 @@ export function ensurePackage(
106111
}
107112

108113
/**
109-
* A function that searches for the nearest package.json file starting from the provided search path or the current working directory if no search path is provided.
114+
* A function that searches for the nearest package.json file starting from the provided search path or the current working directory if no search path is provided.
110115
* It iterates through the directory structure going one level up at a time until it finds a package.json file. If no package.json file is found, it returns undefined.
111116
* @deprecated Use findUp instead @see findUp
112117
*/

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/sdk",
3-
"version": "2.0.0-alpha.4",
3+
"version": "2.0.0-alpha.5",
44
"description": "ZenStack plugin development SDK",
55
"main": "index.js",
66
"scripts": {

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/server",
3-
"version": "2.0.0-alpha.4",
3+
"version": "2.0.0-alpha.5",
44
"displayName": "ZenStack Server-side Adapters",
55
"description": "ZenStack server-side adapters",
66
"homepage": "https://zenstack.dev",

packages/server/src/shared.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
2-
import type { ModelMeta, PolicyDef, ZodSchemas } from '@zenstackhq/runtime';
2+
import { DEFAULT_RUNTIME_LOAD_PATH, type ModelMeta, type PolicyDef, type ZodSchemas } from '@zenstackhq/runtime';
33
import path from 'path';
44
import { AdapterBaseOptions } from './types';
55

@@ -39,7 +39,8 @@ export function getDefaultModelMeta(loadPath: string | undefined): ModelMeta {
3939
if (process.env.ZENSTACK_TEST === '1' && !loadPath) {
4040
try {
4141
// special handling for running as tests, try resolving relative to CWD
42-
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'model-meta')).default;
42+
return require(path.join(process.cwd(), 'node_modules', DEFAULT_RUNTIME_LOAD_PATH, 'model-meta'))
43+
.default;
4344
} catch {
4445
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.');
4546
}
@@ -66,7 +67,7 @@ export function getDefaultPolicy(loadPath: string | undefined): PolicyDef {
6667
if (process.env.ZENSTACK_TEST === '1' && !loadPath) {
6768
try {
6869
// special handling for running as tests, try resolving relative to CWD
69-
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'policy')).default;
70+
return require(path.join(process.cwd(), 'node_modules', DEFAULT_RUNTIME_LOAD_PATH, 'policy')).default;
7071
} catch {
7172
throw new Error(
7273
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.'
@@ -97,7 +98,7 @@ export function getDefaultZodSchemas(loadPath: string | undefined): ZodSchemas |
9798
if (process.env.ZENSTACK_TEST === '1' && !loadPath) {
9899
try {
99100
// special handling for running as tests, try resolving relative to CWD
100-
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'zod'));
101+
return require(path.join(process.cwd(), 'node_modules', DEFAULT_RUNTIME_LOAD_PATH, 'zod'));
101102
} catch {
102103
return undefined;
103104
}

packages/testtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/testtools",
3-
"version": "2.0.0-alpha.4",
3+
"version": "2.0.0-alpha.5",
44
"description": "ZenStack Test Tools",
55
"main": "index.js",
66
"private": true,

packages/testtools/src/schema.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
import type { DMMF } from '@prisma/generator-helper';
44
import type { Model } from '@zenstackhq/language/ast';
5-
import type { AuthUser, CrudContract, EnhancementKind, EnhancementOptions } from '@zenstackhq/runtime';
5+
import {
6+
DEFAULT_RUNTIME_LOAD_PATH,
7+
type AuthUser,
8+
type CrudContract,
9+
type EnhancementKind,
10+
type EnhancementOptions,
11+
} from '@zenstackhq/runtime';
612
import { getDMMF } from '@zenstackhq/sdk';
713
import { execSync } from 'child_process';
814
import * as fs from 'fs';
@@ -284,7 +290,7 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
284290
? path.isAbsolute(opt.output)
285291
? opt.output
286292
: path.join(projectRoot, opt.output)
287-
: path.join(projectRoot, 'node_modules', '.zenstack');
293+
: path.join(projectRoot, 'node_modules', DEFAULT_RUNTIME_LOAD_PATH);
288294

289295
const policy = require(path.join(outputPath, 'policy')).default;
290296
const modelMeta = require(path.join(outputPath, 'model-meta')).default;

0 commit comments

Comments
 (0)