Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit e15072c

Browse files
committed
fix: addressing PR comments
- use `createRequire` instead of dynamic import for better compatibility - use file search to locate nearest package.json file - use longer padding length when formatting warnings
1 parent 8f807ae commit e15072c

3 files changed

Lines changed: 32 additions & 33 deletions

File tree

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"skipFiles": ["<node_internals>/**"],
1212
"type": "node",
1313
"args": ["generate"],
14-
"cwd": "${workspaceFolder}/samples/blog"
14+
"cwd": "${workspaceFolder}/samples/orm"
1515
},
1616
{
1717
"name": "Debug with TSX",

packages/cli/src/actions/action-utils.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isDataSource } from '@zenstackhq/language/ast';
33
import { PrismaSchemaGenerator } from '@zenstackhq/sdk';
44
import colors from 'colors';
55
import fs from 'node:fs';
6+
import { createRequire } from 'node:module';
67
import path from 'node:path';
78
import { CliError } from '../cli-error';
89

@@ -156,47 +157,45 @@ export function getOutputPath(options: { output?: string }, schemaFile: string)
156157
return path.dirname(schemaFile);
157158
}
158159
}
159-
export async function getZenStackPackages(projectPath: string): Promise<Array<{ pkg: string; version: string | undefined }>> {
160+
export async function getZenStackPackages(
161+
searchPath: string,
162+
): Promise<Array<{ pkg: string; version: string | undefined }>> {
163+
const pkgJsonFile = findUp(['package.json'], searchPath, false);
164+
if (!pkgJsonFile) {
165+
return [];
166+
}
167+
160168
let pkgJson: {
161-
dependencies: Record<string, unknown>;
162-
devDependencies: Record<string, unknown>;
169+
dependencies?: Record<string, unknown>;
170+
devDependencies?: Record<string, unknown>;
163171
};
164-
const resolvedPath = path.resolve(projectPath);
165172
try {
166-
pkgJson = (
167-
await import(path.join(resolvedPath, 'package.json'), {
168-
with: { type: 'json' },
169-
})
170-
).default;
173+
pkgJson = JSON.parse(fs.readFileSync(pkgJsonFile, 'utf8'));
171174
} catch {
172175
return [];
173176
}
174177

175178
const packages = Array.from(
176179
new Set(
177-
[...Object.keys(pkgJson.dependencies ?? {}), ...Object.keys(pkgJson.devDependencies ?? {})].filter(
178-
(p) => p.startsWith('@zenstackhq/') || p === 'zenstack',
180+
[...Object.keys(pkgJson.dependencies ?? {}), ...Object.keys(pkgJson.devDependencies ?? {})].filter((p) =>
181+
p.startsWith('@zenstackhq/'),
179182
),
180183
),
181184
).sort();
182185

183-
const result = await Promise.all(
184-
packages.map(async (pkg) => {
185-
try {
186-
const depPkgJson = (
187-
await import(`${pkg}/package.json`, {
188-
with: { type: 'json' },
189-
})
190-
).default;
191-
if (depPkgJson.private) {
192-
return undefined;
193-
}
194-
return { pkg, version: depPkgJson.version as string };
195-
} catch {
196-
return { pkg, version: undefined };
186+
const require = createRequire(import.meta.url);
187+
188+
const result = packages.map((pkg) => {
189+
try {
190+
const depPkgJson = require(`${pkg}/package.json`);
191+
if (depPkgJson.private) {
192+
return undefined;
197193
}
198-
}),
199-
);
194+
return { pkg, version: depPkgJson.version as string };
195+
} catch {
196+
return { pkg, version: undefined };
197+
}
198+
});
200199

201200
return result.filter((p) => !!p);
202201
}

packages/cli/src/actions/generate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ async function checkForMismatchedPackages(projectPath: string) {
336336
}
337337

338338
if (versions.size > 1) {
339-
const message = 'WARNING: Multiple versions of ZenStack packages detected.\n\tThis will probably cause issues and break your types.';
339+
const message =
340+
'WARNING: Multiple versions of ZenStack packages detected.\n\tThis will probably cause issues and break your types.';
340341
const slashes = '/'.repeat(73);
341342
const latestVersion = semver.sort(Array.from(versions)).reverse()[0]!;
342343

@@ -345,10 +346,9 @@ async function checkForMismatchedPackages(projectPath: string) {
345346
if (!version) continue;
346347

347348
if (version === latestVersion) {
348-
console.log(`\t${pkg.padEnd(20)}\t${colors.green(version)}`);
349-
}
350-
else {
351-
console.log(`\t${pkg.padEnd(20)}\t${colors.yellow(version)}`);
349+
console.log(`\t${pkg.padEnd(32)}\t${colors.green(version)}`);
350+
} else {
351+
console.log(`\t${pkg.padEnd(32)}\t${colors.yellow(version)}`);
352352
}
353353
}
354354
console.warn(`\n${colors.yellow(slashes)}`);

0 commit comments

Comments
 (0)