From 886fe486ec7ec6c363155768a1a442ea549dd17e Mon Sep 17 00:00:00 2001 From: Jonathan Stevens Date: Tue, 20 Feb 2024 21:33:45 +0000 Subject: [PATCH 1/6] refactor(pkg-utils.ts): improve findUp function to support searching for multiple paths and add detailed JSDoc comments for better understanding and documentation --- packages/schema/src/utils/pkg-utils.ts | 48 +++++++++++++++++++------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/schema/src/utils/pkg-utils.ts b/packages/schema/src/utils/pkg-utils.ts index ca4ca127d..9f1cc6d87 100644 --- a/packages/schema/src/utils/pkg-utils.ts +++ b/packages/schema/src/utils/pkg-utils.ts @@ -1,20 +1,42 @@ -import fs from 'fs'; -import path from 'path'; +import fs from 'node:fs'; +import path from 'node:path'; import { execSync } from './exec-utils'; export type PackageManagers = 'npm' | 'yarn' | 'pnpm'; -function findUp(names: string[], cwd: string): string | undefined { - let dir = cwd; - // eslint-disable-next-line no-constant-condition - while (true) { - const target = names.find((name) => fs.existsSync(path.join(dir, name))); - if (target) return target; - - const up = path.resolve(dir, '..'); - if (up === dir) return undefined; // it'll fail anyway - dir = up; - } +/** + * A type named FindUp that takes a type parameter e which extends boolean. + * If e extends true, it returns a union type of string[] or undefined. + * If e does not extend true, it returns a union type of string or undefined. + * @author Jonathan Stevens (TGTGamer) + * + * @export + * @template e A type parameter that extends boolean + */ +export type FindUp = e extends true ? string[] | undefined : string | undefined +/** + * Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path. + * Optionally return a single path or multiple paths. + * If multiple allowed, return all paths found. + * If no paths are found, return undefined. + * @author Jonathan Stevens (TGTGamer) + * + * @export + * @template [e=false] + * @param names An array of strings representing names to search for within the directory + * @param cwd A string representing the current working directory + * @param [multiple=false as e] A boolean flag indicating whether to search for multiple levels. Useful for finding node_modules directories... + * @param [result=[]] An array of strings representing the accumulated results used in multiple results + * @returns Path(s) to a specific file or folder within the directory or parent directories + */ +export function findUp(names: string[], cwd: string = process.cwd(), multiple: e = false as e, result: string[] = []): FindUp { + if (!names.some((name) => !!name)) return undefined; + const target = names.find((name) => fs.existsSync(path.join(cwd, name))); + if (multiple == false && target) return path.join(cwd, target) as FindUp; + if (target) result.push(path.join(cwd, target)); + const up = path.resolve(cwd, '..'); + if (up === cwd) return (multiple && result.length > 0 ? result : undefined) as FindUp; // it'll fail anyway + return findUp(names, up, multiple, result); } function getPackageManager(projectPath = '.'): PackageManagers { From 068823523402ee563802aace8c16c0a1e4b78fa2 Mon Sep 17 00:00:00 2001 From: Jonathan Stevens Date: Tue, 20 Feb 2024 21:34:41 +0000 Subject: [PATCH 2/6] deprecate(pkg-utils.ts): deprecate findPackageJson function in favor of findUp function for improved clarity and consistency Signed-off-by: Jonathan Stevens --- packages/schema/src/utils/pkg-utils.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/schema/src/utils/pkg-utils.ts b/packages/schema/src/utils/pkg-utils.ts index 9f1cc6d87..af36ddef3 100644 --- a/packages/schema/src/utils/pkg-utils.ts +++ b/packages/schema/src/utils/pkg-utils.ts @@ -107,6 +107,11 @@ export function ensurePackage( } } +/** + * 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. + * 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. + * @deprecated Use findUp instead @see findUp + */ export function findPackageJson(searchPath?: string) { let currDir = searchPath ?? process.cwd(); while (currDir) { From f1bb7821ba165a270459b2b85cfe0a1b6da33336 Mon Sep 17 00:00:00 2001 From: Jonathan Stevens Date: Tue, 20 Feb 2024 21:36:18 +0000 Subject: [PATCH 3/6] refactor(pkg-utils.ts): update getPackageJson function to use findUp utility function to locate package.json file in the directory hierarchy (respecting deprecation a171002efe8890377d81cddc5c6c3301c5445089) Signed-off-by: Jonathan Stevens --- packages/schema/src/utils/pkg-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/schema/src/utils/pkg-utils.ts b/packages/schema/src/utils/pkg-utils.ts index af36ddef3..92fc8b0c2 100644 --- a/packages/schema/src/utils/pkg-utils.ts +++ b/packages/schema/src/utils/pkg-utils.ts @@ -129,7 +129,7 @@ export function findPackageJson(searchPath?: string) { } export function getPackageJson(searchPath?: string) { - const pkgJsonPath = findPackageJson(searchPath); + const pkgJsonPath = findUp(['package.json'], searchPath ?? process.cwd()); if (pkgJsonPath) { return JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')); } else { From ae59aef96a6192979495f8d66e73ac942ddf4bda Mon Sep 17 00:00:00 2001 From: Jonathan Stevens Date: Tue, 20 Feb 2024 21:37:10 +0000 Subject: [PATCH 4/6] refactor(schema-generator.ts): update statement to use findUp function instead of findPackageJson for better clarity and consistency (respecting deprecation a171002efe8890377d81cddc5c6c3301c5445089) Signed-off-by: Jonathan Stevens --- packages/schema/src/plugins/prisma/schema-generator.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 881983caa..0eeea55c5 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -48,7 +48,7 @@ import { name } from '.'; import { getStringLiteral } from '../../language-server/validator/utils'; import telemetry from '../../telemetry'; import { execPackage } from '../../utils/exec-utils'; -import { findPackageJson } from '../../utils/pkg-utils'; +import { findUp } from '../../utils/pkg-utils'; import { ModelFieldType, AttributeArg as PrismaAttributeArg, @@ -450,7 +450,7 @@ export default class PrismaSchemaGenerator { export function getDefaultPrismaOutputFile(schemaPath: string) { // handle override from package.json - const pkgJsonPath = findPackageJson(path.dirname(schemaPath)); + const pkgJsonPath = findUp(['package.json'], path.dirname(schemaPath)); if (pkgJsonPath) { const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')); if (typeof pkgJson?.zenstack?.prisma === 'string') { From 9633b04dcbb8d9f02028a702b0d2cb469d91dc17 Mon Sep 17 00:00:00 2001 From: Jonathan Stevens Date: Tue, 20 Feb 2024 21:48:42 +0000 Subject: [PATCH 5/6] refactor(cli-util.ts): change findPackageJson function to findUp for better clarity and consistency with other utility functions. This change improves code readability and maintainability. (respecting deprecation a171002efe8890377d81cddc5c6c3301c5445089) Signed-off-by: Jonathan Stevens --- packages/schema/src/cli/cli-util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/schema/src/cli/cli-util.ts b/packages/schema/src/cli/cli-util.ts index 85c38e82a..c8ede65a2 100644 --- a/packages/schema/src/cli/cli-util.ts +++ b/packages/schema/src/cli/cli-util.ts @@ -13,7 +13,7 @@ import { PLUGIN_MODULE_NAME, STD_LIB_MODULE_NAME } from '../language-server/cons import { ZModelFormatter } from '../language-server/zmodel-formatter'; import { createZModelServices, ZModelServices } from '../language-server/zmodel-module'; import { mergeBaseModel, resolveImport, resolveTransitiveImports } from '../utils/ast-utils'; -import { findPackageJson } from '../utils/pkg-utils'; +import { findUp } from '../utils/pkg-utils'; import { getVersion } from '../utils/version-utils'; import { CliError } from './cli-error'; @@ -280,7 +280,7 @@ export async function formatDocument(fileName: string) { export function getDefaultSchemaLocation() { // handle override from package.json - const pkgJsonPath = findPackageJson(); + const pkgJsonPath = findUp(['package.json']); if (pkgJsonPath) { const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')); if (typeof pkgJson?.zenstack?.schema === 'string') { From 1b01c31048daa9d1d684266333f2516caaad9717 Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Tue, 20 Feb 2024 23:59:27 +0000 Subject: [PATCH 6/6] docs: remove author comment for consistency --- packages/schema/src/utils/pkg-utils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/schema/src/utils/pkg-utils.ts b/packages/schema/src/utils/pkg-utils.ts index 92fc8b0c2..ce41dac34 100644 --- a/packages/schema/src/utils/pkg-utils.ts +++ b/packages/schema/src/utils/pkg-utils.ts @@ -8,7 +8,6 @@ export type PackageManagers = 'npm' | 'yarn' | 'pnpm'; * A type named FindUp that takes a type parameter e which extends boolean. * If e extends true, it returns a union type of string[] or undefined. * If e does not extend true, it returns a union type of string or undefined. - * @author Jonathan Stevens (TGTGamer) * * @export * @template e A type parameter that extends boolean @@ -19,7 +18,6 @@ export type FindUp = e extends true ? string[] | undefined : * Optionally return a single path or multiple paths. * If multiple allowed, return all paths found. * If no paths are found, return undefined. - * @author Jonathan Stevens (TGTGamer) * * @export * @template [e=false]