Skip to content

Commit

Permalink
chore: misc CLI fixes (#1939)
Browse files Browse the repository at this point in the history
ymc9 authored Jan 6, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent bcbfb9a commit da08eed
Showing 6 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/schema/src/cli/actions/generate.ts
Original file line number Diff line number Diff line change
@@ -37,8 +37,8 @@ export async function generate(projectPath: string, options: Options) {

// check for multiple versions of Zenstack packages
const packages = getZenStackPackages(projectPath);
if (packages) {
const versions = new Set<string>(packages.map((p) => p.version));
if (packages.length > 0) {
const versions = new Set<string>(packages.map((p) => p.version).filter((v): v is string => !!v));
if (versions.size > 1) {
console.warn(
colors.yellow(
4 changes: 3 additions & 1 deletion packages/schema/src/cli/actions/info.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,9 @@ export async function info(projectPath: string) {
console.log('Installed ZenStack Packages:');
const versions = new Set<string>();
for (const { pkg, version } of packages) {
versions.add(version);
if (version) {
versions.add(version);
}
console.log(` ${colors.green(pkg.padEnd(20))}\t${version}`);
}

3 changes: 1 addition & 2 deletions packages/schema/src/cli/actions/init.ts
Original file line number Diff line number Diff line change
@@ -63,8 +63,7 @@ export async function init(projectPath: string, options: Options) {
if (sampleModelGenerated) {
console.log(`Sample model generated at: ${colors.blue(zmodelFile)}
Please check the following guide on how to model your app:
https://zenstack.dev/#/modeling-your-app.`);
Learn how to use ZenStack: https://zenstack.dev/docs.`);
} else if (prismaSchema) {
console.log(
`Your current Prisma schema "${prismaSchema}" has been copied to "${zmodelFile}".
8 changes: 4 additions & 4 deletions packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
@@ -227,13 +227,13 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str
return result;
}

export function getZenStackPackages(projectPath: string) {
export function getZenStackPackages(projectPath: string): Array<{ pkg: string; version: string | undefined }> {
let pkgJson: { dependencies: Record<string, unknown>; devDependencies: Record<string, unknown> };
const resolvedPath = path.resolve(projectPath);
try {
pkgJson = require(path.join(resolvedPath, 'package.json'));
} catch {
return undefined;
return [];
}

const packages = [
@@ -245,7 +245,7 @@ export function getZenStackPackages(projectPath: string) {
try {
const resolved = require.resolve(`${pkg}/package.json`, { paths: [resolvedPath] });
// eslint-disable-next-line @typescript-eslint/no-var-requires
return { pkg, version: require(resolved).version };
return { pkg, version: require(resolved).version as string };
} catch {
return { pkg, version: undefined };
}
@@ -286,7 +286,7 @@ export async function checkNewVersion() {
return;
}

if (latestVersion && semver.gt(latestVersion, currVersion)) {
if (latestVersion && currVersion && semver.gt(latestVersion, currVersion)) {
console.log(`A newer version ${colors.cyan(latestVersion)} is available.`);
}
}
2 changes: 1 addition & 1 deletion packages/schema/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ export const checkAction = async (options: Parameters<typeof actions.check>[1]):
export function createProgram() {
const program = new Command('zenstack');

program.version(getVersion(), '-v --version', 'display CLI version');
program.version(getVersion()!, '-v --version', 'display CLI version');

const schemaExtensions = ZModelLanguageMetaData.fileExtensions.join(', ');

2 changes: 1 addition & 1 deletion packages/schema/src/utils/version-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
export function getVersion() {
export function getVersion(): string | undefined {
try {
return require('../package.json').version;
} catch {

0 comments on commit da08eed

Please sign in to comment.