Skip to content

Commit cf944bb

Browse files
authored
fix 960: CLI crashes when package.json not found (#968)
1 parent cc98e30 commit cf944bb

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

packages/schema/src/cli/cli-util.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { AstNode, getDocument, LangiumDocument, LangiumDocuments, Mutable } from
77
import { NodeFileSystem } from 'langium/node';
88
import path from 'path';
99
import semver from 'semver';
10+
import { TextDocument } from 'vscode-languageserver-textdocument';
1011
import { URI } from 'vscode-uri';
1112
import { PLUGIN_MODULE_NAME, STD_LIB_MODULE_NAME } from '../language-server/constants';
13+
import { ZModelFormatter } from '../language-server/zmodel-formatter';
1214
import { createZModelServices, ZModelServices } from '../language-server/zmodel-module';
1315
import { mergeBaseModel, resolveImport, resolveTransitiveImports } from '../utils/ast-utils';
16+
import { findPackageJson } from '../utils/pkg-utils';
1417
import { getVersion } from '../utils/version-utils';
1518
import { CliError } from './cli-error';
16-
import { ZModelFormatter } from '../language-server/zmodel-formatter';
17-
import { TextDocument } from 'vscode-languageserver-textdocument';
18-
import { getPackageJson } from '../utils/pkg-utils';
1919

2020
// required minimal version of Prisma
2121
export const requiredPrismaVersion = '4.8.0';
@@ -279,13 +279,19 @@ export async function formatDocument(fileName: string) {
279279
}
280280

281281
export function getDefaultSchemaLocation() {
282-
let location = path.resolve('schema.zmodel');
283-
284282
// handle override from package.json
285-
const pkgJson = getPackageJson();
286-
if (typeof pkgJson?.zenstack?.schema === 'string') {
287-
location = path.resolve(pkgJson.zenstack.schema);
283+
const pkgJsonPath = findPackageJson();
284+
if (pkgJsonPath) {
285+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
286+
if (typeof pkgJson?.zenstack?.schema === 'string') {
287+
if (path.isAbsolute(pkgJson.zenstack.schema)) {
288+
return pkgJson.zenstack.schema;
289+
} else {
290+
// resolve relative to package.json
291+
return path.resolve(path.dirname(pkgJsonPath), pkgJson.zenstack.schema);
292+
}
293+
}
288294
}
289295

290-
return location;
296+
return path.resolve('schema.zmodel');
291297
}

packages/schema/src/plugins/prisma/schema-generator.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { name } from '.';
4848
import { getStringLiteral } from '../../language-server/validator/utils';
4949
import telemetry from '../../telemetry';
5050
import { execSync } from '../../utils/exec-utils';
51-
import { getPackageJson } from '../../utils/pkg-utils';
51+
import { findPackageJson } from '../../utils/pkg-utils';
5252
import {
5353
ModelFieldType,
5454
AttributeArg as PrismaAttributeArg,
@@ -441,15 +441,19 @@ export default class PrismaSchemaGenerator {
441441
}
442442

443443
export function getDefaultPrismaOutputFile(schemaPath: string) {
444-
let result: string | undefined;
445-
446444
// handle override from package.json
447-
const pkgJson = getPackageJson();
448-
if (typeof pkgJson.zenstack?.prisma === 'string') {
449-
result = path.resolve(pkgJson.zenstack.prisma);
450-
} else {
451-
result = './prisma/schema.prisma';
445+
const pkgJsonPath = findPackageJson(path.dirname(schemaPath));
446+
if (pkgJsonPath) {
447+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
448+
if (typeof pkgJson?.zenstack?.prisma === 'string') {
449+
if (path.isAbsolute(pkgJson.zenstack.prisma)) {
450+
return pkgJson.zenstack.prisma;
451+
} else {
452+
// resolve relative to package.json
453+
return path.resolve(path.dirname(pkgJsonPath), pkgJson.zenstack.prisma);
454+
}
455+
}
452456
}
453457

454-
return resolvePath(result, { schemaPath });
458+
return resolvePath('./prisma/schema.prisma', { schemaPath });
455459
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,25 @@ export function ensurePackage(
8585
}
8686
}
8787

88-
export function getPackageJson() {
89-
const pkgJsonPath = path.join(process.cwd(), 'package.json');
90-
if (fs.existsSync(pkgJsonPath)) {
88+
export function findPackageJson(searchPath?: string) {
89+
let currDir = searchPath ?? process.cwd();
90+
while (currDir) {
91+
const pkgJsonPath = path.join(currDir, 'package.json');
92+
if (fs.existsSync(pkgJsonPath)) {
93+
return pkgJsonPath;
94+
}
95+
const up = path.resolve(currDir, '..');
96+
if (up === currDir) {
97+
return undefined;
98+
}
99+
currDir = up;
100+
}
101+
return undefined;
102+
}
103+
104+
export function getPackageJson(searchPath?: string) {
105+
const pkgJsonPath = findPackageJson(searchPath);
106+
if (pkgJsonPath) {
91107
return JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
92108
} else {
93109
return undefined;

0 commit comments

Comments
 (0)