Skip to content

Commit

Permalink
Merge pull request #471 from hey-api/fix/handle-globally-installed
Browse files Browse the repository at this point in the history
fix: handle cases where no package.json and global deps
  • Loading branch information
mrlubos authored Apr 23, 2024
2 parents ab68dfa + 40746f9 commit 09935b4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-panthers-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: handle cases where packages are installed globally
5 changes: 5 additions & 0 deletions .changeset/twenty-turkeys-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: handle cases where package.json does not exist
55 changes: 43 additions & 12 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'node:fs';
import { existsSync, readFileSync } from 'node:fs';
import path from 'node:path';

import { loadConfig } from 'c12';
Expand All @@ -14,6 +14,10 @@ import { postProcessClient } from './utils/postprocess';
import { writeClient } from './utils/write/client';

type Dependencies = Record<string, unknown>;
type PackageDependencies = {
dependencies?: Dependencies;
devDependencies?: Dependencies;
};

// Dependencies used in each client. User must have installed these to use the generated client
const clientDependencies: Record<Config['client'], string[]> = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -269,17 +310,7 @@ const initConfig = async (
* @param userConfig {@link UserConfig} passed to the `createClient()` method
*/
export async function createClient(userConfig: UserConfig): Promise<Client> {
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');
Expand Down

0 comments on commit 09935b4

Please sign in to comment.