From 40746f9f9d230558b8a2e7ac5fe42fe88ab8d57e Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Tue, 23 Apr 2024 11:07:12 +1000 Subject: [PATCH] fix: handle cases where no package.json and global deps --- .changeset/spotty-panthers-change.md | 5 +++ .changeset/twenty-turkeys-reply.md | 5 +++ packages/openapi-ts/src/index.ts | 55 ++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 .changeset/spotty-panthers-change.md create mode 100644 .changeset/twenty-turkeys-reply.md diff --git a/.changeset/spotty-panthers-change.md b/.changeset/spotty-panthers-change.md new file mode 100644 index 000000000..43e7e0637 --- /dev/null +++ b/.changeset/spotty-panthers-change.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: handle cases where packages are installed globally diff --git a/.changeset/twenty-turkeys-reply.md b/.changeset/twenty-turkeys-reply.md new file mode 100644 index 000000000..8d4a3ce47 --- /dev/null +++ b/.changeset/twenty-turkeys-reply.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: handle cases where package.json does not exist diff --git a/packages/openapi-ts/src/index.ts b/packages/openapi-ts/src/index.ts index c4a538387..3401c297c 100644 --- a/packages/openapi-ts/src/index.ts +++ b/packages/openapi-ts/src/index.ts @@ -1,4 +1,4 @@ -import { readFileSync } from 'node:fs'; +import { existsSync, readFileSync } from 'node:fs'; import path from 'node:path'; import { loadConfig } from 'c12'; @@ -14,6 +14,10 @@ import { postProcessClient } from './utils/postprocess'; import { writeClient } from './utils/write/client'; type Dependencies = Record; +type PackageDependencies = { + dependencies?: Dependencies; + devDependencies?: Dependencies; +}; // Dependencies used in each client. User must have installed these to use the generated client const clientDependencies: Record = { @@ -184,6 +188,43 @@ const getTypes = (userConfig: UserConfig): Config['types'] => { return types; }; +const getInstalledDependencies = (): Dependencies => { + const toReducedDependencies = (p: PackageDependencies): Dependencies => + [p.dependencies ?? {}, p.devDependencies ?? {}].reduce( + (deps, devDeps) => ({ + ...deps, + ...devDeps, + }), + {}, + ); + + let dependencies: Dependencies = {}; + + // Attempt to get all globally installed pacakges. + const result = sync('npm', ['list', '-g', '--json', '--depth=0']); + if (!result.error) { + const globally: PackageDependencies = JSON.parse(result.stdout.toString()); + dependencies = { + ...dependencies, + ...toReducedDependencies(globally), + }; + } + + // Attempt to read any dependencies installed in a local projects package.json. + const pkgPath = path.resolve(process.cwd(), 'package.json'); + if (existsSync(pkgPath)) { + const locally: PackageDependencies = JSON.parse( + readFileSync(pkgPath).toString(), + ); + dependencies = { + ...dependencies, + ...toReducedDependencies(locally), + }; + } + + return dependencies; +}; + const initConfig = async ( userConfig: UserConfig, dependencies: Dependencies, @@ -269,17 +310,7 @@ const initConfig = async ( * @param userConfig {@link UserConfig} passed to the `createClient()` method */ export async function createClient(userConfig: UserConfig): Promise { - const pkg = JSON.parse( - readFileSync(path.resolve(process.cwd(), 'package.json')).toString(), - ); - - const dependencies = [pkg.dependencies, pkg.devDependencies].reduce( - (res, deps) => ({ - ...res, - ...deps, - }), - {}, - ); + const dependencies = getInstalledDependencies(); if (!dependencies.typescript) { throw new Error('🚫 dependency missing - TypeScript must be installed');