fork of hey-api/openapi-ts because I need some additional things

Merge pull request #471 from hey-api/fix/handle-globally-installed

fix: handle cases where no package.json and global deps

authored by

Lubos and committed by
GitHub
09935b43 ab68dfa1

+53 -12
+5
.changeset/spotty-panthers-change.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: handle cases where packages are installed globally
+5
.changeset/twenty-turkeys-reply.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: handle cases where package.json does not exist
+43 -12
packages/openapi-ts/src/index.ts
··· 1 - import { readFileSync } from 'node:fs'; 1 + import { existsSync, readFileSync } from 'node:fs'; 2 2 import path from 'node:path'; 3 3 4 4 import { loadConfig } from 'c12'; ··· 14 14 import { writeClient } from './utils/write/client'; 15 15 16 16 type Dependencies = Record<string, unknown>; 17 + type PackageDependencies = { 18 + dependencies?: Dependencies; 19 + devDependencies?: Dependencies; 20 + }; 17 21 18 22 // Dependencies used in each client. User must have installed these to use the generated client 19 23 const clientDependencies: Record<Config['client'], string[]> = { ··· 184 188 return types; 185 189 }; 186 190 191 + const getInstalledDependencies = (): Dependencies => { 192 + const toReducedDependencies = (p: PackageDependencies): Dependencies => 193 + [p.dependencies ?? {}, p.devDependencies ?? {}].reduce( 194 + (deps, devDeps) => ({ 195 + ...deps, 196 + ...devDeps, 197 + }), 198 + {}, 199 + ); 200 + 201 + let dependencies: Dependencies = {}; 202 + 203 + // Attempt to get all globally installed pacakges. 204 + const result = sync('npm', ['list', '-g', '--json', '--depth=0']); 205 + if (!result.error) { 206 + const globally: PackageDependencies = JSON.parse(result.stdout.toString()); 207 + dependencies = { 208 + ...dependencies, 209 + ...toReducedDependencies(globally), 210 + }; 211 + } 212 + 213 + // Attempt to read any dependencies installed in a local projects package.json. 214 + const pkgPath = path.resolve(process.cwd(), 'package.json'); 215 + if (existsSync(pkgPath)) { 216 + const locally: PackageDependencies = JSON.parse( 217 + readFileSync(pkgPath).toString(), 218 + ); 219 + dependencies = { 220 + ...dependencies, 221 + ...toReducedDependencies(locally), 222 + }; 223 + } 224 + 225 + return dependencies; 226 + }; 227 + 187 228 const initConfig = async ( 188 229 userConfig: UserConfig, 189 230 dependencies: Dependencies, ··· 269 310 * @param userConfig {@link UserConfig} passed to the `createClient()` method 270 311 */ 271 312 export async function createClient(userConfig: UserConfig): Promise<Client> { 272 - const pkg = JSON.parse( 273 - readFileSync(path.resolve(process.cwd(), 'package.json')).toString(), 274 - ); 275 - 276 - const dependencies = [pkg.dependencies, pkg.devDependencies].reduce( 277 - (res, deps) => ({ 278 - ...res, 279 - ...deps, 280 - }), 281 - {}, 282 - ); 313 + const dependencies = getInstalledDependencies(); 283 314 284 315 if (!dependencies.typescript) { 285 316 throw new Error('🚫 dependency missing - TypeScript must be installed');